Search

Search papers and lessons

Train, Validation and Test Sets

Level 2 · 9 min read

Split your data into a training set to fit the model, a validation set to choose it, and a test set to report how it really performs.

On this page

  • Colour = topic mix
  • Probability
  • Learning basics
  • Sign in to see your progress

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 D={(xi,yi)}i=1ND = \{(\mathbf{x}_i, y_i)\}_{i=1}^{N} be a dataset of NN labelled examples. We choose a partition

D=Dtrain∪Dval∪Dtest,Dtrain∩Dval=∅,Dtest∩(Dtrain∪Dval)=∅.D = D_{\text{train}} \cup D_{\text{val}} \cup D_{\text{test}}, \qquad D_{\text{train}} \cap D_{\text{val}} = \varnothing, \quad D_{\text{test}} \cap (D_{\text{train}} \cup D_{\text{val}}) = \varnothing.

Here xi\mathbf{x}_i is the input (for example, the pixels of an image), yiy_i is the label we want to predict, and NN is the total count. The three sets are disjoint: no example appears in two of them.

For a model fθf_\theta with parameters θ\theta and a loss ℓ\ell (see Loss Function), the three quantities we care about are

R^train=1∣Dtrain∣∑(x,y)∈Dtrainℓ(fθ(x),y),\hat{R}_{\text{train}} = \frac{1}{|D_{\text{train}}|} \sum_{(\mathbf{x},y) \in D_{\text{train}}} \ell(f_\theta(\mathbf{x}), y),

and similarly R^val\hat{R}_{\text{val}} and R^test\hat{R}_{\text{test}}. 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 R(θ)=E(x,y)∼P[ℓ(fθ(x),y)]R(\theta) = \mathbb{E}_{(\mathbf{x},y)\sim P}[\ell(f_\theta(\mathbf{x}), y)]. 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 mm independent losses with variance σℓ2\sigma_\ell^2 is

SE=σℓm.\mathrm{SE} = \frac{\sigma_\ell}{\sqrt{m}}.

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 SE\mathrm{SE} 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 SE≈0.9×0.1/200≈2.1%\mathrm{SE} \approx \sqrt{0.9 \times 0.1 / 200} \approx 2.1\% for accuracy near 90%) cannot tell them apart. Larger held-out sets are the only fix.

Worked examples

Example
Splitting 10,000 labelled photos

A dataset has N=10,000N = 10{,}000 labelled photos. We choose a 70/15/15 split.

  1. Training: 0.70×10,000=7,0000.70 \times 10{,}000 = 7{,}000 photos.
  2. Validation: 0.15×10,000=1,5000.15 \times 10{,}000 = 1{,}500 photos.
  3. Test: 0.15×10,000=1,5000.15 \times 10{,}000 = 1{,}500 photos.
  4. Check the total: 7,000+1,500+1,500=10,0007{,}000 + 1{,}500 + 1{,}500 = 10{,}000. 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.

Example
Why a small test set misleads

A classifier scores 90% accuracy on a test set of 100 examples. Another classifier scores 92% on the same test set.

  1. Each example is either right or wrong, so accuracy is a proportion pp with variance p(1−p)/mp(1-p)/m.
  2. For the first model, p=0.90p = 0.90 and m=100m = 100: SE=0.9×0.1/100=0.0009=0.03\mathrm{SE} = \sqrt{0.9 \times 0.1 / 100} = \sqrt{0.0009} = 0.03.
  3. The standard error is 3 percentage points, larger than the 2-point gap between the models.
  4. Conclusion: this test set cannot tell the two models apart. Gather more test examples before choosing.
Example
Validation picks the model, test reports it

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.

  1. The lowest validation error is 0.39, so degree 3 is chosen.
  2. Retrain or keep the degree-3 model, and compute its error once on the test set. Suppose it is 0.44.
  3. 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

SituationProblemBetter choice
Small dataset (a few hundred examples)A held-out set is too noisy and removes too much training datak-fold cross-validation (rotate the held-out fold through the data)
Time series or sequential dataA random split lets the model see the futureSplit by time: train on the past, validate and test on later periods
Repeated peeking at the test scoreThe reported number is optimisticKeep test locked; use validation for every decision
Same patient, user or document in several rowsNear-duplicates leak across the splitSplit by group, so each entity lives in exactly one set
Rare classesA random split may leave a class almost absent from validationStratified 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: 0.60×2000=12000.60 \times 2000 = 1200. Validation: 0.20×2000=4000.20 \times 2000 = 400. Test: 400400. Check: 1200+400+400=20001200 + 400 + 400 = 2000.

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.

Sources