Foundations to Frontier
Tests
D. Classical Machine Learning › D0 Core Concepts (concept pages)

Hyperparameter Tuning: Grid, Random, Bayesian (Optuna) and Hyperband

Hyperparameters, such as learning rate, regularisation strength, tree depth or number of neighbours, are set before training and can change performance dramatically. Tuning searches for good values using validation performance. Grid search tries every combination of a few values; random search samples configurations and covers important dimensions far better; Bayesian optimisation builds a model of the validation loss and chooses each new trial where improvement is most likely; and multi-fidelity methods (successive halving, Hyperband) start many trials cheaply and keep only the promising ones. This page explains each method, search-space design and tools like Optuna. In the exercises you implement all five strategies; then you give each the same compute budget on an objective where early results are misleading and compare how close they get to the best configuration.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Tuning the knobs

A new oven has temperature and time knobs. You could try every combination on a neat chart, which wastes many cakes on settings that barely differ. You could try random settings, which explores more distinct temperatures. You could learn from each cake to pick the next setting. Or you could taste every cake after five minutes and only keep baking the promising ones. These are the four main ways to tune a model’s knobs, its hyperparameters.

Grid search versus random search
grid: 3 distinct valuesimportant hyperparameter →random: 9 distinct valuesimportant hyperparameter →
best loss found (0 is optimal, green cross): grid 0.213 · random 0.209
Darker blue is better. When only a few hyperparameters matter, a grid wastes most trials repeating the same values of the important one, while random search explores a new value every trial. Bayesian optimisation goes further by choosing each trial from the results so far; Hyperband saves budget by stopping poor trials early.
Why engineers care

Each training run of a perception network or a large simulation surrogate can cost hours of GPU time. Smart tuning gets a better model for the same budget, or the same model at a fraction of the cost.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A hyperparameter is…
  2. 2. In the demo, random search tests more values of the important knob because…
  3. 3. Stopping bad trials early saves…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Four strategies

Strategy How it chooses trials Strength Weakness
grid every combination of listed values simple, reproducible cost grows exponentially; few distinct values per dimension
random independent samples from distributions covers important dimensions; parallel ignores past results
Bayesian optimisation (GP, TPE) surrogate model + acquisition function (expected improvement) sample-efficient sequential; overhead; struggles in high dimensions
successive halving / Hyperband start many trials with small budget, keep the best 1/η huge savings when early results predict final ones misled if early rankings differ from final ones

Search spaces: sample learning rates and regularisation on a log scale, integers (depth, neighbours) on integer ranges, and categoricals (kernel type) as choices. Tune on validation data or inside cross-validation, and report on a separate test set (D0.4).

import optuna
def objective(trial):
    lr = trial.suggest_float("lr", 1e-5, 1e-1, log=True)
    depth = trial.suggest_int("depth", 2, 10)
    return cross_val_score(make_model(lr, depth), X, y, cv=5).mean()
study = optuna.create_study(direction="maximize", sampler=optuna.samplers.TPESampler(), pruner=optuna.pruners.HyperbandPruner())
study.optimize(objective, n_trials=100); print(study.best_params)
📝 Quick check (Level 1)
0/3 answered
  1. 1. A learning rate should be sampled…
  2. 2. Grid search with 5 values for each of 6 hyperparameters needs…
  3. 3. Optuna's default TPE sampler is a form of…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

How the smarter methods work

Bayesian optimisation (A6.4.2) fits a surrogate, such as a Gaussian process or TPE density ratio, to the (configuration, validation loss) pairs seen so far. It then picks the configuration maximising expected improvement:

EI(x) = (f* − μ)Φ(z) + σφ(z), with z = (f* − μ)/σ.

This balances exploiting low predicted loss μ against exploring high uncertainty σ.

Successive halving with factor η:

  1. Evaluate n configurations with budget b.
  2. Keep the best n/η and give them budget ηb.
  3. Repeat until one remains.

The total cost is about n·b·(number of rounds). Hyperband runs several brackets with different trade-offs between “many cheap trials” and “few full trials”, hedging against early rankings being unreliable. BOHB and Optuna combine Bayesian sampling with Hyperband pruning.

