簡體   English   中英

ValueError:檢查目標時出錯:預期dense_22具有3維,但得到形狀為(1600, 2)的數組

[英]ValueError: Error when checking target: expected dense_22 to have 3 dimensions, but got array with shape (1600, 2)

我對 DL 很陌生,我一直在嘗試使用 seq2seq 模型對這個repo 中的文本(情感分析)進行分類。 我使用的數據集是亞馬遜評論極性(前2000行)。數據集基本上由標簽和相應的文本組成。 我的模型如下:

sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32') #MAX_SEQUENCE_LENGTH = 1000
embedded_sequences = embedding_layer(sequence_input)
l_gru = Bidirectional(GRU(100, return_sequences=True))(embedded_sequences)
l_att = AttLayer()(l_gru)
preds = Dense(2, activation='softmax')(l_att)
model = Model(sequence_input, preds)

model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy',metrics=['accuracy'])
print("model fitting - attention GRU network")
model.summary()
model.fit(x_train, y_train, validation_data=(x_val, y_val),
          epochs=5,verbose = 1, batch_size=50)

model.save('s2s.h5')

輸出:

model fitting - attention GRU network
Model: "model_23"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_24 (InputLayer)        (None, 1000)              0         
_________________________________________________________________
embedding_11 (Embedding)     (None, 1000, 100)         1276800   
_________________________________________________________________
bidirectional_24 (Bidirectio (None, 1000, 200)         120600    
_________________________________________________________________
att_layer_24 (AttLayer)      (None, 1000, 200)         200       
_________________________________________________________________
dense_23 (Dense)             (None, 1000, 2)           402       
=================================================================
Total params: 1,398,002
Trainable params: 1,398,002
Non-trainable params: 0

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-112-ec37b13f1d7e> in <module>()
      1 model.fit(x_train, y_train, validation_data=(x_val, y_val),
----> 2           epochs=5,verbose = 1, batch_size=50)
      3 
      4 model.save('s2s.h5')

2 frames

/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    129                         ': expected ' + names[i] + ' to have ' +
    130                         str(len(shape)) + ' dimensions, but got array '
--> 131                         'with shape ' + str(data_shape))
    132                 if not check_batch_axis:
    133                     data_shape = data_shape[1:]

ValueError: Error when checking target: expected dense_22 to have 3 dimensions, but got array with shape (1600, 2)

測試和驗證數據集的維度:

print(x_train.shape)
print(x_val.shape)
print(y_train.shape)
print(y_val.shape)

輸出:

(1600, 1000)
(400, 1000)
(1600, 2)
(400, 2)

我還提到了其他類似的問題,例如: this 但找不到任何線索。如果這些還不夠,我准備提供有關我的實施的更多詳細信息。 提前致謝。

經過一些實驗后,我意識到我一直在嘗試使用 2D 輸入,而實際代碼使用 3D 輸入。 我提到了這個問題,它有一個幾乎相似的查詢和我的查詢的解決方案。

暫無
暫無

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

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