Skip to Content

How a neural network learns

A neural network starts with random weights and gets better by making a guess, measuring how wrong it was, and nudging every weight a little. Train a real tiny network and watch its loss fall.

10 minFree

One training step
  1. Forward
  2. Loss
  3. Backprop
  4. Update
The network: 2 inputs, 2 hidden neurons, 1 output
0.500.30−0.400.600.40−0.30xHumidityxCloudsh1bias 0.00h2bias 0.00?Rain?
Training example

Evening 1: humidity 0.9, clouds 0.8

Prediction?Right answerrain (1)Loss?
All six evenings (target, then prediction)
  • 0.9 / 0.8rain50%
  • 0.8 / 0.2dry51%
  • 0.2 / 0.9dry50%
  • 0.1 / 0.3dry51%
  • 0.7 / 0.9rain50%
  • 0.4 / 0.4dry51%
Average loss · step 0 / 360
1.5360 steps
Ready

This tiny network guesses whether it will rain on a Chennai evening from two numbers: humidity and cloud cover. Its 6 weights and 3 biases start as random guesses. Press Play to train it.

Changing the learning rate restarts training and keeps the last run as a dashed line. The forward pass, log loss, backpropagation and updates are the real maths. The six evenings and starting weights are made up.

Background: the idea in plain words2 min read

Some rules are hard to write down. Will it rain this evening? You know it has something to do with humidity and clouds, but you can't give the exact formula. It would be useful if a computer could work the rule out from past evenings.

A neural network can do that. It is a stack of simple calculations. Each neuron (one small calculating unit) multiplies its inputs by weights (numbers that say how much each input matters), adds a bias (one extra number that shifts the result), and passes the result through an activation function that squashes it into a useful range. Weights and biases together are the network's parameters. Gemini has billions of them. The network below has nine.

Nobody sets those parameters by hand. They start as random numbers, so the first predictions are poor. Training improves them with a loop of four moves:

  1. Forward pass: run an example through the network and get a prediction.
  2. Loss: measure how wrong the prediction was, as one number. A big loss means a bad guess.
  3. Backpropagation: work backwards to find each weight's gradient, which says which way that weight pushed the loss and how hard.
  4. Update: move every weight a small step in the direction that lowers the loss. This is gradient descent.

Here is a tiny example (the numbers are made up). The network sees one evening: high humidity, many clouds, and it did rain. The network says "30% chance of rain". That is far from the truth, so the loss is high. Every weight is nudged so that an evening like this gets a higher number next time. Then the next evening is shown.

One nudge changes very little. Repeating the loop hundreds of times is what makes the network good. A setting called the learning rate decides how big each step is.

Press Play to watch two training steps in slow motion, then the rest at speed. When it finishes, drag the learning rate to 0.1 or 8 and train again. The last run stays on the chart as a dashed line, so you can compare.

What to notice

  1. One step changes every weight a little. No single example teaches the network the rule. Hundreds of small nudges, each from one example, add up to a network that predicts rain only when humidity and clouds are high.
  2. Loss is the only thing training looks at. The network doesn't "understand" rain. It finds whatever weights make the loss small on the examples it was shown. That's why the quality of training data matters so much.
  3. The gradient gives direction, the learning rate gives step size. Too small (0.1) and the loss crawls down. Too big (8) and each step overshoots, so the network gets stuck. Picking a good learning rate is part of the job.
  4. Hidden layers combine the inputs. Each hidden neuron learns its own mix of humidity and clouds, and the output neuron combines those mixes. The activation function (here, sigmoid) is what lets the network learn curved, non-linear patterns. Without it, stacked layers would still only draw straight lines.
  5. An LLM learns the same way, at a vast scale. Replace "rain or not" with "which token comes next", six evenings with trillions of tokens, and nine parameters with billions. The loop is still forward, loss, backprop, update. See Training an LLM.

In practice You almost never write backpropagation yourself. Libraries such as Keras compute the gradients for you. What you choose is the data, the shape of the network, the loss and the learning rate.

Go deeper

Words to know

1 min read
WordMeaning in simple words
NeuronOne small calculating unit: it multiplies its inputs by weights, adds a bias and squashes the result
WeightA number that says how much one input matters. Training changes it
ParametersAll the weights and biases of a network together
LossOne number that says how wrong a prediction was. Lower is better
BackpropagationWorking backwards from the loss to find how each weight should change
Learning rateHow big a step each weight takes on every update

Check your understanding

1. What does backpropagation produce?
2. You train with a very high learning rate and the loss jumps around instead of falling. What is the most likely cause?
3. After training, the network predicts rain correctly for all six evenings. Where is that knowledge stored?
Sources: the official documentation3 links

Something wrong or unclear in this lesson? Tell us, it gets fixed.