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

TensorFlow · Class

layers.GlobalMaxPooling2D

GlobalMaxPooling2D reduces each 2D feature map to its maximum value, retaining one value per channel and batch item. Use data_format to select channel ordering and keepdims to preserve singleton spatial axes.

How it works

GlobalMaxPooling2D takes a 4D image-feature tensor and finds the maximum across its spatial height and width for every channel. By default it removes those dimensions, but keepdims=True preserves them as singleton dimensions; data_format controls whether channels are last or first.

Syntax

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

Parameters

NameTypeDescription
data_formatstring or NoneString selecting channels_last or channels_first dimension ordering; defaults to the image_data_format in the Keras configuration.
keepdimsbooleanBoolean controlling whether reduced spatial dimensions are retained with length one; defaults to False.
**kwargskeyword argumentsAdditional keyword arguments accepted by the Keras layer base class.

Returns

A pooled tensor: rank 2 with shape (batch_size, channels) when keepdims=False; otherwise rank 4 with singleton spatial dimensions, ordered according to data_format.

Key points

  • The class performs global maximum pooling over two spatial dimensions.
  • For channels-last data, inputs use batch, height, width, then channel axes.
  • For channels-first data, the channel axis comes immediately after the batch axis.
  • With the default keepdims=False, the layer returns one pooled value per channel and batch item.
  • The channels-last convention places feature channels after both spatial dimensions.

Common mistakes

  • When data_format is omitted, the layer follows the Keras image-data-format setting, which normally means channels-last.
  • The default output drops the spatial axes, so the result is rank 2 rather than retaining height and width.

Example: Global max pooling over image features

import numpy as np
import tensorflow as tf

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

The layer reduces each height-by-width feature map to its maximum, producing one value per channel for each batch item.

When to use it

Classification heads

Convert convolutional feature maps into fixed-width vectors before dense prediction layers.

Compact feature extraction

Keep the strongest spatial activation for each learned feature channel.

Shape-preserving model connections

Preserve singleton height and width axes when connecting to shape-sensitive downstream operations.

Interview questions

QWhat does GlobalMaxPooling2D compute?

It performs global max pooling on 2D spatial data. For each channel and batch item, it selects the largest value across all height and width positions, yielding a 2D tensor by default.

Follow-up: What shape is produced with channels_last and keepdims=True? A 4D tensor shaped (batch_size, 1, 1, channels).

QHow do data_format and output shapes interact?

With channels_last, the input is shaped (batch_size, height, width, channels), and the default output is (batch_size, channels). With channels_first, the input is (batch_size, channels, height, width), and the default output is still (batch_size, channels).

Follow-up: How does keepdims=True change the channels_first result? It becomes (batch_size, channels, 1, 1).

QWhat are the layer's configurable arguments?

data_format accepts channels_last or channels_first and determines the ordering of dimensions in the input and retained output. keepdims is a Boolean that decides whether reduced spatial dimensions remain with length one.

Follow-up: What setting determines the default data format when the argument is omitted? The image_data_format value in the Keras configuration file.

  • tf.keras.layers.GlobalMaxPool2D
  • tf.keras.layers.GlobalAveragePooling2D
  • tf.keras.layers.MaxPooling2D
  • tf.keras.layers.GlobalMaxPooling1D

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