AI Notes
Master activation functions for neural networks. Learn ReLU, sigmoid, tanh, softmax, when to use each. AI fundamentals 2024.
Introduction
Activation functions are mathematical functions applied to the output of each neuron in a neural network. Without activation functions, a neural network would simply be a series of linear transformations, and no matter how many layers you stack, the result would still be a linear function. Activation functions introduce non-linearity, enabling networks to learn complex patterns like image recognition, language understanding, and game playing.
Think of it this way: if you combine multiple linear functions (y = ax + b) by composition, you always get another linear function. But real-world problems are rarely linear. The boundary between "cat" and "dog" in pixel space is a complex, curved surface. Activation functions give networks the power to learn these curved decision boundaries.
The Major Activation Functions
Sigmoid (Logistic Function)
| Range | (0, 1) |
| Derivative | σ(x) × (1 - σ(x)) |
| Maximum derivative | 0.25 (at x = 0) |
| x = -5 | σ(x) = 0.007 (nearly 0) |
| x = 0 | σ(x) = 0.500 (exactly middle) |
| x = 5 | σ(x) = 0.993 (nearly 1) |
Sigmoid squashes any input to a value between 0 and 1, making it useful for probability outputs. However, it has serious problems for hidden layers: the maximum gradient is only 0.25, causing gradients to shrink rapidly in deep networks (vanishing gradient problem). Also, outputs are not zero-centered, which slows down gradient descent.
Tanh (Hyperbolic Tangent)
| Range | (-1, 1) |
| Derivative | 1 - tanh²(x) |
| Maximum derivative | 1.0 (at x = 0) |
| Zero-centered | Yes |
| x = -5 | tanh(x) = -0.9999 |
| x = 0 | tanh(x) = 0.0 |
| x = 5 | tanh(x) = 0.9999 |
Tanh is zero-centered (unlike sigmoid), which helps gradient descent converge faster. Its maximum gradient of 1.0 is better than sigmoid's 0.25. However, it still saturates for large inputs, causing vanishing gradients in very deep networks.
ReLU (Rectified Linear Unit)
| Range | [0, ∞) |
| Derivative | 0 if x < 0, 1 if x > 0 |
| x = -3 | ReLU(x) = 0 |
| x = 0 | ReLU(x) = 0 |
| x = 5 | ReLU(x) = 5 |
ReLU revolutionized deep learning. Its gradient is exactly 1 for positive inputs, eliminating the vanishing gradient problem. It is also computationally trivial compared to computing exponentials. Most modern deep networks use ReLU or its variants in hidden layers.
Leaky ReLU
| Range | (-∞, ∞) |
| Derivative | 1 if x > 0, α if x < 0 |
| x = -3 | LeakyReLU(x) = -0.03 |
| x = 5 | LeakyReLU(x) = 5 |
Leaky ReLU addresses the "dying ReLU" problem by allowing a small gradient for negative inputs.
Softmax (Multi-Class Output)
Softmax is used exclusively in the output layer for multi-class classification. It ensures outputs are positive and sum to 1, making them interpretable as class probabilities.
The Dead ReLU Problem
When a neuron's weights are updated such that the weighted sum is always negative, the ReLU output is always zero. Since the gradient is also zero for negative inputs, the weights never update again. The neuron is permanently "dead."
Scenario
Neuron computes: z = w·x + b
After a large gradient update, weights become very negative
For all training inputs: z < 0
ReLU(z) = 0 for ALL inputs
Gradient = 0 for ALL inputs
Neuron never recovers → "dead"
Prevention
- Use small positive bias initialization (b = 0.01)
- Use Leaky ReLU or ELU
- Use smaller learning rates
- Monitor fraction of dead neurons during training
Choosing the Right Activation Function
| Hidden Layers (default) | ReLU |
| Hidden Layers (if dying ReLU is a problem) | Leaky ReLU or ELU |
| Output Layer (binary classification) | Sigmoid |
| Output Layer (multi-class classification) | Softmax |
| Output Layer (regression) | Linear (no activation) |
| RNNs/LSTMs | Tanh (for cell state), Sigmoid (for gates) |
Worked Example: How Activation Choice Affects Learning
Consider a 5-layer network with all sigmoid activations vs all ReLU:
| Layer 1 | sigmoid(0.5) = 0.622 |
| Layer 2 | sigmoid(0.5 × 0.622) = sigmoid(0.311) = 0.577 |
| Layer 3 | sigmoid(0.5 × 0.577) = sigmoid(0.289) = 0.572 |
| Layer 4 | sigmoid(0.5 × 0.572) = sigmoid(0.286) = 0.571 |
| Layer 5 | sigmoid(0.5 × 0.571) = sigmoid(0.286) = 0.571 |
| Layer 1 | ReLU(0.5) = 0.5 |
| Layer 2 | ReLU(0.5 × 0.5) = 0.25 |
| Layer 3 | ReLU(0.5 × 0.25) = 0.125 |
| Layer 4 | ReLU(0.5 × 0.125) = 0.0625 |
| Layer 5 | ReLU(0.5 × 0.0625) = 0.03125 |
| Gradients | all exactly 1 (for positive path) or 0 |
| Sigmoid gradient at layer 1 | 0.25^5 ≈ 0.001 (vanished!) |
| ReLU gradient at layer 1 | 1×1×1×1×1 = 1 (preserved!) |
Modern Activation Functions
GELU (Gaussian Error Linear Unit): Used in Transformers (BERT, GPT). Smoothly gates values based on their magnitude: GELU(x) = x × Φ(x) where Φ is the Gaussian CDF.
Swish: f(x) = x × sigmoid(x). Self-gated, smooth, found by neural architecture search at Google. Slightly outperforms ReLU in deep networks.
Mish: f(x) = x × tanh(softplus(x)). Similar benefits to Swish with slightly better performance in some vision tasks.
Summary
Activation functions are what give neural networks their power to learn complex, non-linear patterns. ReLU and its variants dominate hidden layers due to their computational efficiency and gradient-friendly properties. Sigmoid and softmax serve specific roles in output layers for classification tasks. Understanding why each activation function works (and fails) in different contexts is essential for designing effective neural network architectures.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Activation Functions - Non-linearity in Networks.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Artificial Intelligence topic.
Search Terms
artificial-intelligence, artificial intelligence, artificial, intelligence, neural, networks, activation, functions
Related Artificial Intelligence Topics