tf.keras.ops.adaptive_average_pool

Adaptive average pooling operation.

Applies an adaptive average pooling operation that automatically computes the kernel size and stride to pool the input to the specified output_size. This operation is useful when you want a fixed output size regardless of input size, commonly used in models like ResNet for global feature extraction.

inputs Tensor of rank 4. Input tensor of shape:

  • If data_format="channels_last": (batch_size, height, width, channels).
  • If data_format="channels_first": (batch_size, channels, height, width).
output_size Integer or tuple/list of 2 integers, specifying the target output spatial dimensions (output_height, output_width). If a single integer is provided, the same value is used for both dimensions.
data_format string, either "channels_last" or "channels_first". Defaults to the value found in your Keras config file at ~/.keras/keras.json. If never set, defaults to "channels_last".

A tensor of rank 4 representing the adaptive average pooled result.

Example:

x = np.random.rand(2, 64, 64, 3)
y = keras.ops.adaptive_average_pool(x, output_size=(32, 32))
y.shape
(2, 32, 32, 3)
# Works with any input size
x = np.random.rand(2, 100, 80, 3)
y = keras.ops.adaptive_average_pool(x, output_size=7)
y.shape
(2, 7, 7, 3)