Activation Functions
Activation functions introduce nonlinearity into neural networks. Without them, any depth of linear layers collapses to a single linear transformation. The choice of activation affects gradient flow, training speed, and representational capacity.
Requirements for a Good Activation
- Nonlinear: otherwise stacking layers is useless.
- Differentiable (almost everywhere): required for gradient-based training.
- Non-saturating (ideally): saturating activations kill gradients in deep networks.
- Computationally cheap: called billions of times per training step.
Sigmoid
Derivative: $\sigma’(z) = \sigma(z)(1 - \sigma(z))$
Maximum derivative: $0.25$ at $z = 0$.
Problems:
- Saturates at both tails: $\sigma’(z) \approx 0$ for $\lvert z \rvert \gg 0$. Gradients vanish in deep networks.
- Output not zero-centered: all outputs positive; leads to zig-zagging gradient updates.
- Expensive: requires exponential.
Use: output layer for binary classification; gating mechanisms (LSTM, GRU).
Tanh
Derivative: $\tanh’(z) = 1 - \tanh^2(z)$
Maximum derivative: $1.0$ at $z = 0$.
Advantages over sigmoid: zero-centered output. Stronger gradient at origin.
Problems: still saturates at tails; vanishing gradients in deep networks.
Use: hidden layers in RNNs and shallow networks. Largely replaced by ReLU in feedforward layers.
ReLU (Rectified Linear Unit)
Derivative:
Advantages:
- Non-saturating for $z > 0$: gradients pass through unchanged.
- Computationally trivial (comparison + threshold).
- Induces sparsity: about 50% of neurons are inactive on average.
- Empirically accelerates convergence compared to sigmoid/tanh.
Problems:
- Dying ReLU: if a neuron’s pre-activation is always negative (e.g., after a large negative gradient update), it outputs 0 permanently and receives zero gradient. Learning stops for that neuron.
- Not zero-centered.
- Not differentiable at $z = 0$ (subgradient ${0}$ used in practice).
Use: default activation for hidden layers in MLPs and CNNs.
Leaky ReLU
Typically $\alpha = 0.01$. Fixes dying ReLU by allowing a small gradient for $z < 0$.
PReLU (Parametric ReLU)
Same as Leaky ReLU but $\alpha$ is a learned parameter. Adds minimal overhead; can improve accuracy on large datasets.
ELU (Exponential Linear Unit)
- Smooth, differentiable everywhere.
- Negative outputs bring mean activations closer to zero (reduces bias shift).
- Slower to compute due to exponential.
SELU (Scaled ELU)
With specific $\lambda \approx 1.0507$ and $\alpha \approx 1.6733$. Self-normalizing: activations converge to zero mean and unit variance under mild conditions, making batch normalization unnecessary. Requires LeCun Normal initialization and fully connected architectures.
GELU (Gaussian Error Linear Unit)
where $\Phi$ is the standard normal CDF. Approximated as:
- Smooth, non-monotone.
- Stochastic interpretation: $\text{GELU}(z) = z \cdot P(Z \leq z)$ for $Z \sim \mathcal{N}(0,1)$.
- Default activation in BERT, GPT, and most modern Transformers.
SiLU / Swish
Smooth, non-monotone, unbounded above. Performs well in EfficientNet and many vision models. Close to GELU.
Softmax
- Outputs sum to 1; interpreted as probabilities.
- Numerically stable version: subtract $\max(\mathbf{z})$ before exponentiating.
- Not an activation for hidden layers; used only in the output layer for multi-class classification.
Temperature scaling: $\text{softmax}(\mathbf{z} / T)$. Low $T \to 0$ sharpens to argmax; high $T \to \infty$ flattens to uniform. Used in knowledge distillation and language model decoding.
Comparison
| Activation | Range | Saturates | Zero-centered | Dying neurons | Typical Use |
|---|---|---|---|---|---|
| Sigmoid | $(0,1)$ | Yes (both) | No | No | Output (binary), gates |
| Tanh | $(-1,1)$ | Yes (both) | Yes | No | RNNs, shallow nets |
| ReLU | $[0, \infty)$ | No (pos) | No | Yes | MLPs, CNNs |
| Leaky ReLU | $\mathbb{R}$ | No | No | No | MLPs, CNNs |
| ELU | $(-\alpha, \infty)$ | No | Near-zero mean | No | Deep MLPs |
| GELU | $\mathbb{R}$ | No | No | No | Transformers |
| Swish/SiLU | $\mathbb{R}$ | No | No | No | Vision models |
| Softmax | $(0,1)^K$ | Yes | No | No | Output (multi-class) |