DL Notes
Complete overview of activation functions in deep learning - why they are needed, types, properties, and how to choose the right one for your neural network.
Activation functions introduce non-linearity into neural networks, enabling them to learn complex patterns. Without activation functions, a neural network would be equivalent to a single linear transformation regardless of depth.
Why Activation Functions Are Necessary
Without activation (all linear)
Layer 1: y1 = W1*x + b1
Layer 2: y2 = W2*y1 + b2 = W2*(W1*x + b1) + b2 = (W2*W1)*x + (W2*b1 + b2)
= W_combined * x + b_combined (still linear!)
With activation
Layer 1: y1 = sigma(W1*x + b1)
Layer 2: y2 = sigma(W2*y1 + b2) --> non-linear function of x!
Types of Activation Functions
Activation Function Properties
| Function | Range | Derivative | Vanishing Gradient | Zero-Centered |
|---|---|---|---|---|
| Sigmoid | (0, 1) | s*(1-s), max=0.25 | Yes | No |
| Tanh | (-1, 1) | 1-tanh²(x), max=1 | Yes | Yes |
| ReLU | [0, inf) | 0 or 1 | No (positive) | No |
| Leaky ReLU | (-inf, inf) | alpha or 1 | No | Almost |
| ELU | (-alpha, inf) | alpha*e^x or 1 | No | Almost |
| GELU | (-0.17, inf) | Smooth approx | No | No |
| Swish | (-0.28, inf) | x*sigmoid'(x)+sigmoid(x) | No | Almost |
Implementation in PyTorch
Choosing the Right Activation Function
| | +-- Standard | ReLU (fastest, works well) |
| | +-- Dead neuron problem | Leaky ReLU or ELU |
| | +-- Transformers | GELU |
| | +-- Research/modern | Swish (SiLU) |
| | +-- Binary classification | Sigmoid |
| | +-- Multi-class | Softmax |
| | +-- Regression (unbounded) | None (linear) |
| | +-- Regression (0 to 1) | Sigmoid |
| | +-- Regression (-1 to 1) | Tanh |
| +-- Gates | Sigmoid (0 to 1 for gating) |
| +-- Cell state | Tanh |
Practical Example: Comparing Activations
Interview Questions
- Why can't we use linear activation functions in hidden layers?
Stacking linear layers collapses into a single linear transformation. The network cannot learn non-linear decision boundaries regardless of depth.
- Why is ReLU preferred over sigmoid in deep networks?
ReLU avoids vanishing gradients (derivative is 1 for positive inputs), is computationally cheap, and promotes sparse activations. Sigmoid saturates for large inputs.
- What is the dying ReLU problem?
If a neuron's input is always negative, its gradient is always zero and it never updates. Solutions include Leaky ReLU, ELU, or careful initialization.
- When would you use sigmoid vs softmax in the output layer?
Sigmoid for binary classification or multi-label (independent probabilities). Softmax for multi-class where probabilities must sum to 1.
- What makes GELU popular in Transformers?
GELU is smooth and differentiable everywhere (unlike ReLU's sharp corner). It weights inputs by their probability under a Gaussian, providing a stochastic regularization effect.
Practical Guidelines for Choosing Activation Functions
Selecting the right activation function significantly impacts training speed, convergence, and final model performance. Here are practical guidelines based on years of deep learning research:
For Hidden Layers:
- Default choice: Start with ReLU. It works well in most cases and trains fast due to simple gradient computation.
- If neurons are dying: Switch to Leaky ReLU (α=0.01) or ELU. These maintain gradients for negative inputs.
- For Transformers and NLP: Use GELU or SiLU (Swish). Their smooth curves help attention mechanisms learn better representations.
- For RNNs/LSTMs: Tanh is standard for cell states because it centers outputs around zero, helping gradient flow through time steps.
For Output Layers:
- Binary classification: Sigmoid (outputs probability between 0 and 1)
- Multi-class classification: Softmax (outputs probability distribution summing to 1)
- Regression: Linear (no activation) — allows any output range
- Bounded regression: Sigmoid or Tanh scaled to your target range
Common Pitfalls to Avoid:
- Never use sigmoid/tanh in deep hidden layers — vanishing gradients will stall training after 3-4 layers
- Don't use ReLU in the output layer for regression — it clips negative predictions to zero
- Avoid using the same activation everywhere without thought — different layers may benefit from different functions
- Monitor for dead neurons during training — if a large percentage of activations are zero, consider switching from ReLU to Leaky ReLU
Initialization Matters: The choice of weight initialization should match your activation function. Use He initialization (variance = 2/n) for ReLU variants and Xavier/Glorot initialization (variance = 1/n) for sigmoid and tanh. Mismatched initialization can cause exploding or vanishing activations from the very first forward pass.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Activation Functions Overview.
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, overview, activation functions overview
Related Deep Learning Topics