Spatial downsampling
Reduce height and width in convolutional image models while retaining channel count.
TensorFlow · Class
AveragePooling2D downsamples 2D spatial data by taking windowed means independently for each channel. Its pool size, stride, padding, and channel layout determine the resulting feature-map dimensions.
The layer slides a two-dimensional window across height and width, replacing each window with its mean independently for every channel. Window movement is controlled by strides, while padding determines whether border locations are excluded or included so the output dimensions follow the selected rule.
tf.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 | Window dimensions used to downsample height and width. An integer or pair of integers; one integer is applied to both axes. |
strides | int, tuple of 2 integers, or None | Step size of the pooling window. An integer or pair of integers; defaults to pool_size when None. |
padding | string | Padding mode: valid for no padding or same for evenly distributed spatial padding; matching is case-insensitive. |
data_format | string or None | Ordering of channel and spatial dimensions. |
name | string or None | Optional layer name. |
**kwargs | keyword arguments | Additional layer keyword arguments accepted by the Keras base layer. |
A 4D average-pooled tensor. With channels_last it has shape (batch_size, pooled_height, pooled_width, channels); with channels_first it has shape (batch_size, channels, pooled_height, pooled_width).
import tensorflow as tf
x = tf.random.normal((1, 8, 8, 3))
layer = tf.keras.layers.AveragePooling2D(pool_size=2, strides=2, padding="valid")
y = layer(x)
print(y.shape) # (1, 4, 4, 3)Creates a channels-last image batch and reduces each 2×2 spatial window to one mean value.
Reduce height and width in convolutional image models while retaining channel count.
Replace a learned or max-based reduction with a local mean for smoother feature aggregation.
Control border handling and anisotropic reduction using padding and separate two-axis settings.
AveragePooling2D computes the mean over each pool_size window in the height and width dimensions. The reduction is performed independently for each input channel, and strides determine how far the window moves between outputs.
Follow-up: How does valid padding determine an output dimension? For one spatial axis, it is floor((input_size - pool_size) / stride) + 1 when the input is at least as large as the pool.
pool_size accepts one integer or a pair of integers; a single integer is reused for both spatial dimensions. strides has the same forms and defaults to pool_size when omitted.
Follow-up: Why might separate stride values be useful? They allow different downsampling rates along height and width, such as asymmetric feature-map reduction.
The layer accepts channels_last and channels_first layouts through data_format. Its output is rank four: (batch, pooled_height, pooled_width, channels) for channels_last, or (batch, channels, pooled_height, pooled_width) for channels_first.
Follow-up: How does same padding affect dimensions? Each spatial output dimension is floor((input_size - 1) / stride) + 1.
tf.keras.layers.AvgPool2Dtf.keras.layers.GlobalAveragePooling2Dtf.nn.avg_pool2d