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
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.
Add a correction, keep the original
The Jacobian is I plus the block's Jacobian
Three scalar blocks, by hand
50 blocks: plain vs residual gradient
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
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.
Bytewise
All courses