Lesson 8.2 · 7 min read · about 55 min to master at a very slow pace
Training, Validation and Test Sets
What each data split is for, typical split sizes, the forms of data leakage that quietly ruin an evaluation, and k-fold cross-validation when data is scarce.
Three disjoint sets
| Set | Used for | Never |
|---|---|---|
| Training | Fitting the weights by gradient descent | Judging the final model |
| Validation | Every other choice: architecture, learning rate, regularisation, number of epochs | Computing training gradients |
| Test | One final estimate of generalisation, at the very end | Tuning anything: look at it once |
Typical splits are 80/10/10 or 60/20/20. Shuffle before splitting so all three sets have the same distribution.
Data leakage
Leakage is any route by which information from the evaluation data gets into training, making scores look better than they will be in use:
- The same object in two sets: several images of one patient, or consecutive video frames, split across train and test. Split by patient or by clip instead.
- Time: for time series, train on the past and test on the future, never a random split.
- Preprocessing statistics (means, standard deviations, min/max) computed on all the data. Compute them on the training set only.
- Tuning on the test set, even informally by trying a few models and keeping the best test score.
Scarce data: k-fold cross-validation
Split the training data into folds. Train times, each time validating on a different fold, and average the validation scores. Every example is used for validation exactly once.
Stuck? Common doubts
Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.
First passWhy do I need both a validation set and a test set?
Because every decision you make using the validation score slightly fits the model to the validation set. After choosing among many options, the best validation score is optimistic. The test set, untouched until the end, gives an honest estimate. Reusing it for decisions turns it into another validation set.
Going deeperIf my training set already contains every possible input, do I still need a test set?
No. In the XOR lab all four possible inputs are in the training set, so there is nothing new to generalise to and a test set adds nothing. A test set matters whenever the model must work on inputs it hasn't seen, which is almost always.
First passTo judge generalisation, do I compare training with validation or with test?
With validation while you're still making choices (model size, , when to stop), and with test only once at the end, to report the final number. If you look at the test score while choosing, the choice adapts to the test set and the reported number becomes too optimistic.
Thumb rule: "Validation is for choosing, test is for reporting."
First passWhat information "wouldn't be available at prediction time"? Examples of leakage?
Ask of every feature: "At the moment I make the prediction, would I really have this value?" If not, it leaks the answer.
| Task | Leaky feature | Why it leaks |
|---|---|---|
| Does the patient have pneumonia? | "Took antibiotics" | Given after the diagnosis |
| Will this customer cancel? | "Cancellation reason" | Filled in only once they've cancelled |
| Will this loan default? | "Sent to debt collector" | Happens only after a default |
| Tomorrow's price | Any data from tomorrow onwards | That's the future |
The second kind is test data leaking into training: normalising with statistics from the whole dataset (compute them on the training set only), the same patient or photo in two sets (split by person), or a random split of a time series (train on the past, test on the future).
Thumb rule: "If it comes from the future or from the test set, it's leakage." The symptom is a score that looks too good, then falls apart in real use.
Cheatsheet
Three sets
Leakage
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.