The loss valley
The model guesses y from x using one number, its weight w: guess = w × x. Two training examples: x = 1 should give 2, and x = 2 should give 4. Training must find a good w. We start at w = 0.
Loss is the average squared error. At w = 0 both guesses are 0, so the errors are 2 and 4: loss = (4 + 16) ÷ 2 = 10. The valley shows the loss at every w; lowest at w = 2.
Walking downhill
The computer cannot see the whole valley. It can only measure the slope where it stands, called the gradient. At w = 0 the slope is −10: nudge w up by 0.01 and the loss drops by about 0.1. Downhill is to the right.
Gradient descent steps against the slope: new w = w − learning rate × slope. With a learning rate of 0.1: 0 − 0.1 × (−10) = 1. The loss drops from 10 to 2.5.
At w = 1 the slope is only −5, so the same rule takes a smaller step: 1 − 0.1 × (−5) = 1.5, and the loss falls to 0.625. Near the bottom the ground is flatter, so the steps shrink by themselves.
That is the whole loop: measure the slope, step, repeat. Two more steps reach w = 1.75, then 1.875, and the loss falls to 0.16, then 0.04. The slope shrinks toward 0 at w = 2, which fits both examples exactly.
Step size and scale
The 0.1 is the learning rate: how big a step to take for each unit of slope. People choose it before training. Start again at w = 0 with 0.01 instead: four steps only reach w ≈ 0.37. Too small means slow.
Now try 0.4. From w = 0 the step overshoots: 0 − 0.4 × (−10) = 4, the far wall, where the loss is still 10. The slope there is +10, so it jumps straight back. It bounces for ever; anything bigger climbs out.
Real models have millions or billions of weights, so the valley has that many directions. Backpropagation finds the slope for every weight at once, and each step usually uses a small random batch of examples: stochastic gradient descent.
What happens
;; step 1/9 · setup
model guess = w * x
data x = 1 -> y = 2
x = 2 -> y = 4
start w = 0 (guesses 0 and 0: both wrong)
goal find the w with the smallest loss
For this data the loss is 2.5 × (w − 2)² and its slope is 5 × (w − 2), so with a learning rate of 0.1 each step halves the distance to w = 2. Real losses are rarely this tidy. A real loss surface can have many dips. Gradient descent only walks downhill from where it starts, so it can stop in a dip that is not the lowest one.