【问题标题】:ValueError: Error when checking target: expected dense_22 to have 3 dimensions, but got array with shape (1600, 2)ValueError:检查目标时出错:预期的 dense_22 具有 3 个维度,但得到的数组形状为 (1600, 2)
【发布时间】:2020-03-13 05:20:18
【问题描述】:

我是深度学习的新手,我一直在尝试使用 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。但找不到任何线索。如果这些还不够,我准备提供有关我的实施的更多细节。提前致谢。

【问题讨论】:

    标签: python tensorflow keras deep-learning seq2seq


    【解决方案1】:

    经过一些试验后,我意识到我一直在尝试使用 2D 输入,而实际代码使用的是 3D 输入。我提到了this 问题,它有一个几乎相似的查询和我的查询的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 2019-09-11
      • 2018-06-17
      • 1970-01-01
      • 2020-02-07
      相关资源
      最近更新 更多