apply multiplicative 1centered gaussian noise as it is a regularization layer it is only active at training time args rate float drop probability as with dropout the multiplicative noise will have standard deviation sqrtrate 1 rate seed integer optional random seed to enable deterministic behavior 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 apply multiplicative 1 centered gaussian noise as it is a regularization layer it is only active at training time args rate float drop probability as with dropout the multiplicative noise will have standard deviation sqrt rate 1 rate seed integer optional random seed to enable deterministic behavior 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
import math

from keras import backend
from keras import layers
from keras import ops
from keras.api_export import keras_export


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

    def __init__(self, rate, 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.seed_generator = backend.random.SeedGenerator(seed)
        self.supports_masking = True

    def call(self, inputs, training=False):
        if training and self.rate > 0:
            stddev = math.sqrt(self.rate / (1.0 - self.rate))
            return inputs * backend.random.normal(
                shape=ops.shape(inputs),
                mean=1.0,
                stddev=stddev,
                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,
        }
        return {**base_config, **config}