Vanishing and exploding gradients
Explain that the gradient through a deep network is a product of per-layer Jacobians. Show why that product tends to shrink or grow exponentially with depth.
Builds on: Backpropagation as reverse-mode autodiff, MLPs and why nonlinearities matter
What the animation says
Backprop runs through every layer
Backprop gives an exact gradient for every weight. So why do deep stacks often refuse to train? The first layers barely move, or the updates blow up and training diverges. Going backward, each layer multiplies the incoming gradient by its own local derivative. To reach the input, the gradient passes through every layer, one multiplication each. Tap the layer whose gradient sits behind the most factors.
Three layers by hand
Take a chain of three layers, one number each. The gradient at the top is one. Each layer's factor is its weight times its activation slope: point eight, point five, and point six. One times point eight is point eight. Times point five gives point four. Times point six gives point two four. The typical factor here, the geometric mean, is about point six two. Now stack thirty layers with that typical factor. What reaches the input?
Vanish or explode
Point six two, thirty times over, is about six ten-millionths. The gradient has all but vanished. Nudge the factor up to one point three, and thirty layers give about two thousand six hundred. A product of many factors never drifts gently. Below one it vanishes, above one it explodes. Only factors sitting right at one keep the size stable. A tell: the sigmoid's slope is at most a quarter. So unless the weights stretch the signal more than four times, every sigmoid layer shrinks the gradient.
Real layers: matrices
In a real network, each factor is a matrix: the layer's Jacobian. It's the activation slopes on a diagonal, times the transposed weights. Each backward step multiplies the gradient row by one Jacobian. The input gradient is the top gradient times all of them. If no Jacobian can stretch a vector by more than some c below one, the gradient shrinks at least like c to the L. And each layer's weight gradient scales with the gradient arriving there, so early weights stall.
Watch it in numpy
Here's a thirty-layer tanh network in numpy. Scale sets how much each weight matrix stretches a vector. This loop runs the backward pass: multiply by the tanh slope, then by the transposed weights. It starts from a gradient of norm eight. At scale point five, the norm collapses to about ten to the minus eight. At scale one, it ends near one. A two-times change in the weights moved the gradient about eight orders of magnitude. At scale two, a factor of two per layer would mean two to the thirtieth. What actually happens?
Saturation, and the takeaway
It grows roughly a hundredfold or more, far less than two to the thirtieth. Big weights push tanh inputs into its flat tails, where the slope is nearly zero. Those near-zero slopes cap the blow-up. So remember: the input gradient is the top gradient times one factor per layer. When typical factors are below one, it vanishes. When they are above one, it explodes. Deep networks that train well use design choices that keep each layer's factor close to one.
A long chain of multiplications
Gradient = product of Jacobians
Three layers, then thirty
Watch it happen in numpy
import numpy as np
rng = np.random.default_rng(0)
d, L = 64, 30
for scale in [0.5, 1.0, 2.0]:
Ws = [rng.normal(0, scale/np.sqrt(d), (d, d)) for _ in range(L)]
u, acts = rng.normal(size=(1, d)), []
for W in Ws:
a = u @ W; acts.append(a); u = np.tanh(a)
g = np.ones((1, d))
for W, a in zip(reversed(Ws), reversed(acts)):
g = (g * (1 - np.tanh(a)**2)) @ W.T
print(scale, np.linalg.norm(g))
Remember this
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses