Tokenwerk · The LLM textbook

Chapter 8 · I · Understanding neural networks · 7 minutes

Learning through several layers

Between a dial and the error there are often many calculation steps. How do you still find out where each dial should go? With backpropagation, the method without which no modern AI model would exist.

Telephone game, backwards

With a single weight it was easy: it affects the prediction directly. In a network, though, an early weight sits far up front, and plenty of calculations happen before the output. Still, the error at the end depends on it too.

The solution is a kind of telephone game played backwards: we start at the back with the error and pass forward, step by step, how much each spot contributed to the error. Each station only needs to know what happens right where it is.

Illustration: several people in a row whisper a message into each other’s ears, one after another.

To try it out we take the smallest possible example: two multiplications in a row. Not a particularly clever network, but the learning path is crystal clear. The input is 2, the first weight 1, the second weight 2, the target 6.

First compute forward

Step Calculation Result
Intermediate value Input × first weight 2 × 1 = 2
Prediction Intermediate value × second weight 2 × 2 = 4
Difference Prediction − target 4 − 6 = −2
Squared error Difference × difference (−2) × (−2) = 4

So we don't have to keep writing “first weight”, the numbers get nicknames: the input is called x, the first weight w, the intermediate value h and the second weight v. So: h = w × x, and the prediction is v × h. The letters are just labels for the numbers in the table.

Backwards: who is involved, and how much?

We start at the very back, at the prediction 4. How does it affect the error? The rule from the last chapter: 2 × difference, so 2×(−2)=−4. Meaning: if the prediction were a little larger, the error would go down.

Want to check it yourself? With prediction 4.01 the difference would be −1.99 and the error 3.9601. The error drops by 0.0399 while the prediction rises by 0.01. That is about −4 per unit. It fits.

Now one step back, to the second weight v. The prediction is v × h, and h is 2. Turn v up a little and the prediction grows by twice that amount. So the effect of v on the error is −4×2=−8.

That is the whole trick already: effects along a chain get multiplied. −4 (prediction affects error) times 2 (v affects prediction) gives −8 (v affects error). In mathematics this is called the chain rule.

One more step back

The intermediate value h is multiplied by v = 2. So its effect on the error is also −4×2=−8.

And h came from w × x, with x = 2. A small change to w therefore changes h by twice as much. The effect of w on the error: −8×2=−16.

Derivative we want Combined influence Result
With respect to second weight v −4 × intermediate value 2 −8
With respect to intermediate value h −4 × second weight 2 −8
With respect to first weight w −8 × input 2 −16

Now every weight knows where to go. And the method didn't guess anything or invent targets for the middle. It just went through the known calculation steps backwards.

That is the idea of the chain rule

The chain rule says: when a change runs through several calculation steps in a row, you multiply their individual effects. For our first weight, the shorthand looks like this:

dLdw=dLdy^⋅dy^dh⋅dhdw.\frac{dL}{dw}=\frac{dL}{d\hat y}\cdot\frac{d\hat y}{dh}\cdot\frac{dh}{dw}.

Read from left to right: we want to know how w affects the error L. To get it, we multiply how the prediction affects the error, how the intermediate value affects the prediction and how the weight affects the intermediate value.

With numbers: −4×2×2=−16. Exactly the path from the table, just written down more briefly.

What happens at forks?

Sometimes an intermediate value feeds into several later calculations. Then it affects the error along several paths. The rule for that is simple: multiply along a path, add different paths.

The clever part: every intermediate effect, such as the −8 for h, is computed only once and then reused for everything further forward. So you don't have to walk every path separately. That is why the method is fast enough to work even with billions of weights.

This backward calculation is called backpropagation. The overview of all calculation steps with their dependencies is called the computational graph. That means a web of connections, not a chart with curves.

Collect the derivatives first, then change the weights

We have −16 for w and −8 for v. With learning rate 0.01, w becomes 1−0.01×(−16)=1.16 and v becomes 2−0.01×(−8)=2.08.

New forward pass: intermediate value 1.16×2=2.32, prediction 2.08×2.32=4.8256. The error is about 1.3792. Before, it was 4: much better!

The order matters: first compute all slopes, then change all weights at once. If you adjusted a weight halfway through, the remaining calculations would no longer fit together. Like someone swapping out the sentence halfway through the telephone game.

And the activation function?

It fits right in. With ReLU, the effect on the positive side is 1: small changes pass through unchanged. On the negative side it is 0: everything there is zero anyway, and small changes fizzle out. Exactly at zero there is a kink; programs simply pick a fixed value for it.

So an activation function works in both directions: forward on the value, backward on the effect. You don't need to know the derivative rules by heart. It is enough to know that both directions belong together.

The computer does this later

The good news to finish: you never have to do any of this by hand again. The programming library PyTorch does it automatically. This is called autograd, automatic differentiation. You only describe the forward path. PyTorch remembers every calculation step and computes all the slopes backwards at the push of a button.

What stays with you are the really important decisions: the blueprint, the data and what exactly the model should learn. With that, the foundation is in place. In the next part we turn text into numbers: finally, it is about language!

To finish, you can watch all of this work together. Below, a small network trains live in your browser: two inputs (the position of a point), one hidden layer, one output (green or orange?). In every step it computes forward, measures the loss, passes the blame backward and turns all weights a little. Above all, try this: what can a single neuron do, and what can eight do?

Key points

  • Backpropagation passes the effect on the error from back to front through the network.
  • Along a chain, effects are multiplied; different paths are added. That is the chain rule.
  • First compute all slopes, then change all weights at once.