§ 1.6Module I

Training Error and Generalization Error

1.6 — Training Error and Generalization Error

Recall first. If a model scores perfectly on the examples used to fit it, what can you conclude about its performance on tomorrow’s examples? Write the strongest conclusion you think is justified.

Error is always tied to a loss and a distribution

An error number has meaning only after we specify:

For a dataset D = {(xᵢ, yᵢ)} and a fitted model , let L(y, f̂(x)) be the loss.

Training error

The training error (empirical risk) is the average loss on the examples used to fit the model:

R_train(f̂) = (1/n) Σ L(yᵢ, f̂(xᵢ))

It tells us how well the model fits the observed training sample. A flexible model can make this very small by learning details or noise specific to that sample.

Generalization error

The generalization error is the expected loss on a new example drawn from the target population:

R_gen(f̂) = E_(X,Y)~P [ L(Y, f̂(X)) ]

The distribution P represents the conditions in which the model will actually be used. We usually cannot calculate this expectation exactly, so a properly held-out test set is used as an estimate.12

Common losses

A lower error is better for a loss. Accuracy is a score where higher is better, so do not compare “error” and “accuracy” without checking the direction and definition.

The generalization gap

generalization gap ≈ held-out error − training error

A small gap is not sufficient by itself: both errors could be high because the model underfits, the features are weak, or the data is noisy. A large gap suggests that the model fits the training sample better than it transfers to unseen examples, often because of overfitting or a flawed split.

Worked calculation

Suppose two models predict the same regression target. The residuals are:

ModelTraining residualsTest residuals
A1, −2, 1, −22, −3
B0, 1, 0, −14, −5

Using MSE:

Model B fits the training sample better, but Model A has the lower estimated generalization error. The goal is not the smallest training error in isolation; it is reliable performance on the intended future data.

Training, validation, and test error

If the test set is used repeatedly to choose models, it becomes part of the development feedback. Its reported error then becomes optimistic and no longer represents an untouched final estimate.2

What can make the estimate misleading?

A held-out score can still be misleading when:

Generalization is always relative to a population and a task. A model can generalize from one hospital to similar patients but fail after a sensor or policy change.

Exercise

Three models have these errors:

ModelTraining errorValidation error
A0.020.03
B0.010.40
C0.300.32

Which model is the most suspicious for overfitting? Which is the most suspicious for underfitting? Is A automatically the best model?

Answer

B is suspicious for overfitting: very low training error but much higher validation error. C is suspicious for underfitting: both errors are high and close. A is the best candidate among these numbers, but not automatically: check the split, metric, uncertainty, leakage, and performance on the intended population.

Exam lens

Definition pair: training error measures fit on observed training examples; generalization error is expected loss on new examples from the target distribution.

Diagram to reproduce: show training error generally decreasing as capacity increases, while validation/test error can decrease and then rise. Label the gap and the minimum validation region.

Common traps:

Rapid revision

Sources

Footnotes

  1. Mitchell, T. M. (1997). Machine Learning. McGraw-Hill — training performance, generalization, and learning problem formulation.

  2. scikit-learn, “Cross-validation: evaluating estimator performance.” https://scikit-learn.org/stable/modules/cross_validation.html — held-out evaluation and model-selection workflow. 2