Activation functions in deep networks
Activation functions map neuron inputs to outputs and control nonlinearity, gradient flow and final output behaviour in deep networks.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Activation functions introduce the nonlinearity that lets neural networks learn complex functions. Engineers choose activation functions to control gradient flow (avoid vanishing/exploding gradients), sparsity of activations, and the correct output shape for tasks (e.g., classification with softmax); the choice affects training speed, stability and final accuracy.
Key ideas
Definition: An activation function is a deterministic function φ that maps a neuron's pre-activation scalar (or vector) input z to an output a = φ(z). For a scalar: φ: R → R. For layer-wise operations φ acts elementwise unless defined otherwise (softmax is vectorwise).
Start from a single neuron: output a = φ(z) where z = w·x + b (dot product). In backpropagation we need dL/dw and that uses chain rule: dL/dw = dL/da · da/dz · dz/dw. So the derivative da/dz = φ'(z) is essential for learning; where φ'(z) is small (near zero), gradients vanish; where it is large or unstable, gradients can explode.
Properties to consider (define when used):
- Range: set of possible outputs (e.g., sigmoid range (0,1), tanh (-1,1), ReLU [0,∞)).
- Zero-centred: whether outputs have mean near zero (helps gradient symmetry; tanh is zero-centred, sigmoid is not).
- Saturation: input regions where derivative ≈ 0 (causes vanishing gradient). Example: sigmoid saturates for large |z|.
- Sparsity: many activations exactly zero (e.g., ReLU gives sparse output for negative inputs). Sparse activations can speed computation and regularise.
- Monotonicity: if φ is monotonic, optimisation behaviours differ from non-monotonic activations (e.g., Swish, GELU are non-monotonic in some ranges).
- Smoothness: differentiability; ReLU is non-differentiable at 0 but has a subgradient; ELU and SELU are continuous with a smooth negative branch but are not differentiable at z = 0 (except ELU with α = 1), while GELU is smooth everywhere, which can help optimisation.
Common activation functions (definitions and key facts):
- Linear: φ(z) = z. If all layers are linear, the whole network is linear — cannot learn nonlinear mappings.
- Sigmoid (logistic): φ(z) = 1/(1+e^{-z}). Range (0,1). Not zero-centred; saturates for large |z|.
- Tanh: φ(z) = (e^{z}-e^{-z})/(e^{z}+e^{-z}). Range (-1,1). Zero-centred. Related to sigmoid: tanh(z) = 2·sigmoid(2z) - 1.
- ReLU (Rectified Linear Unit): φ(z) = max(0,z). Range [0,∞). Simple, sparse, avoids some saturation for positive z; can cause "dead neurons" where weights stop updating if gradient 0 on negatives.
- Leaky ReLU: φ(z) = z if z≥0 else αz with small α (e.g., 0.01). Prevents dead neurons by small slope for z<0.
- Parametric ReLU (PReLU): same as Leaky but α is learned per-channel or per-neuron.
- ELU (Exponential Linear Unit): φ(z) = z if z≥0 else α (e^{z}-1). Smooth for z<0, negative outputs push mean towards zero.
- SELU (Scaled ELU): φ(z) = λ·ELU(z) with specific constants λ, α chosen so that, under certain conditions (weights initialised and network dense), activations tend to self-normalise (mean 0, var 1). SELU is used for self-normalising networks. Typical constants from the original paper are α ≈ 1.67326 and λ ≈ 1.05070; self-normalisation assumes feedforward dense layers with LeCun normal weight initialization, inputs roughly zero-mean/unit-variance, and (if using dropout) alpha-dropout.
- Softmax: vector function for output layer: s_i(z) = e^{z_i} / Σ_j e^{z_j}. Produces probability distribution (components sum to 1, each in (0,1)). Used with cross-entropy loss for multi-class classification.
- Swish: φ(z) = z·sigmoid(βz) (β often 1). Smooth, sometimes yields better performance on deep nets.
- GELU (Gaussian Error Linear Unit): φ(z) = z·Φ(z) where Φ is the standard Gaussian CDF. A smooth, non-monotonic activation used in modern transformer models. There is an accurate tanh-based approximation.
Derivations universities ask for (examples):
- Derivative of sigmoid: start from σ(z) = 1/(1+e^{-z}). Compute dσ/dz = e^{-z}/(1+e^{-z})^2 = σ(z)(1-σ(z)). Show steps: differentiate quotient or use σ' = σ·(1-σ).
- Derivative of tanh: tanh(z) = (e^{z}-e^{-z})/(e^{z}+e^{-z}). Use identity tanh'(z) = 1 - tanh^2(z).
- Softmax Jacobian: s_i = e^{z_i}/Σ_k e^{z_k}. Then ∂s_i/∂z_j = s_i(δ_{ij} - s_j). Show partial differentiation steps.
- Softmax + cross-entropy simplification: For one-hot label y (y_k=1 for true class k), L = -Σ_k y_k log s_k. Then ∂L/∂z_j = s_j - y_j. This avoids explicit Jacobian computation in code and is numerically stable with log-sum-exp trick.
What to draw (diagram instructions):
- Plot several activation functions on the same axes: x-axis label "z (pre-activation)" from -6 to +6, y-axis "φ(z)". Draw sigmoid (S-shape approaching 0 and 1), tanh (S-shape centered at 0), ReLU (flat 0 for negative, line y=x for positive), and Leaky ReLU (line with small slope for negative). Mark point z=0 and show derivatives region qualitatively.
- For softmax, draw a simple 3-class simplex representation or draw exponential curves and the normalization denominator label.
Assumptions: definitions above assume real-valued inputs; softmax assumes finite logits and numerical tricks for stability.
Formulas
σ(z) = 1 / (1 + e^{-z})
- σ(z): sigmoid activation (dimensionless), z: pre-activation (dimensionless). Applies for scalar or elementwise use in hidden/output neurons.
σ'(z) = σ(z) · (1 - σ(z))
- σ'(z): derivative of sigmoid (dimensionless). Used in backpropagation.
tanh(z) = (e^{z} - e^{-z}) / (e^{z} + e^{-z})
- tanh(z): hyperbolic tangent activation (dimensionless). Range (-1,1).
tanh'(z) = 1 - tanh(z)^2
- tanh'(z): derivative (dimensionless).
ReLU(z) = max(0, z)
- ReLU(z): rectified linear activation (dimensionless). Non-differentiable at z = 0; subgradient defined.
ReLU'(z) = 1 if z > 0; 0 if z < 0 (subgradient at z=0 in practice taken as 0 or 1)
- ReLU'(z): derivative or subgradient (dimensionless).
LeakyReLU(z) = z if z >= 0 else α·z
- α: small constant (dimensionless), typical α = 0.01.
ELU(z) = z if z >= 0 else α·(e^{z} - 1)
- α: positive constant (dimensionless).
Softmax: s_i(z) = e^{z_i} / Σ_j e^{z_j}
- s_i: probability of class i (dimensionless), z_i: logit for class i (dimensionless). Applies to output layer for multi-class.
Cross-entropy loss (one-hot): L = -Σ_k y_k log s_k
- y_k: one-hot true label (0 or 1), s_k: softmax output.
Gradient of softmax+cross-entropy: ∂L/∂z_j = s_j - y_j
- Used in backprop without computing full Jacobian; dimensionless.
Chain rule for a single weight: dL/dw = (dL/da) · (da/dz) · (dz/dw)
- dL/dw: gradient of loss wrt weight w (dimensionless if inputs dimensionless), dz/dw = x (input).
GELU approximate: GELU(z) ≈ 0.5·z·(1 + tanh(√(2/π)·(z + 0.044715·z^3)))
- A widely used practical approximation (dimensionless).
Swish: φ(z) = z · σ(β z)
- β: parameter (dimensionless), often β = 1.
Worked examples
Example 1 — Sigmoid output and derivative (numerical): Given z = 2.0 (dimensionless). Compute σ(z) and σ'(z). Step 1: σ(2) = 1 / (1 + e^{-2}) = 1 / (1 + 0.1353352832366127) = 1 / 1.1353352832366127 = 0.8807970779778823. Step 2: σ'(2) = σ(2)·(1 - σ(2)) = 0.8807970779778823·(1 - 0.8807970779778823) = 0.8807970779778823·0.1192029220221177 = 0.1049935854035065. Final answers: σ(2) = 0.8807970779778823, σ'(2) = 0.1049935854035065 (dimensionless). For z = -2: σ(-2) = 0.11920292202211755, σ'(-2) = 0.10499358540350662.
Example 2 — Softmax and cross-entropy gradient: Logits z = [2.0, 1.0, 0.0]. True class is index 0 (one-hot y = [1,0,0]). Compute softmax and ∂L/∂z. Step 1: compute exponentials: e^{2}=7.389056099, e^{1}=2.718281828, e^{0}=1. Sum = 11.107337927. Step 2: softmax s = [7.389056099/11.107337927, 2.718281828/11.107337927, 1/11.107337927] = [0.66524096, 0.24472847, 0.09003057]. Step 3: using ∂L/∂z = s - y = [0.665241 - 1, 0.244728, 0.090031] = [-0.33475904, 0.24472847, 0.09003057]. Final answers: softmax = [0.66524096, 0.24472847, 0.09003057], gradient = [-0.33475904, 0.24472847, 0.09003057].
Example 3 — Backprop through a ReLU neuron: Single neuron: z = w·x + b, φ = ReLU, loss L = 0.5·(a - y)^2. Let x = 1.5, w = 0.5, b = -1.0, y = 0.0. Step 1: compute z = 0.5·1.5 + (-1.0) = 0.75 - 1.0 = -0.25. Step 2: a = ReLU(z) = max(0, -0.25) = 0. Step 3: L = 0.5·(0 - 0)^2 = 0. Step 4: gradient dL/da = (a - y) = 0. da/dz = ReLU'(z) = 0 because z < 0. So dL/dw = dL/da · da/dz · dz/dw = 0; weights not updated. This shows dead-region behaviour if many inputs produce negative z. Final answer: dL/dw = 0.
Common mistakes
- Forgetting to subtract max(logits) before softmax exponentiation for numerical stability.
- Differentiating softmax incorrectly: missing the δ_{ij} term in the Jacobian or not using the softmax+cross-entropy simplification.
- Treating ReLU at z=0 as differentiable without noting the subgradient (explain which convention you use).
- Mixing up ranges: claiming tanh outputs (0,1) like sigmoid.
- Forgetting that linear activation in all layers collapses the network to a single linear mapping.
For your exam
- Derivations: show steps for sigmoid and tanh derivatives, softmax Jacobian derivation, and softmax+cross-entropy simplification. Full marks: step-by-step differentiation and comment on assumptions (one-hot labels).
- Numericals: compute activations and derivatives for given z values; compute softmax probabilities and gradients. Full marks: show calculations, intermediate exponentials, sums, and final rounded answers.
- Diagram question: draw plots of sigmoid, tanh, ReLU and Leaky ReLU between -6 and +6, label axes, mark key points (0, saturation regions). Full marks: clean plots and explanation of differences.
- Discussion/short notes: pros and cons of ReLU vs sigmoid, causes of dead neurons and vanishing gradients, use-cases for softmax. Full marks: compare properties and mention mitigation (e.g., LeakyReLU, weight init, batch norm, SELU).
Quick check
- What is the derivative of tanh(z)?
- For softmax outputs s and one-hot label y, what is ∂L/∂z (cross-entropy)?
- Give one main disadvantage of sigmoid in deep hidden layers.
Answers: 1) 1 - tanh(z)^2. 2) s - y. 3) Saturation causing vanishing gradients; also not zero-centred.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?