← simple-learning

How neural networks learn

How a machine with a thousand knobs learns which way to turn each one. Backprop, short for backpropagation, is the rule every neural net is trained with.

Needs multiplication and the slope of a line. Leads to neural networks, GPT.

Start with a shower

The water is too cold. You turn the tap a bit, wait, feel. Warmer, but still cold, so you turn it more. Too hot now, so you turn it back, less this time. A few rounds and it is right. You never knew the "correct" position of the tap. You only ever knew two things: which way made it better, and roughly how much to turn. That is the whole method, and it is all this page is about.

The problem

Now imagine a mixing desk with a thousand knobs and one listener who only says "better" or "worse". A score, one number that says how wrong things are right now, lower is better. With one tap you can just try both directions. With a thousand knobs, trying every combination would take longer than the universe. And a modern language model has billions of knobs. You need something that, in one go, tells you for every knob which way to turn it and how much. That something is a slope, the same "which way and how much" you felt in the shower, and backprop is the way to get all the slopes at once.

1One knob

A knob x and a score: how far x is from 2, squared. Closer to 2 means a smaller score; at 2 it is zero. Drag the knob. The slope is how much the score changes when you turn the knob a tiny bit: the tilt of the curve where the dot sits.

x
x is 0 now. You want it as close to 2 as possible. What would you do?


      

2A chain

Two knobs now, a and b. They are multiplied into e, and the score is how far e is from 6, squared. Each box shows its value and, in blue, its slope: how much the score changes if that box alone moves by a tiny bit.

a
b
a
b
→
e = a · b
→
score = (e − 6)²
Before you look: turn a up by 1. By how much does e change?


      

3The one rule

The slope of the score along a goes through e. So it is two slopes multiplied: how the score changes with e, times how e changes with a. That is the whole rule. Start at the score and walk backwards through the chain, multiplying as you go. That walk backwards is what "backpropagation" means.


        
Why not just nudge each knob and see what happens?With two knobs you could. But every nudge means running the whole machine again, and a language model has billions of knobs, so that is billions of runs for one tiny improvement. The rule gives all the slopes in a single walk backwards, about as cheap as one run forward. That is the entire reason neural nets can be trained at all.

4Let the machine turn the knobs

Now it is mechanical: turn every knob a little against its slope, then recompute, then repeat. The size of "a little" is the learning rate. This loop is called gradient descent.


        
      

5The smallest neural net

Six dots, two colours. Three knobs draw a line: w₁ · x₁ + w₂ · x₂ + c = 0. The score is how far each dot's side is from where it should be. Same rule, same loop, and the line learns to separate the colours. A real neural net is this, repeated millions of times with a bend added between the layers.


        
      

?Check yourself

Same chain as step 2: e = a · b, score = (e − 6)². Now a = 2 and b = 1.

Things to try

  1. Set the learning rate to 0.1 and press Step a few times. The score bounces between two values and never settles. Too big a step overshoots the bottom every time. Learning rate is the first thing anyone tunes.
  2. Drag a to 0. The slope along b becomes 0, whatever b is. Turning b does nothing when it is multiplied by zero, and the rule knows.
  3. Drag a to −1 and b to −1, then step. It still finds a way to make e = 6, just a different one. Many knob settings give the same score; the loop finds one of them, not "the" one.
  4. In step 5, press Reset and then Step once at a time. Watch the number of dots on the wrong side. The score can keep falling even after every dot is on the right side.

What this leaves out

Real nets have millions of boxes, not three, and a bend after each layer, called an activation, without which stacking layers adds nothing. The slopes are not written by hand: a small program applies the rule from step 3 to any chain automatically, which is what PyTorch does. The data comes in batches, the learning rate changes over time, and the step is smoothed with memory of earlier steps (methods like Adam). Every one of those is a layer on the rule you just used.