Skip to the content

fatehi.devx2Learning

Deep learning

Residual connections

Each layer adds its result to its input instead of replacing it, so signals and gradients pass through deep stacks.

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:

y=x+F(x)

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).

A sublayer with its residual connection: x goes through the sublayer and, unchanged, around it on the bus above; the adder sums the two.

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.

Test yourself

  1. 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.

  2. 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.

  3. 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