Foundations to Frontier
Tests
E. Deep Learning › E0 Core Concepts (concept pages)

Vanishing and Exploding Gradients

Backpropagation multiplies the gradient by one Jacobian per layer, so in a deep network it is a product of many factors. If their typical size is below one the gradient shrinks exponentially with depth (vanishing); if above one it grows exponentially (exploding). Saturating activations such as the sigmoid, whose derivative is at most a quarter, cause vanishing; badly scaled weights cause either. This page explains the mechanism, the forward and backward variance recursions, why ReLU with the right weight scale keeps signals level, the symptoms in training, and the remedies: careful initialisation, non-saturating activations, normalisation, residual connections and gradient clipping. In the exercises you measure forward and backward signals through 30-layer networks for different activations and scales, and discover a cancellation that keeps weight gradients balanced in ReLU networks, then test which 16-layer networks can actually be trained.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

The whispering game

In the game of telephone, a message passed along a long line of people either fades to nothing or gets distorted beyond recognition. Gradients in a deep network face the same problem. Each layer multiplies the gradient by some factor on its way back. Multiply many factors below one and the signal fades (vanishing); many above one and it blows up (exploding).

Vanishing and exploding: watch the signal across the layers
10⁰layer 1 → layer 30 · log scale (-2 to 2 decades)
activation spread at the last layer 2.9e-2 · gradient reaching layer 1 is 3.5e-1× the gradient at layer 30
Orange: forward activation spread; blue: backward gradient size. Both are flat only for the right combination of activation and scale: ReLU with scale near √2 ≈ 1.41 (He), tanh near 1 (Xavier). Sigmoid's derivative is at most 1/4, so its gradient collapses whatever the scale. Move the scale off the sweet spot and both curves tilt into straight lines on this log scale: exponential decay or growth with depth. With only 32 units per layer, even at the sweet spot the curves wander randomly by a decade or two (try new weights). Such finite-width fluctuations grow with depth divided by width.
Why engineers care

Recurrent networks for telemetry and very deep networks for perception were untrainable for years because of this effect. Recognising its symptoms (loss stuck at chance, NaN losses, early layers that never change) and knowing the fixes (initialisation, ReLU-family activations, normalisation, residual connections, clipping) is basic training hygiene.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Vanishing gradients mean…
  2. 2. Exploding gradients typically show up as…
  3. 3. The root cause is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The numbers by hand

Per-layer factor. Backprop computes δ_(l−1) = (δ_l W_lᵀ) ⊙ φ′(Z_(l−1)). Its size changes by roughly ‖W‖ × (typical φ′) per layer.

  • Sigmoid: σ′(z) = σ(z)(1 − σ(z)) ≤ 1/4. Ten sigmoid layers with unit weights shrink a gradient by (1/4)¹⁰ ≈ 10⁻⁶.
  • Tanh: its derivative is 1 at 0 but small when saturated. Units at z = 2.5 or −3 have derivatives 0.026 and 0.010.
  • ReLU: its derivative is 1 or 0. With weights of standard deviation s/√n, about half the units pass gradient, so each layer multiplies the backward signal by about s/√2.
    • s = √2 (He) gives a factor of 1 per layer.
    • s = 2 gives √2 per layer, which is 2¹⁵ ≈ 33 000 over 30 layers.
    • s = 1 gives 1/√2 per layer, which is 2⁻¹⁵ over 30 layers.

Forward variance. For ReLU, Var(h_l) ≈ (s²/2) Var(h_(l−1)). With s = 1 the activations halve in variance every layer; with s = √2 they stay level.

How many layers until trouble? A factor q per layer reaches a threshold ε after ⌈log ε / log q⌉ layers: 10 layers for q = 1/4 to reach 10⁻⁶, and 35 layers for q = 1.5 to reach 10⁶.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Maximum of the sigmoid's derivative?
  2. 2. He initialisation uses a weight standard deviation of…
  3. 3. With ReLU and scale s = 1, activation variance per layer…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

