Answers · The mathematics underneath
Why does my test set stop being a fair test if I keep tuning on it?
In shortBecause every time you use the test set to choose something, information from it shapes the model, so it stops being untouched and its score becomes optimistic.
It stops being a fair test because choosing anything by looking at it is a way of fitting to it. The set is no longer untouched, and the score you finally report has been selected because it looked good, so it is biased upward.
The arithmetic of selection
Suppose the true error rate is and the held-out set has examples. The measured rate is not exact. Its standard uncertainty is
That is as a proportion, or percentage points. Now train fifty variants that are all truly equal, so each has the same true error rate of 10 percent. Their measured scores scatter around 10 percent with a standard deviation of about 0.95 points. The best of fifty noisy measurements is roughly 2.8 standard deviations below the mean, because picking the minimum of many noisy numbers picks a lucky one. The arithmetic is , and . You report about 7.3 percent, but the truth is 10 percent. The 2.7 point gap is not a training effect. It is a selection effect.
The natural guess is that the test set is safe because no weight was ever updated on it. That is the wrong condition. The condition is that the held-out examples influenced nothing: not the weights, not which features were built, not which model was chosen, not when training stopped. A decision made by comparing scores on the test set lets information from those examples into the model. Each comparison is weak fitting, and a sequence of comparisons is stronger fitting. The test set has become a second validation set, whatever it is called in the code.
| Set | Only job | What reuse does |
|---|---|---|
| Training pile | adjusts weights and other fitted parts | normal fitting |
| Validation pile | compares options and decides when to stop | each comparison uses it up a little |
| Test pile | reports one number at the end | a second look turns it into a validation pile |
Where the honesty ends
One look at the sealed set is defensible. Three looks with the reasons written down is defensible. Thirty looks means the final number should be described as a validation score, not as an estimate of performance. The effect is small for a handful of comparisons and large for the hundreds that accumulate in a real project. It is the ordinary explanation for a long run of benchmark improvements that fails to reproduce elsewhere. The discipline costs nothing and prevents the most common form of unreproducibility, which is not fraud but many small choices each made honestly against the same examples.
There is a second limit that no amount of sealing removes. The estimate has uncertainty, and that uncertainty shrinks only with the square root of the number of held-out examples. At and , the same formula gives , or 3 percentage points. At it is 0.95 points. To halve the uncertainty again you need four times as many examples, so gives , or about 0.47 points. A small held-out set cannot distinguish a 10 percent system from a 9 percent system, no matter how carefully it is sealed.
The fairness also depends on splitting the right unit. If several rows describe the same patient, user or document, a random row split puts parts of one group on both sides. The model then recognises the group rather than learning the task, and the held-out score measures recognition. Splitting by group fixes that. For forecasting, a random split lets the model see later examples while being tested on earlier ones, which is a form of knowing the future; split by date instead. These failures are separate from tuning on the test set, but they break the same promise: the held-out examples must have influenced nothing.
Write down before the project starts how many times the sealed set may be opened. Once is ideal. Three times with recorded reasons is defensible. Thirty times means the final number is a validation score and not an estimate of performance, because that is what it has become.
Worth keeping
- A held-out score is honest only if the held-out examples influenced no decision, including model choice, feature choice and stopping.
- Selecting the best of many noisy scores is a form of fitting, and the optimism grows with the number of comparisons.
- Uncertainty in an error rate falls with the square root of the number of held-out examples, so four times the examples halves it.
Where this goes next
This question is one step of Why Learning Works At All, a course that works through its subject from the beginning. The page that teaches this part of it is Keeping Some Back, and it reads the whole way through whether or not you have an account.
What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.
Read that pageThe whole courseOther questions people ask about this
- Why does test error go down and then back up as I make the model more complicated?A more flexible model first removes its systematic miss, then starts fitting the noise in its particular training sample, so test error falls and then rises.
- Why can a model get 100% on the training set and still be useless?A perfect training score only shows the model can reproduce the examples it was fitted on; it says nothing about new examples, where it may be no better than guessing.
- Why does adding a penalty on the weights stop overfitting?Adding a penalty makes large weights costly, so the fit keeps them only when the training data saves enough error to pay for them, which limits the settings the model can reach.
- What is the difference between variance and standard deviation?They measure the same spread: variance is the average squared distance from the mean, standard deviation is its square root, so you report the standard deviation and calculate with the variance.
- How do you make a vague claim precise enough to tell whether it's true?Fix what is being talked about, what is asserted, which cases are covered and the cut-off, then name an observation that would prove it false.
- How do you find the parabola that best fits a function near a point?Match the function's height, slope and bending at the chosen point; those three conditions single out exactly one parabola.