Activation Functions
Prerequisites
- Backpropagation — Why derivatives of activations matter
🎯 Learning Objectives
- Know the formula and derivative of each major activation function
- Understand the vanishing gradient issue with sigmoid/tanh
- Explain the dying ReLU problem and its solutions
- Choose the right activation for hidden layers vs output layers
- Derive softmax for multi-class classification
1. Sigmoid
Sigmoid Function
σ(z) = 1 / (1 + e−z)
Range: (0, 1)
Derivative: σ′(z) = σ(z) · (1 − σ(z))
Properties:
- Outputs can be interpreted as probabilities (between 0 and 1).
- σ(0) = 0.5; the function is symmetric about z = 0.
- Maximum derivative: σ′(0) = 0.25.
Problems with Sigmoid
- Saturation: For |z| > 5, σ′(z) ≈ 0, causing vanishing gradients.
- Not zero-centred: Outputs are always positive, which can cause zig-zagging during gradient descent.
- Expensive: Requires computing exponentials.
2. Hyperbolic Tangent (tanh)
tanh Function
tanh(z) = (ez − e−z) / (ez + e−z) = 2σ(2z) − 1
Range: (−1, 1)
Derivative: tanh′(z) = 1 − tanh2(z)
Advantage over sigmoid: Zero-centred (outputs range from −1 to +1), so gradients don’t exhibit the systematic bias. The maximum derivative is tanh′(0) = 1 (larger than sigmoid’s 0.25).
Still suffers from: Saturation at extreme values; vanishing gradients for deep networks.
3. ReLU (Rectified Linear Unit)
ReLU
ReLU(z) = max(0, z)
Range: [0, ∞)
Derivative: ReLU′(z) = { 1 if z > 0
{ 0 if z < 0
{ undefined at z = 0 (typically set to 0 or 1)
Advantages:
- No saturation in the positive region ⇒ no vanishing gradient for z > 0.
- Computationally very cheap (just a threshold).
- Empirically leads to faster convergence than sigmoid/tanh.
- Induces sparsity: many activations are exactly 0.
Dying ReLU Problem
If a neuron’s pre-activation z is always negative (e.g., due to a large negative bias), then ReLU outputs 0 and the gradient is 0 for all inputs. The neuron never updates and is effectively “dead.” This can happen to a significant fraction of neurons if the learning rate is too high.
4. Leaky ReLU & Variants
Leaky ReLU
LeakyReLU(z) = { z if z > 0
{ αz if z ≤ 0 (typically α = 0.01)
Derivative: { 1 if z > 0
{ α if z ≤ 0
By allowing a small gradient when z < 0, Leaky ReLU prevents the dying neuron problem.
Parametric ReLU (PReLU)
Same as Leaky ReLU but α is a learnable parameter, optimised during training.
ELU (Exponential Linear Unit)
ELU(z) = { z if z > 0
{ α(ez − 1) if z ≤ 0
Derivative: { 1 if z > 0
{ ELU(z) + α if z ≤ 0
ELU pushes the mean activation closer to zero and has a smooth curve for z < 0.
5. GELU (Gaussian Error Linear Unit)
GELU
GELU(z) = z · Φ(z)
where Φ(z) is the CDF of the standard normal distribution.
Approximation: GELU(z) ≈ 0.5z(1 + tanh[√(2/π)(z + 0.044715z³)])
GELU is used in modern architectures like BERT and GPT. It provides a smooth, non-monotonic curve that acts as a soft gate.
6. Softmax (Output Layer)
Softmax Function
Applied to the output layer for K-class classification:
softmax(zi) = ezi / ∑j=1K ezj for i = 1, 2, …, K
Properties:
• Each output ∈ (0, 1)
• All outputs sum to 1 (valid probability distribution)
• Paired with cross-entropy loss in classification
Numerical Example
z = (2.0, 1.0, 0.1) for 3 classes.
e2.0 = 7.389, e1.0 = 2.718, e0.1 = 1.105
Sum = 7.389 + 2.718 + 1.105 = 11.212
softmax = (7.389/11.212, 2.718/11.212, 1.105/11.212)
= (0.659, 0.242, 0.099)
Class 1 has the highest probability (65.9%).
7. Comprehensive Comparison Table
| Function | Formula | Range | Derivative | Pros | Cons |
|---|---|---|---|---|---|
| Sigmoid | 1/(1+e−z) | (0,1) | σ(1−σ) | Probabilistic output | Vanishing gradient, not zero-centred |
| tanh | (ez−e−z)/(ez+e−z) | (−1,1) | 1−tanh² | Zero-centred | Vanishing gradient |
| ReLU | max(0,z) | [0,∞) | 0 or 1 | Fast, no saturation | Dying ReLU |
| Leaky ReLU | max(αz,z) | (−∞,∞) | α or 1 | No dying neurons | Extra hyperparameter |
| ELU | z or α(ez−1) | (−α,∞) | 1 or ELU+α | Zero-centred, smooth | Expensive for z<0 |
| GELU | z·Φ(z) | ≈(−0.17,∞) | Complex | State-of-the-art NLP | Computational cost |
| Softmax | ezi/∑ezj | (0,1) | Jacobian matrix | Multi-class probability | Output layer only |
Practical Guidelines
- Default hidden layer: Start with ReLU. It works well in most cases.
- If dying ReLU occurs: Try Leaky ReLU or ELU.
- RNN hidden layers: tanh is traditional (bounded output stabilises recurrent dynamics).
- Binary classification output: Sigmoid.
- Multi-class output: Softmax.
- Regression output: Linear (identity, no activation).
- Transformer models: GELU.
Summary
- Activation functions introduce non-linearity, enabling networks to learn complex mappings.
- Sigmoid: σ(z) = 1/(1+e−z), derivative = σ(1−σ), max 0.25.
- tanh: zero-centred, derivative = 1−tanh2, max 1.0.
- ReLU: max(0,z), avoids vanishing gradient but can “die.”
- Softmax: converts logits to probability distribution for multi-class tasks.
- For GATE: memorise formulas, derivatives, and know which activation to use when.