我正在尝试在一些数据上训练LSTM网络,不幸的是,我一直遇到以下错误:
InvalidArgumentError: indices[] = is not in [0, 4704)
Train on 180596 samples, validate on 45149 samples
Epoch 1/1
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-52-a434c3619685> in <module>()
14 epochs=1,
15 batch_size=128,
---> 16 validation_split=0.2)
c:\program files\python3x64\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1040 initial_epoch=initial_epoch,
1041 steps_per_epoch=steps_per_epoch,
-> 1042 validation_steps=validation_steps)
1043
1044 def evaluate(self, x=None, y=None,
c:\program files\python3x64\lib\site-packages\keras\engine\training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
197 ins_batch[i] = ins_batch[i].toarray()
198
--> 199 outs = f(ins_batch)
200 if not isinstance(outs, list):
201 outs = [outs]
c:\program files\python3x64\lib\site-packages\keras\backend\tensorflow_backend.py in __call__(self, inputs)
2659 return self._legacy_call(inputs)
2660
-> 2661 return self._call(inputs)
2662 else:
2663 if py_any(is_tensor(x) for x in inputs):
c:\program files\python3x64\lib\site-packages\keras\backend\tensorflow_backend.py in _call(self, inputs)
2629 symbol_vals,
2630 session)
-> 2631 fetched = self._callable_fn(*array_vals)
2632 return fetched[:len(self.outputs)]
2633
c:\program files\python3x64\lib\site-packages\tensorflow\python\client\session.py in __call__(self, *args)
1452 else:
1453 return tf_session.TF_DeprecatedSessionRunCallable(
-> 1454 self._session._session, self._handle, args, status, None)
1455
1456 def __del__(self):
c:\program files\python3x64\lib\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
517 None, None,
518 compat.as_text(c_api.TF_Message(self.status.status)),
--> 519 c_api.TF_GetCode(self.status.status))
520 # Delete the underlying status object from memory otherwise it stays alive
521 # as there is a reference to status from this from the traceback due to
InvalidArgumentError: indices[62,0] = 15757 is not in [0, 4704)
[[Node: embedding_15/embedding_lookup = GatherV2[Taxis=DT_INT32, Tindices=DT_INT32, Tparams=DT_FLOAT, _class=["loc:@training_14/RMSprop/Assign_1"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](embedding_15/embeddings/read, embedding_15/Cast, training_14/RMSprop/gradients/embedding_15/embedding_lookup_grad/concat/axis)]]
我试图找到解决方案,但无济于事。
我正在使用的数据是“一次热编码”和标准化的网络流。
至少就NaN和无穷大而言,我至少应该是很好的选择。
看一下输入数据:
In [14]:
print(flows_nd.shape)
print(type(flows_nd))
print(type(flows_nd[0]))
(225745, 4704)
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
这是网络本身:
from keras.models import Sequential
from keras.layers import Dense, Embedding
from keras.layers import LSTM
model = Sequential()
model.add(Embedding(flows_nd.shape[-1], 32))
model.add(LSTM(32))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['acc'])
history = model.fit(flows_nd, enc_labels,
epochs=1,
batch_size=128,
validation_split=0.2)
根据我的阅读,张量流中的vocab大小可能有问题,但是我不确定我可以在Keras层中更改它。
如果有人想近距离观察,我已经组装了Jupyter Notebook with my current code。
任何帮助将不胜感激。预先感谢!
解决方案:
正如Mitiku所指出的,我是将要素的数量而不是词汇量(Keras Embedding Layer Documentation)传递给嵌入层。这就是我将代码更新为有效示例的方式:
#find the maximum vocabulary size
voc_size = (flows_scaled.max()+1).astype('int64')
print(voc_size)
# build the model
from keras.models import Sequential
from keras.layers import Dense, Embedding
from keras.layers import LSTM
model = Sequential()
model.add(Embedding(voc_size, 32))
model.add(LSTM(32))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['acc'])
history = model.fit(flows_scaled, enc_labels,
epochs=1,
batch_size=128,
validation_split=0.2)
答案 0 :(得分:2)
嵌入层期望第一个参数是词汇量。这意味着它是[maximum integer index + 1][1]
但是您要通过的特征数是词汇量。这是不正确的。您实际上可以做的是找到最大数量flow_nd并传递该数字。
voc_size = flows_nd.max()+1
model = Sequential()
model.add(Embedding(voc_size, 32))