Search code examples
pythontensorflowneural-networkkeraskeras-layer

Negative dimension size caused by subtracting 3 from 1 for 'conv2d_2/convolution'


I got this error message when declaring the input layer in Keras.

ValueError: Negative dimension size caused by subtracting 3 from 1 for 'conv2d_2/convolution' (op: 'Conv2D') with input shapes: [?,1,28,28], [3,3,28,32].

My code is like this

model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(1,28,28)))

Sample application: https://github.com/IntellijSys/tensorflow/blob/master/Keras.ipynb


Solution

  • By default, Convolution2D (https://keras.io/layers/convolutional/) expects the input to be in the format (samples, rows, cols, channels), which is "channels-last". Your data seems to be in the format (samples, channels, rows, cols). You should be able to fix this using the optional keyword data_format = 'channels_first' when declaring the Convolution2D layer.

    model.add(Convolution2D(32, (3, 3), activation='relu', input_shape=(1,28,28), data_format='channels_first'))