Worked examples — Grid search and random search
This page is the "prove it on real numbers" companion to the parent topic. We will not re-explain the theory — instead we hunt down every kind of situation a hyperparameter search can throw at you and grind each one out by hand.
Before we touch a single example, let us agree on the words, because we will use them constantly.
Look at the figure below: it is exactly that map. The bright ridge in the middle is where scores are highest, the white star marks the true best configuration, and each orange dot is one flag a grid plants. Notice the grid's dots never land exactly on the star — that gap is the whole story of this page. Keep this map in mind; every later example is a slice or variation of it.

The scenario matrix
Every worked example below is tagged with one cell from this table. Together they cover all of it.
| Cell | Case class | What is "extreme" about it |
|---|---|---|
| A | Low-dimension grid (2 knobs) | The friendly base case — count and cost |
| B | Sign / boundary of a knob | A hyperparameter at its zero / edge value (e.g. , dropout ) |
| C | High-dimension explosion | Adding one knob — the exponential blow-up |
| D | Grid vs Random, equal budget | Same number of trials, unique values per axis |
| E | One knob matters, one is noise | The classic "why random wins" limiting case |
| F | Log scale vs linear scale | Sampling orders of magnitude, not steps |
| G | Real-world word problem | Cloud cost & wall-clock time budgeting |
| H | Exam twist | Cross-validation multiplier + "which method?" trap |
| I | Where grid wins | Tiny discrete space / very low budget — guaranteed coverage |
Cell A — the friendly base case
Step 1 — Multiply, don't add. Why this step? Grid search tries every pairing of a value from list one with a value from list two. For each of the 3 learning rates there are 4 regularizations, giving . We multiply (not add) because the choices are independent — this is the same reason a menu with 3 mains and 4 drinks gives 12 meals, not 7.
Step 2 — Fold in cross-validation.
Why this step? Each of the 12 configurations must itself be trained 5 times to get one trustworthy score, so we multiply again.
Recall Verify
Total configurations equals product of grid sizes ::: , and runs. Units: (configs) × (folds) = (runs). ✓
Cell B — a knob at its boundary value
Step 1 — Read the role of . In Regularization the training objective is Why this step? You cannot judge a boundary value without knowing what the symbol multiplies. Here scales the penalty on large weights.
Step 2 — Set . The whole second term vanishes: Why this step? Substituting the boundary value shows the model with no penalty at all — pure fit-the-data. This is the overfitting extreme, and it is a perfectly valid, informative point on the grid, not a degenerate one. Keep it.
Step 3 — The genuinely degenerate case. Now imagine : the penalty dominates, all weights are crushed toward zero, the model predicts a constant (underfitting). That is the useless extreme. Why this step? We push the knob to its other boundary to map out the full behaviour range: at one edge () the model overfits, at the far edge () it underfits. Showing both edges tells the reader the entire span the knob controls, so no limiting value is left unexplored.
Recall Verify
At the objective reduces to ::: just the data-error term (no penalty); it is the overfitting-prone extreme, fully valid to grid over.
Cell C — one more knob, exponential pain
Step 1 — Write the general count. For knobs with values each: Why this step? Every knob multiplies the count by its number of values (Cell A logic, repeated), so equal-sized lists give a power.
Step 2 — Plug in. Why this step? We substitute the concrete numbers , and then into the general formula so the abstract power becomes two countable model totals we can actually compare.
Step 3 — Take the ratio. Why this step? The ratio isolates the cost of one extra knob. Adding a single hyperparameter multiplies your entire compute bill by 5 (in general, by , the number of values on the new axis). This is the curse of dimensionality in a nutshell.
Recall Verify
Adding one axis with values multiplies cost by ::: ; here , and , .
Cell D — equal budget, unequal coverage
Step 1 — Grid's distinct values. A grid reuses each value across the 3 settings of : Why this step? In a square grid of points there are rows, so only unique values per axis.
Step 2 — Random's distinct values. Each of the 9 draws picks freshly from a continuous range, so (with probability 1) all differ: Why this step? We contrast the two counts on the same budget to expose the core trade-off: random spends all 9 evaluations on 9 fresh values, while grid recycles.
Step 3 — The picture. Project all flags onto the east–west axis. The grid's 9 flags land on only 3 columns (heavy stacking); random's 9 land on 9 separate columns.

