Search code examples
pythonkerasneural-network

Negative dimension size caused by subtracting 5 from 4


I am trying to add another layer in my model but I am getting an error. It works great with 2 layers but when adding another I get the error message

Negative dimension size caused by subtracting 5 from 4

My code:

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, kernel_size=(5, 5), input_shape=(28, 28, 1)),
    tf.keras.layers.MaxPool2D(),
    tf.keras.layers.Conv2D(64, kernel_size=(5, 5)),
    tf.keras.layers.MaxPool2D(),
    tf.keras.layers.Conv2D(128, kernel_size=(5, 5)),
    tf.keras.layers.MaxPool2D(),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(1024, activation="relu"),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])

Solution

  • Each convolution or pooling layer reduces the size of your input. The MaxPool cuts it in half (in each dimension); the Conv2D reduces it by one less than the size of the window for the convolution. So your 28x28 input becomes 24x24 after the first Conv2D, then 12x12 after the first MaxPool, then 8x8 after the second ConvD, then 4x4 after the second MaxPool. So it's now a 4x4, to which you're trying to apply a third 5x5 Conv2D.