# CNN output shape explanation

I have the following sequential model:

model = models.Sequential()
model.summary()


and I got the following summary:

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
reshape_1 (Reshape)          (None, 1, 2, 128)         0
_________________________________________________________________
_________________________________________________________________
conv1 (Conv2D)               (None, 256, 6, 126)       1024
_________________________________________________________________
dropout_1 (Dropout)          (None, 256, 6, 126)       0
_________________________________________________________________
_________________________________________________________________
conv2 (Conv2D)               (None, 80, 9, 124)        122960
_________________________________________________________________
dropout_2 (Dropout)          (None, 80, 9, 124)        0
_________________________________________________________________
flatten_1 (Flatten)          (None, 89280)             0
_________________________________________________________________
dense1 (Dense)               (None, 256)               22855936
_________________________________________________________________
dropout_3 (Dropout)          (None, 256)               0
_________________________________________________________________
dense2 (Dense)               (None, 8)                 2056
_________________________________________________________________
activation_1 (Activation)    (None, 8)                 0
_________________________________________________________________
reshape_2 (Reshape)          (None, 8)                 0
=================================================================
Total params: 22,981,976
Trainable params: 22,981,976
Non-trainable params: 0


The model works fine. But, i want to understand something regarding conv1 layer. Why the width value have been reduced from 128 to 126 I am really confused about it shouldn't be the same value as the previous layer?

Also the same thing for the conv2 layer, the height and width have decreased from (10,126) to (9,124).

Could someone explain me why?

• I guess you have valid convolution. if you want that to be $128$ set convolution to be same. – Media Mar 29 '19 at 12:43
• @Media : after you pointed me to the padding parameter (same or valid). i digged a little bit and i understood why in the first conv layer it dropped from 128 to 126 but it does not really make sense for the second layer to drop from 126 to 124 or from 10 to 9 – A.SDR Mar 29 '19 at 12:55
• They are valid too. It pads those layers too. – Media Mar 29 '19 at 13:07
• +1 to Media, when windows are cut off by the input (image?) edges, the number of windows is smaller than the width of the input. But also, you appear to be trying to zero-pad before the conv layers, and those appear to be padding in the wrong dimensions; try specifying data_format in the padding layers too, or just skip those layers in favor of padding inside the conv layers. – Ben Reiniger Mar 29 '19 at 13:43
• i draw a small example and Now i understand Thank you – A.SDR Mar 29 '19 at 14:11

In the convolution layer, the filter (in your case 3x3) is applied to the images in order to produce the output (feature map), the filter is slide to the right and bottom by a parameter called stride (in your case it is not defined, the default is 1). Now if padding='valid' the output dimension will change, but if you change it to padding='same' the output dimension will be the same as input and this is because of the idea of zero padding (i.e. padding image borders with zero).
It is because of the kind of convolution you've used. It is a valid convolution. If you want the output to be $$128$$, set the convolution to be same. Consider that this is also applicable to the deep layers too. They also can have either of these convolutions.