Skip to content
E
Educatum

ReLU (Rectified Linear Unit)

Curated by Educatum AI teamLast updated:
Basic Concepts
Direct Answer

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:

ReLU(x)=max⁡(0,x)={x,x>00,x≤0,ReLU′(x)={1,x>00,x<0\text{ReLU}(x) = \max(0, x) = \begin{cases} x, & x > 0 \\ 0, & x \le 0 \end{cases}, \qquad \text{ReLU}'(x) = \begin{cases} 1, & x > 0 \\ 0, & x < 0 \end{cases}

(with subgradient [0,1][0, 1] at x=0x = 0, conventionally evaluated as 00 in autodiff libraries).

ReLU became the foundational deep learning activation for three reasons:

  1. Non-saturating positive gradient: ReLU′(x)=1\text{ReLU}'(x) = 1 for x>0x > 0 avoids the exponential vanishing gradient decay caused by sigmoid (≤0.25\le 0.25) and tanh (≤1\le 1) saturation.
  2. Computational efficiency: Requires only a simple comparison/threshold at zero instead of expensive exponentials.
  3. 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 11.
  • Dying ReLU: Occurs when a large negative gradient update pushes a neuron’s weights and bias so that wTx+b≤0w^T x + b \le 0 for every sample in the dataset; since ReLU′(z)=0\text{ReLU}'(z) = 0 for z<0z < 0, 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 xx and pre-activation z=wTx+bz = w^T x + b. If z=1.2z = 1.2, ReLU outputs 1.2 with local gradient 1.0; if z=−0.5z = -0.5, ReLU outputs 0.0 with local gradient 0.0. Normally, other samples produce positive zz and keep updating the neuron. However, if an excessively large learning rate knocks the bias down to e.g. -50, zz 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')
])
  • ReLU Variants (Leaky ReLU, PReLU, ELU, GELU, SiLU/Swish): Leaky ReLU (max⁡(αx,x)\max(\alpha x, x)) retains a small positive slope α\alpha for x<0x < 0 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 11, whereas sigmoid and tanh derivatives vanish in their tails.
  • He / Kaiming Weight Initialization: Scales initial weight variance as Var(W)=2/nin\text{Var}(W) = 2 / n_{\text{in}} 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 γ\gamma and shift β\beta), keeping inputs centered around zero so a healthy fraction stays in the positive active region rather than drifting permanently into the negative dead zone.
View all →

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