ML Notes
Complete guide to activation functions including ReLU, sigmoid, tanh, Leaky ReLU, softmax with mathematical foundations, gradient behavior, and selection guidelines.
Activation functions are the secret ingredient that transforms neural networks from simple linear models into powerful function approximators capable of learning any pattern. Without activation functions, stacking multiple layers would be mathematically equivalent to a single linear transformation — no matter how deep your network, it could only learn linear relationships. Activation functions introduce non-linearity at each layer, enabling deep networks to model arbitrarily complex decision boundaries and transformations.
Why Non-Linearity Is Essential
Consider stacking two linear transformations: y = W₂(W₁x + b₁) + b₂. This simplifies to y = (W₂W₁)x + (W₂b₁ + b₂) = Wx + b — still a single linear transformation. No matter how many linear layers you stack, the result is always linear. But insert a non-linear activation function σ between layers — y = W₂σ(W₁x + b₁) + b₂ — and suddenly the network can represent curves, boundaries, and complex mappings that linear models cannot.
Here is the key insight: activation functions allow each neuron to decide whether and how strongly to "fire" based on its input, creating a hierarchy of increasingly abstract representations through the network layers. This biological inspiration (neurons either fire or do not fire) translates into mathematical power.
The Classic Activation Functions
Sigmoid (Logistic)
The sigmoid function squashes any input to the range (0, 1), making it interpretable as a probability. It was historically popular but has largely been replaced in hidden layers by ReLU due to gradient issues.
σ(x) = 1 / (1 + e^(-x))
Properties:
Output range: (0, 1)
σ(0) = 0.5
Derivative: σ(x) × (1 - σ(x)) — maximum of 0.25 at x=0
Problem: The maximum gradient is only 0.25. Through multiple layers, gradients multiply: 0.25⁵ = 0.001. This "vanishing gradient" makes deep networks nearly impossible to train with sigmoid activations — early layers receive negligible gradient updates and learn extremely slowly.
Use case: Output layer for binary classification (probability output), gates in LSTM cells.
Tanh (Hyperbolic Tangent)
Tanh is a scaled and shifted sigmoid that outputs in the range (-1, 1). Being zero-centered, it generally trains faster than sigmoid because gradient updates do not have a consistent bias direction.
Better than sigmoid for hidden layers but still suffers from vanishing gradients for large inputs where the function saturates. Used in RNN architectures and as the default in some legacy networks.
ReLU (Rectified Linear Unit)
ReLU is the most important activation function in modern deep learning. It is computationally trivial, does not saturate for positive values (no vanishing gradient), and produces sparse activations (many neurons output zero), which acts as implicit regularization.
| Output range | [0, ∞) |
| Derivative | 1 if x > 0, else 0 |
| Computation | Single comparison (extremely fast) |
Why ReLU works so well: For positive inputs, the gradient is always exactly 1 — no matter how deep the network, gradients flow without diminishing. This enables training networks with hundreds of layers, something impossible with sigmoid or tanh.
The Dying ReLU Problem: Neurons that receive large negative inputs always output zero and have zero gradient — they never recover. Once dead, these neurons contribute nothing to the network. This happens when learning rates are too high early in training.
Modern Activation Function Variants
Leaky ReLU and Parametric ReLU
Leaky ReLU fixes the dying neuron problem by allowing a small gradient (typically 0.01) for negative inputs instead of zero. Parametric ReLU (PReLU) learns the optimal slope for negative inputs during training.
# Leaky ReLU: f(x) = x if x > 0, else alpha * x (alpha = 0.01)
import tensorflow as tf
layer = tf.keras.layers.LeakyReLU(alpha=0.01)
# PReLU: alpha is learned during training
layer = tf.keras.layers.PReLU()ELU (Exponential Linear Unit)
ELU produces negative outputs for negative inputs, pushing mean activations closer to zero (like batch normalization) and avoiding the dying neuron problem. However, it requires computing exponentials, making it slower than ReLU.
GELU (Gaussian Error Linear Unit)
Used in Transformers (BERT, GPT), GELU is a smooth approximation of ReLU that weighs inputs by their probability of being positive. It provides smoother gradients and slightly better performance on NLP tasks.
Swish (SiLU)
Defined as x × sigmoid(x), Swish was discovered through automated search and often outperforms ReLU in deep networks. It is smooth, non-monotonic, and used in EfficientNet.
Softmax: The Multi-Class Output Function
Softmax is not a hidden layer activation but specifically designed for multi-class classification output layers. It converts a vector of raw scores into a probability distribution that sums to 1.
Choosing the Right Activation Function
For hidden layers in most architectures, start with ReLU. If you experience dying neurons, switch to Leaky ReLU or ELU. For Transformer architectures, use GELU. For RNNs, use tanh (hidden state) and sigmoid (gates). For output layers: sigmoid for binary classification, softmax for multi-class, linear (no activation) for regression.
Activation Functions in Practice: Implementation Details
When implementing neural networks, the activation function choice interacts with weight initialization. Xavier (Glorot) initialization is designed for sigmoid and tanh, maintaining variance across layers. He initialization is designed for ReLU, accounting for the fact that ReLU zeros out half its inputs. Using the wrong initialization-activation combination leads to exploding or vanishing activations even before gradient issues appear. Most frameworks (TensorFlow, PyTorch) automatically select appropriate initialization when you specify the activation function, but understanding this relationship helps when debugging training instability.
Batch normalization also interacts with activation choice. Normalizing activations before the non-linearity ensures inputs to the activation function are centered and scaled appropriately, reducing sensitivity to initialization and allowing higher learning rates. This combination of proper initialization, batch normalization, and ReLU activation is what makes training very deep networks (50-150+ layers) practical and reliable in modern architectures like ResNet.
Experimental Comparison
In controlled experiments, ReLU networks typically train 6x faster than equivalent sigmoid networks due to the absence of saturation. Leaky ReLU provides marginal improvement over standard ReLU on most benchmarks (1-2% accuracy) but significantly reduces the risk of dead neurons. GELU shows consistent improvement over ReLU for Transformer architectures on NLP tasks (0.5-1% on GLUE benchmarks). For convolutional networks on image tasks, the difference between ReLU, Leaky ReLU, and ELU is often negligible with proper training — choose based on computational budget and whether dying neurons are observed in training logs.
Key Takeaways
Activation functions introduce the non-linearity that makes deep learning powerful. ReLU is the default for hidden layers due to its simplicity and gradient-friendly properties. Sigmoid and softmax serve specific roles in output layers. Understand the vanishing gradient problem to appreciate why ReLU dominates modern architectures. When ReLU fails (dying neurons), Leaky ReLU and ELU provide robust alternatives. The choice of activation function directly impacts training speed, convergence, and final model quality.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Activation Functions in Neural Networks.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, deep, activation, functions, activation functions in neural networks
Related Machine Learning Topics