Grid, random and Bayesian search

Compare grid, random and Bayesian (TPE) hyperparameter search, size a search budget with simple arithmetic, and learn to read a tuning history.

Lesson 42 of 6614 minIntermediate

You will learn to

  • Compare grid, random and Bayesian (TPE) search
  • Size a search budget
  • Read a tuning history

Before this lesson

Intuition

Once you know that hyperparameters matter, you need a way to find good values. Imagine looking for the best spot to set up a market stall in a town. You could walk through every street in a fixed pattern (grid search), pick random streets to check (random search), or pay attention to where earlier visits found crowds and look more around those places (Bayesian search).

Each "visit" is a trial: train the model with one combination of hyperparameters and measure its cross-validated score. Trials are expensive, because each one trains the model once per fold. So the real question is how to find a good combination within a limited budget.

Picture it

Here are nine trials over two hyperparameters. Only the horizontal one matters; the shaded band is where scores are good.

Good scoresImportant hyperparameterUnimportant one
Grid search: 9 trials, but only 3 different values of the hyperparameter that matters.
Good scoresImportant hyperparameterUnimportant one
Random search: the same 9 trials give 9 different values of the important hyperparameter, so the good band is much more likely to be hit.
Good scoresImportant hyperparameterUnimportant one
Bayesian search (illustrative): after a few exploratory trials, new trials concentrate where earlier ones scored well.

The math

Grid search with kk hyperparameters and mjm_j values for the jj-th one needs ∏jmj\prod_j m_j trials. The cost multiplies as you add hyperparameters.

Random search draws each trial independently. Suppose some region holds a share qq of the search space, for example the best 5 percent (q=0.05q = 0.05). The probability that at least one of nn random trials lands in it is

P(hit)=1−(1−q)nP(\text{hit}) = 1 - (1 - q)^n

This does not depend on how many hyperparameters there are, which is why random search scales well.

Bayesian search with TPE (Tree-structured Parzen Estimator) splits the trials so far into a good group (the best scores) and a bad group. It estimates a density l(x)l(x) for the hyperparameters in the good group and g(x)g(x) in the bad group, and proposes the next trial where the ratio l(x)/g(x)l(x) / g(x) is large. Trials run one after another, and each uses everything learned so far.

The total work for any strategy is the number of trials times the number of folds. Folds are cross-validation folds, so a 5-fold search with 20 trials trains 100 models.

Worked example

Suppose a model has 3 hyperparameters and you consider 5 values for each.

  • Grid: 5 × 5 × 5 = 125 combinations. With 5 folds that is 125 × 5 = 625 model fits.
  • Random with 20 trials: 20 × 5 = 100 fits. The chance that at least one trial lands in the best 5 percent of the space is 1 - 0.95²⁰ = 1 - 0.358 = 0.64.
  • Random with 60 trials: 1 - 0.95⁶⁰ = 1 - 0.046 = 0.95, using 300 fits.

So 60 random trials have a 95 percent chance of reaching the top 5 percent region, for less than half the cost of the full grid, which would have visited only 5 values per hyperparameter. A fourth hyperparameter would raise the grid to 625 combinations and 3,125 fits, while 60 random trials would still cost 300 fits.

How to size a budget: start with the time you are willing to wait. Measure the time of one fit, multiply by the folds, and divide your time by that figure to get the number of trials you can afford. Spend the first trials broadly, then narrow the ranges around the best results.

The score gain is usually modest. Search finds a better point than the default; it does not turn a weak model into a strong one.

How mlflow does it

mlflow offers three search methods, and the settings have these limits:

  • Random search: tries a set number of combinations. Iterations defaults to 10 (from 1 to 200).
  • Grid search: tries every combination, with a limit of 500 combinations. A larger grid is refused instead of being cut off silently.
  • Bayesian (TPE, through optuna): Trials defaults to 20 (1 to 200) and Timeout (s) defaults to 600 (10 to 3,600). When the timeout runs out, no new trial starts; the one in progress finishes and the best result so far is kept. Bayesian search is available only when the server has the optuna component installed.
  • CV folds for tuning defaults to 5 (2 to 10).

You can set a tuning method per algorithm in the Models part of the pipeline editor of Pipeline Studio, under the Tuning setting of the algorithm card (None, Random search, Grid search or Bayesian). Parameters you set manually stay fixed and are not searched. You can also tune a finished candidate from its page with Improve and then Tune, which runs a random search with the Search iterations you choose and an optional Optimize metric; the result is a new tuned candidate in the same run.

A tuned candidate shows a Tuning history chart with the score of each trial and the Best so far line, plus its Best hyperparameters. A line that is still climbing at the last trial says the budget was too small; a line that went flat early says more trials would add little. See Hyperparameters and Improve a model.

Common mistakes

  • Using a grid with many hyperparameters. The cost explodes and most trials repeat unimportant values. Use random or Bayesian search once you have more than two or three hyperparameters.
  • Searching ranges that are too wide or too narrow. If the best value sits at the edge of the range, the true best is outside it. Widen the range and search again.
  • Stopping at the first improvement. One lucky trial can be noise. Look at the whole tuning history and at the spread across folds.
  • Tuning many hyperparameters at once on a small dataset. The search then fits the noise of the folds (see the next lesson). Tune the two or three that matter most.
  • Forgetting the cost per trial. A model that takes a minute per fit, 5 folds and 60 trials is five hours. Check the time per fit before choosing the budget.

Try it in mlflow

Tune a model candidate

  1. Open Runs, open a finished classification or regression run and click a model candidate.
  2. Open the Improve menu and choose Tune. Leave Search iterations at its default and press Start tuning.
  3. When it finishes, open the new tuned candidate and read its Tuning history and Best hyperparameters.
  4. Compare the tuned score with the original candidate. A small gain is normal; the next lesson explains how much to trust it.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. You have 4 hyperparameters with 5 candidate values each. How many combinations does a full grid search try?
  2. 2. Why does random search often beat grid search for the same number of trials?
  3. 3. What does Bayesian (TPE) search do differently from random search?