检查目标时出错:预期conv2d具有4个维,但数组具有形状

时间:2018-11-30 06:04:33

标签: python-3.x tensorflow keras conv-neural-network recurrent-neural-network

我已经构建了Keras ConvLSTM神经网络,并且我想基于10次步骤序列来预测一帧:

model = Sequential()
model.add(ConvLSTM2D(filters=128, kernel_size=(3, 3),
                   input_shape=(None, img_size, img_size, Channels),
                   padding='same', return_sequences=True))
model.add(BatchNormalization())

model.add(ConvLSTM2D(filters=64, kernel_size=(3, 3),
                   padding='same', return_sequences=True))
model.add(BatchNormalization())

model.add(ConvLSTM2D(filters=64, kernel_size=(3, 3),
                   padding='same', return_sequences=False))
model.add(BatchNormalization())


model.add(Conv2D(filters=1, kernel_size=(3, 3),
               activation='sigmoid',
               padding='same', data_format='channels_last', name='conv2d'))


model.compile(loss='binary_crossentropy', optimizer='adadelta')

培训:

data_train_x:(10, 10, 62, 62, 12)
data_train_y:(10, 1, 62, 62, 1)


model.fit(data_train_x, data_train_y, batch_size=10, epochs=1, 
validation_split=0.05)

但是出现以下错误:

ValueError: Error when checking target: expected conv2d to have 4 dimensions, but got array with shape (10, 1, 62, 62, 1)

这是'model.summary()'的结果:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv_lst_m2d_4 (ConvLSTM2D)  (None, None, 62, 62, 128) 645632    
_________________________________________________________________
batch_normalization_3 (Batch (None, None, 62, 62, 128) 512       
_________________________________________________________________
conv_lst_m2d_5 (ConvLSTM2D)  (None, None, 62, 62, 64)  442624    
_________________________________________________________________
batch_normalization_4 (Batch (None, None, 62, 62, 64)  256       
_________________________________________________________________
conv_lst_m2d_6 (ConvLSTM2D)  (None, 62, 62, 64)        295168    
_________________________________________________________________
batch_normalization_5 (Batch (None, 62, 62, 64)        256       
_________________________________________________________________
conv2d (Conv2D)              (None, 62, 62, 1)         577       
=================================================================
Total params: 1,385,025
Trainable params: 1,384,513
Non-trainable params: 512
_________________________________________________________________

此模型是另一个模型的修订版本,该模型已正确编译,与前一个模型相比,更改的只是最后两层。以前是这样的:

model.add(ConvLSTM2D(filters=64, kernel_size=(3, 3),
                   padding='same', return_sequences=True))
model.add(BatchNormalization())


model.add(Conv3D(filters=1, kernel_size=(3, 3, 3),
               activation='sigmoid',
               padding='same', data_format='channels_last', name='conv3d'))

之所以做出此更改,是因为我想获得以下形式的4维输出(样本,output_row,output_col,过滤器)

1 个答案:

答案 0 :(得分:1)

错误消息已清除。该模型期望输出等级为4,但是您正在传递等级5的输出。在将data_train_y的第二维输入模型之前,先对其进行压缩。

data_train_y = tf.squeeze(data_train_y, axis=1)