Skip to content
E
Educatum

What is leaky ReLU?

Curated by Educatum AI teamLast updated:
Basic Concepts
Direct Answer

Leaky ReLU is a modified Rectified Linear Unit activation function that retains a small positive slope alpha (such as 0.01 or 0.2) for negative inputs instead of outputting zero. By keeping the gradient non-zero everywhere except at zero, it prevents neurons from becoming permanently stuck in an inactive state (the dying ReLU problem). While preserving ReLU's computational speed, it helps stabilize training in architectures prone to dead units, such as GANs or unnormalized deep networks.

Study Card: Leaky ReLU Activation Function

Direct Answer

Leaky ReLU is a variant of the standard ReLU activation designed to solve the dying ReLU problem. Whereas standard ReLU outputs 00 for all negative pre-activations—producing zero gradient whenever a neuron is inactive—Leaky ReLU allows a small linear slope α∈(0,1)\alpha \in (0, 1) (commonly α=0.01\alpha = 0.01, or 0.20.2 in architectures like DCGAN):

f(x)={xx>0αxx≤0,f′(x)={1x>0αx<0f(x) = \begin{cases} x & x > 0 \\ \alpha x & x \le 0 \end{cases}, \qquad f'(x) = \begin{cases} 1 & x > 0 \\ \alpha & x < 0 \end{cases}

Because f′(x)=α≠0f'(x) = \alpha \neq 0 for negative inputs, gradients can still flow backward through inactive units so weights can recover. Leaky ReLU retains ReLU’s negligible compute cost and avoids positive-side saturation. In modern networks with BatchNorm/LayerNorm and residual connections, standard ReLU, GELU, and SiLU (Swish) remain common defaults, while Leaky ReLU excels in GAN discriminators or networks vulnerable to dead neurons. (Note that with small α=0.01\alpha = 0.01, negative-branch gradients are still attenuated by 100×100\times.)

Key Terms

  • ReLU (Rectified Linear Unit): Piecewise-linear activation f(x)=max⁡(0,x)f(x) = \max(0, x) with zero output and zero gradient for x≤0x \le 0.
  • Dying ReLU: Failure mode where large negative updates shift a neuron’s weights and bias so w⊤x+b≤0\mathbf{w}^\top \mathbf{x} + b \le 0 for every training sample, permanently halting gradient updates to that neuron.
  • Negative Slope (α\alpha): Small constant hyperparameter controlling gradient flow in the negative half-space.
  • Piecewise Linearity: Keeps forward and backward arithmetic extremely cheap without exponentials.

Example

Suppose an unusually large learning rate or outlier batch produces a huge negative gradient step on a hidden ReLU neuron, pushing its bias strongly negative so its pre-activation w⊤x+b<0\mathbf{w}^\top \mathbf{x} + b < 0 across the entire training set. Under standard ReLU, its output and local derivative become identically 00 for all examples, so the neuron never receives another gradient update and stays dead. With Leaky ReLU (α=0.01\alpha = 0.01), an input of −0.5-0.5 outputs −0.005-0.005 with gradient 0.010.01, keeping a path open for future gradient steps to push the neuron back into its active regime.

Code Implementation

import numpy as np
import matplotlib.pyplot as plt


def leaky_relu(x, alpha=0.01):
    """Leaky ReLU activation function."""
    return np.where(x > 0, x, alpha * x)


def leaky_relu_derivative(x, alpha=0.01):
    """Subgradient of Leaky ReLU with respect to x."""
    return np.where(x > 0, 1.0, alpha)


def relu(x):
    """Standard ReLU activation function."""
    return np.maximum(0.0, x)


x = np.linspace(-5, 5, 200)
y_leaky = leaky_relu(x, alpha=0.1)  # Larger alpha=0.1 for clear visual contrast
y_relu = relu(x)

plt.figure(figsize=(8, 5))
plt.plot(x, y_leaky, label="Leaky ReLU (alpha=0.1)", lw=2)
plt.plot(x, y_relu, "--", label="Standard ReLU", lw=2)
plt.axhline(0, color="grey", lw=0.8)
plt.axvline(0, color="grey", lw=0.8)
plt.xlabel("Input (z)")
plt.ylabel("Activation f(z)")
plt.title("Leaky ReLU vs. Standard ReLU")
plt.legend()
plt.grid(True)
plt.show()

neg = np.array([-2.0, -0.5, 2.0])
print("Inputs:", neg)
print("Leaky ReLU (alpha=0.01):", leaky_relu(neg, alpha=0.01))
print("Leaky ReLU derivative:   ", leaky_relu_derivative(neg, alpha=0.01))
print("Standard ReLU:           ", relu(neg))
  • Parametric ReLU (PReLU): Learns the negative slope αc\alpha_c per channel via backpropagation rather than fixing it as a hyperparameter (risks slight overfitting on small datasets).
  • Randomized Leaky ReLU (RReLU): Samples α\alpha randomly during training and fixes it to the midpoint during inference for extra regularization.
  • ELU / GELU / SiLU (Swish): Smooth non-monotonic or negative-tail activations that also prevent dead neurons while providing continuous derivatives (GELU and SiLU are standard in modern Transformers and vision models).
  • Vanishing Gradients: Although Leaky ReLU avoids exact zero gradients for negative inputs, multiplying many α=0.01\alpha = 0.01 factors across depth still decays gradients rapidly, so proper initialization and normalization remain essential.
View all →

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.