Gradient descent and vanishing and exploding gradients
Gradient descent: how step-size and curvature control convergence; vanishing and exploding gradients: why deep chains (including RNNs) can cause gradients to shrink or blow up and how we mitigate them.
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
Gradient descent is the basic algorithm used to train deep networks (update parameters to reduce loss). Understanding its convergence conditions and failure modes helps an engineer choose learning rates, initialisation and stabilisation methods. The problems of vanishing gradients and exploding gradients explain why very deep feed-forward networks and recurrent networks are hard to train and motivate practical fixes (ReLU, careful initialisation, batch normalisation, gating, gradient clipping).
Key ideas
Start from first principles of optimisation and backpropagation.
Gradient: for a scalar loss function L(θ) where θ ∈ R^n, the gradient ∇_θ L is the vector of partial derivatives (∂L/∂θ_i). It points in the direction of steepest ascent in parameter space.
Gradient descent: an iterative algorithm that updates parameters against the gradient to decrease the loss:
- Update rule: θ_{k+1} = θ_k − η ∇_θ L(θ_k), where η is the learning rate (step size).
- Assumption: L is differentiable.
Convergence for simple convex quadratics (university-style derivation): if L(θ) = 1/2 θ^T H θ (H symmetric positive definite), then gradient descent gives a linear map for each step. Write eigen-decomposition H = Q Λ Q^T. In coordinates y = Q^T θ the update decouples to y_{k+1,i} = (1 − η λ_i) y_{k,i}, so each component contracts iff |1 − η λ_i| < 1. Therefore a sufficient and necessary condition for convergence from any initial θ is
- 0 < η < 2 / λ_max, where λ_max is the largest eigenvalue of H (this is often written 0 < η < 2/L where L = λ_max is the Lipschitz constant of ∇L).
- This is the standard step-size condition for a quadratic (and for smooth convex functions a conservative condition uses L, the gradient Lipschitz constant).
Hessian: matrix of second derivatives H = ∇^2_θ L. For quadratics H is constant; its eigenvalues measure curvature. Large curvature (large λ_max) forces smaller η for stable updates.
Continuous-time view: gradient descent approximates the gradient flow dθ/dt = −∇L(θ). Discretisation stability relates to step size; stiff problems (wide spread of eigenvalues) need small η for stability.
Backpropagation through a deep network (or time for RNN): represent a deep network as a composition x^L = f_L( f_{L−1}( ... f_1(x^0) ... ) ). For a scalar loss L_final(x^L), by the chain rule the gradient wrt an earlier layer's pre-activation z^l involves a product of Jacobian matrices:
- ∂L_final/∂z^l = (J^{l+1})^T (J^{l+2})^T ... (J^L)^T ∂L_final/∂z^L, where J^k = ∂z^k/∂z^{k−1} is the Jacobian of layer k.
- In RNNs, the same Jacobian multiplies many times across time steps.
Vanishing gradients: if the norms (spectral norms) of the Jacobians are typically < 1, their product decreases exponentially with depth/time. The earlier layers receive extremely small gradient signals and thus learn very slowly.
Exploding gradients: if typical Jacobian norms are > 1, their product grows exponentially, causing very large parameter updates, numerical overflow, and unstable training.
Formal spectral condition: let ρ(J) be the spectral radius (maximum absolute eigenvalue) of a typical Jacobian. Roughly, if ρ(J) < 1 we expect vanishing, if ρ(J) > 1 we expect exploding, and if ρ(J) ≈ 1 gradients can be stable. This is an informal but practically useful rule.
Practical mitigations (brief statements, each is a standard exam point):
- Activation choice: use non-saturating activations (ReLU) so φ'(z) = 1 for active units (z > 0) and 0 for inactive units (z < 0), avoiding the small derivatives common with sigmoid/tanh.
- Weight initialisation: Xavier/Glorot or He initialisation scale initial weights so that variance of activations and gradients remains roughly constant across layers.
- Batch normalisation: normalises activations per mini-batch which helps keep Jacobian norms near 1 and speeds training.
- Gradient clipping: clip gradients by norm to prevent exploding updates (useful in RNNs).
- Gated architectures: LSTM and GRU use gating to create near-identity paths for gradients, avoiding vanishing over long time spans.
- Residual/skip connections: identity shortcuts (ResNets) keep layer-to-layer Jacobians near identity, greatly alleviating vanishing/exploding gradients in very deep feed-forward nets.
Constants for common initialisations: Xavier/Glorot (for tanh/sigmoid) uses Var(W) = 2/(n_in + n_out) for normal or uniform range ±sqrt(6/(n_in + n_out)); He/Kaiming (for ReLU/Leaky ReLU) uses Var(W) = 2/n_in for normal or uniform range ±sqrt(6/n_in).
Diagram to draw for exam answers: draw a chain of layers from input x^0 to output x^L. At each step show pre-activation z^l, activation x^l = φ(z^l), and label Jacobian J^l = ∂z^l/∂z^{l−1}. Indicate gradient flow backward: ∂L/∂z^L backpropagating as sequential multiplications by (J^k)^T down to ∂L/∂z^0.
Formulas
θ_{k+1} = θ_k − η ∇_θ L(θ_k) — θ_k: parameters at step k (unit: same as parameters), η: learning rate (often dimensionless in practice), ∇_θ L: gradient of loss (loss units per parameter); applies to iterative gradient descent updates.
L(θ) = 1/2 θ^T H θ — quadratic loss with Hessian H (units: loss units); H is symmetric positive definite; used to analyse convergence.
y_{k+1,i} = (1 − η λ_i) y_{k,i} — λ_i: i-th eigenvalue of H (1/some unit depending on loss/parameter units); applies after diagonalising quadratic via eigenvectors; shows componentwise contraction.
0 < η < 2 / λ_max — λ_max: largest eigenvalue of Hessian H; gives a necessary and sufficient condition for linear convergence of gradient descent on a quadratic.
∂L/∂z^l = (J^{l+1})^T (J^{l+2})^T ... (J^L)^T ∂L/∂z^L — J^k = ∂z^k/∂z^{k−1} is the Jacobian of layer k; units: product of Jacobian matrices transforms gradient units appropriately; applies to backpropagation through a composition.
‖∂L/∂z^l‖ ≤ ‖∂L/∂z^L‖ ∏_{k=l+1}^L ‖J^k‖ — spectral/operator norms; shows that gradient magnitude is bounded by product of Jacobian norms; explains exponential decay/growth.
ρ(J) = max_i |λ_i(J)| — spectral radius of matrix J; if ρ(J) < 1 repeated multiplication tends to shrink vectors exponentially, if ρ(J) > 1 it tends to expand them.
Var(init weights) ≈ 2 / (n_{in} + n_{out}) — (Xavier/Glorot, for tanh/sigmoid) with Xavier uniform range ±sqrt(6/(n_{in}+n_{out})); for ReLU/Leaky ReLU use He/Kaiming: Var(init weights) ≈ 2 / n_{in} (normal) with He uniform range ±sqrt(6/n_{in}); n_in and n_out are fan-in and fan-out; units: variance of weight distribution.
Worked examples
Example 1 — scalar quadratic (step-by-step, units minimal): Problem: minimise L(x) = 1/2 · k · x^2 where k = 4 (unitless for clarity), initial x_0 = 0.1, use gradient descent.
- Formula: gradient ∂L/∂x = k x.
- Update: x_{k+1} = x_k − η k x_k = (1 − η k) x_k.
- For convergence from any x_0 we need |1 − η k| < 1 ⇒ 0 < η < 2/k = 2/4 = 0.5.
- If we pick η = 0.3 (within range), contraction factor = 1 − η k = 1 − 0.3·4 = 1 − 1.2 = −0.2 (absolute 0.2) so iterates alternate sign but converge geometrically with factor 0.2.
- Numerical iteration: x_1 = −0.02, x_2 = 0.004, x_3 = −0.0008. Final: after 3 steps x_3 = −8.0e-4. (Units: same units as x; here treated unitless for the loss parameter example.)
Example 2 — simple RNN scalar chain to show vanishing/exploding: Model: x_t = φ( w x_{t−1} + u_t ), assume φ' ≈ c (constant derivative) for linearised analysis, loss at final time depends on x_T. Consider gradient of loss w.r.t. x_{t} involves multiplication by w·c each step.
- Suppose w = 0.8 and c = 0.5 ⇒ product per step = 0.4 < 1. After 10 steps gradient scale = 0.4^{10} = 1.048576e-4, so gradient practically vanishes. Calculation: 0.4^{10} = (0.4^5)^2 = (0.01024)^2 = 1.048576e-4; final ~1.05e-4.
- If w = 1.2 and c = 1.0 ⇒ product = 1.2 > 1, after 10 steps scale = 1.2^{10} ≈ 6.1917, gradient has exploded by
6×. Calculation: 1.2^{10} = e^{10 ln 1.2} ≈ e^{10·0.18232} ≈ e^{1.8232} ≈ 6.19. Final **6.19**. These show exponential decay/growth with depth/time and why gating or carefully chosen w and c are important.
Example 3 — brief calculation for learning-rate upper bound in multivariate quadratic: Let H have eigenvalues {1, 4, 9}. Then λ_max = 9 so stability requires 0 < η < 2/9 ≈ 0.2222. If η = 0.2 the slowest-decaying directions correspond to eigenvalues 1 and 9 with factors 0.8 and −0.8 (magnitude 0.8), while the fastest-to-stabilise direction corresponds to eigenvalue 4 with factor 1 − 0.2·4 = 0.2. This shows ill-conditioning (spread of eigenvalues) slows convergence; full answer should mention condition number κ = λ_max/λ_min = 9.
Common mistakes
- Treating η as strictly unitless without noting gradient units: in physics-style problems check units of η·∇L to match parameter units.
- Saying vanishing/exploding gradients only occur in RNNs — they occur in any deep composition (feed-forward nets with many layers can suffer too).
- Using spectral radius ρ(J) exactly equals the norm product — spectral radius is about eigenvalues; the norm bound uses operator norm and is an inequality, not equality.
For your exam
- Short notes: explain why deep networks/RNNs suffer from vanishing gradients and list mitigation methods (ReLU, initialisation, batch norm, gating, clipping).
- Derivations: show the gradient descent convergence condition for a 1D quadratic and generalise to multivariate using eigen-decomposition; derive the Jacobian product form in backpropagation and show the bound using norms.
- Numericals: compute allowable η for a given Hessian eigenvalue set; compute gradient decay factor for given w and φ'. Be explicit about units and show all steps.
- Diagrams: draw forward chain with z^l, x^l and backward arrows showing Jacobian multiplications; label variables. A full-marks answer includes definitions, assumptions (differentiability, positive-definiteness for quadratic), step-by-step derivation, clear inequality signs, and a short discussion of practical mitigations.
Quick check
- State the gradient descent update rule.
- For L(x) = 1/2 k x^2 with k = 5, what range of η ensures convergence from any x_0?
- If typical Jacobian norm per layer is 0.7 and depth is 20, by what factor are gradients multiplied roughly? Answers: 1) θ_{k+1} = θ_k − η ∇_θ L(θ_k). 2) 0 < η < 2/5 = 0.4. 3) 0.7^{20} ≈ 7.979e-4 (about 8·10^-4).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?