Answers · The mathematics underneath
Why do gradients become tiny in the early layers of a deep network?
In shortBecause the backward pass multiplies one number per layer, and a factor slightly below one, repeated over many layers, collapses towards nothing.
Because the backward pass multiplies one factor per layer between the loss and the layer in question, and a factor a little below one raised to a high power is a very small number. Nothing leaks out on the way down; the gradient is a product, and it is the product that collapses.
The size of the product, not the length of the journey
Take a stack of ten layers in which every layer's local derivative is 0.9. That is not a pathological value: it is what a weight matrix with singular values just under one gives you, or a squashing nonlinearity sitting a little way into its flat region. If the gradient at the output is 1, the gradient arriving at the bottom is . Extend the stack and the exponent does the work: thirty layers give , fifty give , eighty give . Each additional ten layers divides by about 2.9, because and . That is the whole mechanism. The cost of depth is exponential in the number of layers, not linear, so a network that is fine at ten layers is unusable at eighty without doing something about the factor.
| layers travelled | factor 0.9 per layer | factor 1.1 per layer |
|---|---|---|
| 1 | 0.900 | 1.10 |
| 10 | 0.349 | 2.59 |
| 30 | 0.042 | 17.5 |
| 50 | 0.0052 | 117 |
| 80 | 0.00022 | 2049 |
The natural guess is that the early layers get small gradients because they are far from the loss, as though the signal weakened gradually with distance, and that the cure is therefore to train longer or raise the step size. The picture is wrong in a way that matters, because distance is not what enters the calculation. The calculation is a product, so the fall is geometric: after forty layers a factor of 0.99 has already cost you a third of the signal, and after eighty it has cost you more than half, with no single layer doing anything unusual. The second common guess is that some particular layer must be at fault, a saturating unit with a tiny slope. It need not be. A per-layer factor of 0.9 requires no layer to be near saturation, and a run made entirely of healthy-looking layers will still show a gradient at the bottom that is four orders of magnitude too small.
What makes this hard to notice is that it does not look like a failure from outside. The upper layers are only a few factors from the loss, so they train normally; the loss falls for a while as they do what they can with whatever the untrained lower layers happen to emit, then flattens at a mediocre value. Nothing in the loss curve announces that the bottom half of the network has not moved since initialisation. An exploding gradient, the same arithmetic with the factor above one, is the opposite: the loss becomes unreadable in a single step, in the first place anyone looks.
The repair has to be multiplicative
Because the gradient is linear in the loss, multiplying the loss by a constant multiplies every gradient in the model by that same constant, which lifts the small ones clear of the floor of the number format. The constant is divided out again before the parameters are updated, so the direction of the step is untouched. A thousandfold lift moves the early-layer values from around to around , comfortably inside the range a narrow sixteen-bit format can hold. The headroom at the top is what limits the constant: push it too far and the late-layer gradients, already around , overflow. Note that the trick has to be applied before the backward pass, because once a value has rounded to zero in the format, no later multiplication can bring it back.
The argument reverses if the typical factor exceeds one: the same product grows instead of shrinking, and the symptom is an enormous step rather than a stalled one. That case is handled by measuring the overall length of the gradient and, if it is above a threshold, scaling the whole vector down to that threshold. The direction survives exactly, so the step still goes where the gradient pointed, and the relative sizes of different parameters survive too, which is what tells the optimiser which ones are implicated. Scaling each parameter's gradient separately would destroy exactly that information. There is also a third possibility: if every factor is exactly one the product neither grows nor shrinks, which is the target that careful initialisation and normalisation are aiming at. Residual connections change the shape of the product, since gives , so the backward pass becomes a sum over many paths each close to one rather than a single product, and the gradient keeps its size to the bottom.
One further complication undercuts the obvious diagnostic. Most modern optimisers divide each gradient by a running estimate of its own typical magnitude, so a gradient that is uniformly a thousand times too small produces almost the same update as one of the right size: the thousand appears in the numerator and the denominator alike. A scaling problem can therefore be invisible in the loss curve and still be present in the computation. It stops being invisible at the point that matters. Once the values have rounded to zero there is nothing to divide, the running estimate decays to zero with them, and those parameters stop moving altogether. Recording the gradient length at several depths, rather than only at the output, is what separates a vanishing gradient from the other reasons a loss can flatten early.
Worth keeping
- The gradient reaching an early layer is the output gradient multiplied by one factor per layer in between, so its size is set by a product and not by the distance from the loss.
- A per-layer factor of 0.9 over eighty layers leaves about 0.0002 of the output gradient, while the same factor above one gives about two thousand times as much.
- Multiplying the loss by a constant multiplies every gradient by that constant without changing the direction of the update, which is the only way to rescue values that have already underflowed.
Where this goes next
This question is one step of Keeping The Numbers In Range, a course that works through its subject from the beginning. The page that teaches this part of it is The Same Trouble Backwards, and it reads the whole way through whether or not you have an account.
What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.
Read that pageThe whole courseOther questions people ask about this
- What is the difference between the range and the precision of a float32?Range is the span of sizes a float32 can hold before it becomes infinity or zero; precision is how many significant digits it keeps, so nearby values stay distinguishable.
- Why does normalisation subtract the mean and divide by the standard deviation?Subtracting the average removes the offset and dividing by the spread removes the size, so the output has zero average and unit spread whatever scale the input had.
- Why does adding a layer's input to its output make very deep networks easier to train?Because the addition puts a derivative of exactly one on the path through it, so the gradient reaching the early layers is not a long product of small factors and does not shrink with depth.
- What is the difference between variance and standard deviation?They measure the same spread: variance is the average squared distance from the mean, standard deviation is its square root, so you report the standard deviation and calculate with the variance.
- How do you make a vague claim precise enough to tell whether it's true?Fix what is being talked about, what is asserted, which cases are covered and the cut-off, then name an observation that would prove it false.
- How do you find the parabola that best fits a function near a point?Match the function's height, slope and bending at the chosen point; those three conditions single out exactly one parabola.