CNN feature-map downsampling
Reduce height and width in convolutional image models while retaining the strongest local activations.
TensorFlow · Class
MaxPooling2D downsamples 2D spatial data by taking the maximum in sliding windows independently for each channel. Configure its window, stride, padding mode, and tensor data layout.
MaxPooling2D slides a window across an image's height and width independently for every channel, retaining the largest value in each window. The window movement is controlled by strides; padding determines whether border areas are excluded or padded so spatial dimensions can be preserved.
tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None, name=None, **kwargs)| Name | Type | Description |
|---|---|---|
pool_size | int or tuple of 2 integers | Pooling-window size along the two spatial dimensions; an integer uses the same size in both dimensions. |
strides | int, tuple of 2 integers, or None | Distance the window moves along each spatial dimension; defaults to pool_size when None. |
padding | string | Padding mode: valid adds no padding; same pads spatial borders toward equal output dimensions. |
data_format | string or None | Input dimension ordering, either channels_last or channels_first. |
name | string or None | Optional layer name. |
**kwargs | additional keyword arguments | Additional layer keyword arguments. |
A tensor containing the max-pooled 2D feature maps, with spatial dimensions determined by pool_size, strides, padding, and data_format.
import tensorflow as tf
layer = tf.keras.layers.MaxPooling2D(
pool_size=(2, 2), strides=2, padding="same"
)
x = tf.random.normal((1, 28, 28, 3))
y = layer(x)
print(y.shape) # (1, 14, 14, 3)Creates a 2×2 max-pooling layer, applies it to a channels-last image batch, and prints the downsampled shape.
Reduce height and width in convolutional image models while retaining the strongest local activations.
Lower the number of spatial positions before later convolutional or dense processing.
Use same padding when border coverage and a target spatial sizing policy matter.
It performs spatial downsampling by taking the maximum value inside each pool_size window independently for every channel. The window moves over height and width using strides.
Follow-up: How does padding affect the result? With valid padding, no border padding is added; same padding adds balanced padding around spatial edges to target the documented output sizing.
pool_size specifies the spatial window, and it may be one integer or a pair of integers. If strides is None, TensorFlow uses pool_size as the stride.
Follow-up: Why might explicit strides be useful? A smaller stride creates overlapping pooling windows, while a larger one advances farther between windows and can reduce spatial resolution more aggressively.
The layer accepts channels_last or channels_first layouts through data_format. For channels_last, inputs use batch, height, width, channels ordering; channels_first places channels before height and width.
Follow-up: What should be checked when changing data_format? The input layout and the expected output layout must match the selected setting, or dimensions may be interpreted incorrectly.
tf.keras.layers.MaxPool2Dtf.keras.layers.GlobalMaxPool2Dtf.keras.layers.AveragePooling2Dtf.keras.layers.UpSampling2D