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.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. Why Nonlinearity Is Necessary
  5. The Sigmoid Function
  6. Hyperbolic Tangent
  7. Rectified Linear Unit (ReLU)
  8. Leaky ReLU and PReLU
  9. GELU and Swish
  10. Softmax for Classification
  11. Properties Comparison
  12. Common Pitfalls
  13. Summary
  14. Exercises

Learning Objectives

  1. Prove that without nonlinearity, any depth network collapses to a single linear transformation.
  2. Derive the sigmoid and its gradient; explain saturation and vanishing gradients.
  3. Analyze ReLU: non-differentiability at zero, dead neurons, and unbounded activations.
  4. Derive GELU as the expected value under a Gaussian mask.
  5. Prove that softmax is the unique function satisfying certain axiomatic properties.

Notation

  • σ(z)\sigma(z) — sigmoid function
  • ϕ(z)\phi(z) — generic activation function
  • z=wTx+bz = \mathbf{w}^T\mathbf{x} + b — pre-activation
  • Φ(z)\Phi(z) — standard Gaussian CDF
  • ϕGauss(z)\phi_{\text{Gauss}}(z) — 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

-4-224-2-112

Select activations:

Sigmoidσ(x) = 1/(1+e⁻ˣ)
ReLUmax(0, x)
GELUx·Φ(x)
Observe: ReLU has zero gradient for x < 0 (dead neuron problem). GELU and Swish are smooth approximations used in modern transformers. Sigmoid saturates, causing vanishing gradients.

Why Nonlinearity Is Necessary

Theorem. A composition of affine maps is affine.

Proof. Let fl(x)=Wlx+blf_l(\mathbf{x}) = \mathbf{W}_l\mathbf{x} + \mathbf{b}_l for l=1,,Ll = 1, \ldots, L. Then:

fLf1(x)=WLW1x+(bias terms)=W~x+b~,(1)f_L \circ \cdots \circ f_1(\mathbf{x}) = \mathbf{W}_L\cdots\mathbf{W}_1\mathbf{x} + (\text{bias terms}) = \tilde{\mathbf{W}}\mathbf{x} + \tilde{\mathbf{b}}, \tag{1}

which is a single affine map regardless of LL. Adding depth without nonlinearity gains no representational power. \blacksquare


The Sigmoid Function

σ(z)=11+ez=ezez+1.(2)\sigma(z) = \frac{1}{1 + e^{-z}} = \frac{e^z}{e^z + 1}. \tag{2}

Properties:

  • Range: (0,1)(0, 1) — interpretable as probability.
  • Monotonically increasing, smooth (CC^\infty).

Derivative:

σ(z)=σ(z)(1σ(z)).(3)\sigma'(z) = \sigma(z)(1 - \sigma(z)). \tag{3}

Proof. σ(z)=ez(1+ez)2=11+ezez1+ez=σ(z)(1σ(z))\sigma'(z) = \frac{e^{-z}}{(1+e^{-z})^2} = \frac{1}{1+e^{-z}}\cdot\frac{e^{-z}}{1+e^{-z}} = \sigma(z)(1-\sigma(z)). \blacksquare

Maximum gradient: σ(0)=1/4\sigma'(0) = 1/4. For z>4|z| > 4: σ(z)<0.02\sigma'(z) < 0.02.

Saturation problem: In deep networks, the gradient through LL sigmoid layers scales as lσ(zl)(1/4)L\prod_l \sigma'(z_l) \leq (1/4)^L, causing exponential gradient decay (vanishing gradients).


Hyperbolic Tangent

tanh(z)=ezezez+ez=2σ(2z)1.(4)\tanh(z) = \frac{e^z - e^{-z}}{e^z + e^{-z}} = 2\sigma(2z) - 1. \tag{4}

Derivative: tanh(z)=1tanh2(z)\tanh'(z) = 1 - \tanh^2(z).

Advantages over sigmoid: Zero-centered output (1,1)\in (-1, 1); maximum gradient tanh(0)=1\tanh'(0) = 1 (vs. 1/41/4 for sigmoid). Still saturates for large z|z|.


Rectified Linear Unit (ReLU)

