简体   繁体   English

卷积神经网络中的形状误差

[英]Shapes error in Convolutional Neural Network

I'm trying to train a neural network with the following structure: 我正在尝试训练具有以下结构的神经网络:

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

And I'm getting this error: 我收到此错误:

expected dense_1 to have shape (442, 3) but got array with shape (3, 1)

My input is a set of phrases (12501 total) that have been tokenized for the 4000 most relevant words, and there's 3 possible classification. 我的输入是一组短语(共12501个),它们已针对4000个最相关的单词进行了标记,并且有3种可能的分类。 Therefore my input is train_x.shape = (12501, 4000). 因此,我的输入是train_x.shape =(12501,4000)。 I reshaped this to (12501, 4000, 1) for the Conv1D layer. 我将其重塑为Conv1D层的(12501,4000,1)。 Now, my train_y.shape = (12501,3), and I reshaped that into (12501,3, 1). 现在,我的train_y.shape =(12501,3),然后将其重塑为(12501,3,1)。

I'm using the fit function as follows: 我正在使用fit函数,如下所示:

model.fit(train_x, train_y, batch_size=32, epochs=10, verbose=1, validation_split=0.2, shuffle=True)

What am I doing wrong? 我究竟做错了什么?

There's no need to convert label shape for classification. 无需转换标签形状即可分类。 And you can look at your network structure. 您可以查看您的网络结构。

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
_________________________________________________________________

The last output of the model is (None, 442, 3) , but the shape of your label is (None, 3, 1) . 模型的最后一个输出是(None, 442, 3) ,但是标签的形状是(None, 3, 1) You should eventually ending in either a global pooling layer GlobalMaxPooling1D() or a Flatten layer Flatten() , turning the 3D outputs into 2D outputs, for classification or regression. 您最终应该以全局池化层GlobalMaxPooling1D()或Flatten层Flatten()结尾,将3D输出转换为2D输出,以进行分类或回归。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM