How a neural network is trained: loss, optimisers and overfitting

A neural network learns by repeating one loop: make a prediction (the forward pass), measure how wrong it was with a loss function, work out which weights caused the mistake (backpropagation), and let an optimiser adjust them a little. Inside each neuron, an activation function decides how strongly it fires. The skill is to learn the real pattern without memorising the training examples (overfitting).

Everyday analogy

Learning darts. You throw (forward pass), see how far you landed from the bullseye (loss), work out whether your elbow or your wrist caused the miss (backpropagation), and adjust slightly (optimiser). Small, steady corrections beat wild changes.

Example

A model predicts whether a claim will be approved. On its first pass through 50,000 past claims it is right only about half the time. After 20 passes, adjusting its weights after every small batch, it gets about 9 in 10 right on claims it has never seen. (Illustrative numbers.)

Inside one neuron

1 · INPUTSNumbers come in, e.g. 2 and 3
→
2 · WEIGHTSEach is multiplied by a weight, e.g. 0.5 and −0.2
→
3 · SUM + BIASAdd them up plus a bias: 1.0 − 0.6 + 0.1 = 0.5
→
4 · ACTIVATIONAn activation function shapes the result: ReLU(0.5) = 0.5

Activation functions

FunctionWhat it doesOutput rangeUsually used for
ReLUKeeps positive numbers, turns negatives into 00 and upHidden layers (today's default)
Leaky ReLULike ReLU, but lets a tiny bit of negative through so neurons don't go silentAny numberHidden layers when ReLU neurons stop learning
SigmoidSqueezes any number into 0 to 10 to 1The last layer for yes/no answers (fraud or not fraud)
TanhSqueezes into −1 to 1, centred on zero−1 to 1Older networks and RNNs
SoftmaxTurns a list of scores into probabilities that add up to 100%0 to 1, total 1The last layer when choosing between many classes

Why do we need them? Without activation functions, even a 100-layer network could only draw straight lines. They let it learn curves and complex patterns. Why ReLU replaced sigmoid inside networks is explained in vanishing gradients.

The training loop

1 · FORWARDData flows through the layers to a prediction
→
2 · LOSSCompare with the right answer: how wrong?
→
3 · BACKWARDBackpropagation works out each weight's share of the blame
→
4 · UPDATEThe optimiser nudges every weight
→
↻ REPEATThousands to millions of times

Loss functions: how wrong was the guess?

Loss functionUsed when the answer is…How it scores mistakesExample
Mean squared error (MSE)A numberSquares each error, so big misses count a lotPredicting a hospital bill amount
Binary cross-entropyYes or noPunishes confident wrong answers heavilyFraud or not fraud
Categorical cross-entropyOne of many classesSame idea, for many choicesWhich team should handle this email

Optimisers: how the weights get adjusted

OptimiserHow it adjustsIn practice
Gradient descentUses all the data for each stepSteady but slow on big data
Stochastic gradient descent (SGD)Uses one example, or a small batch, per stepFast and a little noisy; the noise can even help
MomentumKeeps moving in a direction that keeps working, like a rolling ballGets past small bumps faster
AdamAdapts the step size for every weight and adds momentumThe usual first choice for deep learning

The maths behind this is explained in Calculus and optimisation for AI.

Training words you'll hear

TermMeaning
EpochOne full pass through all the training data
BatchA small group of examples processed together, e.g. 32 or 64
IterationOne weight update, which means one batch
Learning rateHow big each adjustment step is

Quick sum: 10,000 examples with a batch size of 100 means 100 iterations per epoch. Train for 20 epochs and the weights are updated 2,000 times.

Learning rateWhat happens
Too highOvershoots the best answer and bounces around, or blows up
Too lowLearns painfully slowly and may stall
Just rightThe loss falls steadily, then levels off

Training, validation and test data

SetTypical shareJobLike…
Training70–80%The model learns from itPractice questions
Validation10–15%Tune settings and catch overfitting during trainingA mock exam
Test10–15%Final, honest score, used once at the endThe real exam

Overfitting and underfitting

Underfitting

  • The model is too simple
  • Poor on training data and on new data
  • Like a student who didn't study

Overfitting

  • The model memorised the training data
  • Excellent on training data, poor on new data
  • Like a student who learned the answer key by heart
FixHow it helps
More, and more varied, dataHarder to memorise, easier to find the real pattern
Data augmentationCreates extra examples by flipping, rotating or cropping images, or rewording text
DropoutRandomly switches off some neurons (often 20–50%) during training, so no single neuron becomes a crutch
Early stoppingStops training when the validation error starts rising
Regularisation (L1, L2)Penalises very large weights to keep the model simpler
Batch normalisationKeeps the numbers flowing between layers in a steady range, so training is faster and more stable
A smaller modelFewer layers or neurons, so less room to memorise

Try it yourself

About 15 min

Watch a network learn in your browser

  1. Open TensorFlow Playground (free, no sign-up).
  2. Choose the spiral dataset and press play with learning rate 0.03. Then try 3, then 0.0001.
  3. Set the training data ratio to 10%, add many neurons, and watch the test loss climb while the training loss keeps falling.

What you should see: How the learning rate changes the speed and stability of learning, and what overfitting looks like in real time.

Use any free AI chatbot (ChatGPT, Claude, Gemini or Copilot) where needed. Never paste private or confidential information.

Build it for real · guided projectTeach a neural network to read handwritingAbout 60 min · Copy-paste code · step by step
Test what you learned

Take a quick quiz, or look up any word in the glossary.