Gradient descent stops when the model is "good enough." But good enough at what? Here is the uncomfortable truth: a model that predicts its own training data perfectly can still be useless.
The training data is never the point. We already know the targets for those rows, that is what made them training data. The entire reason for building a model is to make predictions for inputs it has never seen: the next student, the next house, tomorrow's sensor reading. How well a model performs on unseen data is called generalisation, and it is the only score that ultimately matters.
Memorising versus learning
Think of two students preparing for an exam.
The first student works through past papers to understand the method: why each step follows from the last, how to recognise which technique a question calls for. The second student memorises the past papers, every question, every answer, word for word. Quiz them both on last year's paper and the memoriser scores 100%, beating the honest learner. Put a new paper in front of them and the memoriser collapses, while the learner does about as well as they always did.
A machine learning model can be either student, and nothing in the training loss tells you which one you have. A model that has memorised its training data, noise, outliers, quirks and all, is called overfitted. It has fitted itself not just to the underlying pattern but to the random scatter around it, and random scatter does not repeat in new data.
There is an opposite failure, too: a model too simple to capture the pattern at all (imagine forcing a flat horizontal line through clearly rising data). That is underfitting. It does badly everywhere, on training and new data alike, which at least has the virtue of being easy to notice.
See it happen
Remember from Part 3 that adding higher powers of x gives a model more knobs and lets it bend. More knobs means more capacity, and more capacity is exactly what a memoriser needs.
Below, eleven data points (dark dots) were used to fit three models: a straight line (2 knobs), a gentle cubic (4 knobs), and a degree-9 polynomial (10 knobs, almost one per data point). Five more points from the same source were held back and never shown to the models. Fit each model, admire its training score, then reveal the held-back points. And this time the knobs themselves are exposed below the chart: every coefficient arrives set to its fitted value, and you can grab any of them and see what your meddling does to both scores.
The model's knobs (set to the fitted values — drag any of them):
Watch the two numbers as you switch models. The straight line has a modest training error and, once revealed, a similar held-back error: it learned the trend. The degree-9 polynomial drives its training error down close to zero, it snakes through nearly every training point, but between and beyond those points it swings wildly, and its held-back error explodes. That gap between "brilliant on training data" and "terrible on new data" is overfitting. The cubic sits in between: a little more flexible than the line, no real gain, because the underlying pattern here really is close to straight.
Now play with the knobs. Nudge the straight line's w1 and the curve tilts politely, the training MSE creeping up as you stray from the fitted value; you could almost re-fit it by hand. Then switch to degree 9 and breathe on w9, a knob whose fitted value is 0.0003, and the curve convulses. That fragility is what 10 delicately counterbalanced knobs look like: enormous coefficients cancelling each other to the fourth decimal place just to thread through eleven training points. A model that learned the trend barely notices a nudged knob; a model that memorised the points falls apart at a touch.
The fix: hold data back
The memorising student was only exposed because we handed them a paper they had not seen. Models are policed the same way. Before training starts, the dataset is split, a common recipe being:
- Training set (say 70–80%): the only data gradient descent ever sees. Weights and biases are fitted to this and nothing else.
- Validation set (say 10–15%): kept aside during training but checked regularly, like mock exams. If training loss keeps falling while validation loss starts rising, the model has begun memorising, and we stop. This is the early stopping rule from Part 5, now with its full explanation. The validation set also referees choices like the learning rate or how many
x²,x³terms to allow. - Test set (say 10–15%): touched exactly once, at the very end, for the final grade. It has had no influence on any decision, so it is the closest thing we have to genuinely unseen data.
One subtlety worth appreciating: the moment you use data to make a decision about the model, that data stops being "unseen." That is why the validation and test sets are separate, the validation set gets consulted many times and slowly leaks its quirks into your choices, so the final verdict needs data that never influenced anything.
Key takeaways
A model's true score is measured on data it never trained on: that is generalisation. When training performance is excellent but unseen-data performance is poor, the model has overfitted, and the more knobs a model has relative to its data, the easier memorisation becomes. Splitting data into training, validation, and test sets is how practitioners catch it, and early stopping is that split acting as a tripwire during training itself.
Keep this idea close, because the next part hinges on it: it turns out that failing to find the perfect minimum of the loss can be a blessing in disguise.
Next: Local vs Global Minima: what happens when the loss surface has hills and valleys, and why ending up at a local minimum is not always a bad thing.