Classification heads
Convert convolutional feature maps into fixed-width vectors before dense prediction layers.
TensorFlow · Class
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.
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.
tf.keras.layers.GlobalMaxPooling2D(data_format=None, keepdims=False, **kwargs)| Name | Type | Description |
|---|---|---|
data_format | string or None | String selecting channels_last or channels_first dimension ordering; defaults to the image_data_format in the Keras configuration. |
keepdims | boolean | Boolean controlling whether reduced spatial dimensions are retained with length one; defaults to False. |
**kwargs | keyword arguments | Additional keyword arguments accepted by the Keras layer base class. |
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.
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.
Convert convolutional feature maps into fixed-width vectors before dense prediction layers.
Keep the strongest spatial activation for each learned feature channel.
Preserve singleton height and width axes when connecting to shape-sensitive downstream operations.
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).
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).
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.GlobalMaxPool2Dtf.keras.layers.GlobalAveragePooling2Dtf.keras.layers.MaxPooling2Dtf.keras.layers.GlobalMaxPooling1D