L.14Part 3 / Learning
Contents

Overfitting

Gradient descent searches stubbornly for the setting that shrinks the error on the examples we give it. If the model has too many dials, it can end up making no mistake at all on those examples — and precisely then it becomes utterly useless in the real world. To memorise by heart and to understand are two completely different things, and the distance between them can be measured exactly.

Terms: degree training set test set underfitting overfitting regularisation validation set

The illusion of a perfect fit

Until now we have worked with a straight line fitted with only two dials. The line was well-behaved and rigid: it could not pass exactly through all our measurement points. We may be tempted to make the model more flexible: instead of a line, we use a supple curve with several undulations, adding one dial for each new bend. The number of possible bends is the degree of the curve.

At first sight the logic seems unshakeable: the more dials we have, the cleverer and more capable the model ought to be. If we give it enough dials, the fit becomes astonishing: through 14 points we can pass a curve of degree 13 that touches every one of them with surgical precision. On those 14 points the error drops to absolute zero!

Seen close up, however, that perfect curve is a disaster. Between the known points it makes wild leaps skyward and plunges into deep chasms. If you ask it how long a journey takes for a distance it has not yet seen, it will give absurd answers — that a journey of 15 kilometres takes ten hours, say, or even minus five minutes. The model is the very image of a pupil who has memorised the answers to a multiple-choice test without understanding the subject: he scores full marks on the test he knows, but freezes completely at the first new question.

The data seen and the data unseen

To unmask this trap, researchers use a simple and elegant method: they divide the available measurements into two heaps from the very beginning. Some of the examples are put at the algorithm’s disposal so that it can search for the ideal dials; they form the training set. The other part is locked in a safe and takes no part at all in the training; it forms the test set.

In this way we compute the cost twice: once on the training data (which shows us how well the machine has memorised what it was shown) and once on the test data (which measures its true understanding). This rigorous separation is the cornerstone of the whole of modern artificial intelligence: the only honest way to check whether a machine has understood a concept is to put it to the test on examples it has never seen before.

The same measurements, curves of different degrees
training test the fitted curve the true relationship
2
14
0

The U-shaped curve: the map of learning

If we follow the behaviour of the two costs as we increase the number of dials (the degree of the curve), the most famous pattern in machine learning comes to light. The training cost (the blue line) falls unceasingly: more dials always mean a tighter fit on the known examples. The test cost (the violet line), by contrast, falls at first, reaches an ideal minimum, and then begins to climb steeply!

The region to the left of the minimum is called underfitting (or underfitting): the model is too rigid, too poor in dials to capture the real shape of the phenomenon. The region to the right of the minimum is called overfitting (or overfitting): the model is so flexible that it begins to follow every chance wobble in the training data, believing that every accident is a universal law.

The cost on the two sets, for each degree

The lowest point of the violet curve marks the golden setting. Take care: this optimum cannot be discovered by looking at the training data alone, because on the training data the model seems better and better as it becomes ever more useless! Without a test set, a builder of models is as blind as a pupil marking his own homework with the textbook open.

Signal and noise: why the illusion appears

Every measurement in real life contains two ingredients mixed inseparably: the signal (the true physical relationship, which will repeat itself tomorrow as well) and the noise (the chance deviations produced by wind, inattention or a sudden braking, which will never repeat themselves identically). The ideal model ought to listen only to the signal and ignore the noise completely.

But the model has no way of knowing which is which: in the raw figures, the noise and the signal look the same! A simple model does not have enough dials to follow the little zigzags of the noise, so it retains only the general tendency of the signal. A model with dozens of dials, on the other hand, has “freedom” in abundance. Gradient descent will use every bit of it to reduce the cost, contorting the curve to fit every noisy point.

To generalise means to have sound judgement about situations you have not yet met. And to generalise well, you must not spend your whole capacity for attention memorising every accidental detail of the past.

The surest cure: more examples

Overfitting is not an eternal condemnation of a rich model but a consequence of the ratio between the number of parameters and the number of examples. The same curve of degree ten that ludicrously memorises 14 points turns into a conscientious pupil if we offer it 500 points: however much it might wish to, it can no longer contort itself through 500 random deviations, and so it is forced to learn only the real tendency.

You can check this in the figure itself: raise the number of training points towards 60. You will see the chasm on the right of the violet curve fade away spectacularly, and degree 13 become almost as well-behaved as degree 2!

Here is one of the great secrets behind the success of today’s vast neural networks (such as the models behind the conversational assistants). They possess hundreds of billions of dials — a capacity that ought in theory to lead to blind memorisation — but they are fed with trillions of words gathered from the whole written culture of mankind.

Putting on a rein: regularisation

But what do we do if we cannot gather more data from the real world? We can put a mathematical brake directly on the freedom of the dials! In the first figure we have a lever called “rein” (in technical terms, regularisation). This adds to the cost an extra tax for large values of the parameters: the more violently a curve tries to loop, the more harshly it is punished.

Move the rein lever to the right when the degree is 13: you will see that unruly curve calm down bit by bit, growing tame until it becomes a smooth and elegant shape. Regularisation says to the algorithm, in effect: “You may use all the dials you like, but keep them as close to zero as you can, unless the data insists categorically on a change.”

Alongside regularisation, programmers often also use early stopping: they monitor the error on the test data continuously during gradient descent and stop the training at exactly the moment when the test error begins to rise, before the model has had a chance to memorise by heart.

The secret safe: the validation set

One essential subtlety remains, into which even specialists often fall. If we choose the optimal degree or the value of the rein by keeping half an eye on the test cost, then the test set has taken part indirectly in our decisions! It is no longer truly an independent check from the shadows; it has been contaminated.

The golden rule of research therefore divides the data into three distinct parts: on the the training set the machine adjusts its internal parameters; on a second set, called the validation set, the human tests various settings (how many dials to use, how tight the rein should be); and the the test set remains untouched in a sealed safe until the last day of the project, opened a single time to measure the real performance.

The curves with bends that we have used here have a clear limit: they try to solve everything on a single axis, bending in ever more complicated ways. To make sense of the real world, though — a painting, say, or a melody — we need a far more harmonious architecture: the combination of several different measurements into a living network. In the next lesson we build the fundamental brick of that world: the artificial neuron.