Part 1: Start hereChapter 3 of 159
How models learn: the training loop
Beginner4 min1 code sample3 review cards
Chapter 1 fitted a line in one step with a formula. Most models have no such formula. They are trained by a loop that is almost embarrassingly simple, and the same loop trains a line, a spam filter and a language model with hundreds of billions of parameters. Learn it once.
What you will learn
- Describe the training loop of prediction, loss, gradient and update
- Write gradient descent for a line in plain numpy and watch the loss fall
- See how the learning rate decides between slow, good and unstable training
The four steps#
- Predict. Run the examples through the model with its current parameters.
- Measure. A loss function turns the gap between predictions and true answers into one number: how wrong the model is. Lower is better.
- Find the direction. The gradient tells you, for every parameter, whether increasing it makes the loss go up or down, and how fast (chapter 23).
- Update. Move every parameter a small step against the gradient, so the loss goes down. The step size is the learning rate.
Then repeat, thousands or billions of times. This is gradient descent. The picture to hold is a ball rolling downhill on a landscape whose height is the loss.
The loop in code#
For a line y = w x + b with the mean squared error as the loss, the gradients have a simple closed form: the gradient of the loss with respect to w is the average of 2 × error × x, and with respect to b the average of 2 × error. The script generates points around the line y = 3x + 2, starts with w = b = 0 and runs the loop.
import numpy as np
rng = np.random.default_rng(0)
x = rng.uniform(-3, 3, 200)
y = 3.0 * x + 2.0 + rng.normal(0, 0.8, 200) # the truth is w = 3, b = 2, plus noise
w, b = 0.0, 0.0 # start knowing nothing
learning_rate = 0.05
for step in range(101):
prediction = w * x + b # 1. predict
error = prediction - y
loss = np.mean(error ** 2) # 2. measure
grad_w = np.mean(2 * error * x) # 3. which way is downhill?
grad_b = np.mean(2 * error)
w -= learning_rate * grad_w # 4. step against the gradient
b -= learning_rate * grad_b
if step in (0, 1, 2, 5, 10, 25, 50, 100):
print(f"step {step:3d}: loss {loss:8.3f} w = {w:5.2f} b = {b:5.2f}")Output from a real run
step 0: loss 36.411 w = 1.03 b = 0.26 step 1: loss 17.437 w = 1.72 b = 0.48 step 2: loss 8.867 w = 2.17 b = 0.65 step 5: loss 2.154 w = 2.78 b = 1.03 step 10: loss 1.008 w = 2.98 b = 1.41 step 25: loss 0.682 w = 2.98 b = 1.82 step 50: loss 0.667 w = 2.97 b = 1.93 step 100: loss 0.667 w = 2.97 b = 1.94
The loss collapses from 36 to about 1 in ten steps and the parameters settle near the true 3 and 2 (w = 2.97, b = 1.94: not exact, because of the noise in a finite sample). The loss bottoms out around 0.67, close to the variance of the noise we added (0.8 squared is 0.64), which is the best any line can do on this data. A real network does precisely this, only with millions or billions of parameters, and a method called backpropagation to compute all the gradients efficiently (chapter 64).
The learning rate decides everything#
The step size is the most important setting in the loop. Too small and training crawls. Too big and every step overshoots the valley, and the loss grows instead of shrinking. Try it yourself: in the explorable, start on either side, pick a learning rate, and press Step.
Quick check
You train for a while and the loss gets bigger with every step, growing without bound. What is the most likely cause?
From a line to everything else#
Nothing in the loop depends on the model being a line. Replace the line by a neural network and the squared error by another loss, compute the gradients with backpropagation, and you have deep learning. Replace the data by the whole internet and the model by a transformer, and you have a language model. The rest of the course is about better models, better data, better losses, better ways of computing the gradient and better ways of knowing whether the result is any good.
Check yourself
Answer in your head first, then open the answer and say how it went. That is what schedules your review.
What are the four steps of the training loop?
Why does the loss fall quickly at first and then slowly?
Finished this chapter?
Your progress is saved in this browser.
Up next: 4. Your machine learning toolkit · 4 min
3 questions from this chapter are in your review queue.Practise your reviews
About this chapter
Builds on1. What is machine learning?
This unlocks23. Derivatives and gradients38. Linear regression39. Gradient descent in practice
On the learning pathsAI from zeroMachine learning engineer
Checked against python 3.14, numpy 2.5, scipy 1.18, scikit-learn 1.9, pandas 3.1, torch 2.14, gymnasium 1.4 on 2026-10-24. AI moves fast, and library versions, model names and prices change often. Where current documentation disagrees with this page, trust the documentation, and tell us on the corrections page.