ReLU (Rectified Linear Unit)
ReLU (Rectified Linear Unit) is a piecewise-linear activation function defined as f(x) = max(0, x), passing positive inputs unchanged and clamping negative inputs to zero. Its constant derivative of 1 for positive values preserves gradient flow and speeds up training compared to sigmoid or tanh, though units can suffer from the dying ReLU problem when pre-activations stay persistently negative.
Study Card: ReLU (Rectified Linear Unit)
Direct Answer
ReLU (Rectified Linear Unit) is a piecewise-linear activation function defined as:
(with subgradient at , conventionally evaluated as in autodiff libraries).
ReLU became the foundational deep learning activation for three reasons:
- Non-saturating positive gradient: for avoids the exponential vanishing gradient decay caused by sigmoid () and tanh () saturation.
- Computational efficiency: Requires only a simple comparison/threshold at zero instead of expensive exponentials.
- Sparse activations: Zeroing negative pre-activations induces representational sparsity.
Its main drawback is the dying ReLU problem: if a neuron’s pre-activations become negative for all training examples, its gradient remains zero permanently.
Key Terms
- Activation Function: Element-wise non-linearity applied to neuron pre-activations; piecewise-linear ReLU units allow deep networks to represent exponentially many linear regions.
- Vanishing Gradient Problem: Attenuation of backpropagated gradients through deep layers; mitigated on active ReLU paths where local derivatives equal .
- Dying ReLU: Occurs when a large negative gradient update pushes a neuron’s weights and bias so that for every sample in the dataset; since for , no non-zero gradient can flow back to recover that unit. Mitigated via careful learning rates, He/Kaiming initialization, Batch Normalization, or leaky variants (Leaky ReLU, GELU, SiLU).
- Backpropagation: Reverse-mode gradient computation multiplying upstream error signals by local activation derivatives.
Example
Consider a hidden unit with input and pre-activation . If , ReLU outputs 1.2 with local gradient 1.0; if , ReLU outputs 0.0 with local gradient 0.0. Normally, other samples produce positive and keep updating the neuron. However, if an excessively large learning rate knocks the bias down to e.g. -50, may stay negative for every sample in the mini-batch—freezing that neuron’s output at 0 indefinitely.
Code Implementation
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
def relu(x):
"""ReLU activation function."""
return np.maximum(0, x)
# Plot ReLU
x = np.linspace(-5, 5, 100)
y = relu(x)
plt.plot(x, y)
plt.xlabel("Input")
plt.ylabel("Output")
plt.title("ReLU Activation Function")
plt.grid(True)
plt.show()
# Example usage in a Keras Sequential model
model = keras.Sequential([
keras.Input(shape=(784,)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
Related Concepts
- ReLU Variants (Leaky ReLU, PReLU, ELU, GELU, SiLU/Swish): Leaky ReLU () retains a small positive slope for to prevent dead neurons, while smooth activations like GELU and SiLU are widely used in modern Transformers and vision backbones.
- Vanishing Gradient Problem: Active ReLU units propagate gradients with multiplier , whereas sigmoid and tanh derivatives vanish in their tails.
- He / Kaiming Weight Initialization: Scales initial weight variance as specifically to account for ReLU zeroing out roughly half of the pre-activations.
- Batch Normalization: Normalizes pre-activations across each mini-batch to zero mean and unit variance (with learnable scale and shift ), keeping inputs centered around zero so a healthy fraction stays in the positive active region rather than drifting permanently into the negative dead zone.