Foundations / Activation Functions
Activation Functions
BeginnerNon-linear activations let networks model complex functions. Without them, stacked affine layers collapse to a single affine map.
Common activations
Sigmoid
Tanh
ReLU
Leaky ReLU (α = 0.1)
GELU
Softmax (2 classes)
| Function | Formula | Range | Typical use |
|---|---|---|---|
| Sigmoid | (0, 1) | Binary output | |
| Tanh | (−1, 1) | RNN hidden states | |
| ReLU | [0, ∞) | Default for hidden layers | |
| Leaky ReLU | (−∞, ∞) | Avoid dead neurons | |
| GELU | [≈ −0.170, ∞) | Transformers | |
| Softmax | (0, 1) for C ≥ 2; sums to 1 | Multi-class output |
Choosing an activation
Φ is the standard normal cumulative distribution function. Exact GELU has an attained minimum of approximately −0.170. For softmax, m = maxⱼ xⱼ improves numerical stability without changing the result; each probability lies in (0, 1) for finite logits and C ≥ 2 (C = 1 gives probability 1).
- Start with ReLU (or GELU for transformers) for hidden layers.
- Sigmoid/tanh saturate and cause vanishing gradients in deep stacks.
- Match the output activation to the task: sigmoid for binary, softmax for multi-class, none for regression.
Interview questions & answers
Q1. Why do hidden layers need non-linear activation functions?
Answer: Composing only affine transformations still gives a single affine transformation, regardless of depth. Non-linear activations allow a network to represent curved decision boundaries and more complex functions.
Q2. What is the dying ReLU problem, and how can you address it?
Answer: A ReLU neuron can output zero for all relevant inputs when its pre-activation stays negative, leaving it with zero local gradient. A smaller learning rate, appropriate initialization or an activation with a nonzero negative-side slope, such as Leaky ReLU, can help.
Q3. When should you use sigmoid instead of softmax at the output?
Answer: Use sigmoid for binary classification or independent multi-label predictions. Use softmax when predicting one class from mutually exclusive categories because it normalizes scores into probabilities that sum to one. In practice, numerically stable loss functions often take raw logits directly.