← All tutorials

On this page

1 Approximation error 2 Estimation error 3 Optimization error 4 Irreducible error 5 Bias, variance and learning curves 6 Testing on unseen people 7 Diagnosing errors on a real project 8 References

Understanding Model Errors

Find which kind of error is largest before choosing a fix.

A model's error on new data is the sum of four parts, each with its own cause and fix, and two curves measured from your own runs show which is largest. The running example is a gesture model trained on sensor signals recorded from people.

The total error is the sum of four parts.
E = Eirr + Eapp + Eest + Eopt
The true error E splits into four parts, each with its own cause and its own fix. Eirr is the irreducible error set by noise in the labels or inputs, which no model removes. Eapp is what the best model in your class still misses because the class is too weak; a bigger model lowers it. Eest comes from fitting a finite sample; more data lowers it. Eopt is what training left behind by stopping short of the best weights; a better optimizer or schedule lowers it. Because the parts add, fixing the wrong one cannot move the total.
True error, as four parts fix Approximation model too weak stronger model Estimation not enough data more data Optimization bad training tune optimizer Irreducible error (label noise) no fix removes it

Figure 1. Four parts of the error, each with its fix; find the largest before fixing.

1 Approximation error

The model class is every function the model form can represent. A true function outside it is never found, as a straight line cannot fit a curve (Regression); the fix is a more expressive model, such as a deeper MLP.

2 Estimation error

An expressive class can still see too little data. Overfitting is memorizing the training data, noise and all, so the model fails on anything new; its sign is the generalization gap:

The gap between test and training error.
gap = Etest − Etrain
Training error is measured on data the model fit, test error on data it never saw. A wide gap, low training error with high test error, means the model learned the sample instead of the signal, so more data or regularization helps. A small gap with both errors high points at the model or the training instead.

The fixes are more data, regularization or augmentation.

3 Optimization error

Training can still miss the best weights: it stops in a local minimum or moves very slowly near a saddle point, a flat point that is not a minimum.

Optimization error is the extra error caused by imperfect training.
Eopt = E(θfound) − E(θbest)
θfound are the weights training stopped at, and θbest the best weights the same model class reaches on the same training data. E is the error each set gives, so the difference is the error training gave away. It is zero only if training found the best weights; a lower minimum that training never reached, or a run stopped early, shows up here (Bottou and Bousquet 2008).

The fix is a better optimizer or schedule.

4 Irreducible error

Irreducible error is the part no model, data or optimizer removes, because the noise lives in the data:

The target is a learnable signal plus noise.
y = f(x) + ε
The target y is the true function f of the input x plus noise ε (epsilon) that the input does not determine. A perfect model learns f exactly and still misses by ε on every example, because nothing in x predicts it. That leftover is the Bayes error, the lowest test error any model can reach; only new information in the input lowers it.

A second kind of sensor is one source of such new information.

On the handwritten-digit benchmark MNIST, error that looked irreducible was not (LeCun et al. 1998; Ciresan et al. 2012, trained on distorted digits; Wan et al. 2013; Northcutt et al. 2021).

LeNet-5, 1998 0.95% committee of 35 nets, 2012 0.23% DropConnect, 2013 0.21% 0.15%: 15 wrong test labels 0%1% test error

Figure 2. MNIST test error by paper; the dashed line marks 15 wrong labels out of 10,000.

Later models closed most of the 1998 gap, so most of it was never irreducible.

5 Bias, variance and learning curves

Approximation and estimation error trade off: the bias-variance trade-off. Bias is the error of a model too simple for the data, the same on any sample; variance is the error of a flexible model that fits its sample's noise, so a new sample gives a new model.

0 5 10 15 1 10 100 model capacity (log scale) error (%) bias variance test error irreducible error 1

Figure 3. Toy curves; bias falls, variance rises, and marker 1 is the lowest test error.

To find the largest part on a real model, measure error against model capacity (how much the model can represent) and against training-set size.

train error test error model capacity Underfitting (approximation) both fall as capacity grows → stronger model data size Overfitting (estimation) test falls toward train with data → more data model capacity No progress (optimization) train stays high as capacity grows → tune optimizer data size At the floor (irreducible) both low, close, no longer falling → better input

Figure 4. Each curve shape points to the largest error; the fix follows the arrow.

Figure 5. Toy curves; more data lowers test error and moves its minimum (the dot) right.

Both curves are drawn after training, never inside the loop: each point is one full training run per cross-validation fold, so the capacity curve trains 4 depths on 5 folds, 20 models.

validation_curve trains the decision tree clf on 500 noisy two-moons points at each max_depth and returns accuracy per depth and fold.

