base pooling layer def init self poolsize strides pooldimensions poolmodemax paddingvalid dataformatnone namenone kwargs super initnamename kwargs self poolsize argumentvalidation standardizetuple poolsize pooldimensions poolsize strides poolsize if strides is none else strides self strides argumentvalidation standardizetuple strides pooldimensions strides allowzerotrue self poolmode poolmode self padding padding self dataformat backend standardizedataformatdataformat self inputspec inputspecndimpooldimensions 2 def callself inputs if self poolmode max return ops maxpool inputs poolsizeself poolsize stridesself strides paddingself padding dataformatself dataformat elif self poolmode average return ops averagepool inputs poolsizeself poolsize stridesself strides paddingself padding dataformatself dataformat else raise valueerror poolmode must be either max or average received fself poolmode def computeoutputshapeself inputshape return computepoolingoutputshape inputshape self poolsize self strides self padding self dataformat def getconfigself config super getconfig config update poolsize self poolsize padding self padding strides self strides dataformat self dataformat return config base pooling layer
from keras import backend
from keras import ops
from keras.layers.input_spec import InputSpec
from keras.layers.layer import Layer
from keras.ops.operation_utils import compute_pooling_output_shape
from keras.utils import argument_validation


class BasePooling(Layer):
    

    def __init__(
        self,
        pool_size,
        strides,
        pool_dimensions,
        pool_mode="max",
        padding="valid",
        data_format=None,
        name=None,
        **kwargs,
    ):
        super().__init__(name=name, **kwargs)

        self.pool_size = argument_validation.standardize_tuple(
            pool_size, pool_dimensions, "pool_size"
        )
        strides = pool_size if strides is None else strides
        self.strides = argument_validation.standardize_tuple(
            strides, pool_dimensions, "strides", allow_zero=True
        )
        self.pool_mode = pool_mode
        self.padding = padding
        self.data_format = backend.standardize_data_format(data_format)

        self.input_spec = InputSpec(ndim=pool_dimensions + 2)

    def call(self, inputs):
        if self.pool_mode == "max":
            return ops.max_pool(
                inputs,
                pool_size=self.pool_size,
                strides=self.strides,
                padding=self.padding,
                data_format=self.data_format,
            )
        elif self.pool_mode == "average":
            return ops.average_pool(
                inputs,
                pool_size=self.pool_size,
                strides=self.strides,
                padding=self.padding,
                data_format=self.data_format,
            )
        else:
            raise ValueError(
                "`pool_mode` must be either 'max' or 'average'. Received: "
                f"{self.pool_mode}."
            )

    def compute_output_shape(self, input_shape):
        return compute_pooling_output_shape(
            input_shape,
            self.pool_size,
            self.strides,
            self.padding,
            self.data_format,
        )

    def get_config(self):
        config = super().get_config()
        config.update(
            {
                "pool_size": self.pool_size,
                "padding": self.padding,
                "strides": self.strides,
                "data_format": self.data_format,
            }
        )
        return config