global average pooling operation for 2d data args dataformat string either channelslast or channelsfirst the ordering of the dimensions in the inputs channelslast corresponds to inputs with shape batch height width channels while channelsfirst corresponds to inputs with shape batch features height weight it defaults to the imagedataformat value found in your keras config file at keraskeras json if you never set it then it will be channelslast keepdims a boolean whether to keep the temporal dimension or not if keepdims is false default the rank of the tensor is reduced for spatial dimensions if keepdims is true the spatial dimension are retained with length 1 the behavior is the same as for tf reducemean or np mean input shape if dataformat channelslast 4d tensor with shape batchsize height width channels if dataformat channelsfirst 4d tensor with shape batchsize channels height width output shape if keepdimsfalse 2d tensor with shape batchsize channels if keepdimstrue if dataformatchannelslast 4d tensor with shape batchsize 1 1 channels if dataformatchannelsfirst 4d tensor with shape batchsize channels 1 1 example x np random rand2 4 5 3 y keras layers globalaveragepooling2dx y shape 2 3 global average pooling operation for 2d data args data_format string either channels_last or channels_first the ordering of the dimensions in the inputs channels_last corresponds to inputs with shape batch height width channels while channels_first corresponds to inputs with shape batch features height weight it defaults to the image_data_format value found in your keras config file at keras keras json if you never set it then it will be channels_last keepdims a boolean whether to keep the temporal dimension or not if keepdims is false default the rank of the tensor is reduced for spatial dimensions if keepdims is true the spatial dimension are retained with length 1 the behavior is the same as for tf reduce_mean or np mean input shape if data_format channels_last 4d tensor with shape batch_size height width channels if data_format channels_first 4d tensor with shape batch_size channels height width output shape if keepdims false 2d tensor with shape batch_size channels if keepdims true if data_format channels_last 4d tensor with shape batch_size 1 1 channels if data_format channels_first 4d tensor with shape batch_size channels 1 1 example x np random rand 2 4 5 3 y keras layers globalaveragepooling2d x y shape 2 3
from keras import ops
from keras.api_export import keras_export
from keras.layers.pooling.base_global_pooling import BaseGlobalPooling


@keras_export(
    [
        "keras.layers.GlobalAveragePooling2D",
        "keras.layers.GlobalAvgPool2D",
    ]
)
class GlobalAveragePooling2D(BaseGlobalPooling):
    

    def __init__(self, data_format=None, keepdims=False, **kwargs):
        super().__init__(
            pool_dimensions=2,
            data_format=data_format,
            keepdims=keepdims,
            **kwargs,
        )

    def call(self, inputs):
        if self.data_format == "channels_last":
            return ops.mean(inputs, axis=[1, 2], keepdims=self.keepdims)
        return ops.mean(inputs, axis=[2, 3], keepdims=self.keepdims)