Skip to content

Overfitting, Bias–Variance & Regularization

A low training loss is not the goal: make the model better on data it has never seen

Quick recap
  • Training loss measures the examples the model learned from. Validation loss measures new ones.
  • Underfitting is too simple; overfitting is too flexible and memorizes noise.
  • Regularization, early stopping, and more data can improve generalization.

This lesson follows Backpropagation and uses the Decision Boundary Playground for a live train/validation comparison.

1. Two scores

A model that gets every training example right may still fail on the next example. The training score measures the examples used to fit the parameters. The validation score measures examples held out from fitting.

The generalization gap

A small gap is reassuring; a large gap is a warning. The gap is not proof of a cause by itself. It can come from overfitting, label leakage, an unrepresentative validation set, or a change in the world after training.

2. Three fits

Six training points and five validation points. A polynomial of degree 1, 2, or 5 is fitted to the training points only. The validation points are used to judge the result.

Degree 1 is too stiff, degree 2 follows the underlying curve, and degree 5 can pass through every training point. The validation score disagrees with the training score at degree 5.

3. Bias and variance

Underfitting is high bias: the model class is too simple to represent the pattern. Both training and validation errors are high.

Overfitting is high variance: the model is sensitive to the particular training sample. The training error is low, but a small change in the data can change the fit a lot, and validation error rises.

The bias–variance view

The decomposition describes expected error over repeated samples. You do not observe it directly, but it explains why more capacity is not always better and why more data helps variance.

4. Regularization

A regularizer adds a penalty for large weights. The model still fits the data, but it has to pay for complexity:

L2 regularization

λ controls the trade-off. At λ = 0 the penalty disappears; as λ grows, the weights are pushed toward zero. This is also called weight decay.

Example: data loss 1, weights (2, 1). The penalty is λ(4 + 1):

Small penalty
Large penalty

Regularization is not a guarantee. It can reduce variance but increase bias if λ is too large. The validation set chooses λ, not the training set.

Early stopping is another practical control: monitor validation loss and stop when it starts to rise. More data and data augmentation can help too, when they add realistic variety rather than noise.

5. Worked example

Worked example: compare three polynomial fits

Each row is fitted on the same six training points. The validation points are never used to choose the coefficients.

degreetrain MSEvalidation MSEverdict
10.17420.1248underfit
20.05040.0107good
50.00000.2001overfit

Training MSE always falls as degree rises. Validation MSE does not: it picks degree 2. This is the whole reason to hold data out.

6. Where it fails

Validation is only useful if it represents the data the model will meet. A random split of a time series can leak the future into the past; a split by patient can put the same patient on both sides; a test set used for model selection stops being a test set.

The world can also change after training. A model that generalizes on last year's data may fail on this year's distribution. Monitoring and retraining are engineering responses, not mathematical guarantees.

A low training loss is evidence about the training examples. It is not evidence that the model has learned the task.

7. Going deeper

For squared loss, the expected prediction error at a point can be decomposed into bias², variance, and irreducible noise. Bias measures how far the average model is from the truth; variance measures how much the model moves when the training set changes.

Cross-validation repeats the train/validation split over several folds and averages the validation score. It gives a more stable estimate when the dataset is small, at the cost of more training runs.

L2 regularization changes the gradient by adding 2λw. The update first shrinks the weight and then takes the data-gradient step. Dropout and data augmentation are different regularizers with a similar goal: prevent the model from relying on one brittle pattern.

8. Practice

Try it

Train MSE is 0.000 and validation MSE is 0.200. Which is more likely: underfitting or overfitting?

Try it

Train MSE is 0.800 and validation MSE is 0.850. What is the more likely diagnosis?

Try it

Why can increasing λ reduce validation error even though it increases training error?

9. Resources

The MIT chapter covers validation, regularization, and ridge regression. The Crash Course connects the train/validation gap to practical model building. Both links were opened and checked for this lesson.

Continue the track

Open the playground for this topic

Decision Boundary Playground →

Add and drag points, switch between linear, quadratic, and neural models, and watch the boundary and both accuracy scores respond.

Next on the recommended path

Convolutional Neural Networks →

The mathematics behind this topic: Probability & statistics.

All Machine Learning lessons

Site soundtrack