← All AI/ML articles

Hyperparameter tuning, visualised.

Models learn their parameters from data, but someone has to choose the settings around the learning. Explore a real map of validation error, then send grid search, random search and Bayesian optimisation across it.

1. Read the landscape2. Search it with a budget3. Check the winner honestly
Each cell is one hyperparameter setting (kernel width γ across, regularisation λ up). Bright = low 5-fold cross-validation error. Numbered dots = evaluations in order. Gold ring = best found so far, cyan diamond = the lowest-CV cell among all 961 evaluated cells. That cell is a benchmark: it is the best observed CV score on this grid, not necessarily the best underlying setting, and its score is itself optimistic. Click any cell to inspect it. Dashed box = allowed search range.
Evaluations0
Best CV error0
vs lowest of 9610
Behind the scenes · 1

What a setting actually does

The model here is kernel ridge regression with a Gaussian kernel. Its parameters (one weight per training point) are learned from the data. Two hyperparameters are set from outside: the kernel width γ (large γ means a wiggly, local fit) and the ridge penalty λ (large λ means a smoother, more cautious fit). Click a cell on the map to see the curve that setting produces.


Cyan dots = the 30 training points. Yellow = fit for the selected cell. Dashed white = the true curve (unknown in real life).
Behind the scenes · 2

Three ways to spend the same budget

Grid search tries evenly spaced settings. Random search tries randomly chosen ones. Bayesian optimisation fits a cheap statistical model of the landscape to the results so far and picks the next setting where improvement looks most promising. All three strategies get exactly the same number of evaluations (the budget is always a square, k², so a k×k grid fits exactly). Bayesian optimisation counts its 5 random starting evaluations inside that total. The chart shows the best error each strategy has found after each evaluation, for your current budget and random draw.


Amber = grid, pink = random, cyan = Bayesian, grey dotted = lowest-CV cell of the 961 (a benchmark). Log scale. A single run is only one random draw.
Behind the scenes · 3

One run proves little: repeat it

To compare strategies fairly, repeat the search many times and average. This button runs 20 independent searches of random search and Bayesian optimisation at budgets 9, 16, 25 and 36 (the same budget for every strategy in each column, with Bayesian’s 5 random starts counted inside it) on the current landscape and range, and compares them with a grid of the same size. It takes a moment.


Behind the scenes · 4

The winner’s score is optimistic

Picking the best of many settings by cross-validation error selects partly for luck. The error of the winning setting on fresh test data is often worse than its cross-validation error (not always, since the test sample is also noisy). The more settings you compare, the larger this effect can be. The same applies to the lowest-CV cell of the whole map: it is the best observed score among 961 settings, and quoting its CV score would be optimistic too. A final test set that was never used for tuning gives an honest check. Also note that a CV estimate from only 30 points can fall below the noise variance σ² by sampling variability: σ² is the minimum expected test MSE under this data-generating process, not a bound on every estimate.


The mathematics

The mathematics behind hyperparameter tuning

Numbers below update live from the current landscape, search and selected cell.

A. The model

ƒ̂(x) = Σi αi exp(−γ(x − xi)²) + &ybar;   ,   (K + λI) α = y − &ybar;

B. The tuning objective: K-fold cross-validation

CV(γ, λ) = (1/K) Σk=1K MSEk(γ, λ)   ,   θ* = argmin CV(θ)

C. Grid versus random search

Grid cost = kd   ,   P(random search hits a region holding a share p of settings) = 1 − (1 − p)B

D. Bayesian optimisation

EI(θ) = (μ* − μ(θ)) Φ(u) + s(θ) φ(u)   ,   u = (μ* − μ(θ)) / s(θ)

Symbols: μ* is the best objective value observed so far, μ(θ) is the surrogate’s predicted mean at setting θ, s(θ) is its predictive standard deviation (not the noise σ), and Φ and φ are the standard normal CDF and density. This is the form for minimisation under a Gaussian predictive distribution. Expected improvement (EI) balances settings that look good with settings that are still uncertain. Here the surrogate models log10 of the CV error, standardised to mean 0 and sd 1 over the results so far, so μ*, μ, s and EI are all in standardised log units, not raw MSE. The panel below also shows the back-transformed predicted CV error.


E. Selection optimism

Reading it
  • The tuned CV error is a minimum over many noisy estimates, so it tends to be too low. That includes the lowest-CV cell of the map, which is the best observed score on a 31×31 grid, not necessarily the best underlying setting.
  • A CV estimate or a 200-point test estimate can fall below σ² by chance: σ² is the minimum expected test MSE, not a floor for every individual estimate.
  • Nested cross-validation or a held-out test set measures the whole procedure honestly.
  • Strategy comparisons here come from one landscape. Another model or dataset can rank them differently.
  • Budget, search range and the scale (log versus linear) of each hyperparameter all matter.
Experiment

Break it on purpose

Same budget, three strategies

Give every strategy exactly 16 evaluations (Bayesian: 5 random starts plus 11 guided) and compare what each finds with the lowest-CV cell of the map.

Over-tuning

Noisy data and a larger budget of 64 random trials, chosen on purpose to stress selection. Compare the winner’s cross-validation error with its error on fresh test data.

A bad search range

Restrict the search to λ ≥ 1 (strong regularisation only). How much worse is the best setting any strategy can find?

Check yourself

Three quick questions

Key takeaways

  • Parameters are learned from data; hyperparameters are chosen around the learning and tuned against a validation score.
  • Grid search cost grows exponentially with the number of hyperparameters; random search spreads the same budget over more distinct values per axis.
  • Bayesian optimisation uses past results to choose the next trial and often helps when each evaluation is expensive, but it is not guaranteed to win on every problem.
  • Search ranges and scales matter: if the best region is outside the range, no strategy can find it, and many hyperparameters should be searched on a log scale.
  • Be honest about the winner: the best tuned score is optimistic, so confirm it on data that was not used for tuning.
← Back to all articles