【问题标题】:Invalid argument: indices[124,0] = 2629 is not in [0, 64) (multi-input model)无效参数:indices[124,0] = 2629 不在 [0, 64) 中(多输入模型)
【发布时间】:2022-01-25 18:20:22
【问题描述】:

我正在关注Deep Learning with Python 第 7.1.2 节多输入模型。在代码清单 7.1 中,我遇到以下错误:

InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument:  indices[124,0] = 2629 is not in [0, 64)
     [[node functional_11/embedding_8/embedding_lookup (defined at E:/Studies/PythonCode_DLBook/Codes/Chap7_Code2.py:30) ]]
     [[functional_11/embedding_9/embedding_lookup/_16]]
  (1) Invalid argument:  indices[124,0] = 2629 is not in [0, 64)
     [[node functional_11/embedding_8/embedding_lookup (defined at E:/Studies/PythonCode_DLBook/Codes/Chap7_Code2.py:30) ]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_29208]

Errors may have originated from an input operation.
Input Source operations connected to node functional_11/embedding_8/embedding_lookup:
 functional_11/embedding_8/embedding_lookup/26947 (defined at C:\Users\abdul\anaconda3\envs\PIAIC\lib\contextlib.py:113)

Input Source operations connected to node functional_11/embedding_8/embedding_lookup:
 functional_11/embedding_8/embedding_lookup/26947 (defined at C:\Users\abdul\anaconda3\envs\PIAIC\lib\contextlib.py:113)

Function call stack:
train_function -> train_function

使用的代码是:

from tensorflow.keras.models import Model
from tensorflow.keras import layers
from tensorflow.keras import Input
import numpy as np

text_vocabulary_size = 10000
question_vocabulary_size = 10000
answer_vocabulary_size = 500

text_input = Input(shape=(100,), dtype='int32', name='text')
embedded_text = layers.Embedding(64, text_vocabulary_size)(text_input)
encoded_text = layers.LSTM(32)(embedded_text)

question_input = Input(shape=(100,),dtype='int32',name='question')
embedded_question = layers.Embedding(32, question_vocabulary_size)(question_input)
encoded_question = layers.LSTM(16)(embedded_question)

concatenated = layers.concatenate([encoded_text, encoded_question],axis=-1)
answer = layers.Dense(answer_vocabulary_size,
activation='softmax')(concatenated)
model = Model([text_input, question_input], answer)
model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['acc'])

num_samples = 1000
max_length = 100
text = np.random.randint(1, text_vocabulary_size,size=(num_samples, max_length))
question = np.random.randint(1, question_vocabulary_size,size=(num_samples, max_length))
answers = np.random.randint(0, 1,size=(num_samples, answer_vocabulary_size))

model.fit([text, question], answers, epochs=10, batch_size=128)
model.fit({'text': text, 'question': question}, answers,epochs=10, batch_size=128)

我确实意识到 Embedded_text 层出现错误,因为它的输入与传入的数据形状不匹配。

但是我不知道如何解决这个问题,事实上我目前不知道如何设置/检查不同层之间的输入数据形状和数据形状。 因此,如果有人展示如何在设计模型时检查图层形状以及如何解决此类问题,那将非常有帮助。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    对于 TF,一种常用的方法是使用model.summary() 来检查网络每一层的输出形状。运行代码返回

    Model: "model"
    __________________________________________________________________________________________________
    Layer (type)                    Output Shape         Param #     Connected to
    ==================================================================================================
    text (InputLayer)               [(None, 100)]        0
    __________________________________________________________________________________________________
    question (InputLayer)           [(None, 100)]        0
    __________________________________________________________________________________________________
    embedding (Embedding)           (None, 100, 10000)   1000000     text[0][0]
    __________________________________________________________________________________________________
    embedding_1 (Embedding)         (None, 100, 10000)   1000000     question[0][0]
    __________________________________________________________________________________________________
    lstm (LSTM)                     (None, 100)          4040400     embedding[0][0]
    __________________________________________________________________________________________________
    lstm_1 (LSTM)                   (None, 100)          4040400     embedding_1[0][0]
    __________________________________________________________________________________________________
    concatenate (Concatenate)       (None, 200)          0           lstm[0][0]
                                                                     lstm_1[0][0]
    __________________________________________________________________________________________________
    dense (Dense)                   (None, 500)          100500      concatenate[0][0]
    ==================================================================================================
    Total params: 10,181,300
    Trainable params: 10,181,300
    Non-trainable params: 0
    

    所以这将是故障排除的第一步,如果您想查看每一层的预期输入形状,model.get_config() 是一种方法。我会向您推荐问题here

    此外,我建议您阅读layers.LSTMlayers.Embedding 的文档,以牢牢掌握您传入的参数和您正在创建的层。希望这有助于故障排除过程:)

    【讨论】:

    • 谢谢,至于mode.summary(),我确实检查了它,但是因为我是新手,所以无法从中得到任何东西。至于解决问题,感谢重定向,将检查它们,如果有任何帮助,请在此处发表评论。
    猜你喜欢
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    相关资源
    最近更新 更多