我正在尝试训练具有以下结构的神经网络:
model = Sequential()
model.add(Conv1D(filters = 300, kernel_size = 5, activation='relu', input_shape=(4000, 1)))
model.add(Conv1D(filters = 300, kernel_size = 5, activation='relu'))
model.add(MaxPooling1D(3))
model.add(Conv1D(filters = 320, kernel_size = 5, activation='relu'))
model.add(MaxPooling1D(3))
model.add(Dropout(0.5))
model.add(Dense(num_labels, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model
我收到此错误:
expected dense_1 to have shape (442, 3) but got array with shape (3, 1)
我的输入是一组短语(总共12501个),它们已针对4000个最相关的单词进行了标记,并且有3种可能的分类。因此,我的输入是train_x.shape =(12501,4000)。我将其重塑为Conv1D层的(12501,4000,1)。现在,我的train_y.shape =(12501,3),然后将其重塑为(12501,3,1)。
我正在使用fit函数,如下所示:
model.fit(train_x, train_y, batch_size=32, epochs=10, verbose=1, validation_split=0.2, shuffle=True)
我在做什么错了?
答案 0 :(得分:1)
无需转换标签形状即可进行分类。然后您可以查看您的网络结构。
print(model.summary())
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d_1 (Conv1D) (None, 3996, 300) 1800
_________________________________________________________________
conv1d_2 (Conv1D) (None, 3992, 300) 450300
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 1330, 300) 0
_________________________________________________________________
conv1d_3 (Conv1D) (None, 1326, 320) 480320
_________________________________________________________________
max_pooling1d_2 (MaxPooling1 (None, 442, 320) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 442, 320) 0
_________________________________________________________________
dense_1 (Dense) (None, 442, 3) 963
=================================================================
Total params: 933,383
Trainable params: 933,383
Non-trainable params: 0
_________________________________________________________________
模型的最后输出是(None, 442, 3)
,但是标签的形状是(None, 3, 1)
。您最终应该最终在全局池层GlobalMaxPooling1D()
或展平层Flatten()
中结束,将3D输出转换为2D输出,以进行分类或回归。