Tokenwerk · The LLM textbook

Chapter 7 · I · Understanding neural networks · 7 minutes

How numbers actually learn

Should a dial go left or right, and how far? The answer is called gradient descent. It is the engine behind every training run, ChatGPT included.

Hiking in the fog

Imagine you are standing on a mountainside in thick fog, or blindfolded, and want to get down to the valley. You can't see anything. But with your feet you can feel around you which way is downhill. So you take a step in that direction. Then you feel around again. And so on, until there is no downhill left anywhere.

Illustration: a blindfolded hiker feels her way downhill in the fog with one foot; a lake lies in the valley below.

That is exactly how a neural network learns. The “mountain” is the error: the higher, the worse. Where you stand is set by the positions of the dials. And “feeling which way is downhill” is what we learn to compute in this chapter.

Trying it out: a little more, a little less

Our mini model again: input times weight. The input is 2, the target 6, the weight is currently at 1. So the prediction is 2 and the squared error is 16.

The simplest way to find out which way is downhill: turn the dial a tiny bit in both directions and look. With weight 0.99 the model says 1.98 and the error is 16.1604, slightly worse. With weight 1.01 it says 2.02 and the error is 15.8404, slightly better!

So: making the weight larger helps.

How steep is the slope?

We changed the weight by 0.02, from 0.99 to 1.01. The error changed by −0.32, from 16.1604 to 15.8404. Dividing the two: −0.32/0.02=−16.

This number is the slope: how strongly does the error change when I turn the dial? Negative means: more weight, less error. Positive would mean: more weight, more error.

In mathematics this slope is called the derivative. It holds locally, only exactly where we are standing right now. A few steps further on, the slope can look completely different.

The figure shows what happens: two points on the loss curve, joined by a line. Pull the points together and the line becomes the tangent, which touches the curve exactly where you are standing. Its slope is the derivative.

What the funny symbol means

We call the error LL and the weight ww. The derivative is written like this:

dLdw.\frac{dL}{dw}.

Read it as: “the derivative of L with respect to w”. In plain words: how sensitively does the error react when I turn w a tiny bit? For us the answer at the start is −16.

The symbol looks like a fraction, but it is a fixed notation. Our calculation with 0.99 and 1.01 is an approximation you can always use to check what it means.

Going downhill

The slope shows which way is uphill. But we want to go down. So we go in the opposite direction: with slope −16 we make the weight larger, with slope +16 we would make it smaller.

And how big should the step be? That is set by the learning rate, a number we choose ourselves. With learning rate 0.05, the step is 0.05 times the slope.

The calculation is: new weight = old weight − learning rate × slope. With our numbers: 1−0.05×(−16)=1.8. Minus times minus gives plus, which is why the weight gets larger.

This procedure is called gradient descent. With many dials we compute a slope like this for each one. All of them together are called the gradient. It shows for every dial which way is uphill, and every time we go exactly the other way.

The learning rule as a formula

wnew=wold−ηdLdw.w_{new}=w_{old}-\eta\frac{dL}{dw}.
Symbol Meaning here Value
woldw_{old} Weight before the learning step 1
wneww_{new} Weight after the learning step 1.8
η\eta Learning rate; “eta” is just its short name 0.05
dL/dwdL/dw Slope of the error at the current weight −16

This is the same calculation as in the text, just in shorthand.

Did it work?

With weight 1.8 the prediction is 1.8×2=3.6. The difference from the target is 3.6−6=−2.4, squared 5.76. Before 16, now 5.76: much better!

But we haven't arrived yet. Training means many such steps in a row. Before each step the slope is worked out again, because the slope looks different at every spot. The −16 from the beginning only holds at the beginning.

Where does the computer get the slope from?

Trying out 0.99 and 1.01 is intuitive, but far too slow with billions of dials. Luckily there are calculation rules that give you the slope directly. For our example we only need two.

Rule 1: multiplying. The prediction is weight times input, so w × 2. If you turn the weight a tiny bit further, the prediction grows by twice that bit. So the slope of the prediction is simply the input: 2.

Rule 2: squaring. The error is the difference squared, u × u. Picture u as the side length of a square; then u × u is its area. If the side gets a tiny bit h longer, two narrow strips are added (u × h each) plus a tiny corner (h × h). The corner is so small that it doesn't count. So the area grows by 2 × u × h: the slope of the square is 2 × u.

Both in a row. Turn the weight a tiny bit, and the prediction changes by 2 times that bit (rule 1). The difference changes in the same way, because the target 6 stays fixed. And every change in the difference changes the error by 2 × u times as much (rule 2). With u = −4 that is −8 times as much. Together: 2 × (−8) = −16. Exactly the number that trying out gave us, just without trying.

That is where the rule “slope = 2 × difference × input” comes from: the 2 from squaring, the difference as the side length, the input from multiplying. Chaining rules like this is called the chain rule, and in the next chapter it becomes the star: a network is nothing but many such steps in a row. There is a small rule like this for every kind of arithmetic and every function (square root, logarithm, ReLU …). You don't have to memorize any of them: later PyTorch knows them all and applies them automatically.

Why big steps can fail

In the experiment, choose a learning rate of 0.3. The first step gives 1−0.3×(−16)=5.8. The prediction is then 11.6 and the error (11.6−6)²=31.36. Worse than before!

The direction was right, but the step was so big that we jumped across the valley onto the opposite slope. The hiker in the fog only knows what it looks like directly under their feet. Giant leaps are risky.

That is why the learning rate is one of the most important settings in training. By the way, it does not belong to the model but to the learning method: we choose it; it is not trained.

Key points

A learning step consists of three parts:

  1. Compute the prediction: the forward pass from chapter 5.
  2. Work out the slope for every dial: in large networks, the backward pass from the next chapter does this.
  3. Turn all dials a bit downhill: an update rule takes care of that, the so-called optimizer.

These three steps, repeated millions of times: that is training. ChatGPT included.