leaky version of a rectified linear unit activation layer this layer allows a small gradient when the unit is not active formula python fx alpha x if x 0 fx x if x 0 example python leakyrelulayer leakyrelunegativeslope0 5 input np array10 5 0 0 5 10 result leakyrelulayerinput result 5 2 5 0 5 10 args negativeslope float 0 0 negative slope coefficient defaults to 0 3 kwargs base layer keyword arguments such as name and dtype leaky version of a rectified linear unit activation layer this layer allows a small gradient when the unit is not active formula python f x alpha x if x 0 f x x if x 0 example python leaky_relu_layer leakyrelu negative_slope 0 5 input np array 10 5 0 0 5 10 result leaky_relu_layer input result 5 2 5 0 5 10 args negative_slope float 0 0 negative slope coefficient defaults to 0 3 kwargs base layer keyword arguments such as name and dtype
import warnings

from keras import activations
from keras.api_export import keras_export
from keras.layers.layer import Layer


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

    def __init__(self, negative_slope=0.3, **kwargs):
        if "alpha" in kwargs:
            negative_slope = kwargs.pop("alpha")
            warnings.warn(
                "Argument `alpha` is deprecated. "
                "Use `negative_slope` instead."
            )
        super().__init__(**kwargs)
        if negative_slope is None or negative_slope < 0:
            raise ValueError(
                "The negative_slope value of a Leaky ReLU layer "
                "cannot be None or negative value. Expected a float."
                f" Received: negative_slope={negative_slope}"
            )
        self.supports_masking = True
        self.negative_slope = negative_slope

    def call(self, inputs):
        return activations.leaky_relu(
            inputs, negative_slope=self.negative_slope
        )

    def get_config(self):
        config = super().get_config()
        config.update({"negative_slope": self.negative_slope})
        return config

    def compute_output_shape(self, input_shape):
        return input_shape