Lesson 3: How Machines Learn: Training, Testing and Generalisation
Training a machine learning model is like teaching a student with a workbook full of worked examples. The model is shown an example, makes a guess, is told the right answer, and adjusts itself a little so that next time its guess is closer. Repeat that thousands of times and the small adjustments add up to real skill. Nothing mystical happens: the model is simply nudged, step by step, towards guesses that match the labels in its training data.
To make those nudges, the model needs a way to measure how wrong it is. That measure is called a loss function, and it turns each mistake into a single number: small when the guess is close, large when it is far off. For a house-price model, the loss might be the difference between the predicted price and the real price. For a spam filter, it might count how many emails were put in the wrong folder. The training algorithm then changes the model's internal settings — its parameters — in whatever direction makes the loss smaller. The most common way of doing this is called gradient descent, which you will meet properly in Lesson 6: imagine walking downhill in fog, always stepping in the direction where the ground falls fastest, until you reach the bottom of the valley.
Testing on data the model has never seen
Here is the trap every beginner falls into: a model that does brilliantly on its training data may be useless in the real world. If a student memorises the workbook instead of learning the subject, they will fail a new exam. A model can do the same — it can memorise its training examples instead of learning the general pattern. That is why data is always split before training begins. Most of it becomes the training set, which the model learns from, and the rest is held back as a test set, which the model never sees until the end. The test score is the honest measure of how well the model has learned, because it shows how it does on fresh examples.
The word for what we really want is generalisation: the ability to do well on new data, not just on the data the model was trained on. Generalisation is the whole point of machine learning. A model that generalises has found the real pattern — for example, that larger houses in the same area tend to cost more — rather than memorising quirks of the training set, such as one unusually cheap house with a large garden. When a model fails to generalise, it is usually because it memorised too much or because its training data did not represent the real world. Both problems have fixes, and you will learn them in Unit 3.
When training goes wrong
Training does not always go smoothly, and the loss curve is the first place to look when it does not. If the loss barely falls, the learning rate may be too small, the model may be too simple, or the features may carry no useful signal. If the loss jumps up and down wildly, the learning rate is probably too large, and the model is leaping over the best settings instead of settling into them. If the training loss falls nicely but the test loss starts rising, the model has begun to memorise — it is overfitting, and you should stop training earlier or simplify the model. None of these is a disaster; each is a symptom with a known remedy, and learning to read them is a large part of becoming competent. A good habit is to plot the training and test loss after every run, so you can see what happened instead of guessing from a single final score.
There is one more habit that separates good machine learning from guesswork: never let the test set influence your decisions. The moment you peek at the test results and change the model to improve them, the test set stops being an honest check — it has become part of the training. Professionals keep a third split, called a validation set, for exactly that purpose: they tune the model using the validation set and keep the test set locked away until the very end. It feels slow and strict, but it is the only way to know, before you hand a model to real users, whether it actually works.
Key Terms
- Training
- The process of showing a model labelled examples and adjusting it so its guesses get closer to the right answers.
- Loss function
- A measure of how wrong a model's guesses are, turned into a single number that training tries to make smaller.
- Parameter
- An internal setting of a model, such as a weight, that training adjusts to improve its guesses.
- Gradient descent
- A training method that adjusts a model's parameters step by step in the direction that reduces the loss fastest.
- Training set
- The part of the data a model learns from.
- Test set
- Examples held back from training and used only at the end, to give an honest measure of how the model does on new data.
- Generalisation
- How well a model performs on new, unseen data — the real goal of machine learning.
- Validation set
- A separate split of data used for tuning choices during development, so the test set stays untouched until the end.
Discussion Questions
- Why is a model that scores 100% on its training data not necessarily a good model?
- A student memorises past exam papers instead of learning the subject. How is that like a model that fails to generalise, and what would you do differently?
- Why do professionals keep a test set locked away instead of using it to improve the model?
Practice Questions
- What does a loss function do during training?
- Explain gradient descent in one sentence, using the downhill-walking picture.
- Why is data split into a training set and a test set?
- What is generalisation, and why is it the real goal of machine learning?
- What is a validation set for?
Show answers
- It measures how wrong the model's guesses are as a single number. Training then adjusts the model to make that number smaller.
- It adjusts the model's settings step by step in the direction that reduces the loss fastest, like walking downhill in fog. Each step follows the steepest fall in the loss, moving the model towards its best settings.
- So the model can be honestly tested on examples it has never seen. A score on training data can come from memorising, so only fresh data shows whether the model has really learned.
- Generalisation is doing well on new, unseen data. A model is only useful if it works on cases it was not trained on, which is what happens in the real world.
- It is used to tune the model during development while the test set stays untouched. That way the final test score stays an honest check, not something the model was adjusted to pass.