Grid Search against Random Search

Same budget, two ways of spending it. Grid search tries nine values of everything; random search tries every value of the one that matters.

Grid Search against Random Search

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

Hyperparameters are not equally important. Usually one or two dominate and the rest barely matter.
A grid of n×n points tries only n distinct values of each parameter, however large n×n is.
The same budget spent randomly tries n×n distinct values of each, so it explores the important one far more finely.
The gap widens with dimension. Bergstra and Bengio made this argument in 2012 and it has held up.

Overview

Both methods compare model configurations using validation scores. Grid search is systematic over a small, discrete set; random search spends each trial on a new sample from the parameter ranges. The choice depends on the search space and the cost of each fit.

In this guide
  1. Why the search space matters
  2. Compare a matched budget
  3. Choosing a method
  4. Sources and next steps

Why the search space matters

Suppose there are two parameters, but only one has a large effect on validation score. A grid with ten values for each parameter uses 100 trials to test each value of the important parameter only ten times. A random search with the same 100-trial budget can sample many more distinct values along both ranges. If the second parameter barely matters, more of those trials cover useful settings of the first.

This is not a guarantee that one random run will win. Results depend on the ranges, sampling distributions, noise in the validation score and random seed. Bergstra and Bengio’s random-search study explains why random search can be more efficient when only a subset of hyperparameters matters.

Compare a matched budget

  1. Choose Search as Grid and set Trials to a fixed budget. Note which regions receive evaluations and the best score found.
  2. Choose Random with the same Trials budget. Change Random seed and observe how the sampled points and best score vary.

The visualiser shows a two-parameter score surface. A real model-selection run should use the same data splits and scoring metric for both methods so the comparison is fair.

Choosing a method

UseWhen it fits
Grid searchA few parameters have small, meaningful discrete sets, or an exhaustive check is important.
Random searchRanges are broad, some parameters matter more than others, or each fit is expensive.
A staged searchRandom search identifies a promising region, then a smaller grid compares nearby values.

Choose distributions that match the parameter. For a learning rate spanning several orders of magnitude, sample on a logarithmic scale; a uniform sample over raw values will concentrate most trials near the upper end. Keep the validation strategy fixed, and reserve the test set until the final evaluation. If fits are costly, successive halving can stop weak configurations early; Bayesian optimisation is another option when each evaluation is expensive.

Sources and next steps

See Bergstra and Bengio, “Random Search for Hyper-Parameter Optimization”, and scikit-learn’s parameter-search guide. Continue with cross-validation to choose a reliable validation strategy.

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why does a 4x4 grid of 16 trials explore an important hyperparameter poorly?

  2. How should a learning rate be sampled in a random search?

  3. What should you check if the best value found sits at the edge of the search range?

Cheat sheet

Grid Search against Random Search

Compare grid and random hyperparameter search side by side. See how random sampling covers more of the space with the same trial budget.

MACHINE LEARNING · vizlearn.in/machine_learning/grid_vs_random_search.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.