Start studying
Module 2 · Deep Learning Module contents
Module 2  ·  Card 13 / 25advanced

Vanishing and Exploding Gradients

In deep networks gradients are multiplied together layer after layer.

If those numbers are small the gradient vanishes before reaching early layers.

If they are large it explodes and training becomes unstable.

Vanishing gradients mean the early layers barely learn anything.

ReLU, residual connections, normalization and gradient clipping are the standard defences.

Learn in depth ↓
Module 02 · Deep Learning · 25 concepts
In depth  /  15 lines

Vanishing and Exploding Gradients, in more detail

Backpropagation multiplies a term for each layer it passes through.

Multiplying twenty numbers of about 0.5 leaves you with roughly one in a million.

The gradient reaching the first layer is then far too small to move anything.

That is a vanishing gradient, and the early layers simply stop learning.

The network still trains, but only its last few layers actually change.

The symptom is a loss that improves a little and then stops well short of good.

Sigmoid and tanh make it worse, since their slope is under one almost everywhere.

Multiplying numbers above one instead compounds upward and the gradient explodes.

Then one step throws the weights into nonsense and the loss becomes not-a-number.

Recurrent networks feel both most sharply, because depth equals sequence length.

ReLU helps the vanishing case, since its slope is exactly one for positive inputs.

Residual connections give the gradient a path that skips the multiplication entirely.

Normalisation layers keep the scale of activations steady from layer to layer.

Gradient clipping catches the exploding case after the fact.

Together these are why networks with a hundred layers can be trained at all.

Want the full technical article? Wikipedia ↗
Deep dive

What is underneath Vanishing and Exploding Gradients

A product, not a sum

∂L∂a[1]=∂L∂a[L]∏l=2L∂a[l]∂a[l−1]\frac{\partial \mathcal{L}}{\partial \mathbf{a}^{[1]}} = \frac{\partial \mathcal{L}}{\partial \mathbf{a}^{[L]}} \prod_{l=2}^{L} \frac{\partial \mathbf{a}^{[l]}}{\partial \mathbf{a}^{[l-1]}}

The gradient reaching an early layer is the product of one Jacobian per layer above it.

0.950≈0.005,1.150≈1170.9^{50} \approx 0.005, \qquad 1.1^{50} \approx 117

Products of many numbers do not drift. They collapse or they blow up, and depth sets the exponent.

That is the whole phenomenon. Vanishing and exploding gradients are the same equation with the factor on either side of 1.

What sets the factor

∂a[l]∂a[l−1]=diag(g′(z[l])) W[l]\frac{\partial \mathbf{a}^{[l]}}{\partial \mathbf{a}^{[l-1]}} = \mathrm{diag}\big(g'(\mathbf{z}^{[l]})\big) \, W^{[l]}

Two things multiply at every layer: the activation's derivative and the weight matrix.

max⁡zσ′(z)=0.25\max_z \sigma'(z) = 0.25

A sigmoid can contribute at most a quarter, so its best case still divides the gradient by four per layer.

σmax⁡(W[l])\sigma_{\max}\big(W^{[l]}\big)

The weights contribute their largest singular value, which is why initialisation scale and weight decay both show up in this problem.

Recurrent networks are the extreme case: the same matrix is applied at every timestep, so the factor is raised to the sequence length rather than the depth.

What the two failures look like

A vanishing gradient is silent. The loss falls a little and then flattens, early layers stop changing, and the network behaves as though it were shallow.

An exploding gradient is loud. The loss jumps to a huge value or to NaN in a single step, and the weights are ruined immediately.

∥g∥→∞  ⟹  L=NaN\|\mathbf{g}\| \to \infty \implies \mathcal{L} = \mathrm{NaN}

The loud one is easier to live with, because it is obvious and the fix is one line.

The fixes, in the order they were found

  1. Use ReLU or GELU instead of sigmoid or tanh, so the activation contributes 1 rather than a fraction.
  2. Initialise with He or Xavier, so the weight factor starts near 1 instead of somewhere arbitrary.
  3. Add residual connections, which give the gradient a path that multiplies by 1.
  4. Add normalisation layers, which rescale activations and keep the Jacobians bounded.
  5. Clip the gradient norm, which caps the explosion without touching its direction.
a[l]=a[l−1]+F(a[l−1])  ⟹  ∂a[l]∂a[l−1]=I+∂F∂a[l−1]\mathbf{a}^{[l]} = \mathbf{a}^{[l-1]} + F\big(\mathbf{a}^{[l-1]}\big) \implies \frac{\partial \mathbf{a}^{[l]}}{\partial \mathbf{a}^{[l-1]}} = I + \frac{\partial F}{\partial \mathbf{a}^{[l-1]}}

The residual connection is the deepest of these.

Adding the identity puts a 1 into every factor of the product, so the gradient cannot decay geometrically no matter how many layers there are.

That single change is what took networks from about 20 layers to more than a thousand.

How to see it before it costs a day

Print the norm of the gradient at each layer after the first backward pass, not after an hour of training.

A healthy network shows norms within an order of magnitude of each other across depth. A tenfold drop per layer is the problem, visible in one step.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
Why do gradients vanish in a deep network?
Deep Learning

25 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close