Training loss keeps falling, validation loss rises. What's happening?
Question
You train a churn model and log the loss on the training set and on a validation set:
| epoch | train loss | val loss |
|---|---|---|
| 1 | 0.69 | 0.70 |
| 5 | 0.45 | 0.48 |
| 10 | 0.30 | 0.41 |
| 15 | 0.18 | 0.44 |
| 20 | 0.09 | 0.53 |
| 30 | 0.03 | 0.68 |
What is happening after epoch 10? Which epoch's model would you keep? Name three ways to fix the problem.
Show hints (3)
- "COMPARE THE COLUMNS"
Up to epoch 10 both losses fall. After that they move in opposite directions. What is the model learning that doesn't carry over to new rows?
- "WATCH THE GAP"
The gap between val and train loss is 0.03 at epoch 5 and 0.65 at epoch 30. A big gap and two high, close losses point to different problems.
- "WHAT TO KEEP"
Keep the model with the lowest validation loss, not the last one. Then ask: what gives the model fewer chances to memorize noise?
Show checked explanation
The model is overfitting. After epoch 10 it starts to memorize noise in the training rows, so training loss keeps falling while validation loss rises. Keep the epoch 10 model, where validation loss is lowest (0.41). To fix it, stop early, add regularization, get more data or use a simpler model.
import numpy as np x_train = np.arange(8) # 0, 1, ..., 7 y_train = np.array([1, 4, 4, 4, 4, 6, 4, 0]) x_val = np.arange(7) + 0.5 # 0.5, 1.5, ..., 6.5 y_val = np.array([2, 4, 5, 6, 5, 3, 2]) def mse(coefs, x, y): return np.mean((np.polyval(coefs, x) - y) ** 2) for degree in [1, 2, 4, 7]: coefs = np.polyfit(x_train, y_train, degree) train = mse(coefs, x_train, y_train) val = mse(coefs, x_val, y_val) print(f"degree {degree}: train {train:.2f}, val {val:.2f}")
degree 1: train 3.23, val 2.35 degree 2: train 0.98, val 0.23 degree 4: train 0.16, val 1.59 degree 7: train 0.00, val 3.41
Code run on CPython 3.12 with NumPy 2 against the example above. Checked by an editor other than the author.
Walkthrough
The gap between the losses grows from 0.03 to 0.65. The model fits rows it has seen much better than new rows. That is overfitting.
Making a model more complex has the same effect as training it longer. The code fits polynomials to 8 training points. Then it measures the error on 7 validation points that lie between them:
| degree | train error | val error | reading |
|---|---|---|---|
| 1 | 3.23 | 2.35 | underfit: high bias |
| 2 | 0.98 | 0.23 | good fit |
| 4 | 0.16 | 1.59 | overfit: high variance |
| 7 | 0.00 | 3.41 | memorized the noise |
A straight line (degree 1) can't follow the hill in the data, so both errors are high. That is high bias: the model is too simple.
Degree 7 has 8 coefficients, one for each training point. It passes through every point, so its training error is 0. Between the points it swings far away, and its validation error is the worst in the table. That is high variance: move one training point a little, and the curve changes a lot.
Degree 2 sits in the middle. It follows the shape and ignores most of the noise.
Training longer works in a similar way. At epoch 1 both losses are near 0.7: the model underfits. Late epochs fit the noise. The best model is where validation loss is lowest.
Fixes for high variance:
- Early stopping: keep the checkpoint with the lowest validation loss, here epoch 10.
- Regularization: a penalty on large weights (L1 or L2), or dropout in a neural network.
- More data: the noise in one row matters less when there are many rows.
- A simpler model: fewer layers or features, a lower degree, shallower trees.
Common mistakes
- Keeping the last epoch because its training loss is the lowest. Training loss only shows how well the model remembers rows it has seen.
- Stopping at the first epoch where validation loss goes up. Validation loss is noisy. Wait a few epochs (the patience), then go back to the best checkpoint.
- Choosing the epoch with the test set. Then the test score is too optimistic. Choose with the validation set and use the test set once, at the end.
- Adding regularization when both losses are high and close. That is underfitting, and a penalty makes the model even simpler.
Follow-up questions
- "Both losses are high and close together. What do you do?" That is high bias: the model is too simple. Use a bigger model or better features, lower the regularization, or train longer. More data alone won't help much: the model can't fit even the rows it has.
- "Why does more data reduce overfitting?" With 8 points, a degree 7 curve can pass through every one of them. With hundreds of points from the same curve, it can't. It has to follow the shape they share, and the noise in single rows averages out.
What a strong answer covers
Names overfitting from the growing gap and keeps the checkpoint with the lowest validation loss. Reads high bias and high variance from the two errors. Matches the fix to the problem: more capacity for bias; more data, regularization or early stopping for variance.
Open in interactive practice deck →