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

Keras · Class

layers.AveragePooling2D

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.

How it works

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.

Syntax

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

Parameters

NameTypeDescription
pool_sizeint or tuple of 2 integersInteger or tuple of 2 integers controlling the vertical and horizontal downscaling window.
stridesint, tuple of 2 integers, or NoneInteger, tuple of 2 integers, or None; controls the window step and defaults to pool_size when omitted.
paddingstringPadding mode, either valid or same, case-insensitive.
data_formatstringInput dimension ordering: channels_last or channels_first.
name, **kwargsoptionalOptional layer name and additional base-layer keyword arguments.

Returns

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

Key points

  • Use this layer to downsample two-dimensional spatial feature maps by averaging local regions.
  • Each output value is the arithmetic mean of the corresponding local window for one channel.
  • A scalar pool_size applies equally to height and width; a pair allows asymmetric window dimensions.
  • Same padding adds balanced spatial padding so pooling can include boundary regions.
  • For the default channels-last layout, the output keeps the batch and channel axes while replacing height and width with pooled dimensions.

Common mistakes

  • Leaving strides unset makes the step size equal to pool_size, so the default 2×2 window also creates non-overlapping pooling.
  • With valid padding, border positions are not padded; choose same when edge coverage and a larger spatial result are needed.
  • data_format must match the input layout: channels_last places channels after height and width, while channels_first places them before those spatial dimensions.

Example: Downsample a 2D feature map

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.

When to use it

Spatial downsampling

Reduce height and width in convolutional feature maps while preserving per-channel signals.

Local averaging

Smooth local activations before later convolutional or classification layers.

Padding-aware architectures

Control boundary handling explicitly when feature-map dimensions must follow a predictable formula.

Interview questions

QWhat does AveragePooling2D do?

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.

QHow do pool_size and strides affect output?

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.

QWhat output shape does it produce?

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.

  • AveragePooling1D
  • AveragePooling3D
  • GlobalAveragePooling2D
  • MaxPooling2D

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