簡體   English   中英

InvalidArgumentError: indices[1656,1] = 83 is not in [0, 83), 當我想訓練 LSTM

[英]InvalidArgumentError: indices[1656,1] = 83 is not in [0, 83), when I want to train the LSTM

這是 LSTM 的訓練部分,我沒有弄錯。這里首先輸入並嵌入單詞和字符。

# input and embedding for words
word_in = Input(shape=(max_len,))
emb_word = Embedding(input_dim=n_words + 2, output_dim=20,
                 input_length=max_len, mask_zero=True)(word_in)

# input and embeddings for characters
char_in = Input(shape=(max_len, max_len_char,))
emb_char = TimeDistributed(Embedding(input_dim=n_chars + 1, 
output_dim=10,input_length=max_len_char, mask_zero=True))(char_in)
# character LSTM to get word encodings by characters
char_enc = TimeDistributed(LSTM(units=50, return_sequences=False,
                            recurrent_dropout=0.5))(emb_char)

# main LSTM
x = concatenate([emb_word, char_enc])
x = SpatialDropout1D(0.3)(x)
main_lstm = Bidirectional(LSTM(units=50, return_sequences=True,
                           recurrent_dropout=0.5))(x)
out = TimeDistributed(Dense(n_tags + 1, activation="softmax")) 
(main_lstm)

model = Model([word_in, char_in], out)

history = model.fit([X_word_tr,
                 np.array(X_char_tr).reshape((len(X_char_tr), max_len, 
max_len_char))],
                np.array(y_tr).reshape(len(y_tr), max_len, 1),
                batch_size=32, epochs=10, validation_split=0.2, 
verbose=1)

它給了我這個錯誤:我不知道如何更改或更改哪些值以適合 model

-------------------------------------------------------------------
 InvalidArgumentError:  indices[1656,1] = 83 is not in [0, 83)
 [[node time_distributed_1/embedding_lookup (defined at 
 /Users/zia/anaconda3/lib/python3.7/site- 
 packages/tensorflow_core/python/framework/ops.py:1751) ]] 
 [Op:__inference_keras_scratch_graph_5857]

 Function call stack:
 keras_scratch_graph

嘗試將input_dim=n_chars + 1更改為input_dim=n_chars + 2因為我已經看到添加了 2 個字符的類似問題(通常,對於字符嵌入,在字符字典中添加了“PAD”和“UNK”標簽)。 因此加 2。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM