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
Activation functions
| Function | What it does | Output range | Usually used for |
|---|---|---|---|
| ReLU | Keeps positive numbers, turns negatives into 0 | 0 and up | Hidden layers (today's default) |
| Leaky ReLU | Like ReLU, but lets a tiny bit of negative through so neurons don't go silent | Any number | Hidden layers when ReLU neurons stop learning |
| Sigmoid | Squeezes any number into 0 to 1 | 0 to 1 | The last layer for yes/no answers (fraud or not fraud) |
| Tanh | Squeezes into −1 to 1, centred on zero | −1 to 1 | Older networks and RNNs |
| Softmax | Turns a list of scores into probabilities that add up to 100% | 0 to 1, total 1 | The 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
Loss functions: how wrong was the guess?
| Loss function | Used when the answer is… | How it scores mistakes | Example |
|---|---|---|---|
| Mean squared error (MSE) | A number | Squares each error, so big misses count a lot | Predicting a hospital bill amount |
| Binary cross-entropy | Yes or no | Punishes confident wrong answers heavily | Fraud or not fraud |
| Categorical cross-entropy | One of many classes | Same idea, for many choices | Which team should handle this email |
Optimisers: how the weights get adjusted
| Optimiser | How it adjusts | In practice |
|---|---|---|
| Gradient descent | Uses all the data for each step | Steady but slow on big data |
| Stochastic gradient descent (SGD) | Uses one example, or a small batch, per step | Fast and a little noisy; the noise can even help |
| Momentum | Keeps moving in a direction that keeps working, like a rolling ball | Gets past small bumps faster |
| Adam | Adapts the step size for every weight and adds momentum | The usual first choice for deep learning |
The maths behind this is explained in Calculus and optimisation for AI.
Training words you'll hear
| Term | Meaning |
|---|---|
| Epoch | One full pass through all the training data |
| Batch | A small group of examples processed together, e.g. 32 or 64 |
| Iteration | One weight update, which means one batch |
| Learning rate | How 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 rate | What happens |
|---|---|
| Too high | Overshoots the best answer and bounces around, or blows up |
| Too low | Learns painfully slowly and may stall |
| Just right | The loss falls steadily, then levels off |
Training, validation and test data
| Set | Typical share | Job | Like… |
|---|---|---|---|
| Training | 70–80% | The model learns from it | Practice questions |
| Validation | 10–15% | Tune settings and catch overfitting during training | A mock exam |
| Test | 10–15% | Final, honest score, used once at the end | The 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
| Fix | How it helps |
|---|---|
| More, and more varied, data | Harder to memorise, easier to find the real pattern |
| Data augmentation | Creates extra examples by flipping, rotating or cropping images, or rewording text |
| Dropout | Randomly switches off some neurons (often 20–50%) during training, so no single neuron becomes a crutch |
| Early stopping | Stops training when the validation error starts rising |
| Regularisation (L1, L2) | Penalises very large weights to keep the model simpler |
| Batch normalisation | Keeps the numbers flowing between layers in a steady range, so training is faster and more stable |
| A smaller model | Fewer layers or neurons, so less room to memorise |
Try it yourself
About 15 minWatch a network learn in your browser
- Open TensorFlow Playground (free, no sign-up).
- Choose the spiral dataset and press play with learning rate 0.03. Then try 3, then 0.0001.
- 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.