tf.keras.layers.MaxPooling2D with padding='same'

Hi, my understanding is that when padding=‘same’ for tf.keras.layers.MaxPooling2D, the input and output shape should be the same. For example in week1 assignment 2, P1 = tfl.MaxPool2D(pool_size=(8, 8), strides=(8, 8), padding=‘same’)(A1) , if input shape is (64, 64, 8), the output shape should still be (64, 64, 8), but the assignment said the output is (8, 8, 8), why does output shape shrinks.

This thread might be helpful.

My question is about MaxPooling2D not TransposeConvolution. Your link is about TransposeConvolution

Padding is padding.

Hello @zhenzhen_su,

MaxPooling means to reduce size, so its padding is never meaning to preserve the original size, although in some cases it ends up keeping the size. The tensorflow doc should have explained the expected output shape, have you read it?

If you want to dig deep on how it works, maybe you can do some experiments yourself, and share your findings and understanding so we can discuss them later (as I am in the middle of something now)?


1 Like

In addition to the previous comments note that as a general matter, “same” padding only gets you the same output size when stride = 1 and that is typically not true with pooling layers. Here’s yet another thread which discusses that point.

That is something new! I have never thought about it from this angle. I am going to remember and test it along the way!

Thanks, Paul!