Why this matters: more resolution on the axis that might be the important one — for free, same budget.
Recall Verify
For trials in a square grid, distinct values per axis ::: (here 3), while random gives (here 9).
Cell E — the "one knob matters" limit
Step 1 — Evaluate grid's three values. Why this step? Since does nothing, only the value decides the score. Grid's best is with score .
Step 2 — Grid's ceiling. No matter which of the 9 lattice points you pick, is one of , so the best achievable score is . Grid can never reach because is not on its lattice. Why this step? We fix the upper bound grid can ever hit on this problem, so we have a concrete number to beat.
Step 3 — Why random can beat it. Random draws 9 different values across . The chance that at least one lands in the good band, say within of (a band of width , i.e. of the range), is Why this step? Each draw independently misses the band with probability ; all nine miss with probability ; subtract from 1 for "at least one hit." A chance of landing near the true peak — versus grid's guaranteed miss.
Recall Verify
Grid's best score here is at , and .
Cell F — sampling across orders of magnitude
Step 1 — Uniform on the raw value. The range has total width . The decade has width . Fraction: Why this step? Uniform sampling spreads points evenly on the number line, so the tiny low decade almost never gets sampled — nearly all points crowd near .
Step 2 — LogUniform: uniform on the exponent. Using the recipe above, take : the range becomes , total width . The decade becomes , width . Fraction: Why this step? Learning rates matter by their order of magnitude ( vs is a real difference; vs is not). Sampling uniformly in space gives each decade equal love — see Learning Rate Scheduling for why the scale is multiplicative.
Recall Verify
Under Uniform, the lowest decade gets of samples; under LogUniform it gets .
Cell G — real-world budget word problem
Step 1 — Count runs. Why this step? This is the base counting from Cell A applied twice: multiply the grid axes to get configurations, then multiply by folds to get actual training runs. Every downstream number rides on this count, so we nail it first.
Step 2 — Total GPU-time. Why this step? Cost and time both flow from total compute, so we convert the run count into a single physical quantity — GPU-hours — using .
Step 3 — Money. Why this step? Cost tracks total GPU-hours regardless of parallelism (8 GPUs for 3 h costs the same as 1 GPU for 24 h). 57.60 > \40$ — over budget. ✗
Step 4 — Wall-clock with 8 GPUs. Why this step? Runs are independent, so parallelism divides wall-clock time (not cost).
Step 5 — The fix: cap the budget with random search. Replace the exhaustive grid with a random search capped at 60 runs instead of 120: Why this step? We halve the run count by swapping the exhaustive grid for a capped random budget, because random search can stop at any number of trials while a grid must finish every combination to stay meaningful. This drops cost from \57.60$28.80$40$ limit ✓) and wall-clock from 3 h to 1.5 h — the earlier theory ("random can stop anytime") is exactly what rescues the deadline. See Early Stopping and Bayesian Optimization for even smarter spending.
Recall Verify
Full grid = 120 runs = 24 GPU-hours = $57.60 (over budget), wall-clock 3 h; capped random = 60 runs = 12 GPU-hours = $28.80, 1.5 h.
Cell H — the exam twist
Step 1 — Fix part (a). They forgot the fold multiplier: Why this step? "Number of models tried" (100) ≠ "number of times the fit routine ran" (500). Exams love this gap. The reported 100 undercounts compute by .
Step 2 — Part (b): match method to space. Checklist from the parent note:
- 6 dimensions (≥4) → favors random.
- Continuous knobs → favors random (no forced discretization).
- One dominant knob suspected → random gives it distinct values instead of (Cells D & E).
With trials, grid gives values per axis (useless), while random gives up to 64 distinct values on the important axis. Why this step? Solving shows a 6-D grid at 64 trials tests only 2 settings of the crucial knob — catastrophic. So the correct answer is random search (or, for even smarter spending, Bayesian Optimization, with the top configurations combined via ensembling).
Recall Verify
A grid with 5-fold CV is trainings, not 100; and a 6-D grid at 64 trials tests only values per axis.
Cell I — the case where grid wins
Step 1 — Count the whole space. Why this step? When knobs are discrete and few, the space is finite and tiny — the "map" from figure s01 collapses to just 4 lonely points, not a continuous surface.
Step 2 — Grid's guarantee. A grid over all 4 combinations with a budget of 4 evaluates every one exactly once. Probability it misses the best: Why this step? Exhaustiveness is grid's superpower — when the whole space fits in the budget, coverage is total and deterministic.
Step 3 — Random's risk on the same budget. Random with replacement might draw duplicates and skip a combination. The chance random's 4 draws cover all 4 distinct combinations is Why this step? There are equally likely draw-sequences and only that hit each combination once; the ratio is the "collect all 4" probability. So random has only a chance of even seeing the best config — a chance of missing full coverage. Grid wins decisively here.
Recall Verify
With 4 combinations and 4 trials, grid misses the best with probability ; random covers all four with probability .
Recall gauntlet
Grid search count for 3 learning rates and 4 regularizations
Adding one hyperparameter with values multiplies grid cost by
Distinct values per axis in an -point square grid vs random
Fraction of the lowest decade sampled by LogUniform over
"100 models trained" with 5-fold CV really means
The correct search for a 6-D mostly-continuous space with one dominant knob
When does grid search beat random
Prerequisite links for this page: Cross-Validation, Overfitting and Underfitting, Regularization, Learning Rate Scheduling, Training-Validation-Test Split, Early Stopping, Bayesian Optimization, Ensemble Methods, and the parent Grid search and random search.