Keras导入数据集出错 ValueError: Negative dimension size caused by subtracting 3 from 1 for 'conv2d_2/convol

参考:https://stackoverflow.com/questions/41651628/negative-dimension-size-caused-by-subtracting-3-from-1-for-conv2d

出错的主要原因如下:

默认情况下,Convolution2D (https://keras.io/layers/convolutional/)期望输入的格式是“sample、rows、cols、channels”,即“channels-last”。您的数据似乎是这种格式的(sample、channels、rows、cols)。在声明Convolution2D层时,应该能够使用可选关键字data_format = 'channels_first'修复这个问题。

在参考TensorFlow实战Google深度学习框架时,作者采用了这种方式:

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols, )
    x_validation = x_validation.reshape(x_validation.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], 5, img_cols, 1)
    x_validation = x_validation.reshape(x_validation.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

这跟上文给的链接中点赞最高的解决办法一样,但是对我不好使。我采用了第二种解决方法,加入了“ padding = 'same' ”,问题解决。

Keras导入数据集出错 ValueError: Negative dimension size caused by subtracting 3 from 1 for 'conv2d_2/convol_第1张图片

你可能感兴趣的:(深度学习)