Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

layers.MaxPooling2D

MaxPooling2D downsamples 2D spatial data by taking channel-wise maxima over moving windows. Configure the window, stride, padding, and channel layout to control the resulting feature-map size.

How it works

MaxPooling2D moves a rectangular window across an image-like tensor and keeps the largest value in each channel for every window position. Its stride controls movement, padding controls boundary handling, and the selected data format determines where the channel dimension appears.

Syntax

keras.layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding="valid", data_format=None, name=None, **kwargs)

Parameters

NameTypeDescription
pool_sizeint or tuple of 2 integersInteger or tuple of two integers controlling the downscaling window along the two spatial dimensions.
stridesint, tuple of 2 integers, or NoneInteger, tuple of two integers, or None specifying the window movement; None uses pool_size.
paddingstringPadding mode: valid for no padding or same for evenly padded boundaries.
data_formatstring or NoneDimension ordering, either channels_last or channels_first.
namestring or NoneOptional layer name.
**kwargskeyword argumentsAdditional keyword arguments accepted by the base Layer class.

Returns

A 4D tensor whose spatial dimensions are pooled; with channels_last its shape is (batch_size, pooled_height, pooled_width, channels), and with channels_first the channels dimension remains second.

Key points

  • Use this layer to downsample two spatial dimensions of image-like data.
  • For each window location, the layer selects the greatest value rather than averaging or learning weights.
  • Stride controls how far the pooling window moves vertically and horizontally.
  • A scalar pool_size creates a square pooling window.
  • Same padding adds boundary padding so spatial dimensions are retained under the documented conditions.
  • The default channels-last convention represents batches of height-by-width feature maps with channels last.

Common mistakes

  • With valid padding, border positions receive no extra values, so the spatial dimensions usually shrink.
  • Choose data_format consistently with the tensor layout; channels-last and channels-first expect different dimension orders.

Example: Apply 2D max pooling

import keras

model = keras.Sequential([
    keras.Input(shape=(28, 28, 3)),
    keras.layers.MaxPooling2D(pool_size=2),
])

output = model(keras.ops.random.uniform((1, 28, 28, 3)))
print(output.shape)

This model applies a 2-by-2 maximum window with the default stride and valid padding, reducing each spatial dimension while preserving channels.

When to use it

Spatial downsampling

A convolutional vision model needs smaller feature maps between convolution blocks.

Local response selection

A feature extractor should retain the strongest local activation in each channel.

Layout-aware pooling

A model uses image tensors in a framework-wide channels-first convention.

Interview questions

QWhat does MaxPooling2D compute?

The layer performs max pooling over 2D spatial data independently for each channel. It slides a window defined by pool_size using strides and outputs the maximum value in each window.

Follow-up: How does valid output size differ from same? For valid padding, each spatial dimension follows floor((input_size - pool_size) / stride) + 1; same padding keeps the height and width dimensions equal to the input when the stride permits it.

QHow do pool_size and strides work?

pool_size accepts one integer or two integers, and a single integer is reused for height and width. strides has the same forms; when it is None, it defaults to pool_size.

Follow-up: Why might explicit strides be useful? They let the pooling window overlap or skip farther between positions instead of tying movement to the window dimensions.

QWhat input and output shapes does it use?

The layer expects a rank-4 input. With channels_last, the shape is (batch_size, height, width, channels), while channels_first uses (batch_size, channels, height, width); the output keeps the same channel placement with pooled spatial dimensions.

Follow-up: What should be checked when integrating this layer into a model? Verify that the preceding and following layers use the same data-format convention and calculate the resulting spatial size before connecting shape-sensitive layers.

  • keras.layers.MaxPooling1D
  • keras.layers.GlobalMaxPooling2D
  • keras.layers.UpSampling2D
  • keras.layers.Conv2D

Source: MaxPooling2D layer - Keras. Checked 2026-10-15.