On the path Attention paperEach layer adds its result to what it received, so a deep stack still trains.
Deep stacks are hard to train
Stacking more layers should make a network more capable, but past some depth plain stacks train worse, not better: the gradient, passed back through many layers by the chain rule, is multiplied many times over and tends to fade or blow up, and each layer must learn to pass on everything the next one needs.
Add instead of replace
A residual connection changes what a layer, here a sublayer F, is asked to do. Its input goes around it on a bypass and is added back to its output:
x is the sublayer’s input and y what goes on; F(x) must have the same shape as x for the sum to make sense.
The sublayer now learns only a correction to its input. Doing nothing is easy, F(x) = 0, so extra layers cannot easily make things worse. And on the way back the gradient of y passes to x through the sum unchanged, plus whatever passes through F, so a direct route runs from the loss to the first layer however deep the stack. Residual connections made networks of a hundred layers and more trainable (He and colleagues, 2015).
Seen along the whole stack, the vector that runs past all the adders, the residual stream, is the model’s working memory: every sublayer reads from it and writes a small change back into it.
On the path Attention paper
In the paper. Every sublayer of the Transformer, each attention and each feed-forward, has a residual connection, followed by a layer normalisation: the output is LayerNorm(x + Sublayer(x)) (section 3.1). For the sums to work, every sublayer and the embeddings produce vectors of the same width, dmodel = 512. Dropout is applied to each sublayer’s output before it is added. Putting the normalisation after the addition (“post-norm”) is the paper’s choice; most later models normalise before the sublayer instead (“pre-norm”), which trains more stably in very deep stacks.
Test yourself
What does a sublayer have to learn when it sits on a residual connection?
Only a change to its input: the output is the input plus the sublayer’s result. Doing nothing means learning F(x) = 0.
Why do residual connections help the gradient?
The sum passes the gradient from y to x unchanged, so there is a direct route from the loss to every layer, not only one through all the sublayers in between.
Why does every part of the Transformer produce vectors of width 512?
Because each sublayer’s output is added to its input; the two must have the same shape.
Where it leads
Kept in this browser only, never sent.
Paths through it
- Attention paperstep 9 of 17Follow this path