As mentioned in the question, i've noticed that sometimes there are pooling layers with padding.

More specifically, I found this Keras tutorial, where there's a net which contains MaxPooling layers with padding.

If padding=same in convolutional layers, our output size (at least height and width, depth can change based on the number of filters) is the same as the input.

I expected the same with the MaxPooling layer, but Keras model.summary() (as shown in the article) shows that the output size after the pooling layers is half of the input.

What's the point of adding padding to the Pooling layer if we still get an output which is half of the input?


2 Answers 2


The whole purpose of pooling layers is to reduce the spatial dimensions (height and width). Therefore, padding is not used to prevent a spatial size reduction like it is often for convolutional layers. Instead padding might be required to process inputs with a shape that does not perfectly fit kernel size and stride of the pooling layer.

This is an example where it perfectly fits and your pooling layer does not require any padding: Pooling with kernel size 2x2, stride 2, no padding

Side note: The output dimensions are calculated using the usual formula of $O=\frac{I-K+2P}{S}+1$ with $I$ as input size, $K$ as kernel size, $P$ as padding and $S$ as stride.

However, lets take another example where it does not fit as nicely:

Pooling with kernel size 2x2, stride 2 and padding

Here you need padding since your input size is not an integer multiple of your kernel size. Therefore, you need to add padding on one side in order make it work.

So padding="same" in Keras does not mean the spatial dimensions do not change. It just means that padding is added as required to make up for overlaps when the input size and kernel size do not perfectly fit.

Also this question for a discussion what the difference between same and valid padding is for pooling layers.

  • $\begingroup$ Never thought about that and makes perfectly sense. So basically when i see networks with maxpooling without "same" padding, it's because the pooling layer fits perfectly the input map? What happens if, in the example you mentioned, i do not apply padding? I just loose some "info" or the operation of pooling will not proceed because of size "mismatch"? $\endgroup$ Commented Feb 2, 2020 at 10:45
  • 1
    $\begingroup$ @MattiaSurricchio If you choose valid padding for a pooling layer it will drop the left over inputs (see the linked question in my answer for an explanation). But I would not say this means to expect a perfectly matching input shape. Rather it is a different way to treat "leftovers": instead of augmenting left over information, e.g, by zero padding, you just drop it. $\endgroup$
    – Jonathan
    Commented Feb 2, 2020 at 11:06
  • $\begingroup$ Does pooling (when padding is used) add marginal values to both left/up and right/bottom? Or it always adds to the right and bottom? For convolutional layers (when padding is used) it may add marginal values to both left/up and right/bottom depending on the kernel size. $\endgroup$
    – mdslt
    Commented Jan 27, 2023 at 16:21

I think the reason is to make pool_size and stride arguments compatible with the output shape of previous layers. The command can be read as: in case it's not compatible, add padding all hyperparameters compatible.

As shown in the docs, the MaxPool2d() layer has padding='valid' as default. Evidently the author had a preference for 'same'.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.