AI Run Lab

All chapters

How a model reduces its error

A loss measures the current mistake. A gradient tells the optimizer which small change would reduce that loss locally. Follow one weight through an entire training step.

Open this chapter with experiments and audio

How wrong is the prediction?

Take a model with one adjustable weight. It predicts an output by multiplying its input by the weight. With input x = 5 and weight w = 2, it predicts 10. The example supplies target y = 25. Subtract the target from the prediction to get −15. Squaring this difference gives a loss of 225. Squaring makes errors on either side contribute positively.

prediction = wx   ·   L = (wx − y)²

You could change the weight slightly and measure the difference. Increasing it to 2.01 raises the prediction to 10.05 and reduces the loss to 223.5025. A step of 0.01 lowered loss by about 1.50. This finite experiment suggests a local sensitivity of roughly −150 loss units per unit of weight.

Keep w at 2 and reduce h from 0.1 to 0.01. The average slope changes from −147.5 to −149.75, approaching the local derivative −150. The larger step includes more of the curve to the right of the starting point.

Where does the gradient come from?

The derivative is the limiting local rate of change. Each unit increase in w changes the prediction by x, here 5. At prediction 10, the derivative of squared error with respect to prediction is 2 × (10 − 25) = −30. The chain rule multiplies these sensitivities. The resulting derivative with respect to w is 5 × −30 = −150.

new weight = 2 − 0.01 × (−150) = 3.5

The learning rate sets the scale of the update. After this update the prediction is 17.5 and loss is 56.25. Recomputing the gradient at the new weight matters. Continuing to use −150 would ignore the shape of the loss as the parameter moves.

Follow several training steps

Start at w = 2 with a learning rate of 0.01. After two updates the weight is 4.25. Notice that the update shrinks as the prediction approaches 25. For this particular example a rate of 0.02 reaches w = 5 in one step. That value works because the loss has an unusually simple shape. Networks with many weights have different curvatures in different directions.

A learning rate of 0.04 gives 8 and then 2 again. Both weights produce loss 225. The optimizer has a correct gradient at each point, yet its finite steps jump across the minimum. In a real training run, a loss curve can therefore help reveal a poorly chosen update size.

What does backpropagation automate?

x, target, weight = 5.0, 25.0, 2.0
for step in range(3):
    prediction = x * weight
    loss = (prediction - target) ** 2
    print(step, weight, prediction, loss)
    gradient = 2 * x * (prediction - target)
    weight -= 0.01 * gradient

In this script the derivative fits on one line. A framework can record the operations used to compute a loss, then propagate derivatives backwards through the recorded operations. An optimizer consumes those derivatives to update the parameters. For a language model the loss usually measures the probability assigned to target tokens, but derivatives still connect that loss to trainable numbers.

At w = 5 in this example, what is the next update?

Prediction equals target. The gradient is zero, so this gradient-descent update leaves w at 5.

PyTorch · Optimizing Model Parameters

Can zero training error identify the two weights?

One observation has two inputs, 2 and 1, and a target of 5. Let the model predict 2a + b, where a and b are adjustable weights. Starting from a = 0 and b = 0 gives prediction 0, error −5 and squared loss 25. Raising a by a small amount changes the prediction twice as much as raising b by the same amount. The two controls therefore receive different training signals.

Call the prediction p. The slope of (p − 5)² with respect to p is 2(p − 5), initially −10. Follow each path back to a weight. The prediction changes by 2 per unit of a and 1 per unit of b. The slopes for the weights are −10 × 2 = −20 and −10 × 1 = −10. With learning rate 0.1, subtracting those gradients times 0.1 gives a = 2 and b = 1. The new prediction is 2 × 2 + 1 = 5.

Could another pair of weights do equally well here and disagree on the next case?

Yes. With a = 0 and b = 5 the prediction is also 5. For new inputs [1, 2], the first model predicts 1 × 2 + 2 × 1 = 4. The second predicts 1 × 0 + 2 × 5 = 10. The original observation cannot distinguish them. Another independently varying observation can constrain the two weights. This is one reason held-out performance and coverage of the input space matter.

Follow the individual updates in the gradient experiment →

Further reading · PyTorch · MSELoss · PyTorch · Automatic differentiation