繁体   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