spatial 1d version of dropout this layer performs the same function as dropout however it drops entire 1d feature maps instead of individual elements if adjacent frames within feature maps are strongly correlated as is normally the case in early convolution layers then regular dropout will not regularize the activations and will otherwise just result in an effective learning rate decrease in this case spatialdropout1d will help promote independence between feature maps and should be used instead args rate float between 0 and 1 fraction of the input units to drop call arguments inputs a 3d tensor training python boolean indicating whether the layer should behave in training mode applying dropout or in inference mode passthrough input shape 3d tensor with shape samples timesteps channels output shape same as input reference tompson et al 2014https arxiv orgabs1411 4280 spatial 2d version of dropout this version performs the same function as dropout however it drops entire 2d feature maps instead of individual elements if adjacent pixels within feature maps are strongly correlated as is normally the case in early convolution layers then regular dropout will not regularize the activations and will otherwise just result in an effective learning rate decrease in this case spatialdropout2d will help promote independence between feature maps and should be used instead args rate float between 0 and 1 fraction of the input units to drop dataformat channelsfirst or channelslast in channelsfirst mode the channels dimension the depth is at index 1 in channelslast mode is it at index 3 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 call arguments inputs a 4d tensor training python boolean indicating whether the layer should behave in training mode applying dropout or in inference mode passthrough input shape 4d tensor with shape samples channels rows cols if dataformat channelsfirst or 4d tensor with shape samples rows cols channels if dataformat channelslast output shape same as input reference tompson et al 2014https arxiv orgabs1411 4280 spatial 3d version of dropout this version performs the same function as dropout however it drops entire 3d feature maps instead of individual elements if adjacent voxels within feature maps are strongly correlated as is normally the case in early convolution layers then regular dropout will not regularize the activations and will otherwise just result in an effective learning rate decrease in this case spatialdropout3d will help promote independence between feature maps and should be used instead args rate float between 0 and 1 fraction of the input units to drop dataformat channelsfirst or channelslast in channelsfirst mode the channels dimension the depth is at index 1 in channelslast mode is it at index 4 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 call arguments inputs a 5d tensor training python boolean indicating whether the layer should behave in training mode applying dropout or in inference mode passthrough input shape 5d tensor with shape samples channels dim1 dim2 dim3 if dataformat channelsfirst or 5d tensor with shape samples dim1 dim2 dim3 channels if dataformat channelslast output shape same as input reference tompson et al 2014https arxiv orgabs1411 4280 spatial 1d version of dropout this layer performs the same function as dropout however it drops entire 1d feature maps instead of individual elements if adjacent frames within feature maps are strongly correlated as is normally the case in early convolution layers then regular dropout will not regularize the activations and will otherwise just result in an effective learning rate decrease in this case spatialdropout1d will help promote independence between feature maps and should be used instead args rate float between 0 and 1 fraction of the input units to drop call arguments inputs a 3d tensor training python boolean indicating whether the layer should behave in training mode applying dropout or in inference mode pass through input shape 3d tensor with shape samples timesteps channels output shape same as input reference tompson et al 2014 https arxiv org abs 1411 4280 spatial 2d version of dropout this version performs the same function as dropout however it drops entire 2d feature maps instead of individual elements if adjacent pixels within feature maps are strongly correlated as is normally the case in early convolution layers then regular dropout will not regularize the activations and will otherwise just result in an effective learning rate decrease in this case spatialdropout2d will help promote independence between feature maps and should be used instead args rate float between 0 and 1 fraction of the input units to drop data_format channels_first or channels_last in channels_first mode the channels dimension the depth is at index 1 in channels_last mode is it at index 3 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 call arguments inputs a 4d tensor training python boolean indicating whether the layer should behave in training mode applying dropout or in inference mode pass through input shape 4d tensor with shape samples channels rows cols if data_format channels_first or 4d tensor with shape samples rows cols channels if data_format channels_last output shape same as input reference tompson et al 2014 https arxiv org abs 1411 4280 spatial 3d version of dropout this version performs the same function as dropout however it drops entire 3d feature maps instead of individual elements if adjacent voxels within feature maps are strongly correlated as is normally the case in early convolution layers then regular dropout will not regularize the activations and will otherwise just result in an effective learning rate decrease in this case spatialdropout3d will help promote independence between feature maps and should be used instead args rate float between 0 and 1 fraction of the input units to drop data_format channels_first or channels_last in channels_first mode the channels dimension the depth is at index 1 in channels_last mode is it at index 4 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 call arguments inputs a 5d tensor training python boolean indicating whether the layer should behave in training mode applying dropout or in inference mode pass through input shape 5d tensor with shape samples channels dim1 dim2 dim3 if data_format channels_first or 5d tensor with shape samples dim1 dim2 dim3 channels if data_format channels_last output shape same as input reference tompson et al 2014 https arxiv org abs 1411 4280
from keras import backend
from keras import ops
from keras.api_export import keras_export
from keras.layers.input_spec import InputSpec
from keras.layers.regularization.dropout import Dropout


class BaseSpatialDropout(Dropout):
    def __init__(self, rate, seed=None, name=None, dtype=None):
        super().__init__(rate, seed=seed, name=name, dtype=dtype)

    def call(self, inputs, training=False):
        if training and self.rate > 0:
            return backend.random.dropout(
                inputs,
                self.rate,
                noise_shape=self._get_noise_shape(inputs),
                seed=self.seed_generator,
            )
        return inputs

    def get_config(self):
        return {
            "rate": self.rate,
            "seed": self.seed,
            "name": self.name,
            "dtype": self.dtype,
        }


@keras_export("keras.layers.SpatialDropout1D")
class SpatialDropout1D(BaseSpatialDropout):
    

    def __init__(self, rate, seed=None, name=None, dtype=None):
        super().__init__(rate, seed=seed, name=name, dtype=dtype)
        self.input_spec = InputSpec(ndim=3)

    def _get_noise_shape(self, inputs):
        input_shape = ops.shape(inputs)
        return (input_shape[0], 1, input_shape[2])


@keras_export("keras.layers.SpatialDropout2D")
class SpatialDropout2D(BaseSpatialDropout):
    

    def __init__(
        self, rate, data_format=None, seed=None, name=None, dtype=None
    ):
        super().__init__(rate, seed=seed, name=name, dtype=dtype)
        self.data_format = backend.standardize_data_format(data_format)
        self.input_spec = InputSpec(ndim=4)

    def _get_noise_shape(self, inputs):
        input_shape = ops.shape(inputs)
        if self.data_format == "channels_first":
            return (input_shape[0], input_shape[1], 1, 1)
        elif self.data_format == "channels_last":
            return (input_shape[0], 1, 1, input_shape[3])

    def get_config(self):
        base_config = super().get_config()
        config = {
            "data_format": self.data_format,
        }
        return {**base_config, **config}


@keras_export("keras.layers.SpatialDropout3D")
class SpatialDropout3D(BaseSpatialDropout):
    

    def __init__(
        self, rate, data_format=None, seed=None, name=None, dtype=None
    ):
        super().__init__(rate, seed=seed, name=name, dtype=dtype)
        self.data_format = backend.standardize_data_format(data_format)
        self.input_spec = InputSpec(ndim=5)

    def _get_noise_shape(self, inputs):
        input_shape = ops.shape(inputs)
        if self.data_format == "channels_first":
            return (input_shape[0], input_shape[1], 1, 1, 1)
        elif self.data_format == "channels_last":
            return (input_shape[0], 1, 1, 1, input_shape[4])

    def get_config(self):
        base_config = super().get_config()
        config = {
            "data_format": self.data_format,
        }
        return {**base_config, **config}