skip to content

Why does random search over hyperparameters usually beat an exhaustive grid on the same budget?

level: middleimportance: must knowfreq 78%

answer

  1. same budget, very different coverage
  2. most axes barely move the score
  3. a grid repeats values along each axis
  4. distinct value per axis, every trial
  5. low effective dimensionality

basics

~20 s

At the same trial count, random search tries a fresh value of every hyperparameter in every trial, while a grid reuses the same few values per axis. When only two or three hyperparameters matter, random search resolves those axes far more finely.

solid answer

~50 s

A grid fixes a short list of values per hyperparameter and evaluates the full Cartesian product, so a 4x5x3x3 grid is 180 configurations, and with 5-fold cross-validation that is 900 fits. Yet each axis is still probed at only 4, 5, 3 and 3 distinct values, no matter how large the total gets. Random search draws each configuration independently from a distribution per hyperparameter, so 180 trials give 180 distinct values on every axis. That matters because response surfaces in practice have low effective dimensionality: a couple of hyperparameters move the score and the rest are nearly flat. The grid spends most of its budget replicating measurements along the flat directions. Random search is also anytime and budget-decoupled: you choose the number of trials from your compute, not from the size of the space, and adding an irrelevant hyperparameter costs nothing instead of multiplying the run.

go deeper

for a junior

Be ready to say plainly what each method does: a grid tries every combination of listed values, random search draws configurations from ranges you specify. Know that the grid's cost is a product, so it explodes quickly.

for a middle

You are expected to produce the counting argument on the spot, including the cross-validation fold multiplier, and to explain that random search wins because only a few hyperparameters actually move the score.

for a senior

Show the operating judgment: pick ranges wide enough that the optimum is not on a boundary, seed and log every trial, treat the budget as a compute decision, and say out loud when a small discrete space makes the grid the better choice.

for a principal

Own the tradeoff across a team: an exhaustive grid is auditable and trivially explained, random search is cheaper and anytime. Decide which the organisation needs, and set a convention for logging trials so results are comparable across projects.

## The two procedures **Grid search** asks you to enumerate a finite list of candidate values for each hyperparameter and then evaluates every combination — the Cartesian product. Its cost is the product of the list lengths, multiplied by the number of cross-validation folds. **Random search** asks you instead to declare a *distribution* per hyperparameter (uniform over an interval, log-uniform over decades, a categorical choice, an integer range), then draws N independent configurations from that joint distribution and evaluates each one. Its cost is N times the number of folds, and N is a number you pick. ## The counting argument Take a support-ticket triage model where a single fit takes 30 minutes, and a grid of four values for one hyperparameter, five for a second, three for a third and three for a fourth. That is `4*5*3*3 = 180` configurations. Under 5-fold cross-validation each configuration is fit five times, so the search is 900 fits — roughly 450 hours of compute if run serially. For those 900 fits, how many distinct values of the first hyperparameter did you actually try? Four. If that hyperparameter is the only one that matters, you spent 900 fits to measure four points on a curve, each measured 225 times over. A random search with the same 180 trials measures 180 distinct values of it — and, simultaneously, 180 distinct values of every other axis. ## Low effective dimensionality The underlying empirical claim, made by Bergstra and Bengio, is that the validation score as a function of the hyperparameters is dominated by a small subset of them; the remaining axes are close to flat over any sensible range. The dimensionality of the space is high, but its *effective* dimensionality is low — and you usually do not know in advance which axes are the live ones. A grid must commit to a resolution on every axis before it starts, and pays for that resolution multiplicatively. Random search spends every trial informatively on every axis at once. A second, purely probabilistic argument: suppose the top 5% of one hyperparameter's range is the region you need to hit. A single random draw hits it with probability 0.05, so N independent draws hit it with probability `1 - 0.95^N`. At N = 60 that is about 95%. You get a strong coverage guarantee on the important axis without knowing which axis it was. ## Irrelevant hyperparameters This is the sharpest practical difference. Add a fifth hyperparameter with three candidate values to the grid above and the run triples, from 900 fits to 2700, whether or not the new axis does anything. Add it to a random search and the run costs exactly the same 180 trials; you simply also get 180 distinct values of the new axis, which is enough to see that it is flat. ## Other properties worth naming - **Anytime behaviour.** Random trials are exchangeable, so you can stop after any number of them and the best-so-far is a fair draw from the space. A grid interrupted halfway has only explored whichever corner of the space the loop happened to reach first, so the partial result is systematically skewed. - **Parallelism.** Both are embarrassingly parallel — this is not a differentiator, and claiming a grid parallelises better is wrong. - **Reproducibility.** Seed the draws and log every configuration with its score; otherwise a random search is not repeatable and you cannot inspect the response surface afterwards. - **Boundaries.** If the best configuration sits at the edge of a searched range, the range was too narrow, not the search too short. ## When a grid is still the right call - One or two hyperparameters, both known to matter, and a cheap fit — the exhaustive answer is simply better and easier to explain. - A small purely discrete space, for example three candidate distance metrics crossed with four neighbourhood sizes. Twelve combinations are exhaustible, and random sampling over a tiny discrete space wastes trials on duplicate draws. - You want the full response surface for a report or a plot, where the regular lattice is the point. ## What an interviewer is listening for The counting argument stated concretely, the low-effective-dimensionality reason behind it, the fact that the cross-validation folds multiply the cost of every configuration, and the honesty to say that on a two-axis discrete space a grid is fine.

  • Walk me through the cost of a 4x5x3x3 grid with 5-fold cross-validation when one fit takes 30 minutes.
    The Cartesian product is 180 configurations. Cross-validation fits each one once per fold, so 180 x 5 = 900 fits, and at 30 minutes each that is about 450 hours serially. The fold multiplier is the part people forget: every extra fold rescales the whole search.
  • Why does adding an irrelevant hyperparameter hurt a grid far more than a random search?
    A grid's cost is the product of the axis sizes, so an extra axis with three candidate values triples the run whether or not it matters. A random search still runs the trial count you chose; the new axis just gets one more sampled value per trial, which is enough to show it is flat.
  • When would you still prefer the exhaustive grid?
    When the space is small and discrete — a handful of categorical choices crossed with a couple of integer settings — and a fit is cheap. Exhaustive coverage is then affordable, reproducible without a seed, gives the full response surface, and avoids random search wasting draws on duplicates.

A grid is a fishing net with a fixed mesh laid over the whole lake; random search is scattering the same number of hooks. If the fish are all along one line, the scattered hooks cover that line at far more depths.

saying these in an interview costs you the question

  • Calls random search a lazy approximation of a grid
  • Claims a grid guarantees finding the global optimum
  • Thinks a bigger grid always yields a better model
  • Forgets each configuration costs k fits under k-fold cross-validation
  • Assumes a grid samples each axis at more distinct values
  • Says random search cannot be parallelised

context