Training Error and Generalization Error: The Gap That Matters
Why training scores flatter a model, how held-out data estimates real error, and how much uncertainty that estimate carries.
Key takeaways
- Training error is an optimistic estimate because the model was chosen precisely to make that number small.
- A held-out set that played no part in any choice gives an honest estimate of error on similar new data.
- Held-out estimates are noisy: with 100 test examples, an accuracy near 90 percent is uncertain by several points.
- A large gap points toward too much flexibility or too little data, while high error on both sides points toward an inadequate model or inputs.
- No held-out split from old data protects you if the world that produces new cases has changed.
Training error is how often, or how badly, a model misses on the examples used to fit it. Generalization error is its expected error on new cases from the same source. You can compute the first exactly and care mostly about the second, which you can only estimate.
The difference between them is the generalization gap. It tells you how much of the model's apparent skill came from the particular examples it happened to see.
Two averages that look alike
Both quantities are averages of a loss. They differ in what gets averaged.
training error: R̂(h) = (1/n) · ∑ L(yᵢ, h(xᵢ)) over the n training pairs
generalization error: R(h) = E[ L(y, h(x)) ] over a fresh random pair
gap: R(h) − R̂(h) Here h is the fitted model and E means expected value over the process that generates data. If that process is a fixed distribution and examples are drawn independently from it, the usual theory applies. Those are assumptions, and they carry real weight.
Why the training score flatters
Imagine a model that stores every training pair in a lookup table. Asked about a stored input, it returns the stored label. Asked about anything else, it guesses "no". Assuming no two identical inputs carry different labels, its training error is zero. On continuous inputs, where a new case almost never repeats an old one exactly, it is no better than always saying "no".
Real models are subtler versions of the same problem. Training searches many candidate functions and returns the one that scores best on this sample. Some of that score reflects real structure and some reflects luck: noise that happened to line up with a feature. The wider the search, the more room for luck.
A single function chosen before looking at the data does not have this bias. Its average loss on a random sample is as likely to be unlucky as lucky. The optimism comes from selecting.
Holding data back
The standard remedy is to set examples aside. You fit on the training set, compare options on the validation set, and report on a test set used only at the end. Each time a score influences a decision, that data becomes part of the fitting process in a weaker way.
If you try two hundred configurations and keep the best validation score, that winning score is itself optimistic. The test set exists to give one number free of that selection. When data is scarce, k-fold cross-validation reuses examples by rotating which portion is held out.
Any preprocessing that learns from data belongs inside the training portion. Scaling factors, imputed values, selected features and resampling must be fitted without the held-out rows, or the estimate quietly turns back into a training score.
How precise is the held-out number?
A test set is a sample, so its accuracy varies from sample to sample. For an accuracy p measured on n independent examples, the standard error is about √(p(1 − p)/n). A rough 95 percent interval is p plus or minus 1.96 standard errors.
For a measured accuracy of 0.90:
| Test examples | Standard error | Rough 95% interval |
|---|---|---|
| 100 | 0.030 | 0.90 ± 0.059 |
| 1,000 | 0.0095 | 0.90 ± 0.019 |
| 10,000 | 0.0030 | 0.90 ± 0.0059 |
One hundred times more test data narrows the interval by a factor of ten, since the error shrinks with the square root of n. This simple formula is unreliable when n is small or p is close to 0 or 1, where better interval methods exist. It also assumes independence, so a thousand rows from ten people carry far less information than a thousand people. See confidence intervals for test metrics.
A worked case of selection optimism
Suppose you have a test of 20 yes-or-no questions and 1,000 "models" that answer each by flipping a fair coin. Every one of them has a true accuracy of exactly 50 percent.
The chance that one coin-flipping model gets at least 16 of 20 right is small: there are 6,196 such outcomes out of 2²⁰ = 1,048,576, about 0.6 percent. Across 1,000 independent models, you expect about 6 to reach that mark. The chance that at least one does is 1 − (1 − 0.0059)¹⁰⁰⁰, which is above 99 percent.
Pick the best of the thousand and you will probably report 80 percent accuracy or better for a method with no skill. Give the winner 20 fresh questions and you should expect about 10 correct.
Real model searches are less extreme, because the candidates are correlated and have some real skill, but the mechanism is the same. The more options you compare on a small set, the less the winning score means. This is the subject of multiple comparisons among models.
What theory says about the gap
For a finite set of candidate models and a loss between 0 and 1, a standard argument combining Hoeffding's inequality with a union bound gives this: with probability at least 1 − δ, every candidate h in the set H satisfies
|R(h) − R̂(h)| ≤ √( ln(2|H|/δ) / (2n) ) Take |H| = 1,000, δ = 0.05 and n = 1,000. Then ln(40,000) ≈ 10.60, dividing by 2,000 gives about 0.0053, and the square root is about 0.073. With n = 10,000 the bound falls to about 0.023.
Two lessons stand out. The gap bound shrinks as data grows. It grows with the number of candidates, but only through a logarithm. The bound is a worst case over all candidates and is often much looser than what you observe. For large neural networks, simple counting bounds are usually too weak to be informative at all, and explaining why those models generalize remains an active research question.
How much to hold out
No fixed percentage is right. What matters is the number of examples in the held-out set and the size of the difference you need to detect.
- With a few hundred examples in total, a single split gives a very noisy score and wastes training data. Cross-validation is usually the better choice.
- With tens of thousands, a split such as 80 percent training, 10 percent validation and 10 percent test is a common starting point.
- With millions, even 1 percent held out may be plenty for overall metrics, though rare classes and small groups need their own count.
Work backward from the precision you want. Using the standard error formula at an accuracy of 0.90, getting a 95 percent half-width of about 0.01 requires 1.96² × 0.09 / 0.01², or roughly 3,460 independent examples.
When you compare two models on the same test set, look at the examples where they disagree. Their errors are paired, and a paired comparison can detect a difference that two separate intervals would seem to hide.
Keep the split stable across experiments, for instance by assigning rows with a hash of a stable identifier. Otherwise a change in score may come from a change in which rows landed in the test set, and yesterday's training rows may become today's test rows.
Reading the gap
Compare training and validation error side by side, ideally against a reference point such as human performance or a simple baseline.
| Training error | Validation error | Likely situation | Things to try |
|---|---|---|---|
| High | High, similar | Underfitting or weak inputs | Richer model, better features, longer training |
| Low | Much higher | Overfitting | More data, regularization, simpler model |
| Low | Low | Working so far | Confirm on the test set, check subgroups |
| Higher than validation | Lower | Something to explain | Check split, regularization effects, easy validation data |
The last row is not always a bug. Dropout and augmentation make training harder than evaluation, which can push training loss above validation loss. A leak or an unrepresentative split can do the same, so look. Plotting both errors against dataset size or training time, as in reading a learning curve, shows which direction to move.
The gap that a split cannot see
Everything above estimates error on data like the data you have. If you deploy next year, in another region, or on a different device, the relevant error is on a different distribution. A random split will not warn you.
You can make evaluation resemble deployment. Split by time if you will predict the future. Split by group if you will meet new people, sites or machines. Keep a small sample from the target setting if you can get one. Then keep measuring after launch, because the estimate expires as conditions move.
Also look below the average. A model with good overall error can be poor for a small segment that contributes little to the mean. Report error for the groups that matter, with the number of examples in each, since small groups have wide intervals.
Common questions
Is a zero training error always bad?
No. Some modern models fit their training data exactly and still predict well. Zero training error simply stops telling you anything about new data, so you must rely on held-out measurements.
Can I reuse the test set after improving the model?
Each reuse that influences a decision erodes its independence. Occasional checks do limited harm on a large set, but repeated tuning against it will inflate your numbers. Keep a record of how often it was used, and collect fresh data when you can.