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

TensorFlow · Class

layers.AveragePooling2D

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.

How it works

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.

Syntax

tf.keras.layers.AveragePooling2D(pool_size, strides=None, padding='valid', data_format=None, name=None, **kwargs)

Parameters

NameTypeDescription
pool_sizeint or tuple of 2 integersWindow dimensions used to downsample height and width. An integer or pair of integers; one integer is applied to both axes.
stridesint, tuple of 2 integers, or NoneStep size of the pooling window. An integer or pair of integers; defaults to pool_size when None.
paddingstringPadding mode: valid for no padding or same for evenly distributed spatial padding; matching is case-insensitive.
data_formatstring or NoneOrdering of channel and spatial dimensions.
namestring or NoneOptional layer name.
**kwargskeyword argumentsAdditional layer keyword arguments accepted by the Keras base layer.

Returns

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).

Key points

  • This Keras layer performs average pooling over two spatial dimensions.
  • Each output location contains the mean of its corresponding spatial window.
  • Channels are reduced spatially without mixing values between channels.
  • The strides parameter controls the window's movement across height and width.
  • A scalar pool_size applies the same window length to both spatial axes.
  • The layer produces a rank-four tensor for either supported channel ordering.

Common mistakes

  • Leaving strides unset makes the window advance by the pool size, rather than by an independently chosen step.
  • With valid padding, border positions that cannot contain a complete pooling window are omitted.
  • Same padding adds border padding to preserve the spatial dimensions implied by its stride.

Example: Average-pool an image batch

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.

When to use it

Spatial downsampling

Reduce height and width in convolutional image models while retaining channel count.

Local feature averaging

Replace a learned or max-based reduction with a local mean for smoother feature aggregation.

Configurable feature-map resizing

Control border handling and anisotropic reduction using padding and separate two-axis settings.

Interview questions

QWhat operation does AveragePooling2D perform?

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.

QHow do pool_size and strides behave?

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.

QWhat input and output layouts are supported?

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.AvgPool2D
  • tf.keras.layers.GlobalAveragePooling2D
  • tf.nn.avg_pool2d

Source: tf.keras.layers.AveragePooling2D. Checked 2026-10-15.