python - 使用 Keras 训练时的 Tensorflow InvalidArgumentError(索引)

标签 python tensorflow keras lstm one-hot-encoding

我试图在一些数据上训练 LSTM 网络,不幸的是我一直遇到以下错误:
InvalidArgumentError: indices[] = 不在 [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 和 Infinity 值而言,我至少应该做好准备。 下面是输入数据的概览:

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)

最佳答案

嵌入层期望第一个参数是词汇表的大小,即 maximum integer index + 1

但是您将特征数量作为词汇量传递。您可以做的是,在 flows_nd 中找到最大数,并将最大数加一传递给嵌入层。

voc_size = flows_nd.max()+1

model = Sequential()
model.add(Embedding(voc_size, 32))

关于python - 使用 Keras 训练时的 Tensorflow InvalidArgumentError(索引),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51223936/

相关文章:

python - ValueError : features should be a dictionary of `Tensor` s. 给定类型:<class 'NoneType' >

python - tf 和 tf.keras 密集层在我的设置中显示出完全不同的行为

python - 一维卷积神经网络输入形状 `ValueError`

python - 在命令行程序中创建 "Scrollable"输出

python - Tensorflow卷积网络——如何计算维度(形状)?

python-3.x - VGG16 过滤器的可视化

python - 有关如何创建自定义 tf.keras 优化器 (optimizer_v2) 的建议

python - unicodedata.normalize 缺少一个字符进行转换

python - csv.DictReader 中的行数

python 2.7 livewires - 'module' 对象没有属性 'init'