Lesson 6 of 8 · A First Network
How a Network Learns
How a neural network learns from its mistakes: the forward pass, the loss, backpropagation and gradient descent, with an interactive learning rate demo.
- 7 min read
- Beginner
- Reviewed
- The four steps of the training loop
- What a loss function measures
- How gradient descent uses the slope of the loss to improve the weights
- Why the learning rate matters so much
The training loop
A new network starts with small random weights, so its first answers are random guesses. It improves by repeating the same four steps thousands or millions of times:
- Forward pass: run the inputs through the network to get a prediction.
- Loss: measure how wrong the prediction is, as a single number.
- Backpropagation: work out, for every weight, which way to nudge it to make the loss smaller.
- Update: nudge every weight a little in that direction.
forward pass -> loss -> backpropagation -> update weights -> repeatMeasuring the mistake
Two common choices:
- Mean squared error, for predicting numbers such as prices. Take each prediction's error, square it, and average the squares. Squaring makes every error positive and punishes big errors much more than small ones.
- Cross-entropy, for choosing between classes such as "cat" and "dog". It gives a small loss when the network is confident and right, and a very large loss when it is confident and wrong.
predictions: 3.0 5.0 8.0
correct: 2.0 5.0 6.0
errors: 1.0 0.0 2.0
squared: 1.0 0.0 4.0
mean squared error = (1 + 0 + 4) / 3 = 1.67Finding the way downhill
Picture the loss as a hilly landscape, with one direction for each weight. Training is a walk downhill in thick fog: you cannot see the valley, but you can feel which way the ground slopes under your feet, take a small step that way, and repeat.
Backpropagation computes the gradient efficiently. It starts at the loss and works backwards through the layers, using the chain rule from calculus to share out the blame for the error to every weight. Gradient descent then moves each weight a small step the opposite way, downhill:
new weight = old weight − learning rate × gradientThe graph below shows a loss that depends on a single weight, w. Move the sliders to change where training starts, the learning rate and the number of steps.
The same thing in code
Here is the same descent, printed step by step. The slope of (w − 3)² + 4 is 2 × (w − 3).
w = 12.0
learning_rate = 0.2
for step in range(1, 11):
loss = (w - 3) ** 2 + 4
gradient = 2 * (w - 3)
print(f"step {step:2d}: w = {w:7.4f}, loss = {loss:8.4f}")
w = w - learning_rate * gradient
print(f"after 10 steps: w = {w:.4f} (the best value is 3)")The steps get smaller by themselves: as the curve flattens near the bottom, the gradient shrinks, and so does each update.
Choosing the learning rate
- Too small: each step is tiny and training takes far too long.
- Too large: each step jumps past the valley. The loss bounces around or grows until the numbers overflow.
- About right: the loss falls quickly and steadily.
There is no single correct value. For real networks with the Adam optimiser, 0.001 is a common starting point, and people try a few values and compare the loss curves.
Common mistakes
Finish the training loop, then compare three learning rates after 20 steps. One is too small, one is about right and one makes training blow up.
def train(learning_rate, steps=20):
w = 0.0
for _ in range(steps):
gradient = 2 * (w - 3) # slope of the loss (w - 3) squared
# Update w with one gradient descent step here.
pass
return round(w, 3)
for rate in [0.01, 0.1, 1.1]:
print(rate, train(rate))
# Output must be exactly:
# 0.01 0.997
# 0.1 2.965
# 1.1 -112.013
Why does gradient descent subtract the gradient instead of adding it?
Summary
- Training repeats four steps: forward pass, loss, backpropagation, update.
- The loss turns all the mistakes into one number to make as small as possible.
- Backpropagation finds the gradient; gradient descent steps the weights downhill.
- The learning rate sets the step size: too small is slow, too large blows up.
Next: put all four steps together and train a real network in your browser.
Finished reading? Tick it off to track your progress.