applies dropout to the input the dropout layer randomly sets input units to 0 with a frequency of rate at each step during training time which helps prevent overfitting inputs not set to 0 are scaled up by 1 1 rate such that the sum over all inputs is unchanged note that the dropout layer only applies when training is set to true in call such that no values are dropped during inference when using model fit training will be appropriately set to true automatically in other contexts you can set the argument explicitly to true when calling the layer this is in contrast to setting trainablefalse for a dropout layer trainable does not affect the layer s behavior as dropout does not have any variablesweights that can be frozen during training args rate float between 0 and 1 fraction of the input units to drop noiseshape 1d integer tensor representing the shape of the binary dropout mask that will be multiplied with the input for instance if your inputs have shape batchsize timesteps features and you want the dropout mask to be the same for all timesteps you can use noiseshapebatchsize 1 features seed a python integer to use as random seed call arguments inputs input tensor of any rank training python boolean indicating whether the layer should behave in training mode adding dropout or in inference mode doing nothing applies dropout to the input the dropout layer randomly sets input units to 0 with a frequency of rate at each step during training time which helps prevent overfitting inputs not set to 0 are scaled up by 1 1 rate such that the sum over all inputs is unchanged note that the dropout layer only applies when training is set to true in call such that no values are dropped during inference when using model fit training will be appropriately set to true automatically in other contexts you can set the argument explicitly to true when calling the layer this is in contrast to setting trainable false for a dropout layer trainable does not affect the layer s behavior as dropout does not have any variables weights that can be frozen during training args rate float between 0 and 1 fraction of the input units to drop noise_shape 1d integer tensor representing the shape of the binary dropout mask that will be multiplied with the input for instance if your inputs have shape batch_size timesteps features and you want the dropout mask to be the same for all timesteps you can use noise_shape batch_size 1 features seed a python integer to use as random seed call arguments inputs input tensor of any rank training python boolean indicating whether the layer should behave in training mode adding dropout or in inference mode doing nothing
from keras import backend
from keras.api_export import keras_export
from keras.layers.layer import Layer


@keras_export("keras.layers.Dropout")
class Dropout(Layer):
    

    def __init__(self, rate, noise_shape=None, seed=None, **kwargs):
        super().__init__(**kwargs)
        if not 0 <= rate <= 1:
            raise ValueError(
                f"Invalid value received for argument "
                "`rate`. Expected a float value between 0 and 1. "
                f"Received: rate={rate}"
            )
        self.rate = rate
        self.seed = seed
        self.noise_shape = noise_shape
        self.seed_generator = backend.random.SeedGenerator(seed)
        self.supports_masking = True
        self.built = True

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

    def compute_output_shape(self, input_shape):
        return input_shape

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