A product, not a sum
The gradient reaching an early layer is the product of one Jacobian per layer above it.
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
Two things multiply at every layer: the activation's derivative and the weight matrix.
A sigmoid can contribute at most a quarter, so its best case still divides the gradient by four per layer.
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.
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
- Use ReLU or GELU instead of sigmoid or tanh, so the activation contributes 1 rather than a fraction.
- Initialise with He or Xavier, so the weight factor starts near 1 instead of somewhere arbitrary.
- Add residual connections, which give the gradient a path that multiplies by 1.
- Add normalisation layers, which rescale activations and keep the Jacobians bounded.
- Clip the gradient norm, which caps the explosion without touching its direction.
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.