DEEP LEARNING · INTERVIEW QUESTION
What are activation functions? Compare ReLU, GELU, and Swish.
🟡 intermediate · Neural Network Fundamentals
Hint: Non-linearity between layers. Think about dead neurons and smooth gradients.
Answer
Activation functions introduce non-linearity, enabling neural networks to learn complex, non-linear relationships.
Comparison
| Function | Formula | Pros | Cons |
|---|---|---|---|
| ReLU | Simple, efficient | Dead neurons | |
| Leaky ReLU | No dead neurons | Extra hyperparameter | |
| GELU | Smooth, probabilistic | Slightly slower | |
| Swish/SiLU | Smooth, non-monotonic | Slightly slower |
When to Use
- GELU: Transformers (BERT, GPT, ViT) — standard choice
- ReLU/LeakyReLU: CNNs — simple and effective
- Swish/SiLU: EfficientNet, modern architectures
- Sigmoid/Tanh: Only for gates (LSTM) or output layers
python
import torch.nn as nn
# Common activations
relu = nn.ReLU()
gelu = nn.GELU()
silu = nn.SiLU() # Swish with β=1
# In a model
model = nn.Sequential(
nn.Linear(768, 3072),
nn.GELU(), # standard for transformers
nn.Linear(3072, 768),
)Key issue with ReLU: 'dead neurons' — once a neuron outputs 0, it may never activate again. GELU and Swish are smooth and avoid ReLU's entire flat negative half-line, but each still has isolated points with zero derivative. One zero output on an input does not by itself mean a ReLU unit is permanently dead.