🐍 Exercise 1: five tuning strategies
Implement grid search, random search over log-scaled ranges, successive halving with budget accounting, Hyperband brackets, and Bayesian optimisation with a Gaussian-process surrogate and expected improvement; test them on a known objective.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Expected improvement is large where…
  2. 2. Successive halving with η = 3 keeps…
  3. 3. Hyperband runs several brackets because…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Practical tuning

  • Start from sensible defaults and tune the few hyperparameters that matter most: learning rate first, then regularisation and capacity.
  • Budget: random search with 50–100 trials is a strong baseline. Use Bayesian methods when trials are expensive and few; use Hyperband or ASHA pruning when partial training is informative.
  • Parallelism: random search and Hyperband/ASHA parallelise trivially; sequential Bayesian optimisation needs batch variants.
  • Noise: validation scores are noisy (D0.4). Repeat the top configurations, and don’t over-tune small differences.
  • Never tune on the test set. Use nested CV or a final hold-out for the reported number.

Interview questions.

  • Why is random search better than grid search? With low effective dimensionality, random search samples each important hyperparameter at n distinct values rather than n^(1/d) (Bergstra and Bengio, 2012).
  • When can early stopping of trials mislead? When good configurations start slowly, for example small learning rates that end best.
📝 Quick check (Level 3)
0/3 answered
  1. 1. 100 random trials, each with a 5% chance of hitting the top region; chance of at least one hit?
  2. 2. Best strategy for 8 GPUs and cheap partial evaluations?
  3. 3. Reported performance after tuning must come from…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

The same budget, five strategies

Exercise 1.

  • Grid search finds lr = 10⁻², l2 = 10⁻³ in 90 epochs.
  • Random search lands within 0.3 decades of the optimal learning rate.
  • Successive halving over 27 configurations spends 108 epochs.
  • Bayesian optimisation finds the minimum of a 1-D function at 0.731 (true 0.73) in 13 evaluations.

Exercise 2: 2 430 epochs each (30 full runs of 81 epochs). The objective:

  • validation loss = final loss + learning-speed term · epochs^(−1/2) + noise;
  • the learning rate matters 12 times more than L2;
  • larger learning rates learn faster, so short runs favour them;
  • the optimum lies between grid points, as it usually does.

Results are the mean regret of the chosen configuration’s final loss over 20 runs:

Strategy Mean regret Median
grid 6 × 5 0.074 0.074
random, 30 trials 0.025 0.023
Bayesian (GP-EI), 30 trials 0.005 0.004
successive halving (repeated 81-config brackets) 0.009 0.005
Hyperband 0.010 0.007
  • Random search is 3× better than the grid, because it tries 30 learning rates instead of 6.
  • With full-length trials, Bayesian optimisation is best: the objective is smooth and two-dimensional, the setting it was designed for.
  • The multi-fidelity methods come second. They evaluate hundreds of configurations, but their early rankings are biased toward fast-learning large learning rates, which costs accuracy. Their advantage grows when early results are more predictive or when the search space is larger.
🐍 Exercise 2 (research): tuning with a fixed compute budget
Give grid search, random search, Gaussian-process Bayesian optimisation, repeated successive halving and Hyperband the same 2 430-epoch budget on a learning-curve objective whose short-budget rankings are biased; compare the regret of the configurations they select over 20 runs.

Hyperparameter tuning in AI, robotics and aerospace

  • Controller tuning: Bayesian optimisation of PID or MPC gains on hardware, where each trial is a costly flight or drive test.
  • Perception networks: ASHA/Hyperband pruning on GPU clusters.
  • Simulation surrogates: tuning kernel and regularisation parameters of Gaussian-process or neural surrogates.
  • AutoML: Auto-sklearn and AutoGluon combine Bayesian optimisation, meta-learning and ensembling.

References

  1. J. Bergstra, Y. Bengio (2012). “Random search for hyper-parameter optimization.” JMLR 13.
  2. J. Snoek, H. Larochelle, R. P. Adams (2012). “Practical Bayesian optimization of machine learning algorithms.” NeurIPS.
  3. L. Li, K. Jamieson, G. DeSalvo, A. Rostamizadeh, A. Talwalkar (2018). “Hyperband: a novel bandit-based approach to hyperparameter optimization.” JMLR 18.
  4. S. Falkner, A. Klein, F. Hutter (2018). “BOHB: robust and efficient hyperparameter optimization at scale.” ICML.
  5. T. Akiba et al. (2019). “Optuna: a next-generation hyperparameter optimization framework.” KDD.