简体   繁体   English

使用Keras + Tensorflow训练ConvNet时出现不兼容的形状错误

[英]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 = 1000n_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. 如果删除MaxPool1DDropout图层,则模型训练就很好。 Am I not specifying those layers correctly? 我没有正确指定这些图层吗?

Any help would be appreciated! 任何帮助,将不胜感激!

So - the problem lies in two facts: 所以-问题在于两个事实:

  1. The input shape should be (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个功能。
  2. As you are solving classification task - you need to squash your input to vector (from a sequence). 解决分类任务时-您需要将输入压缩到向量(从序列中)。 I'd advice you to use Flatten before Dropout layer. 我建议您在Dropout层之前使用Flatten

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

相关问题 训练时 Tensorflow 不兼容的形状错误 - Tensorflow Incompatible shapes error while training 训练可变自动编码器时出现不兼容的形状错误 - Incompatible shapes error while training variational autoencoder 在 Keras 中训练变分自动编码器引发“InvalidArgumentError:不兼容的形状”错误 - Training Variational Auto Encoder in Keras raises “InvalidArgumentError: Incompatible shapes” error 使用 Keras+tensorflow 计算梯度输入时出错 - Error computing gradients wrt input with Keras+tensorflow Tensorflow + Keras训练:InvalidArgumentError:不兼容的形状:[7,128,2,2] vs [7,128,3,3] - Tensorflow + Keras training: InvalidArgumentError: Incompatible shapes: [7,128,2,2] vs [7,128,3,3] 不兼容的形状:Tensorflow/Keras Sequential LSTM with Autoencoder - Incompatible Shapes: Tensorflow/Keras Sequential LSTM with Autoencoder Tensorflow 错误:用于广播的形状不兼容 - Tensorflow Error: Incompatible Shapes for Broadcasting Keras + tensorflow批处理图像分类 - Keras+tensorflow batch image classification 使用 categorical_crossentropy 在 TensorFlow 中训练图像分类时出现“ValueError: Shapes (None, 1) and (None, 32) are incompatible” - "ValueError: Shapes (None, 1) and (None, 32) are incompatible" when training image classification network in TensorFlow using categorical_crossentropy Tensorflow RNN 模型形状不兼容错误 - Tensorflow RNN Model Shapes are Incompatible Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM