Lesson 3 of 4 Hyperparameters, Tuning and Model Fit

Diagnosing Overfitting and Underfitting and Applying Regularization

This lesson explains overfitting and underfitting, the two ways a model fails to generalise, and regularisation, the main family of remedies for overfitting.

Learning outcomes

After completing this lesson, you will be able to:

  • Recognize overfitting and underfitting from training and validation results.
  • Fix overfitting with regularisation, dropout and more data.
  • Fix underfitting with more capacity and features.
  • Read learning curves through the bias-variance trade-off.

Recognizing the problem

Overfitting means the model has learned noise and quirks of the training data: training performance is high, but validation performance is much lower, a situation of high variance. Underfitting means the model is too simple or too constrained to capture the pattern: both training and validation performance are poor, a situation of high bias. Learning curves that plot training and validation loss over epochs or data size make the difference visible.

Sort into groups

Overfit or underfit?

Exam skills 2.2.5

Diagnose each set of results.

Log in to try this activity Interactive activities are free with an account.

Fixing overfitting

An overfitted model performs well on training data but poorly on new data, because it has memorised noise instead of learning general patterns. The most effective remedy is more training data, or augmentation that creates realistic variations of it. Regularisation penalises complexity: L1 (lasso) adds the absolute values of the weights to the loss and drives some weights to zero, acting as feature selection, while L2 (ridge) adds the squared weights and shrinks them all.

Dropout randomly disables neurons during training, so the network cannot rely on any single one. Early stopping ends training before memorisation sets in. Simpler models, such as shallower trees or fewer layers, and feature selection reduce capacity, and cross-validation chooses settings reliably.

The following diagram compares training and validation AUC for three candidate churn models.

Training and validation AUC for the three churn models. Model A scores poorly on both, so it underfits. Model B scores 0.99 in training but 0.74 in validation, a large gap, so it overfits. Model C's shallower, regularised trees trade some training performance for 0.85 in validation.
The scenario is illustrative.
Sort into groups

Which fix for which problem?

Exam skills 2.2.5

Decide whether each change helps with overfitting or with underfitting.

Log in to try this activity Interactive activities are free with an account.

Fixing underfitting

An underfitted model performs poorly even on its training data, because it is too simple to capture the patterns present. The remedies are the opposite of those for overfitting: more informative features or better feature engineering, a more complex model with deeper trees or more layers, longer training and less regularisation. Comparing training and validation metrics tells the two problems apart: poor scores on both indicate underfitting, while a large gap between them indicates overfitting. The following table summarises both.

Overfitting (high variance)
Training performanceHigh
Validation performanceMuch lower than training
FixesMore data, augmentation, L1/L2, dropout, early stopping, simpler model
Underfitting (high bias)
Training performanceLow
Validation performanceLow, similar to training
FixesMore features, more complex model, longer training, less regularisation

The bias-variance trade-off and learning curves

Model error can be thought of as the sum of bias, the error from assumptions that are too simple for the true pattern, variance, the error from sensitivity to the particular training sample, and irreducible noise. Increasing model capacity, for example with deeper trees or more layers, lowers bias but raises variance; regularisation does the reverse. The goal is the point where the validation error is lowest, not the point where training error is lowest.

Learning curves help choose the right remedy. When validation error keeps falling as the training set grows and a gap to training error remains, the model has high variance and more data is likely to help. When training and validation error converge at a high value, the model has high bias, and more data alone will not help; better features or more capacity are needed. Plotting loss over epochs gives a similar view during training: the epoch at which validation loss starts to rise while training loss keeps falling marks the onset of overfitting, which is where early stopping should end training.

Regularization strength is itself a hyperparameter. L2 penalties, dropout rates, tree depth and the minimum number of samples per leaf should be tuned against a validation set or with cross-validation, for example with SageMaker automatic model tuning, rather than set by habit. A very strong setting produces underfitting just as surely as no regularisation produces overfitting.

The following diagram shows the learning curves that mark the onset of overfitting.

Training and validation loss over epochs. Training loss keeps falling, but validation loss reaches its minimum and then rises as the model starts to memorise the training data. The epoch where validation loss is lowest marks the onset of overfitting, which is where early stopping should end training.
The curves are schematic.
Worked example

Reading the numbers

Three models trained on the same churn data report these results.

Model                      Train AUC   Validation AUC   Diagnosis
A  Logistic regression      0.66        0.65            Underfitting: add features or capacity
B  XGBoost max_depth=15     0.99        0.74            Overfitting: lower depth, add regularization
C  XGBoost max_depth=5,     0.87        0.85            Good fit
   lambda=2, subsample=0.8

Model B memorised the training set. Adding more trees to it would make the gap larger, while Model C's shallower, regularised trees trade a little training performance for much better generalisation.

Check yourself

What is the difference between L1 and L2 regularisation?

Show answer

L1 drives some weights to zero, which selects features. L2 shrinks all weights.

Exam Tips

Remember for the exam

  • High training, low validation performance: overfitting.
  • Low training and validation performance: underfitting.
  • L1 drives weights to zero (feature selection); L2 shrinks all weights.
  • Dropout and early stopping reduce overfitting in neural networks.
  • Underfitting: more features, more capacity, less regularisation.