Activation Functions
A rigorous analysis of nonlinear activation functions: the necessity of nonlinearity, derivation of sigmoid/tanh saturation, the ReLU family, smoothness properties, Lipschitz constants, and the connection between activation choice and gradient flow.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Why Nonlinearity Is Necessary
- The Sigmoid Function
- Hyperbolic Tangent
- Rectified Linear Unit (ReLU)
- Leaky ReLU and PReLU
- GELU and Swish
- Softmax for Classification
- Properties Comparison
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Prove that without nonlinearity, any depth network collapses to a single linear transformation.
- Derive the sigmoid and its gradient; explain saturation and vanishing gradients.
- Analyze ReLU: non-differentiability at zero, dead neurons, and unbounded activations.
- Derive GELU as the expected value under a Gaussian mask.
- Prove that softmax is the unique function satisfying certain axiomatic properties.
Notation
- — sigmoid function
- — generic activation function
- — pre-activation
- — standard Gaussian CDF
- — standard Gaussian PDF
Core Intuition
Neural networks derive their expressive power from nonlinear activation functions applied between linear transformations. Without nonlinearity, a 100-layer network computes the same function as a single matrix multiplication. The choice of activation profoundly affects gradient flow during training, the network's ability to approximate complex functions, and computational efficiency.
Interactive: Activation Functions
Select activations:
Why Nonlinearity Is Necessary
Theorem. A composition of affine maps is affine.
Proof. Let for . Then:
which is a single affine map regardless of . Adding depth without nonlinearity gains no representational power.
The Sigmoid Function
Properties:
- Range: — interpretable as probability.
- Monotonically increasing, smooth ().
Derivative:
Proof. .
Maximum gradient: . For : .
Saturation problem: In deep networks, the gradient through sigmoid layers scales as , causing exponential gradient decay (vanishing gradients).
Hyperbolic Tangent
Derivative: .
Advantages over sigmoid: Zero-centered output ; maximum gradient (vs. for sigmoid). Still saturates for large .
Rectified Linear Unit (ReLU)
Derivative:
(Undefined at ; in practice, use subgradient .)
Advantages:
- No saturation for : gradient is exactly 1.
- Sparse activation: approximately 50% of neurons output zero.
- Computationally trivial: just a threshold.
Dead neuron problem: If for all training examples (e.g., large negative bias), then always and the neuron receives no gradient updates. It is permanently "dead."
Leaky ReLU and PReLU
Typically . Prevents dead neurons since the gradient is for negative inputs.
PReLU (Parametric ReLU): is a learnable parameter, optimized during training via backpropagation.
GELU and Swish
GELU (Gaussian Error Linear Unit):
Interpretation: Multiply the input by the probability that a Gaussian random variable is less than it. This provides a smooth, probabilistic "gate."
Approximation: .
Derivative:
Swish: where is learnable. When , Swish GELU.
Both are smooth, non-monotonic (slightly negative for ), and have become standard in modern transformers (GPT, BERT use GELU).
Softmax for Classification
For a vector (logits):
Properties:
- Output sums to 1: .
- Translation invariant: .
- Jacobian: .
Temperature scaling: — as , concentrates on ; as , becomes uniform.
Properties Comparison
Key properties of common activations:
- Sigmoid: smooth, bounded , saturates, max gradient
- Tanh: smooth, bounded , saturates, max gradient , zero-centered
- ReLU: piecewise linear, unbounded, non-saturating for , sparse, dead neuron risk
- Leaky ReLU: piecewise linear, unbounded, no dead neurons, non-zero everywhere
- GELU: smooth, non-monotonic, approximately linear for large , standard in transformers
- Swish: smooth, non-monotonic, self-gated, interpolates between linear and ReLU
Common Pitfalls
Pitfall 1. Using sigmoid in hidden layers of deep networks. The gradient factor per layer causes vanishing gradients. Use ReLU or GELU instead.
Pitfall 2. Initializing weights too large with ReLU. Pre-activations become large positive → all neurons fire → no sparsity benefit. Use He initialization ().
Pitfall 3. Forgetting numerical stability in softmax. Computing for large overflows. Always subtract before exponentiating (doesn't change the result due to translation invariance).
Summary
- Nonlinearity is essential: without it, depth is meaningless.
- Sigmoid/tanh saturate and cause vanishing gradients in deep networks.
- ReLU solved saturation but introduced dead neurons.
- GELU/Swish are smooth, non-monotonic, and standard in modern architectures.
- Softmax converts logits to probabilities for classification.
- Activation choice directly impacts gradient flow and trainability.
Exercises
Exercise 1. Prove that and derive .
Exercise 2. Show that and use this to prove it is convex.
Exercise 3. Derive the GELU derivative (equation 9) from the product rule.
Exercise 4. Prove that softmax is invariant to adding a constant: .
Exercise 5. For a network of depth with sigmoid activations and all pre-activations at , compute the gradient magnitude at the first layer relative to the last. How does this scale with ?