Vanishing and Exploding Gradients
The gradient that reaches an early layer is a product with one factor per layer above it. Many factors below 1 multiply out to almost nothing, and many above 1 blow up, so deep networks only train when each factor stays close to 1. ReLU, careful initialization and residual connections are three ways of making that happen.
What the symbols mean
- Weight into layer k
Connects the neuron of layer k − 1 to the neuron of layer k.
- Activation slope
At most 1/4 for the sigmoid; 1 or 0 for ReLU.
What the symbols mean
- Fan-in
The number of inputs each neuron in the layer receives.
What the symbols mean
- The block's own computation
A layer or two of weights and activations; the block adds its output to its input.
Explanation
Vanishing and exploding gradients are simply what happens when you multiply a lot of numbers together. The gradient for an early layer is a product with one factor per layer above it, and each factor is a weight times an activation slope. If those factors are mostly less than 1, the product shrinks toward zero: the gradient vanishes and the early layers barely learn. If they're mostly more than 1, it blows up: the gradient explodes and training lurches around wildly. Think of multiplied by itself 50 times (about ) versus multiplied 50 times (about ). Nothing here is new math; it's the chain rule from this unit, applied many times in a row. The whole lesson is about keeping those factors close to 1.
A deep, thin network. Picture a chain of layers with one neuron each: and . There's only one path from the first layer to the loss, so the chain rule just multiplies. Each step back from to costs a factor , which gives the first formula. A real network has many neurons per layer, so the factors become matrices and diagonal slope matrices, as in the last lesson, but the story is the same: a long product.
Why the sigmoid shrinks gradients. From Activation Functions, . Write , a number between 0 and 1. Then , which is at most , reached only at (that is, ). So every sigmoid factor satisfies . With weights around size 1, each layer back divides the gradient by at least 4, and layers divide it by : ten layers already make it less than a millionth. Bigger weights don't rescue you, because a big pushes far from 0 where the sigmoid is flat (). This is why deep sigmoid networks were close to untrainable for decades. There are two pictures further down that show the shrinking layer by layer.
Exploding gradients. Factors bigger than 1 are just as bad in the other direction. A recurrent network, one that reads a sentence word by word, reuses the same weight at every step, so 50 words means the same factor 50 times: with a factor of , that's . One huge gradient step can throw the weights somewhere useless. The everyday fix is gradient clipping: if the gradient's length is above a threshold, scale it down to the threshold before stepping.
Fix 1: ReLU. is exactly for any neuron that's on. The factor becomes alone, with no built-in division by 4. (The price is that a neuron that's off passes nothing back, which you saw last lesson.)
Fix 2: start the weights at the right size. With ReLU the factor is about the weight's size, so the weights need the right spread. Here's where comes from. A neuron computes . If the weights are independent with mean 0, then (variances of independent terms add; see Expected Value). For ReLU, with symmetric around 0, so half the time and . So
For the signal to keep its size layer after layer, the factor in front must be 1: . The backward pass runs through the same weights, so the gradients keep their size too.
Fix 3: residual connections. Let each block add its input back to its output: . Differentiate and the factor becomes instead of . Think about why that's so powerful. By the "add over paths" rule from the first lesson, the skip connection is a path whose factor is exactly 1, so there's always a route for the gradient that never shrinks. With in each of 20 blocks, the residual product is , while without the skips it would be .
A common mistake. It's tempting to think a bigger learning rate fixes vanishing gradients, because the early layers just need bigger steps. It doesn't. The learning rate scales every layer's step by the same amount, so the last layers, whose gradients were healthy, now overshoot, while the first layers are still thousands of times slower than the rest. The problem is the ratio between layers, and only changing the factors themselves (the activation, the weights' size, the architecture) fixes a ratio.
See it
Worked examples
A chain of 4 one-neuron sigmoid layers has every weight equal to and biases chosen so that every comes out (the sigmoid's best case). The input is and . Compare with , then redo the error signals with ReLU neurons that are all on.
- 1
Every is , so every activation is and every slope is .
A ReLU layer has inputs per neuron. Derive the weight variance that keeps the signal's size steady from layer to layer, and find the standard deviation to initialize with.
- 1
A neuron computes . Leave out the bias, which starts at .
Practice
Type your answer and check it. Solve them all to complete the lesson.
A chain of 10 sigmoid layers has every . The gradient reaching the first layer is at most of the gradient at the output. What is ?
What is the largest value the sigmoid's derivative ever takes?
A ReLU layer has inputs per neuron. What standard deviation does He initialization use for its weights?
A recurrent network reads a 50-word sentence, reusing the same weight at every word. On one training example the gradient picks up a factor of per word as it travels back through the sentence. By what factor has it grown by the time it reaches the first word? Give 3 significant figures.
In a deep sigmoid network, the sigmoid's slope is at most , so even in the best case each layer divides the gradient by 4. How many layers back does it take for the gradient to fall below one millionth of its size at the output?
A network stacks 20 residual blocks , and in each block . By what factor does the gradient change across all 20 blocks? Give 4 decimal places.
(Statistics review.) Someone initializes a deep ReLU network with instead of . Using , by what factor does the standard deviation of shrink over 10 layers?
Which of these does NOT help a deep network with vanishing gradients?
The hardest one here. Get it, and you've got this lesson.
A chain of three one-neuron sigmoid layers (no biases) has input and weights , , , with target and . Find the ratio , to 3 significant figures.
Where it's used
Residual connections are why networks with 100 or more layers train at all: image models made of residual blocks went from about 20 layers to well over 100 in a single step, and every transformer block in today's language models wraps its attention and its feed-forward layer in a skip connection, so the gradient always has a path with factor 1. He initialization is the default for ReLU layers in essentially every library, and gradient clipping is a standard line in the training loop of recurrent and language models. Each one is a direct answer to "keep the product of the factors near 1".