TL;DR
A model learns from the examples it sees, so the only honest test of it is on examples it has never seen. We therefore split the data into three parts. The training set fits the model's parameters. The validation set is used to make choices such as model size or learning rate. The test set is locked away and used once, at the end, to report how well the model generalises.
Where you'll see it: every supervised machine learning project, from a spam filter trained on email to a language model tuned on held-out text, and in the held-out benchmarks used to compare models.
Intuition
Practising for an exam you have not seen
Think of a student preparing for an exam. Practice problems are the training set: the student works through them, learns from the mistakes, and gets better at them. Mock exams are the validation set: the student takes them to find out which study method works, and changes the plan based on the scores. The real exam is the test set. Its questions are new, and its score is the one that counts.
The trap is obvious once you say it out loud. If the student practised on the real exam questions, a perfect score would tell you nothing about whether they understood the subject. A model is in the same position. Score it on the data it was trained on and it will look excellent, because it has partly memorised that data. That gap between how well a model does on data it has seen and on data it has not is what Overfitting is.
Why three parts, not two
Two sets seem enough: train on one, test on the other. But in practice you make many decisions. You pick the number of layers, the strength of L2 Regularization, the learning rate, when to stop with Early Stopping. Each time you look at a score and pick the best option, you are fitting to that score a little. If the same held-out set drives every decision and also reports the final result, the reported number is too optimistic. The validation set absorbs that selection. The test set stays untouched, so its score is still an honest estimate.
A picture of the workflow
flowchart LR
A[All labelled data] --> B[Split once, at random]
B --> C[Training set]
B --> D[Validation set]
B --> E[Test set, locked away]
C --> F[Fit parameters]
F --> G[Score on validation]
G --> H{Change settings?}
H -- yes --> F
H -- no --> I[Score once on test]
The math
Setting up the split
Let be a dataset of labelled examples. We choose a partition
Here is the input (for example, the pixels of an image), is the label we want to predict, and is the total count. The three sets are disjoint: no example appears in two of them.
For a model with parameters and a loss (see Loss Function), the three quantities we care about are
and similarly and . The training loss is minimised by the fitting procedure. The validation loss guides the choice of settings. The test loss is reported.
Why the held-out loss is an honest estimate
Suppose the test examples are drawn independently from the same distribution as everything the model will meet in use. Then, for any fixed model, the average loss on the test set is an unbiased estimate of the true expected loss . Unbiased means that, averaged over random test sets, it hits the true value. Independence matters here, which is why Statistical Independence appears as a helpful prerequisite. If test examples are copies of training examples, or near-copies, they are not independent draws and the estimate is biased downwards.
The price of a single held-out set is noise. The standard error of an average of independent losses with variance is
So a test set of 100 examples gives a score that can easily move by a few percentage points just from which examples happened to land in it. This is the Mean and Variance fact that drives the sizing advice below.
Choosing the sizes
There is no universal ratio. The rough rule is that the training set should be large enough for the model to learn, and the validation and test sets large enough that is small compared with the differences you are trying to detect. If two models differ by 0.5% in accuracy, a test set of 200 examples (with for accuracy near 90%) cannot tell them apart. Larger held-out sets are the only fix.
Worked examples
A dataset has labelled photos. We choose a 70/15/15 split.
- Training: photos.
- Validation: photos.
- Test: photos.
- Check the total: . Every photo is in exactly one set.
Interpretation: we fit on 7,000, compare settings on 1,500, and report on 1,500 that were never used for any decision.
A classifier scores 90% accuracy on a test set of 100 examples. Another classifier scores 92% on the same test set.
- Each example is either right or wrong, so accuracy is a proportion with variance .
- For the first model, and : .
- The standard error is 3 percentage points, larger than the 2-point gap between the models.
- Conclusion: this test set cannot tell the two models apart. Gather more test examples before choosing.
We train polynomial regressions of degree 1, 2, 3 and 4 on the training set. Their validation errors are 0.82, 0.41, 0.39 and 0.47.
- The lowest validation error is 0.39, so degree 3 is chosen.
- Retrain or keep the degree-3 model, and compute its error once on the test set. Suppose it is 0.44.
- Report 0.44 as the expected error on new data. Do not report 0.39: that number was used to pick the model, so it is optimistic.
Tradeoffs and failure modes
| Situation | Problem | Better choice |
|---|---|---|
| Small dataset (a few hundred examples) | A held-out set is too noisy and removes too much training data | k-fold cross-validation (rotate the held-out fold through the data) |
| Time series or sequential data | A random split lets the model see the future | Split by time: train on the past, validate and test on later periods |
| Repeated peeking at the test score | The reported number is optimistic | Keep test locked; use validation for every decision |
| Same patient, user or document in several rows | Near-duplicates leak across the split | Split by group, so each entity lives in exactly one set |
| Rare classes | A random split may leave a class almost absent from validation | Stratified split, which keeps class proportions in each set |
Common failure modes:
- Data leakage. Any preprocessing that uses statistics from the whole dataset, such as a mean for normalisation or a vocabulary built from all text, leaks information from the validation and test sets into training. Fit preprocessing on the training set only and apply it to the rest.
- Overlap between sets. Duplicate rows scattered across the split make validation look better than it is.
- A validation set that is too small makes model selection a coin toss, and a test set that is too small makes the report meaningless.
History
The idea that a model should be judged on data it was not fitted to is old. The formal reuse of data for validation was developed in the 1970s, when Mervyn Stone and Seymour Geisser independently worked out cross-validation, a way to estimate predictive performance by rotating which part of the data is held out. That made it possible to get honest estimates from small datasets without setting aside a large fixed block.
The three-way split, with a separate validation set for tuning and a test set for the final report, became standard practice in machine learning as hyperparameter tuning and shared benchmarks became routine. The vocabulary took a while to settle, and textbooks such as Hastie, Tibshirani and Friedman's The Elements of Statistical Learning helped establish it.
Check yourself
QuestionYou have 2,000 labelled examples and split them 60/20/20 into training, validation and test sets. How many examples are in each set?Show answer
Training: . Validation: . Test: . Check: .
QuestionWhy can't we use the test set to choose the learning rate, even if we only try two values?Show answer
The test set is meant to be an unbiased estimate of performance on new data. Choosing between two learning rates by their test scores means the chosen score is the better of two noisy estimates, so it is systematically too optimistic. Use the validation set for that choice and keep the test set for one final report.
QuestionA model has validation accuracy 0.85 with a standard error of about 0.02. A second model has validation accuracy 0.86. Can you confidently say the second is better?Show answer
Not yet. The gap is 0.01, which is smaller than the standard error of 0.02 for each estimate. The difference is within the noise. Collect more validation data, or compare the models with a paired test on the same examples, before deciding.
QuestionYou are building a model that predicts next-week sales from the past two years of daily data. Should you split the data at random?Show answer
No. A random split would put later days into training and earlier days into validation or test, so the model learns from the future. Split by time: train on the earlier period, validate on a later one, and test on the most recent one. This matches how the model will be used.