使用 Keras 训练时的 Tensorflow InvalidArgumentError(索引)
Posted
技术标签:
【中文标题】使用 Keras 训练时的 Tensorflow InvalidArgumentError(索引)【英文标题】:Tensorflow InvalidArgumentError (indices) while training with Keras 【发布时间】:2018-12-15 20:41:21 【问题描述】:
我正在尝试在一些数据上训练 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)]]
我已尝试找到解决此问题的方法,但无济于事。 我使用的数据是 One Hot Encoded 和规范化的网络流。 至少在 NaN 和 Infinity-values 方面,我至少应该很好。 下面看一下输入数据:
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)
从我读到的内容来看,tensorflow 中的词汇大小可能有问题,但我不确定是否可以从我的 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)
【问题讨论】:
【参考方案1】:嵌入层期望第一个参数是词汇的大小,即maximum integer index + 1
但是您将特征数量作为词汇量传递。您可以做的是,在flows_nd中找到最大数,并将最大数加一传递给Embedding层。
voc_size = flows_nd.max()+1
model = Sequential()
model.add(Embedding(voc_size, 32))
【讨论】:
以上是关于使用 Keras 训练时的 Tensorflow InvalidArgumentError(索引)的主要内容,如果未能解决你的问题,请参考以下文章
使用基本的低级 TensorFlow 训练循环训练 tf.keras 模型不起作用
如何使用 gpu 并行训练 tensorflow.keras 模型? TensorFlow 版本 2.5.0
使用 optuna 进行优化时的 TensorFlow / keras 问题
使用 tensorflow 训练 keras:标记对象或同一对象上的多个标签的冗余