Training, Validation, and Test Sets: Why You Need All Three
The logic behind data splitting, why the test set must remain untouched, and the consequences of data leakage — the most common and most damaging mistake in medical AI research.
A model is supposed to work on patients it has never seen. The only way to estimate whether it will is to hold some patients back, train without them and then check. That idea is simple enough, and you already apply it whenever you distinguish a derivation cohort from a validation cohort.
Machine learning needs three sets where a clinical prediction study often gets by with two, and it has an unusually large number of ways for information to slip from one set into another. That slippage is called data leakage. It inflates results, it is very common in the published literature, and most of it is preventable with some care at the start.
What each set is for
The training set is what the model learns from. Its parameters are adjusted to fit these cases, so performance on them says very little. A large network can fit its training set almost perfectly whether or not it has learned anything general.
The validation set is used to make decisions during development. Which architecture? How long to train? Which preprocessing? What threshold? The team tries an option, checks the validation score and keeps what works. (A warning about vocabulary: to an engineer, "validation" means this tuning set. To a clinician it means an independent study that confirms a result. The two meanings cause a surprising amount of confusion in meetings, and engineers sometimes say "development set" or "tuning set" to avoid it.)
The test set is used once, at the end, to estimate how the finished model performs on new cases.
Why two sets aren't enough
It's reasonable to ask why the validation set can't double as the test set. The model was never trained on it, after all.
The trouble is that the team has been making choices based on it. Each choice is a small act of fitting. If we try fifty configurations and keep the one with the best validation score, some of that score is real quality and some is luck: that configuration happened to suit those particular cases. The winner's validation score is therefore optimistic, in the same way that the best of fifty subgroup analyses is optimistic.
You know this problem from statistics as multiple comparisons, or from prediction modeling as optimism in apparent performance. The test set is the cure: cases that influenced no decision at all, so that the number they give is an unbiased estimate.
That only holds if the test set really did influence nothing. If we look at the test result, go back and adjust the model, and test again, the test set has become a validation set and we no longer have a clean estimate. 4.7 Comparing Models discusses what repeated use of a test set does to the numbers.
How to split
There is no magic ratio. Something like 70% training, 15% validation and 15% test is common, but the absolute sizes count for more than the proportions. The test set needs enough cases, and enough positives in each subgroup you'll report, to give confidence intervals narrow enough to be useful. 2.2 How Much Data Do You Need? shows the calculation, and suggests sizing the test set first and training on what's left.
A few rules apply whatever the ratio.
Split by patient. All of a patient's images, slices, studies and follow-ups go into the same set. This is the most important rule in the article.
Stratify. Make sure each set gets its share of positive cases and, where possible, of important subgroups. With a rare finding, a purely random split can leave the test set with only a handful of positives.
Split once, early, and save the result. The split should be a fixed list of patient identifiers stored with the project, made before anyone has trained anything. Re-splitting later, after seeing results, amounts to choosing a favorable test set.
Consider holding out a whole site, or a later time period. A random split tests the model on new patients from the same hospitals and the same years. Holding out a site, or the most recent year, tests something closer to what deployment will be like (see 2.6 Multi-Site and Multi-Device Data).
When the dataset is small, a single validation set may be too small to trust, and the team will probably use cross-validation for the development decisions. The test set is still kept apart. 4.6 Cross-Validation Strategies for Medical Data covers the variants, including grouping by patient and by site.
Data leakage
Leakage is any route by which information about the test cases, or about the answer, reaches the model during development. The result is a performance estimate that is too high, sometimes by a little and sometimes absurdly.
It's worth knowing how widespread this is. In 2023 Sayash Kapoor and Arvind Narayanan surveyed the scientific literature for reported leakage in machine-learning-based research and found it had affected 294 papers across 17 fields, medicine among them. The review of COVID-19 imaging models by Michael Roberts and colleagues, described in 2.1, found leakage of several kinds among its reasons for judging none of the models usable.
Here are the forms it usually takes in medical imaging.
The same patient on both sides
If the data is split by image and patients have several images each, the model is tested partly on people it trained on. It can recognize the patient's anatomy, implants or scanner and recall the label.
The slice-level version is the most dramatic. A brain MRI is cut into a hundred or more 2D slices, and neighboring slices are nearly identical. Shuffle slices into training and test sets and almost every test slice has a near-twin in training. Ekin Yagis and colleagues measured this in 2021 by training the same Alzheimer's and Parkinson's classifiers with slice-level and with subject-level splits. The slice-level splits inflated accuracy by 29 to 55 percentage points, depending on the dataset. They also tried it on a dataset whose labels had been assigned at random, where there is nothing to learn: the slice-level split reported 96% accuracy, and the subject-level split reported the 50% you would expect.
Duplicates and overlapping sources
The same study can enter a dataset twice: exported on two occasions, present in two public collections, or included under an old and a new patient ID after a records merge. Public datasets are sometimes built from one another, so combining two of them, or training on one and "externally" testing on another, can mean testing on training images. It's worth checking for duplicate images directly, by comparing image content, before splitting.
Preprocessing done before the split
Any step that learns something from the data has to learn it from the training set only. Normalization statistics are the mild case (see 2.5 Data Preprocessing and Augmentation). Feature selection is the serious one. If we pick the twenty radiomic features most correlated with the outcome using all the patients, and then split and train on those twenty, the test patients helped choose the features. With many candidate features and few patients, this alone can produce an impressive AUC from pure noise. The same applies to oversampling, synthetic data generation and any augmentation that creates stored copies: do them after the split, on the training set alone.
Inputs that contain the answer
Sometimes the model's input includes something that is a consequence of the label. The chest drain in a pneumothorax film is one example (see 1.6 What AI Cannot Do). In tabular data it might be a medication that is only prescribed once the diagnosis is made, or a "time to result" field that is only filled in for abnormal cases. In images it can be burned-in text, or the fact that positive cases went through a different scanner or export path. The question to ask of every input is whether it would be available, in this form, at the moment the model is meant to make its prediction.
Time
If the model predicts the future, the training data must come from before the test data. A model trained on a random mix of years and tested on the same mix has been allowed to learn from the future, which it won't be able to do in use.
Looking at the test set
The last form involves no bug at all. The team checks test performance "just to see," notices a weakness and fixes it. Or runs five final candidates on the test set and reports the best. Or tunes the decision threshold on the test set. Each of these makes the test set part of development.
Keeping the test set locked
A few habits make a clean test set much more likely.
- Decide the split before development starts and store the test cases somewhere the development code doesn't read from. In stricter setups a person outside the development team holds them.
- Write the analysis plan before unlocking: the primary metric, the threshold, the subgroups.
- Freeze the whole pipeline, preprocessing included, and run the test once.
- Keep a log of every time the test set was evaluated and why. If the answer is "once," that's a sentence for the methods section.
- If the test set does get used up, say so, and find fresh data. A new external dataset is the best replacement.
For a model headed to a regulator, the independence of the test data is something reviewers ask about directly. It is the subject of one of the Good Machine Learning Practice principles (see 6.7 Good Machine Learning Practice).
How to spot leakage
You often can't see leakage in the code, but you can see its effects.
Results that are too good are the first sign. If experts agree with each other 85% of the time and the model scores 99%, the likeliest explanation is leakage or a shortcut, and a breakthrough is some way down the list.
A large drop on external data is another. Some drop is normal. A fall from 0.97 to 0.70 suggests the internal number was never real.
It also helps to ask the engineers three plain questions. How was the split done, and at what level? Was anything computed on the full dataset before the split? How many times has the test set been evaluated? A team with good habits will answer all three without hesitation.
When reading someone else's paper, look for the phrase "split at the patient level" or its equivalent. If the paper reports thousands of images, doesn't say how many patients, and doesn't say how the split was done, we'd treat the headline number with some caution (see 9.3 Common Methodological Pitfalls in Medical AI Papers).
Key takeaways
- The training set fits the model, the validation set guides development decisions, and the test set gives the final estimate.
- A validation score is optimistic because choices were made using it. Only data that influenced no decision gives an unbiased number.
- Split by patient, stratify, and fix the split as a saved list before development begins.
- Leakage is common. Its usual forms are patient overlap, duplicates, preprocessing or feature selection before the split, inputs that encode the answer, training on the future, and repeated looks at the test set.
- Freeze the pipeline and the analysis plan, then evaluate the test set once.
- Suspiciously good results and large drops on external data are the visible signs of leakage.
What to read next
- 3.4 Hyperparameter Tuning and Model Selection: the decisions the validation set is used for
- 3.5 Overfitting, Underfitting, and Regularization: what the gap between training and validation performance tells you
- 4.6 Cross-Validation Strategies for Medical Data: when a single validation set is too small
- 5.4 Multi-Site External Validation: testing beyond your own institution
- 9.3 Common Methodological Pitfalls in Medical AI Papers: finding leakage in published work
This article is part of the AI in Clinical Research Knowledge Base.