我正在關注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 層上,因為它的輸入與傳入的資料形狀不匹配。
但是我不知道如何解決這個問題,事實上我目前不知道如何設定/檢查不同層之間的輸入資料形狀和資料形狀。因此,如果有人展示如何在設計模型時檢查圖層形狀以及如何解決此類問題,那將非常有幫助。
uj5u.com熱心網友回復:
對于 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()是一種方法。我會向你推薦這里的問題。
此外,我建議您閱讀layer.LSTM和layers.Embedding的檔案,以全面掌握您傳入的引數和您正在創建的層。希望這有助于故障排除程序:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/393775.html