import sklearn.model_selection
from sklearn import datasets, tree
moons = datasets.make_moons
X, y = moons(500, noise=0.3)
dt = tree.DecisionTreeClassifier
clf = dt()
depths = [2, 4, 8, 16]
grid = dict(param_range=depths)
grid["param_name"] = "max_depth"
ms = sklearn.model_selection
vc = ms.validation_curve
tr, te = vc(clf, X, y, **grid)
tr_err = 1 - tr.mean(axis=1)
te_err = 1 - te.mean(axis=1)
  1. 1Data and a decision tree.
  2. 2Depths to try, the capacity setting.
  3. 3Five-fold scores at every depth.
  4. 4Accuracy to error, the capacity curve.

For the data curve, learning_curve retrains clf at its default five sizes, 10 to 100 percent of each training fold.

lc = ms.learning_curve
n, _, te_d = lc(clf, X, y)
# test error at each size n
err_d = 1 - te_d.mean(axis=1)
  1. 1Five training sizes.
  2. 2Test accuracy to error, the data curve.

The irreducible-error check passes only when the last capacity step, the last data step and the train-to-test gap all stay under tol, so it tests that the curves stopped falling, not that they are small; a smaller tol demands flatter curves.

tol = 0.005  # half a percent
# each curve's last drop
d_cap = -np.diff(tr_err)[-1]
d_data = -np.diff(err_d)[-1]
gap = te_err[-1] - tr_err[-1]
worst = max(d_cap, d_data, gap)
at_floor = worst < tol
  1. 1Tolerance, half a percent.
  2. 2Last drop of each curve, and the gap.
  3. 3Irreducible only if all three are under it.

6 Testing on unseen people

A wearable model is trained on recordings from a group of people and used by others. A random split puts the same person in training and test, so the test score looks better than it will on a new person. Hold out whole people instead, one per fold (LeaveOneGroupOut); the worst per-person score shows how a new person may fare. The code reads the 500 rows as 10 people of 50.

It costs one full training run per held-out person, once, when choosing or checking a model.

# person id per row: 10 x 50
ids = np.arange(10)
groups = np.repeat(ids, 50)
logo = ms.LeaveOneGroupOut()
cv = logo.split(X, y, groups)
cvs = ms.cross_val_score
acc = cvs(clf, X, y, cv=cv)
print(acc.min(), acc.mean())
  1. 1Person id per row, 10 people of 50.
  2. 2Folds that each hold out one person.
  3. 3One score per held-out person.
TestWhat it measures
Offlinecross-validation across people: accuracy on a person never seen
Online streaminglatency, throughput and false alarms per hour, with data arriving live
Stressaccuracy and recovery under added noise or a shifted sensor placement
Cross-subjecttest on a person never seenday 1day 2Temporaltrain on day 1, test on day 2day 1day 2Domain shifttest in a shifted setupusualshiftedtraintest
Figure 6. Each test holds out a new person, a later day or a shifted setup.

A new person who still fails can get a small trainable add-on that adapts the model without retraining it (Adapters). Per-person normalization needs no labels (z-score per person, preprocessing); the rest is under Cleaning sensor data.

7 Diagnosing errors on a real project

On a real project the curves and the per-person test combine into one learning curve whose folds hold out whole people, with preprocessing refit inside each training fold. It reuses clf, X, y, lc and groups from Section 5 and Section 6.

import sklearn.pipeline as pl
import sklearn.preprocessing as pp
# refit on each training fold
sc = pp.StandardScaler()
pipe = pl.make_pipeline(sc, clf)
gkf = ms.GroupKFold(5)
cv = gkf.split(X, y, groups)
res = lc(pipe, X, y, cv=cv)
n, tr, te = res
tr_err = 1 - tr.mean(axis=1)
te_err = 1 - te.mean(axis=1)
  1. 1Scaler in the pipeline, refit per training fold.
  2. 2Five folds, each person on one side.
  3. 3Learning curve on those folds, as error.

It costs one training run per size and fold, five default sizes (10% to 100%) times five folds, 25 runs (scikit-learn learning_curve documentation), once, never inside training.

Overfitting, training error sinking while test error climbs, is reduced by constraining the model or adding data: Regularization and Data Augmentation. How error falls as data and models grow is on Scaling Laws.

8 References

  1. LeCun, Y., et al. (1998). Gradient-based learning applied to document recognition. Proceedings of the IEEE.
  2. Bottou, L., and Bousquet, O. (2008). The tradeoffs of large scale learning. NeurIPS. (approximation, estimation and optimization error)
  3. Ciresan, D., et al. (2012). Multi-column deep neural networks for image classification. CVPR.
  4. Wan, L., et al. (2013). Regularization of neural networks using DropConnect. ICML.
  5. Northcutt, C. G., et al. (2021). Pervasive label errors in test sets destabilize machine learning benchmarks. NeurIPS Datasets and Benchmarks. arXiv:2103.14749.