Measuring it

Exercise 1. Thirty layers of width 64, batch 64, with loss = sum of the outputs. Columns: the backward signal ‖∂L/∂h‖ entering layer 1 relative to layer 30; the weight-gradient norm of layer 1 relative to layer 30; the standard deviation of the last activations; and the absolute layer-1 gradient norm.

activation, scale backward signal, layer 1 / 30 weight gradient, layer 1 / 30 last activation std layer-1 gradient
sigmoid, 1 2.8 × 10⁻¹⁹ 6.6 × 10⁻²⁰ 0.13 3.3 × 10⁻¹⁷
tanh, 1 0.080 0.87 0.11 41
ReLU, √2 (He) 4.1 0.80 0.84 1.8 × 10³
ReLU, 1 1.8 × 10⁻⁴ 0.80 2.6 × 10⁻⁵ 0.078
ReLU, 2 9.5 × 10⁴ 0.80 2.8 × 10⁴ 4.2 × 10⁷
  • Sigmoid gradients vanished by about 19–20 orders of magnitude. Layer 1 effectively receives nothing.
  • For ReLU, the wrong scale makes the backward signal and the forward activations vanish or explode exponentially, as predicted (2⁻¹⁵ and 2¹⁵ factors, roughly).
  • But the per-layer weight gradients stayed balanced (ratio 0.80 at every scale). A weight gradient is H_(l−1)ᵀ δ_l, the product of a forward activation and a backward signal. In a bias-free ReLU network the forward growth exactly cancels the backward decay. The damage shows in their absolute size instead: the layer-1 gradient was 4.2 × 10⁷ at scale 2, against 1.8 × 10³ for He. That makes a fixed learning rate wildly wrong.
torch.nn.init.kaiming_normal_(layer.weight, nonlinearity="relu")   # He: std = sqrt(2 / fan_in)
for name, p in model.named_parameters(): print(name, p.grad.norm())  # monitor per-layer gradient norms
🐍 Exercise 1: forward and backward signals in deep networks
Build 30-layer networks with sigmoid, tanh and ReLU activations and different weight scales; measure the backpropagated signal, the per-layer weight-gradient norms and the activation spread.
📝 Quick check (Level 2)
0/3 answered
  1. 1. In exercise 1, sigmoid gradients at layer 1 were smaller than at layer 30 by about…
  2. 2. For bias-free ReLU nets, per-layer weight-gradient ratios stayed equal because…
  3. 3. A scale-2 ReLU network's main problem is…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory and remedies

Jacobian products. ∂L/∂h₀ = ∂L/∂h_L · Π_l J_l, where J_l = W_lᵀ diag(φ′(z_l)). Its size behaves like the product of the J_l’s singular values. For random matrices, the log-norm performs a random walk with a drift set by E log ‖J‖, giving exponential trends with fluctuations that grow like √(depth/width) (Hanin, 2018). This is why the demo’s curves wander, even at the right scale.

Dynamical isometry. Pennington, Schoenholz and Ganguli (2017) showed that fast training needs not only a mean singular value of 1 but a concentrated spectrum. Orthogonal weights with tanh can achieve it, while ReLU cannot with Gaussian weights.

Remedies.

  • Initialisation (E0-7): Xavier for tanh, He for ReLU, orthogonal, LSUV.
  • Activations (E1): ReLU-family activations avoid saturation; SELU self-normalises.
  • Normalisation (E5): batch and layer normalisation re-standardise activations each layer.
  • Residual connections (E0-9): h_(l+1) = h_l + F(h_l) has Jacobian I + J_F, which keeps gradients near 1.
  • Gradient clipping (E0-8): caps exploding updates.
  • Gated recurrent units (LSTM, GRU) for sequences: an additive cell state.

RNNs. A recurrent network multiplies by the same W at every time step, so its gradients scale like the largest eigenvalue of W to the power of the sequence length. This is the original vanishing-gradient analysis (Hochreiter, 1991; Bengio, Simard and Frasconi, 1994).

