# What's the purpose of padding with Maxpooling?

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?

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:

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:

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.

• 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"? – Mattia Surricchio Feb 2 '20 at 10:45
• @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. – Sammy Feb 2 '20 at 11:06

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'.