global average pooling operation for temporal data args dataformat string either channelslast or channelsfirst the ordering of the dimensions in the inputs channelslast corresponds to inputs with shape batch steps features while channelsfirst corresponds to inputs with shape batch features steps 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 temporal dimension are retained with length 1 the behavior is the same as for tf reducemean or np mean call arguments inputs a 3d tensor mask binary tensor of shape batchsize steps indicating whether a given step should be masked excluded from the average input shape if dataformat channelslast 3d tensor with shape batchsize steps features if dataformat channelsfirst 3d tensor with shape batchsize features steps output shape if keepdimsfalse 2d tensor with shape batchsize features if keepdimstrue if dataformatchannelslast 3d tensor with shape batchsize 1 features if dataformatchannelsfirst 3d tensor with shape batchsize features 1 example x np random rand2 3 4 y keras layers globalaveragepooling1dx y shape 2 4 global average pooling operation for temporal 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 steps features while channels_first corresponds to inputs with shape batch features steps 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 temporal dimension are retained with length 1 the behavior is the same as for tf reduce_mean or np mean call arguments inputs a 3d tensor mask binary tensor of shape batch_size steps indicating whether a given step should be masked excluded from the average input shape if data_format channels_last 3d tensor with shape batch_size steps features if data_format channels_first 3d tensor with shape batch_size features steps output shape if keepdims false 2d tensor with shape batch_size features if keepdims true if data_format channels_last 3d tensor with shape batch_size 1 features if data_format channels_first 3d tensor with shape batch_size features 1 example x np random rand 2 3 4 y keras layers globalaveragepooling1d x y shape 2 4
from keras import backend
from keras import ops
from keras.api_export import keras_export
from keras.layers.pooling.base_global_pooling import BaseGlobalPooling


@keras_export(
    [
        "keras.layers.GlobalAveragePooling1D",
        "keras.layers.GlobalAvgPool1D",
    ]
)
class GlobalAveragePooling1D(BaseGlobalPooling):
    

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

    def call(self, inputs, mask=None):
        steps_axis = 1 if self.data_format == "channels_last" else 2
        if mask is not None:
            mask = backend.cast(mask, inputs[0].dtype)
            mask = ops.expand_dims(
                mask, 2 if self.data_format == "channels_last" else 1
            )
            inputs *= mask
            return ops.sum(
                inputs, axis=steps_axis, keepdims=self.keepdims
            ) / ops.sum(mask, axis=steps_axis, keepdims=self.keepdims)
        else:
            return ops.mean(inputs, axis=steps_axis, keepdims=self.keepdims)

    def compute_mask(self, inputs, mask=None):
        return None