ValueError: Error when checking target: expected dense_22 to have 3 dimensions, but got array with shape (1600, 2)
ValueError: Error when checking target: expected dense_22 to have 3 dimensions, but got array with shape (1600, 2)
我是 DL 的新手,我一直在尝试使用 seq2seq 模型从这个 repo 中对文本(情感分析)进行分类。我使用的数据集是 amazon review polarity(前 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)
我还提到了其他类似的问题,例如:。但是找不到任何 leads.I 如果这些还不够,我准备提供有关我的实施的更多详细信息。提前致谢。
经过一些试验后,我意识到我一直在尝试使用 2D 输入,而实际代码使用的是 3D 输入。我提到了 个问题,它有一个几乎相似的查询和我的查询的解决方案。
我是 DL 的新手,我一直在尝试使用 seq2seq 模型从这个 repo 中对文本(情感分析)进行分类。我使用的数据集是 amazon review polarity(前 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)
我还提到了其他类似的问题,例如:
经过一些试验后,我意识到我一直在尝试使用 2D 输入,而实际代码使用的是 3D 输入。我提到了