LineLapse BlogMake your own →
← All posts

How does a neural network learn?

#ai#machine-learning#neural-networks

When people ask how a neural network learns, they usually want to know what is actually changing inside it. The short answer: a neural network learns by adjusting its numbers, called weights, to make its mistakes smaller. It makes a prediction, measures how wrong it was, works out which weights contributed to the error, and nudges each one slightly in the direction that reduces it. Repeat that millions of times and the network gets good at the task.

How a Neural Network Learnsforward pass · loss · backprop · updateForward Pass1Loss Function2error scoreBackpropagation3Update Weights4gradient descentweights encode patternsno rules giventhe heartbeat of every networkpredictionerrorgradientrepeat
▶ Watch it draw

What a neural network is made of

A neural network is a stack of layers. Each layer takes a list of numbers, multiplies them by weights, adds them up, and passes the result through a simple function that bends the output. The next layer does the same with those results. At the start, the weights are small random numbers, so the network's first answers are essentially guesses.

Nothing in the network "knows" what a cat or a sentence is. Everything it has learned lives in those weights, which is why learning means changing them.

Step 1: make a prediction

Training starts with an example where the right answer is known: an image labelled "cat", or a sentence with the next word hidden. The example goes in at the first layer, flows forward through each layer, and a prediction comes out. This is called the forward pass.

Step 2: measure the error with a loss function

Next, a loss function compares the prediction with the correct answer and turns the gap into one number. A big loss means the network was far off; a small loss means it was close. Everything that follows has one goal: make this number smaller across many examples.

Step 3: backpropagation assigns the blame

The hard question is which of the millions of weights to change. Backpropagation answers it. It works backward from the loss, layer by layer, using the chain rule from calculus to compute, for every weight, how much a tiny change to that weight would change the loss. That value is the weight's gradient.

A helpful picture is a team that missed a deadline. You trace backward through who handed what to whom, and work out how much each person's step contributed to the miss.

Step 4: gradient descent takes a small step

Now every weight is nudged a little in the direction that lowers the loss. This is gradient descent. Imagine standing on a hilly landscape in fog, where height is the loss. You can't see the valley, but you can feel which way the ground slopes under your feet, so you step downhill.

The size of the step is the learning rate. Too large, and you overshoot the valley and bounce around. Too small, and training crawls. Choosing it sensibly is one of the practical decisions in training.

How a neural network learns over many rounds

Real training does not use one example at a time. The data is split into small batches, and the network runs the four steps above on each batch: predict, measure loss, backpropagate, update. One full pass through all the data is an epoch, and training usually runs many of them. Early on, the loss drops quickly. Later, improvements get smaller and the network settles.

Along the way the layers tend to specialise. In image models, early layers often pick up simple patterns like edges, while later ones combine them into more complex shapes. Nobody programs this in; it emerges because those weights reduced the loss.

Learning versus memorising

A network that only memorises its training examples will fail on new ones. To check, practitioners hold back data the network never trains on and measure the loss there too. If training loss keeps falling while the held-back loss rises, the network is overfitting. Techniques like more data, simpler models, or regularisation help keep it learning general patterns.

This same loop sits underneath much larger systems, including large language models, whose training repeats the idea at enormous scale. The numbers those models adjust include the embeddings that place words in space.

Quick recap

  • A neural network learns by changing its weights, not by storing facts.
  • A forward pass makes a prediction; a loss function scores how wrong it was.
  • Backpropagation computes each weight's gradient: its share of the blame.
  • Gradient descent nudges every weight a small step downhill, sized by the learning rate.
  • Repeating this over many batches and epochs, while checking on unseen data, produces a network that generalises.

Make your own →