The equation
- : whatever the layer computes: an attention block, a convolution, a feed-forward network.
- : the input, added back unchanged.
The layer no longer produces the output. It produces the change to the input, which is why is called the residual.
For that addition to be defined, the input and output must have the same width, which is why transformer blocks preserve throughout.
What it does to the gradient
The identity term is the whole point. Whatever the layer's own Jacobian does, the derivative can never be smaller than the identity contributes.
Through stacked layers the gradient reaching the bottom is a product of such terms.
Without the , that is a product of matrices with norms typically below one, and it decays exponentially.
That is the vanishing gradient, and it is what stopped networks deeper than about twenty layers from training at all.
With it, there is a path from the loss to every layer that is multiplied by one at each step.
Why depth stopped hurting
Before residuals, adding layers past a certain depth made training error worse, not just test error.
That was not overfitting. A deeper network can represent everything a shallower one can, by setting the extra layers to the identity. Plain layers find the identity hard to learn.
With a residual, the identity is what the layer does when its weights are zero. It is the default rather than something to be discovered.
So a layer that has nothing useful to add can cheaply do nothing, and depth becomes safe.
One block, in order
- Keep a copy of the input.
- Run it through the sublayer.
- Add the copy back to the result.
- Normalise, either before step two or after step three depending on the convention.
- Pass on, with the width unchanged.
The other reading
A residual network can be seen as an ensemble of many shallower paths: each residual connection offers a choice of going through the block or around it, so blocks give routes.
That explains why removing a single trained layer from a residual network usually barely changes the output, while doing the same to a plain network destroys it.