簡體   English   中英

Keras - LSTM 密集層中的輸入形狀錯誤

[英]Keras - Wrong input shape in LSTM dense layer

我試圖建立一個lstm使用文本分類Keras

這是模型結構:

model_word2vec = Sequential()
model_word2vec.add(Embedding(input_dim=vocabulary_dimension,
                    output_dim=embedding_dim,
                    weights=[word2vec_weights,
                    input_length=longest_sentence,
                    mask_zero=True,
                    trainable=False))
model_word2vec.add(LSTM(units=embedding_dim, dropout=0.25, recurrent_dropout=0.25, return_sequences=True))
model_word2vec.add(Dense(3, activation='softmax'))
model_word2vec.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])


results = model_word2vec.fit(X_tr_word2vec, y_tr_word2vec, validation_split=0.16, epochs=3, batch_size=128, verbose=0)

其中y_tr_word2vec是一個 3 維的one-hot編碼變量。

當我運行上面的代碼時,我收到此錯誤:

ValueError: Error when checking model target: expected dense_2 to have 3 dimensions, but got array with shape (15663, 3)

我想問題可能與y_tr_word2vec形狀或batch size ,但我不確定。

更新:

我已將return_sequences=False , y_tr_word2vecone-hot更改為categorical ,密集層中有1神經元,現在我使用sparse_categorical_crossentropy而不是categorical_crossentropy

現在,我收到此錯誤: ValueError: invalid literal for int() with base 10: 'countess'

因此,現在我認為,在fit()期間,包含句子的輸入向量X_tr_word2vec出現問題。

問題是這段代碼

model_word2vec.add(LSTM(units=dim_embedding, dropout=0.25, recurrent_dropout=0.25, return_sequences=True))
model_word2vec.add(Dense(3, activation='softmax'))

您已經設置return_sequences=True ,這意味着 LSTM 將返回一個 3D 數組到密集層,而密集不需要 3D 數據...所以刪除 return_sequences=True

model_word2vec.add(LSTM(units=dim_embedding, dropout=0.25, recurrent_dropout=0.25))
model_word2vec.add(Dense(3, activation='softmax'))

你為什么設置 return_sequences=True?

暫無
暫無

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

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