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

Keras · Class

layers.GlobalAveragePooling2D

GlobalAveragePooling2D averages each channel across an image feature map's height and width. It normally returns one value per channel per example, or preserves singleton spatial axes when keepdims=True.

How it works

GlobalAveragePooling2D averages each channel across the two spatial axes of a 4D image-feature tensor. By default it removes those axes, leaving one value per channel; setting keepdims=True retains them as singleton dimensions, which can help preserve convolution-compatible rank.

Syntax

keras.layers.GlobalAveragePooling2D(data_format=None, keepdims=False, **kwargs)

Parameters

NameTypeDescription
data_formatstring or NoneInput dimension ordering: either "channels_last" or "channels_first". If omitted, the Keras configuration determines it.
keepdimsbooleanWhether to retain the reduced spatial axes with length 1; defaults to False.
**kwargskeyword argumentsAdditional base-layer keyword arguments.

Returns

For keepdims=False, a 2D tensor shaped (batch_size, channels). For keepdims=True, a 4D tensor shaped (batch_size, 1, 1, channels) with channels_last or (batch_size, channels, 1, 1) with channels_first.

Key points

  • The layer summarizes two spatial dimensions by averaging each channel's values.
  • For channels_last, inputs must have batch, height, width, and channel axes in that order.
  • For channels_first, inputs place the channel axis before height and width.
  • With keepdims=False, each example is represented by one pooled value per channel.
  • With keepdims=True and channels_last, the two reduced axes remain as singleton dimensions.
  • With keepdims=True and channels_first, singleton height and width axes follow the channels axis.

Common mistakes

  • Choose data_format to match the tensor layout: channels_last expects height and width before channels, while channels_first expects channels before the spatial dimensions.
  • The default output is rank 2, so downstream layers should not assume the height and width axes remain available.

Example: Global average pooling over image features

import numpy as np
import keras

x = np.random.rand(2, 4, 5, 3).astype("float32")
y = keras.layers.GlobalAveragePooling2D()(x)
print(y.shape)  # (2, 3)

A channels-last batch is reduced across height and width, producing one value per channel for each example.

When to use it

Classifier heads

Convert convolutional feature maps into per-example channel features before Dense layers.

Compact feature representations

Reduce spatial dimensions without specifying a pooling window or stride.

Shape-preserving integration

Keep rank compatible with convolution-style tensors while replacing spatial dimensions by 1.

Interview questions

QWhat does GlobalAveragePooling2D do to a 4D feature map?

It performs global mean pooling over height and width independently for every channel. With the default keepdims=False, an input shaped (batch, height, width, channels) becomes (batch, channels).

Follow-up: What changes with keepdims=True? The spatial axes remain, each with length 1, so channels_last output is (batch, 1, 1, channels), or channels_first output is (batch, channels, 1, 1).

QHow does data_format affect this layer?

data_format selects the dimension ordering. channels_last uses (batch, height, width, channels), whereas channels_first uses (batch, channels, height, width).

Follow-up: How is the default selected? If data_format is omitted, Keras uses the image_data_format value from the Keras configuration file.

QWhat are the constructor arguments and defaults?

The constructor is keras.layers.GlobalAveragePooling2D(data_format=None, keepdims=False, **kwargs). data_format controls layout, and keepdims controls whether singleton spatial dimensions are retained.

Follow-up: How would you verify the resulting shape? Apply the layer to a representative NumPy array and inspect the output shape, as in the documentation example.

  • GlobalAveragePooling1D
  • GlobalAveragePooling3D
  • AveragePooling2D

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