Generalization error

Also known as: out-of-sample error · test error

On this page 7

Generalization error is the expected loss of a trained model on new data drawn from the same distribution as its training data, as opposed to the loss it achieves on the examples it was trained on. In model evaluation within artificial intelligence and machine learning, it is the quantity that actually matters for deployment, and it is also called out-of-sample error or, when estimated on a held-out set, test error.

Because the true data distribution is unknown, generalization error cannot be computed directly. It is estimated, and the quality of that estimate depends on how the evaluation data were collected, how often they were reused and whether they truly resemble the data the model will meet in production.

Formal definition and the generalization gap

Formally, generalization error is the expected value of the loss function over the true joint distribution of inputs and labels. Training error, also called empirical risk, is the average loss over the finite training sample. The difference between the two is the generalization gap, which measures how much the model has fitted peculiarities of its training data rather than the underlying pattern.

The table below distinguishes the related error quantities that practitioners report.

QuantityComputed onWhat it tells you
Training errorTraining setHow well the model fits seen data
Validation errorValidation setGuidance for tuning and model selection
Test errorHeld-out test setUnbiased estimate of generalization error
Generalization errorFull data distributionTrue expected performance, never observed directly
Generalization gapDifference of test and training errorDegree of overfitting
Error quantities used in model evaluation

A small gap with high error points to underfitting, while a large gap with low training error points to overfitting. Both cases mean the model will disappoint in use, but they require opposite remedies.

Bias, variance and irreducible noise

For squared-error loss, generalization error decomposes into bias squared, variance and irreducible noise. This bias-variance decomposition explains why simply adding model capacity does not always help.

ComponentSourceTypical remedy
BiasModel too simple to capture the true patternMore features, more capacity, less regularization
VarianceModel too sensitive to the specific sampleMore data, regularization, ensembling
Irreducible noiseRandomness or missing information in the labelsCannot be removed by modeling
Components of generalization error under squared loss

Classical theory predicts a U-shaped test error curve as capacity grows. Modern overparameterized networks often show double descent instead: test error rises near the point where the model can just interpolate the training data, then falls again as capacity keeps increasing. This behavior shows that the classical trade-off is a useful heuristic rather than a universal law.

How generalization error is estimated

Every estimate of generalization error relies on data the model did not train on. The main methods trade statistical efficiency against computational cost and the risk of optimistic bias.

MethodProcedureStrengthWeakness
Holdout test setReserve a fixed share, often 10-20%Simple and fastHigh variance on small datasets
k-fold cross-validationRotate k folds as test data, usually k = 5 or 10Uses all data for both rolesk times the training cost
Leave-one-outTest on each single example in turnNearly unbiasedVery expensive, high variance
Nested cross-validationInner loop tunes, outer loop evaluatesAvoids selection biasHeavy computation
BootstrapResample with replacement, test on left-out pointsGives confidence intervalsCan be biased without correction
Common estimation methods

Whatever method is used, the estimate is only trustworthy if the test set is touched once, after all design decisions are final. Repeatedly checking test results and adjusting the model turns the test set into a second validation set and produces an optimistically biased estimate.

Theoretical bounds on generalization

Statistical learning theory provides bounds that relate generalization error to training error, sample size and model complexity. Classical bounds based on VC dimension or Rademacher complexity state that the gap shrinks roughly with the square root of complexity divided by the number of samples.

For deep networks with millions or billions of parameters, these bounds are usually vacuous, meaning they predict errors above 100 percent while the real models generalize well. Research on PAC-Bayes bounds, margin-based bounds and implicit regularization by stochastic gradient descent tries to close this gap, but no single theory fully explains why large networks generalize.

Practical pitfalls in measuring generalization

Most failures to predict real-world performance come from violated assumptions about the data, not from arithmetic mistakes.

  • Data leakage: duplicates or near-duplicates shared between training and test sets inflate test scores.
  • Benchmark contamination: in large language models, test questions that appeared in pretraining data make reported scores unreliable.
  • Distribution shift: production data that differ from the test set, for example in time, geography or user behavior, raise the real error.
  • Group leakage: splitting records from the same patient, customer or document across sets hides overfitting to individuals.
  • Small test sets: a test set of a few hundred examples can carry a confidence interval of several percentage points.

A sound evaluation protocol therefore splits data by the unit of independence, such as patient, user or time period, keeps a frozen final test set that no one inspects during development, and adds an external test set collected under deployment conditions. Reporting the estimate with a confidence interval, rather than as a single number, tells decision makers how much the measured error could plausibly differ from the true generalization error. For systems that change over time, periodic re-evaluation on fresh production samples is the only way to confirm that the estimate still holds.

In everyday language

A student who memorizes last year’s exam answers may score perfectly on that exam. A new exam shows whether the student really understands the subject. Generalization error is the score on the new exam.

Machine learning models face the same test. Doing well on familiar examples is easy. Doing well on examples the model has never seen is what counts, and generalization error measures how often the model gets those wrong.

Seen in the real world

A hospital data science team builds a model that flags chest X-rays for urgent review. On a random test split, it reaches an error rate of 6 percent. Before deployment, the team checks for patient overlap and finds that many patients have several images split across training and test data.

After re-splitting by patient, the test error rises to 11 percent, a more honest estimate. The team then evaluates the model on images from a second hospital that uses different scanners, where the error reaches 15 percent, revealing a distribution shift.

The team adds training images from varied scanners and uses patient-level cross-validation for model selection. The final model shows a test error of 9 percent on the external hospital data, and the team reports that figure, not the original 6 percent, to clinical reviewers.