Measuring mistakes with a loss function
Before a network can improve, it needs a score for how wrong it is. That score comes from a loss function. The higher the loss, the worse the predictions.
For our tiny example, imagine we want to predict y = 2x using one weight w. If the correct answer is 6 and the network guesses 4, the error is 2. The loss is often the squared error: (6 - 4)² = 4.
Key idea: the loss function turns "good" or "bad" predictions into a number. Learning means making that number smaller.
The gradient
If you are standing on a hill and want to go down, the gradient tells you which way is uphill. To go down, you walk in the opposite direction.
In a neural network, the hill is the loss function. The weights are your position. The gradient tells you how to change the weights to increase the loss. So the network updates the weights in the opposite direction.
For one weight, the gradient is just the slope of the loss curve at the current point. If the slope is positive, decreasing the weight lowers the loss.
Gradient descent
Gradient descent is the algorithm:
- Make a prediction.
- Measure the loss.
- Compute the gradient.
- Nudge every weight a tiny step opposite the gradient.
- Repeat many times.
The size of the step is controlled by the learning rate. Too small and training takes forever. Too big and you might overshoot the bottom of the hill and start bouncing around.
Analogy: gradient descent is like a hiker with a flashlight in thick fog. They can only feel the ground sloping under their feet. They take small steps downhill until the ground feels flat.
Backpropagation
Real neural networks have millions of weights spread across many layers. How do we know the gradient for every single one?
The answer is backpropagation. It starts at the output, computes the error, and works backward through the network using the chain rule from calculus. Each layer passes the error to the layer behind it, like a row of dominoes falling in reverse.
You do not need to memorize the chain rule right now. The important thing is that backpropagation gives every weight a direction to move.
Key idea: forward pass makes a prediction. Backward pass computes gradients. Gradient descent updates the weights. Repeat millions of times.
Loss curve demo
We want to learn y = 2x from three points. Try moving the weight w with the slider. Watch how the loss changes and how the gradient points uphill or downhill.
Gradient descent in Python
This Python script is the same process you just explored. It starts with a random weight, measures how wrong the network is, computes the gradient of the loss, and takes a small step downhill.
Run this a few times and watch w move toward 2.0. The loss keeps shrinking, which is exactly what the interactive slider shows on the loss curve.
Check yourself
What would happen if the learning rate was huge, like 10? Why do we usually use a small learning rate?
Your answer:
Sample answer: A huge learning rate makes the weight take giant steps. It can overshoot the lowest point and bounce back and forth, never settling. A small learning rate takes careful steps toward the bottom.
Quick quiz
1. What does a loss function measure?
2. Which way do we move weights to reduce loss?
3. What is backpropagation?
Keep learning about LLMs
Now you understand how a network learns. Next, see how an LLM is trained on billions of tokens to predict the next word.
Part of the “LLMs Under the Hood” tutorial series.