Train a neural network

Here you get to see how a neural network actually learns. We follow a single training example all the way round: the network guesses, we measure how wrong the guess was, the error is sent backwards through the network, and every weight is nudged a little.

1
Forward pass
The input is sent through the network, which makes a guess.
2
Loss
We compare the network's guess with the correct answer and work out how large the error is.
3
Backpropagation
The error is sent backwards through the network, so every weight learns how much it contributed to it.
4
Update
Every weight is nudged a little in the direction that reduces the error.

What should the network learn?

Pick a logic gate. The network has to learn to give the right answer for all four combinations of the inputs x₁ and x₂.

AI Activities

Train a neural network

Follow the network as it learns, step by step, from a random guess to a fully trained model.

Svenska
Advanced

What does backpropagation actually do?

The error is shared out backwards

Backpropagation is the method that works out how each weight should change so the network guesses better next time.

Once the network has made its guess, we first work out how large the error was. Then we work backwards through the network. Starting at the output, we ask:

How much did this neuron contribute to the error?
And then: how much did the neuron before it contribute?

In the end every weight knows how much influence it had on the error. That number is called the error signal.

Derivatives: how sensitive is the error?

To decide how much a weight contributed to the error we use derivatives. A derivative answers the question:

If the weight is increased a tiny bit, how much larger or smaller does the error become?

At every calculation step z (the neuron's weighted sum), a (the neuron's activation, the sum after sigmoid), ŷ (the network's guess) and L (the loss, how wrong the guess was) are concrete numbers, worked out for a given input and given weights. What gets differentiated is not those individual numbers but the relationship between them. Hold every other value fixed and let w vary, and a curve appears – the derivative is the slope of that curve at exactly the point where the weight currently sits. Not the height, but how steep it is there.

The chain rule

A weight does not affect the error directly. It first affects the next neuron, that neuron affects the next, and finally the network's output, which in turn affects the loss.

WeightNeuronOutputLoss

To work out how much the weight affects the loss we have to account for every step in the chain. That is why the local slopes are multiplied together:

How the weight affects the neuron×How the neuron affects the output×How the output affects the loss= How the weight affects the loss

Each link in the chain has its own ready-made formula. The notation ∂A/∂B reads: how much A changes when B is nudged slightly.

How the loss changes when the guess changesHow the activation changes when the sum changesHow the sum changes when the weight changes

The chain rule multiplies these slopes together, step by step backwards through the network – which is exactly what Phase 3 · Backprop does, and why the signal travels backwards: the forward pass works out the network's answer, backpropagation works out the responsibility for the error.

The gradient says what to do

The result for a weight is called its gradient, and it tells us two things:

Direction – should the weight increase or decrease?
Size – how much should it change?

The steeper the curve, the more sensitive the loss is to changes in the weight. Right at the bottom of the valley the slope is zero – there is no way to reduce the loss further by moving the weight in either direction. So the loss is at its smallest at the bottom of the curve.

Letting every weight take one small step down its own curve, in the opposite direction to the gradient, and repeating that round after round, is called gradient descent. That is exactly what happens in Phase 4 · Update weights, and it is how the network slowly works its way down towards the bottom of the valley.

Try it yourself: the slope at a point
w
w
L(w)
Slope dL/dw
The yellow line is the tangent – the slope of the curve at that exact point. That number is the derivative. The green arrow shows which way gradient descent nudges w: downhill, towards the bottom of the valley.