All courses

Residual connections

Show that y = x + f(x) has Jacobian I + ∂f/∂x. Explain how this identity path lets gradients and information pass through very deep stacks.

Builds on: Vanishing and exploding gradients, The transformer block and the running vector

▶ Watch this lesson Start the whole course

What the animation says

Products vanish

A gradient reaching an early layer is a product: one factor for each layer it passes through. Take three blocks with derivatives zero point two, minus zero point one, and zero point three. Multiply them and you get minus zero point zero zero six. Almost nothing gets through. With fifty or a hundred blocks, that product shrinks or blows up exponentially. Yet LLMs stack dozens of blocks, and the first ones still learn. The trick is one choice: add, don't replace.

Add a correction

A residual block outputs its input plus a correction: x plus f of x. If f adds nothing, x passes through untouched. The block only has to learn an edit. Backward, the gradient at y is copied straight to x along the skip path. A second copy goes through f, scaled by f's derivative, and the two copies add. So x receives the gradient times one plus f prime. Gradient five arrives at y, and f prime is zero point two. What reaches x?

Three blocks, every path

Six: five along the skip path, plus one through f. Now make the three earlier blocks residual. Each factor becomes one point two, zero point nine, one point three. Their product is one point four zero four, instead of minus zero point zero zero six. Expand it into paths. Skipping every block contributes exactly one. Paths through one block add zero point four. Through two, zero point zero one. Through all three, minus zero point zero zero six. Now fifty random small blocks, with the gradient size starting near eight. Plain versus residual?

Fifty blocks in code

Plain shrinks to about ten to the minus forty-nine. Residual stays around ten. Here's the experiment. Each block gets a random, small Jacobian. This line is the plain layer: the Jacobian alone. Each step scales the size by about a tenth. This line is the residual block: identity plus the Jacobian. The identity keeps the gradient whole. Over fifty blocks, the residual size grows only about one point three times. But what if every block nudges the same way? A hundred blocks, each f prime zero point zero five. Total factor?

What to remember

About one hundred thirty-one. Small same-sign nudges still compound. So the rule: each residual block's Jacobian is the identity plus the block's own Jacobian. The identity gives gradients and information a direct path through every block. The total is still a product. It stays near identity only if contributions are small and don't all push one way. Suppose one block dies completely. Tap the path the gradient still uses.

How does a 100-block stack still train?

You saw that the gradient reaching an early layer is a **product** of per-layer Jacobians, so it shrinks or grows exponentially with depth. Yet LLMs stack dozens of blocks and the earliest ones still learn. What changes when each block *adds* its result to the running vector instead of replacing it? And why does that one design choice let gradients survive the trip down?

Add a correction, keep the original

A residual block outputs its input **plus** a correction: $y = x + f(x)$. If $f$ contributes nothing, $x$ passes through untouched. So the block's default is to change nothing, and it only has to learn an edit. Going backward, the same holds. The gradient arriving at $y$ is copied straight to $x$ along the identity path. A second copy also goes through $f$. A bad or tiny $f$ cannot block the direct copy.

The Jacobian is I plus the block's Jacobian

Differentiate $y = x + f(x)$ for $x, y \in \mathbb{R}^{1\times d}$: $$\frac{\partial y}{\partial x} = I + \frac{\partial f}{\partial x}$$ $I$ is the $d\times d$ identity, from the derivative of $x$. The gradient becomes $g_x = g_y + g_y\,\tfrac{\partial f}{\partial x}$. Stack blocks on the running vector (residual stream): $r^{(\ell)} = r^{(\ell-1)} + f_\ell(r^{(\ell-1)})$. Unrolling gives $$r^{(L)} = r^{(0)} + \textstyle\sum_{\ell=1}^{L} f_\ell(r^{(\ell-1)})$$ Let $J_\ell = \partial f_\ell/\partial r^{(\ell-1)}$. Then the Jacobian of $r^{(L)}$ with respect to $r^{(0)}$ is the product of the $L$ factors $(I + J_\ell)$. Its expansion always contains a bare $I$ term, which is the path that skips every block.

Three scalar blocks, by hand

Use block derivatives $f'_1=0.2,\ f'_2=-0.1,\ f'_3=0.3$. **Plain stack:** $0.2\cdot(-0.1)\cdot 0.3 = -0.006$. Almost nothing gets through. **Residual:** $1.2\cdot 0.9\cdot 1.3 = 1.404$. Expanded into paths: - skip all blocks: $1$ - through one block: $0.2-0.1+0.3=0.4$ - through two: $-0.02+0.06-0.03=0.01$ - through all three: $-0.006$ Total $1.404$. The skip path alone carries $1$, at any depth.

50 blocks: plain vs residual gradient

Here random small block Jacobians are applied to a gradient whose starting norm is about 8. Each plain step scales its size by about 0.1, so after 50 steps it is around $10^{-49}$. The residual version stays the same order as the start, around 10. On average each step multiplies the squared norm by only about $1.01$, so the norm grows just ~1.3× over 50 blocks. Row-vector convention: $g_x = g_y J$.
import numpy as np
rng = np.random.default_rng(0)
d, L = 64, 50
g = rng.standard_normal(d)          # gradient at the top
gp, gr = g.copy(), g.copy()
for _ in range(L):
    J = rng.standard_normal((d, d)) * 0.1 / np.sqrt(d)  # small block Jacobian
    gp = gp @ J                     # plain layer
    gr = gr @ (np.eye(d) + J)       # residual block
print(np.linalg.norm(g), np.linalg.norm(gp), np.linalg.norm(gr))

Remember this

$y = x + f(x)$ has Jacobian $I + \partial f/\partial x$. The $I$ gives gradients and information a direct path through every block, so even a dead or tiny block cannot cut the signal off. The total is still a product of $(I + J_\ell)$ factors. It stays near $I$ only if the blocks' contributions are small and do not all push the same way. Small same-sign contributions still compound: 100 scalar blocks with $f' = 0.05$ give $1.05^{100} \approx 131$.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.