Spatial downsampling
Reduce height and width in convolutional feature maps while preserving per-channel signals.
Keras · Class
AveragePooling2D downsamples 2D spatial data by replacing local height–width windows with channel-wise averages. Configure the window, stride, padding, and channel layout to control the resulting feature-map size.
AveragePooling2D slides a rectangular window over the height and width of a 4D input and replaces each window with its channel-wise mean. The window movement is controlled by strides; padding determines whether borders are excluded or extended so the spatial dimensions follow the selected rule.
keras.layers.AveragePooling2D(pool_size, strides=None, padding="valid", data_format=None, name=None, **kwargs)| Name | Type | Description |
|---|---|---|
pool_size | int or tuple of 2 integers | Integer or tuple of 2 integers controlling the vertical and horizontal downscaling window. |
strides | int, tuple of 2 integers, or None | Integer, tuple of 2 integers, or None; controls the window step and defaults to pool_size when omitted. |
padding | string | Padding mode, either valid or same, case-insensitive. |
data_format | string | Input dimension ordering: channels_last or channels_first. |
name, **kwargs | optional | Optional layer name and additional base-layer keyword arguments. |
A 4D tensor containing pooled spatial values; channels_last yields (batch_size, pooled_height, pooled_width, channels), and channels_first yields (batch_size, channels, pooled_height, pooled_width).
import numpy as np
import keras
x = np.arange(1, 17, dtype="float32").reshape((1, 4, 4, 1))
y = keras.layers.AveragePooling2D(pool_size=2)(x)
print(y.shape)
print(y.numpy())Creates a single-channel 4×4 input and averages each non-overlapping 2×2 window, producing a 2×2 spatial output.
Reduce height and width in convolutional feature maps while preserving per-channel signals.
Smooth local activations before later convolutional or classification layers.
Control boundary handling explicitly when feature-map dimensions must follow a predictable formula.
It computes the mean of each pool_size window independently for every channel and moves that window by strides across height and width. The layer therefore reduces spatial resolution without mixing channel values.
Follow-up: How does valid padding determine the spatial size? For each spatial dimension, it uses floor((input_size - pool_size) / strides) + 1 when the input is at least as large as the pool.
pool_size sets the vertical and horizontal averaging-window dimensions. An integer is applied to both dimensions, while a two-integer tuple can specify them separately.
Follow-up: What happens when strides is None? Keras uses pool_size as the stride, which normally makes adjacent pooling windows non-overlapping.
The layer returns a 4D tensor. With channels_last its shape is (batch_size, pooled_height, pooled_width, channels); with channels_first it is (batch_size, channels, pooled_height, pooled_width).
Follow-up: How does same padding differ from valid? Same pads evenly around the spatial boundaries so the output size follows floor((input_size - 1) / strides) + 1, whereas valid adds no padding.
AveragePooling1DAveragePooling3DGlobalAveragePooling2DMaxPooling2D