DL Notes
Understanding Rectified Linear Unit (ReLU) - the most popular activation function in deep learning, its variants, advantages, and the dying ReLU problem.
The Rectified Linear Unit (ReLU) is the most widely used activation function in modern deep learning. Its simplicity and effectiveness revolutionized the training of deep neural networks.
Mathematical Definition
Visual Representation
Why ReLU Works So Well
- No vanishing gradient: Derivative is 1 for positive inputs
- Computational efficiency: Just a threshold operation (no exp)
- Sparse activation: ~50% of neurons output zero
- Biological plausibility: Neurons either fire or don't
Implementation
The Dying ReLU Problem
When a neuron's input is always negative, its output is always zero and it receives zero gradient. The neuron effectively "dies" and stops learning.
ReLU Variants
import torch
import torch.nn as nn
x = torch.linspace(-3, 3, 7)
# Standard ReLU
relu = nn.ReLU()
print(f"ReLU: {relu(x).data}")
# Leaky ReLU (small slope for negative)
leaky = nn.LeakyReLU(0.01)
print(f"LeakyReLU: {leaky(x).data}")
# Parametric ReLU (learned slope)
prelu = nn.PReLU()
print(f"PReLU: {prelu(x).data}")
# ELU (exponential for negative)
elu = nn.ELU(alpha=1.0)
print(f"ELU: {elu(x).data}")
# ReLU6 (capped at 6, used in mobile nets)
relu6 = nn.ReLU6()
print(f"ReLU6: {relu6(x).data}")ReLU vs Sigmoid vs Tanh
| Property | ReLU | Sigmoid | Tanh |
|---|---|---|---|
| Computation | Very fast | Slow (exp) | Slow (exp) |
| Gradient flow | Excellent (positive) | Poor (max 0.25) | Moderate |
| Sparsity | Yes (~50% zeros) | No | No |
| Dead neurons | Yes | No | No |
| Output range | [0, inf) | (0, 1) | (-1, 1) |
| Best for | Hidden layers | Output (binary) | RNN hidden |
Best Practices with ReLU
Interview Questions
- Why did ReLU enable training of much deeper networks?
ReLU's constant gradient of 1 for positive inputs prevents vanishing gradients. Unlike sigmoid (max derivative 0.25), ReLU preserves gradient magnitude through many layers.
- What causes the dying ReLU problem and how to fix it?
Large negative biases or large learning rates push neurons into permanently negative territory. Solutions: Leaky ReLU, proper initialization (He), lower learning rates, BatchNorm.
- Why is ReLU not differentiable at x=0 but still works?
In practice, the probability of x being exactly 0 is essentially zero for continuous inputs. We define the derivative as 0 or 1 at that point and it works fine.
- Why does ReLU produce sparse activations?
Roughly half the inputs to any ReLU are negative (assuming centered data), producing zero outputs. This sparsity is computationally efficient and acts as implicit regularization.
- When should you NOT use ReLU?
For output layers (use sigmoid/softmax), when you need bounded outputs, in RNN gates (need sigmoid for 0-1 gating), or when dead neurons are a persistent problem.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for ReLU Activation Function.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Deep Learning topic.
Search Terms
deep-learning, deep learning, deep, learning, activation, functions, relu, function
Related Deep Learning Topics