Overfitting the validation set

Tune long enough and you start fitting the folds. See why the best score is optimistic and how a final hold-out keeps the estimate honest.

Lesson 43 of 6612 minAdvanced

You will learn to

  • Explain selection bias from many trials
  • Keep a final hold-out
  • Use nested cross-validation when it matters

Before this lesson

Intuition

Suppose 100 students each take the same test by guessing, and you pick the student with the highest score for an important job. That score is high only because you picked the maximum of 100 random results. If the student retakes the test, the result will drop back toward the average. The choice itself created the optimism.

Hyperparameter search does the same to models. Every trial gives a cross-validated score with some random error, because the folds are one finite sample of the data. If you try many settings and keep the one with the best score, you are partly picking the settings whose errors happened to be favourable. The score you report is then too high for new data. This is selection bias, and it is how you "overfit the validation set": not by training on it, but by choosing on it, again and again.

The defence is simple. Keep some rows that played no role in training, tuning or choosing, and score the final model on them once.

Picture it

0.800.820.840.86110100Number of candidates tried (log scale)ScoreBest validation score foundTrue quality of every candidateOptimism
Illustrative expected values: 100 candidates that are all truly 0.80 accurate, with a cross-validation score noise of 0.02. The best score found keeps rising with the number of candidates, while the true quality does not move.

The math

Say each candidate has true quality μ\mu and its cross-validated score has random error with standard deviation σ\sigma. If you try nn candidates that are equally good and keep the best score, the expected result is about

E[best of n]=μ+σ⋅mn\mathbb{E}[\text{best of } n] = \mu + \sigma \cdot m_n

where mnm_n is the expected maximum of nn standard normal values. It grows slowly: about 1.54 for 10 candidates, 2.25 for 50 and 2.51 for 100. The bias grows with the number of trials and with the noise σ\sigma, and noise is larger when the dataset is smaller.

An honest estimate needs rows that took no part in the choice. The hold-out score from nhn_h rows has a standard error for accuracy of about

SE≈p(1−p)nh\text{SE} \approx \sqrt{\frac{p(1-p)}{n_h}}

so a small hold-out is honest but imprecise.

Nested cross-validation gets both honesty and precision from limited data. An outer loop splits the data into folds. For each outer fold, an inner loop does the whole tuning on the remaining rows only, and the chosen model is scored on the outer fold. The mean of the outer scores estimates the quality of the whole procedure "tune, then fit".

Worked example

You try 100 settings of a model. Unknown to you, all of them have a true accuracy of 0.80, and the cross-validated score of each has a noise of 0.02.

  • Reported best score: 0.80 + 2.508 × 0.02 = 0.80 + 0.050 = 0.850.
  • True quality of the winner: 0.80.
  • A hold-out of 1,000 rows gives a score around 0.80, with a standard error of sqrt(0.8 × 0.2 / 1000) = sqrt(0.00016) = 0.013. So you will see something like 0.79 to 0.82, and it exposes the 0.85.

In real life, the settings are not all equal, so some of the gain is real. The point is that you cannot tell which part, unless you test on rows not used to choose.

The cost of nested cross-validation: with 5 outer folds and an inner search of 20 trials with 5 folds each, the tuning runs 5 × 20 × 5 = 500 fits, plus 5 fits to score the chosen models. That is five times the cost of a single search, which is why it is worth it mainly for small datasets, where a hold-out would be noisy.

How mlflow does it

mlflow first keeps part of your data aside as a hold-out set (by default 70 percent of the rows train and 30 percent are held out; with a group column, the share counts groups). It then cross-validates every algorithm on the training part. Two consequences:

  • The leaderboard scores are cross-validated averages, so they are exposed to the selection effect described above, and more so after you tune or pick the winner among many models.
  • The plots of a model candidate (confusion matrix, ROC curve, residuals and others) and the sample row on the Predict tab come from the hold-out rows, which no model trained on. So the hold-out is where you check the leaderboard's promise. The model page's Validation tab can also show the fold-by-fold scores, so you can see the spread behind an average.

Tuning inside mlflow uses the same training part, so the hold-out stays untouched by the search. As a rule, tune as little as is useful, look at the hold-out once you have chosen a final candidate, and do not keep tuning after looking at it: that turns the hold-out into just another validation set. See Cross-validation and group folds for the split settings.

Common mistakes

  • Reporting the best cross-validation score as the expected performance. The maximum of many noisy scores is optimistic. Report the hold-out score, or a nested estimate.
  • Peeking at the hold-out repeatedly. Each time you adjust the model after looking at it, you leak information, and the hold-out slowly becomes a validation set. Use it for the final check only.
  • Treating tiny differences as real. If two settings differ by less than the fold-to-fold spread, they are tied. Prefer the simpler model.
  • Using a hold-out that is too small. With 50 rows the standard error is large, so the check is nearly useless. Use more rows, or nested cross-validation.
  • Tuning on data that includes the test period. For time-ordered data, keep the final hold-out as the most recent period, so the check looks like real use.
  • Skipping the check because the gap was small once. The bias grows with the number of trials. Check again whenever you run a much larger search.

Try it in mlflow

Compare CV and hold-out scores

  1. Open Runs, open a finished classification run and note the cross-validated Accuracy of its best model on the leaderboard.
  2. Open that model candidate, go to the Plots tab and open Confusion matrix. It is computed on the hold-out rows the model never trained on.
  3. Compute the accuracy from the matrix (the diagonal divided by the total) and compare it with the leaderboard value.
  4. Repeat for a tuned candidate. If the tuned model gains on the leaderboard but not on the hold-out, the gain was partly noise.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. You try 200 hyperparameter combinations and report the best cross-validated score. Why is this number too optimistic?
  2. 2. What is the purpose of a final hold-out set?
  3. 3. When is nested cross-validation worth its cost?