[英]Incompatible shapes error while training ConvNet using Keras+Tensorflow
I am trying to build a simple convolutional neural network to classify time series into one-of-six classes. 我正在尝试构建一个简单的卷积神经网络,以将时间序列分为六类之一。 I am having an issue with training the network due to incompatible shapes error.
由于形状不兼容错误,我在训练网络时遇到问题。
In the following code, n_feats = 1000
, n_classes = 6
. 在以下代码中,
n_feats = 1000
, n_classes = 6
。
Fs = 100
input_layer = Input(shape=(None, n_feats), name='input_layer')
conv_layer = Conv1D(filters=32, kernel_size=Fs*4, strides=int(Fs/2), padding='same', activation='relu', name='conv_net_coarse')(input_layer)
conv_layer = MaxPool1D(pool_size=4, name='c_maxp_1')(conv_layer)
conv_layer = Dropout(rate=0.5, name='c_dropo_1')(conv_layer)
output_layer = Dense(n_classes, name='output_layer')(conv_layer)
model = Model(input_layer, output_layer)
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
print(model.summary())
Here is the model summary. 这是模型摘要。
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_layer (InputLayer) (None, None, 1000) 0
_________________________________________________________________
conv_net_coarse (Conv1D) (None, None, 32) 12800032
_________________________________________________________________
c_maxp_1 (MaxPooling1D) (None, None, 32) 0
_________________________________________________________________
c_dropo_1 (Dropout) (None, None, 32) 0
_________________________________________________________________
output_layer (Dense) (None, None, 6) 198
=================================================================
Total params: 12,800,230
Trainable params: 12,800,230
Non-trainable params: 0
_________________________________________________________________
None
When I run, model.fit(X_train, Y_train)
, where X_train
shape is (30000, 1, 1000)
and Y_train
shape is (30000, 1, 6)
, I get incompatible shapes error: 当我运行
model.fit(X_train, Y_train)
,其中X_train
形状为(30000, 1, 1000)
Y_train
(30000, 1, 1000)
而Y_train
形状为(30000, 1, 6)
Y_train
(30000, 1, 6)
,我得到了不兼容的形状错误:
InvalidArgumentError (see above for traceback): Incompatible shapes: [32,0,6] vs. [1,6,1]
[[Node: output_layer/add = Add[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"](output_layer/Reshape_2, output_layer/Reshape_3)]]
[[Node: metrics_1/acc/Mean/_197 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_637_metrics_1/acc/Mean", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
If I remove the MaxPool1D
and Dropout
layers, the model trains just fine. 如果删除
MaxPool1D
和Dropout
图层,则模型训练就很好。 Am I not specifying those layers correctly? 我没有正确指定这些图层吗?
Any help would be appreciated! 任何帮助,将不胜感激!
So - the problem lies in two facts: 所以-问题在于两个事实:
(number_of_examples, timesteps, features)
where the feature is something recorded per time step. (number_of_examples, timesteps, features)
要素(number_of_examples, timesteps, features)
,其中要素是每个时间步记录的内容。 This means that you should reshape your data to (number_of_examples, 1000, 1)
as your time sequence has 1000 timesteps and 1 feature. (number_of_examples, 1000, 1)
因为您的时间序列具有1000个时间步长和1个功能。 Flatten
before Dropout
layer. Dropout
层之前使用Flatten
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.