Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Deep Learning & Neural Networks — Alpha School

Lesson 3: How Networks Learn: Loss and Gradient Descent

A network starts out knowing nothing: its weights are set to small random numbers, so its first guesses are nearly random. Learning is the process of nudging those numbers, a little at a time, until the guesses get better. To know whether a nudge helped, the network needs a score for how wrong it is. That score is called the loss, and the whole of training is the attempt to make the loss as small as possible.

The loss is computed by comparing the network's output with the correct answer. For a picture of the digit 3, the network should put a high number on its "3" output and low numbers elsewhere; the loss measures how far it is from that ideal. One common choice for classification adds up the squared differences between the outputs and the ideal answers, then averages over the examples. A large loss means the network is far off; a loss near zero means its answers are nearly right. The exact formula matters less at this stage than the idea: one number that says how wrong the model is right now.

Walking Downhill

Gradient descent is the method used to reduce the loss. Picture the loss as the height of a landscape, where every direction is a different weight, and the network's current weights place it somewhere on a hillside. The gradient is the slope at that spot: it tells you which way is uphill, and how steep it is. Gradient descent takes a step in the opposite direction — downhill — and repeats. Each step is small, controlled by a number called the learning rate, and after many steps the network settles near the bottom of a valley, where the loss is low.

The learning rate is a trade-off. Too small, and training crawls: the network may need an enormous number of steps to reach the valley. Too large, and it leaps over the valley, bouncing from side to side and possibly getting worse instead of better. In practice the learning rate is one of the first things a practitioner tunes, often starting small and watching the loss fall steadily. There is no formula that gives the right value in advance; it is found by experiment.

Why This Works at All

It is worth pausing on how remarkable this is. The network has thousands of weights, so the landscape has thousands of directions, yet following the downhill slope still works. The reason is that the gradient is computed exactly, using calculus, for every weight at once — the network knows, for each weight, whether increasing it would raise or lower the loss. The next lesson explains how that computation is done efficiently. For now, hold on to the picture: a single score for wrongness, a slope that points downhill, and many small steps.

One more caution belongs here. Gradient descent finds a low point, but not necessarily the lowest point in the whole landscape. The landscape has many valleys, and the network settles into whichever one it walks into first. In practice that is usually good enough: a network does not need the perfect set of weights, only a set that generalises well to new examples — a theme the course returns to again and again.

The Loss Landscape, Visualised in Your Head

It is hard to picture a landscape with thousands of dimensions, so shrink it to two. Imagine a valley with a river running along its bottom, steep sides, and a flat stretch near the end. Gradient descent is a walker in that valley in thick fog: they can feel the slope under their feet, but they cannot see where the valley leads. With a sensible step size they follow the river downhill and reach the flat ground. With steps that are too large they lurch from one side of the valley to the other, crossing the river again and again without ever settling. With steps that are too small they move so slowly that they run out of daylight before reaching the bottom.

Real loss landscapes are messier than any valley: they have many dips, ridges and flat plateaus where the slope is nearly zero and progress stalls. A plateau is a particular frustration, because the gradient is tiny and the walker barely moves, even though a better valley may lie just beyond it. This is one reason training uses tricks like momentum — letting the walker build up speed in a consistent direction — and why the optimiser you choose matters. The picture to keep is not a smooth bowl but a rugged terrain, and training is the art of crossing it efficiently.

Key Terms

Loss
A single number measuring how wrong a model's predictions are on its training examples.
Gradient
The slope of the loss: for each weight, whether increasing it would raise or lower the loss.
Gradient descent
The training method of repeatedly stepping downhill on the loss landscape.
Learning rate
The size of each gradient-descent step; too small is slow, too large can overshoot.
Epoch
One full pass through the entire training set.
Local minimum
A valley in the loss landscape that is low nearby but may not be the lowest point overall.
Cost function
Another name for the loss function: the quantity training tries to minimise.

Discussion Questions

  1. Why does training need a single number like the loss, instead of just counting how many answers are right?
  2. Imagine walking downhill in fog with very long strides. When would that help, and when would it hurt?
  3. If two different sets of weights give the same low loss, does it matter which one training finds? Why?

Practice Questions

  1. What does a loss of zero mean?
  2. The gradient tells you the uphill direction. Which way does gradient descent step?
  3. What happens if the learning rate is far too large?
  4. A network trains for 3 epochs on 1,000 examples. How many individual examples has it seen in total (counting repeats)?
  5. Why is the loss usually averaged over many examples rather than computed on one?
Show answers
  1. The model's predictions match the correct answers exactly on the examples measured. In practice a loss of exactly zero is rare and can even be a warning sign of memorisation.
  2. Downhill — the opposite of the gradient. Each step moves the weights a little in the direction that reduces the loss.
  3. The steps overshoot the valley and the loss can bounce around or even grow. Training becomes unstable instead of steadily improving.
  4. 3,000. One epoch is one full pass through all 1,000 examples, so 3 epochs means 3,000 example presentations.
  5. So that one unusual example cannot dominate the training signal. The average shows how the model is doing overall, which is what we want to improve.
Keep reading to complete this lesson