【问题标题】:Invalid argument: indices[207,1] = 1611 is not in [0, 240) - Tensorflow 2.x (Python)无效参数:indices[207,1] = 1611 不在 [0, 240) - Tensorflow 2.x (Python)
【发布时间】:2020-03-01 23:36:54
【问题描述】:

我正在使用 RNN LSTM 模型对人格类型进行分类。当我开始训练模型时,我遇到了意外的索引错误。我尝试使用一些使用回溯的解决方案,但没有使用 TF 2.0 的此问题的信息。

我会留下我的Google Colab 如果你想看看。

型号:

model = keras.Sequential()
model.add(keras.layers.Embedding(input_dim = 240, output_dim = 64)) # The maxlen of the training and validation data is 240.
model.add(keras.layers.Bidirectional(keras.layers.LSTM(64)))
model.add(keras.layers.Dense(64, activation = 'relu'))
model.add(keras.layers.Dense(16, activation = 'softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()

训练模型:

fitModel = model.fit(train_data_padded, train_label_seq,
                     epochs = 10,
                     batch_size = 295, # The length of the data is 295
                     validation_data = (validation_padded, validation_label_seq),
                     verbose = 1)

追溯:

Train on 236 samples, validate on 59 samples
Epoch 1/10
236/236 [==============================] - 0s 71us/sample
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-33-48497b2e653e> in <module>()
      3                                          batch_size = 295, #how many we will load it at once (number of samples per gradient)
      4                                          validation_data = (validation_padded, validation_label_seq), #(x_val, y_val) validation_padded, validation_label_seq
----> 5                      verbose = 1)

11 frames
/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument:  indices[207,1] = 1611 is not in [0, 240)
     [[node sequential_3/embedding_3/embedding_lookup (defined at <ipython-input-31-bd83004f8334>:5) ]]
  (1) Invalid argument:  indices[207,1] = 1611 is not in [0, 240)
     [[node sequential_3/embedding_3/embedding_lookup (defined at <ipython-input-31-bd83004f8334>:5) ]]
     [[VariableShape/_22]]
0 successful operations.
0 derived errors ignored. [Op:__inference_distributed_function_22239]

Errors may have originated from an input operation.
Input Source operations connected to node sequential_3/embedding_3/embedding_lookup:
 sequential_3/embedding_3/embedding_lookup/20179 (defined at /usr/lib/python3.6/contextlib.py:81)

Input Source operations connected to node sequential_3/embedding_3/embedding_lookup:
 sequential_3/embedding_3/embedding_lookup/20179 (defined at /usr/lib/python3.6/contextlib.py:81)

Function call stack:
distributed_function -> distributed_function

【问题讨论】:

  • 你有一个值为 1161 的索引意味着你的词汇表有超过 240 个值,这就是问题所在。
  • @MatiasValdenegro 你是对的,填充的训练数据的形状是 (236, 240),所以现在我使用 56,640 作为 input_dim。它醒来了,但我在训练摘要中得到了 NaN 值。
  • 例如:0s 533us/sample - loss: nan - accuracy: 0.0000e+00 - val_loss: nan - val_accuracy: 0.0000e+00
  • 你试过input_dim = 240 + 1吗?
  • 看起来您的训练进行得很顺利,但您在验证阶段出现了错误。检查validation_padded - 不应该有超过239的值(因为它是嵌入层的最大允许值)。不要更改尺寸 - 它们是正确的

标签: python tensorflow keras deep-learning


【解决方案1】:

使用pd.DataFrame(x_train).max()检查最大值

我得到了 32552,所以只需添加 32552+1 作为 input_dim

i.e input_dim  = 32553

【讨论】:

    【解决方案2】:

    嵌入层的第一个参数是字典大小,而不是最大特征长度

     model.add(keras.layers.Embedding(*try dictionary size here, output_dim = 64)) 
    

    【讨论】:

    • 你说的字典大小是多少?
    猜你喜欢
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多