All posts

Train, validation and test sets

/AI/4 min read

Three slices of data, three jobs: learn, choose, and report. Skip the middle one and tune on the test set, and the best of 20 equally good models scores 91.71% on a 1,000-example test that all of them would really score 90% on — an improvement that does not exist.

A model is only useful if it works on data it has not seen. Every evaluation practice in machine learning is built around measuring that honestly, and the most basic one is splitting the data into three parts before anything else happens.

settypical shareused to
training70–80%fit the model's weights
validation10–15%choose between models, settings and when to stop
test10–15%report the final result — once

The reason there are three, not two, is the interesting part.

Training: where the model learns

The training set is what gradient descent sees. The model's weights are adjusted to reduce error on exactly these examples, so performance on them is always optimistic. A large enough model can memorise its training set completely and still be useless on anything else. Training accuracy tells you the model can fit the data, nothing more.

Validation: where you make decisions

Building a model involves many choices the training process does not make for you: the architecture, the learning rate, how much regularisation, which features to include, when to stop training. Each is decided by trying options and comparing them on data the model did not train on — the validation set.

Validation data is therefore used, indirectly. Every choice made by looking at validation scores fits the final model a little to those particular examples.

Test: where you find out

The test set answers one question: how well will the finished model do on new data? To answer it honestly, it must play no part in any decision. Ideally it is looked at once, at the end.

What goes wrong without the middle set

Suppose you skip validation and choose between models by their test score. It feels harmless — you are only picking the best one.

Simulate it. Twenty model configurations, all with the same true accuracy of 90%. Each is scored on the same 1,000-example test set; random variation means each gets a slightly different score. Pick the best:

configurations compared on the test setaverage score of the "best" one
190.00%
591.11%
2091.71%
10092.39%

None of them is better than 90%. But pick the winner from 20 and you will report 91.71% — an improvement that exists only because you chose the configuration that happened to get lucky on those particular 1,000 examples. Try 100 and you will report 92.39%.

This is overfitting to the test set, and it happens with no change to any model weights at all. It is why test sets must be reserved, and why public benchmarks that the whole field tunes against gradually stop measuring what they claim to.

LEARN · CHOOSE · REPORT train 70%val 15%test 15% 90%92.5% true accuracy 5 → 91.11%20 → 91.71%100 → 92.39% configurations compared on the test set → every model is 90%; choosing on the test set reports up to 92.39%
Selection on a noisy score is optimistic by construction. The validation set absorbs that optimism so the test set does not.

How big each set needs to be

The test set's size decides how precisely you know the result. For a model around 90% accurate:

test examplesstandard error95% interval
1003.00%±5.88%
1,0000.95%±1.86%
10,0000.30%±0.59%

With 100 test examples, "91%" means anywhere from about 85% to 97%. Differences between models smaller than the interval are not evidence of anything. When data is plentiful, test and validation sets can be a small percentage of the total and still contain tens of thousands of examples; when it is scarce, cross-validation — rotating which slice is held out and averaging — gets more out of it.

Splitting correctly

The split must mimic how the model will meet new data. Random splitting is right only when examples are independent. Common traps:

  • Time. Forecasting models must be validated on data later than their training data. A random split lets the model peek at the future.
  • Groups. If one patient contributes many scans, or one user many messages, all of that person's data must fall in one set. Otherwise the model can recognise the person rather than learn the task.
  • Duplicates. Near-identical examples on both sides of the split — the same image resized, the same document reposted — inflate test scores.
  • Preprocessing. Statistics used to normalise data, fill missing values or select features must be computed on the training set only, then applied to the others.

Each of these is a form of leakage: information about the test examples reaching the model or the modeller before the final evaluation.

What to take away

Training data fits the weights, validation data makes the choices, test data reports the result. Merging the last two looks harmless but is not: choosing the best of 20 equally good models on a 1,000-example test set reports 91.71% for models that are all really 90%.

Size the test set for the precision you need — ±1.86% at 1,000 examples — split in a way that matches how the model will be used, and keep the test set out of every decision until the end.