Interview questions.

  • How do you diagnose vanishing gradients? Log per-layer gradient norms. Early layers have tiny gradients and their weights barely change, and the loss plateaus near chance.
  • Why does a residual connection help? Its Jacobian is I + J_F, so the gradient has an identity path that does not shrink with depth.
📝 Quick check (Level 3)
0/3 answered
  1. 1. A residual block's Jacobian is…
  2. 2. The original vanishing-gradient analysis concerned…
  3. 3. Finite-width fluctuations of signal size grow like…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Which deep networks can actually be trained?

History.

  • S. Hochreiter (1991, diploma thesis) analysed vanishing gradients.
  • Y. Bengio, P. Simard and P. Frasconi (1994) showed that long-term dependencies are hard to learn.
  • X. Glorot and Y. Bengio (2010) studied deep feedforward networks and introduced Xavier initialisation.
  • K. He et al. (2015) introduced initialisation for ReLU, and residual networks in 2016.
  • J. Pennington et al. (2017) introduced dynamical isometry.

Exercise 2. Sixteen-layer MLPs of width 32 on two moons (128 points), trained with full-batch SGD at learning rate 0.1 for 150 steps.

activation, scale seed 0: loss start → end, accuracy seed 1: loss start → end, accuracy
sigmoid, 1 0.690 → 0.687, 0.555 0.710 → 0.693, 0.516
tanh, 1 0.986 → 0.131, 0.961 0.626 → 0.144, 0.961
ReLU, √2 (He) 0.546 → 0.121, 0.984 0.677 → 0.139, 0.953
ReLU, 1 0.692 → 0.091, 0.977 0.693 → 0.130, 0.961
ReLU, 2 52.5 → diverged 23.2 → diverged
  • Sixteen sigmoid layers never left chance. The loss stayed at ln 2 = 0.693 and the accuracy near 50%: the vanishing gradient froze everything but the top layers.
  • Doubled ReLU weights blew up. The initial loss was already 23–53, from exploding outputs, and the first SGD steps produced NaNs.
  • Tanh (Xavier scale) and He-scaled ReLU both trained.
  • Too-small ReLU weights trained just as well here. Over 16 layers the signal shrinks by “only” about (1/√2)¹⁶ ≈ 0.004, which the biases and the first SGD steps can recover. In a deeper network the factor shrinks exponentially, and the gap opens.
🐍 Exercise 2 (research): trainability of 16-layer networks
Train 16-layer MLPs with sigmoid, tanh and ReLU at different weight scales on two moons with plain SGD and see which ones learn, stall or diverge.

Vanishing and exploding gradients in AI, robotics and aerospace

  • Recurrent models of telemetry and trajectories: vanishing gradients limit how far back in time a plain RNN can learn. Use LSTM or GRU, or state-space models (E17).
  • Very deep perception backbones: residual connections and normalisation are what make 50–150-layer networks trainable for detection on spacecraft and drone imagery.
  • Monitoring: logging per-layer gradient norms in training pipelines catches these problems early, before compute is wasted.

References

  1. S. Hochreiter (1991). “Untersuchungen zu dynamischen neuronalen Netzen.” Diploma thesis, TU München.
  2. Y. Bengio, P. Simard, P. Frasconi (1994). “Learning long-term dependencies with gradient descent is difficult.” IEEE Trans. Neural Networks 5(2).
  3. X. Glorot, Y. Bengio (2010). “Understanding the difficulty of training deep feedforward neural networks.” Proc. AISTATS.
  4. K. He, X. Zhang, S. Ren, J. Sun (2015). “Delving deep into rectifiers: surpassing human-level performance on ImageNet classification.” Proc. ICCV.
  5. B. Hanin (2018). “Which neural net architectures give rise to exploding and vanishing gradients?” Advances in NeurIPS 31.