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

TensorFlow · Class

layers.GlobalAveragePooling2D

A Keras layer that averages each channel across the height and width of 2D feature maps. It can either remove those spatial axes or preserve them as size-one dimensions.

How it works

The layer averages each channel across the input's two spatial axes, height and width. By default it removes those axes and returns one value per channel; setting keepdims=True preserves them as dimensions of length one. The data_format setting determines whether channels come last or first.

Syntax

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

Parameters

NameTypeDescription
data_formatstring or NoneChannel layout: either "channels_last" or "channels_first". If omitted, the layer uses the image_data_format value from the Keras configuration.
keepdimsbooleanWhether to retain the reduced spatial dimensions as length-one axes; defaults to False.
**kwargskeyword argumentsAdditional keyword arguments accepted by the Keras layer base class.

Returns

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

Key points

  • This is a Keras layer for global average pooling over two-dimensional spatial feature maps.
  • With channels_last, inputs contain batch, height, width, and channel dimensions in that order.
  • With channels_first, the channel dimension precedes the two spatial dimensions.
  • The default output retains the batch and channel axes while removing height and width.
  • When keepdims is enabled with channels_last, both reduced spatial axes remain with size one.
  • Its reduction semantics match taking a mean across the selected spatial dimensions with TensorFlow or NumPy.

Common mistakes

  • If data_format is omitted, the channel ordering comes from the Keras configuration, so ensure that configuration matches the layout of the tensors you provide.
  • The default output is rank 2 rather than a four-dimensional tensor; downstream layers expecting singleton spatial axes require keepdims=True.

Example: Average spatial feature maps

import numpy as np
import tensorflow as tf

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

A channels-last batch of feature maps is reduced over height and width, leaving one value per channel for each example.

When to use it

Convert final convolutional feature maps into one vector per example before Dense layers.

Convolutional classifier heads

Summarize spatial feature maps from a pretrained vision backbone into compact per-channel representations.

Transfer learning

Use keepdims=True when later operations require singleton height and width axes.

Shape-preserving model blocks

Interview questions

QWhat does GlobalAveragePooling2D do to a 4D feature map?

It computes the mean over height and width independently for every channel and every batch item. With the default keepdims=False, a channels-last input shaped (batch, height, width, channels) becomes (batch, channels).

Follow-up: What changes with keepdims=True? The spatial axes remain, producing (batch, 1, 1, channels) for channels-last or (batch, channels, 1, 1) for channels-first.

QHow does data_format affect this layer?

data_format selects the position of the channel axis. channels_last expects (batch, height, width, channels), while channels_first expects (batch, channels, height, width).

Follow-up: How would a mismatch show up? The layer would average the wrong axes or interpret the dimensions incorrectly, so the configured format must match the input layout.

QWhat output shape should you expect from a channels-last input?

For an input with shape (2, 4, 5, 3), the default output has shape (2, 3). The 4-by-5 spatial locations are averaged, while the three channel values remain separate.

Follow-up: Why is this useful before a classifier? It converts spatial feature maps into a compact per-example channel vector without requiring a Flatten layer over every spatial location.

  • tf.keras.layers.GlobalAveragePooling1D
  • tf.keras.layers.AveragePooling2D
  • tf.reduce_mean
  • tf.keras.layers.Dense

Source: tf.keras.layers.GlobalAveragePooling2D | TensorFlow v2.16.1. Checked 2026-10-15.