ReLU(z)=max(0,z)={zz>00z0.(5)\text{ReLU}(z) = \max(0, z) = \begin{cases} z & z > 0 \\ 0 & z \leq 0 \end{cases}. \tag{5}

Derivative:

ReLU(z)={1z>00z<0.(6)\text{ReLU}'(z) = \begin{cases} 1 & z > 0 \\ 0 & z < 0 \end{cases}. \tag{6}

(Undefined at z=0z = 0; in practice, use subgradient [0,1]\in [0,1].)

Advantages:

  • No saturation for z>0z > 0: gradient is exactly 1.
  • Sparse activation: approximately 50% of neurons output zero.
  • Computationally trivial: just a threshold.

Dead neuron problem: If z0z \leq 0 for all training examples (e.g., large negative bias), then ReLU(z)=0\text{ReLU}'(z) = 0 always and the neuron receives no gradient updates. It is permanently "dead."


Leaky ReLU and PReLU

LeakyReLU(z)={zz>0αzz0,α(0,1).(7)\text{LeakyReLU}(z) = \begin{cases} z & z > 0 \\ \alpha z & z \leq 0 \end{cases}, \quad \alpha \in (0, 1). \tag{7}

Typically α=0.01\alpha = 0.01. Prevents dead neurons since the gradient is α0\alpha \neq 0 for negative inputs.

PReLU (Parametric ReLU): α\alpha is a learnable parameter, optimized during training via backpropagation.


GELU and Swish

GELU (Gaussian Error Linear Unit):

GELU(z)=zΦ(z)=zP(Zz),ZN(0,1).(8)\text{GELU}(z) = z \cdot \Phi(z) = z \cdot P(Z \leq z), \quad Z \sim \mathcal{N}(0,1). \tag{8}

Interpretation: Multiply the input by the probability that a Gaussian random variable is less than it. This provides a smooth, probabilistic "gate."

Approximation: GELU(z)0.5z(1+tanh[2/π(z+0.044715z3)])\text{GELU}(z) \approx 0.5z(1 + \tanh[\sqrt{2/\pi}(z + 0.044715z^3)]).

Derivative:

GELU(z)=Φ(z)+zϕGauss(z).(9)\text{GELU}'(z) = \Phi(z) + z\phi_{\text{Gauss}}(z). \tag{9}

Swish: Swish(z)=zσ(βz)\text{Swish}(z) = z\cdot\sigma(\beta z) where β\beta is learnable. When β=1.702\beta = 1.702, Swish \approx GELU.

Both are smooth, non-monotonic (slightly negative for z<0z < 0), and have become standard in modern transformers (GPT, BERT use GELU).


Softmax for Classification

For a vector zRK\mathbf{z} \in \mathbb{R}^K (logits):

softmax(z)k=ezkj=1Kezj.(10)\text{softmax}(\mathbf{z})_k = \frac{e^{z_k}}{\sum_{j=1}^K e^{z_j}}. \tag{10}

Properties:

  • Output sums to 1: ksoftmax(z)k=1\sum_k \text{softmax}(\mathbf{z})_k = 1.
  • Translation invariant: softmax(z+c1)=softmax(z)\text{softmax}(\mathbf{z} + c\mathbf{1}) = \text{softmax}(\mathbf{z}).
  • Jacobian: softmaxizj=softmaxi(δijsoftmaxj)\frac{\partial \text{softmax}_i}{\partial z_j} = \text{softmax}_i(\delta_{ij} - \text{softmax}_j).

Temperature scaling: softmax(z/T)\text{softmax}(\mathbf{z}/T) — as T0T \to 0, concentrates on argmax\arg\max; as TT \to \infty, becomes uniform.


Properties Comparison

Key properties of common activations:

  • Sigmoid: smooth, bounded (0,1)(0,1), saturates, max gradient =0.25= 0.25
  • Tanh: smooth, bounded (1,1)(-1,1), saturates, max gradient =1= 1, zero-centered
  • ReLU: piecewise linear, unbounded, non-saturating for z>0z>0, sparse, dead neuron risk
  • Leaky ReLU: piecewise linear, unbounded, no dead neurons, non-zero everywhere
  • GELU: smooth, non-monotonic, approximately linear for large zz, 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 1/4\leq 1/4 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 (Var(w)=2/nin\text{Var}(w) = 2/n_{\text{in}}).

Pitfall 3. Forgetting numerical stability in softmax. Computing ezke^{z_k} for large zkz_k overflows. Always subtract maxkzk\max_k z_k 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 tanh(z)=2σ(2z)1\tanh(z) = 2\sigma(2z) - 1 and derive tanh(z)=1tanh2(z)\tanh'(z) = 1 - \tanh^2(z).

Exercise 2. Show that ReLU(z)=12(z+z)\text{ReLU}(z) = \frac{1}{2}(z + |z|) 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: softmax(z+c1)=softmax(z)\text{softmax}(\mathbf{z} + c\mathbf{1}) = \text{softmax}(\mathbf{z}).

Exercise 5. For a network of depth LL with sigmoid activations and all pre-activations at z=0z=0, compute the gradient magnitude at the first layer relative to the last. How does this scale with LL?