Hyperparameter Optimization
Grid, random search, Bayesian optimization and Hyperband.
Learning rates, penalties, depths, widths: most models have settings that training does not learn, and the right values can matter as much as the choice of model. Tuning them is optimization with an objective that is noisy, expensive and gradient-free. This lesson covers the main strategies, from random search to Bayesian optimization and Hyperband, and the trap they share: overfitting the validation set.
What you’ll learn
- Formulate hyperparameter optimization as noisy, expensive black-box (and bilevel) optimization, and explain why random search beats grid search when only a few hyperparameters matter.
- Derive the expected-improvement acquisition function, and compare Gaussian-process, tree-Parzen and random-forest surrogates.
- Compute the budgets of successive halving and Hyperband, and state the assumption that makes early stopping of bad configurations safe.
- Report tuned results honestly: equal tuning budgets for baselines, held-out assessment, and the optimism of the best validation score.
The problem
A configuration fixes everything training does not learn: continuous values (learning rate, penalty), integers (depth, width), categorical choices (optimizer, kernel) and conditional ones (the momentum only matters if the optimizer uses it). Hyperparameter optimization seeks
a bilevel problem: the inner problem is training, the outer one is validation.
| Property of | Consequence |
|---|---|
| each evaluation is a full training run | budgets of tens to thousands of evaluations; every query must count |
| noisy: seeds, data order and the validation sample all move it | the best observed value is optimistic; differences smaller than the noise mean nothing |
| no gradient in general (and none at all for discrete choices) | black-box methods; gradient-based tuning exists for continuous hyperparameters by differentiating through training, at a cost in memory or approximation |
| cheap approximations exist: fewer epochs, a data subset, a smaller model | multi-fidelity methods spend most of the budget on promising configurations |
Search spaces matter as much as search algorithms. Scale parameters such as learning rates and penalties should be searched on a log scale (sample uniformly), since and differ as much as and . And the evaluation protocol of Validation & Cross-Validation applies unchanged: the objective is a validation estimate, so the final assessment needs data the search never touched.
Grid versus random search
Grid search evaluates the Cartesian product of a few values per hyperparameter; random search samples configurations independently from a distribution over . Bergstra & Bengio (2012) argued that random search is more efficient because of low effective dimensionality: for a given dataset, "only a few of the hyper-parameters really matter," and which ones matter changes from dataset to dataset.
With a budget of trials in dimensions, a grid tries only distinct values of each hyperparameter, while random search tries distinct values of every one. If only one hyperparameter matters, the grid wastes most of its budget re-testing the same few values of it.
Suppose the configurations within the best fraction of the search space (measured by the sampling distribution) are all good enough. Each random trial lands there with probability , independently of the others, so
This does not depend on the dimension of , only on the volume fraction . Requiring probability at least :
How many independent random trials do you need so that, with probability at least 95%, at least one lands in the best 5% of the search space?
Show the answer
59 trials
Need , so : 59 trials (, while 58 gives ). The "top 5%" is measured by the sampling distribution, so a poorly chosen range (or a linear scale for a learning rate) can make the good region tiny.
Bayesian optimization
Bayesian optimization uses the evaluations so far. It fits a probabilistic surrogate of to the observed pairs, and chooses the next configuration by maximizing an acquisition function that trades exploitation (low predicted loss) against exploration (high uncertainty). The most common acquisition is expected improvement over the best value observed so far, popularized for expensive black boxes by Jones, Schonlau & Welch (1998).
Let the surrogate's posterior at be . The improvement is . Write with , and ; improvement is positive exactly when :
Use and :
EI rises as the predicted mean falls () and as the uncertainty grows (): both exploitation and exploration pay.
The best validation error so far is . The surrogate predicts three candidates: A with , ; B with , ; C with , . Which one has the highest expected improvement?
- A
- B, the only one predicted to beat
- C
- They are equal
Show the answer
C. C
EI is about for A, for B and for C. C's mean is worst, but its uncertainty leaves a real chance of a large improvement; B is predicted to improve, but only by about and with almost no chance of more. That is exploration, quantified.
| Surrogate | Models | Good at |
|---|---|---|
| Gaussian process (Snoek, Larochelle & Adams, 2012) | , with a Matérn 5/2 kernel, GP hyperparameters integrated out, cost-aware and parallel variants | few continuous hyperparameters and small budgets |
| Tree-structured Parzen estimator (Bergstra, Bardenet, Bengio & Kégl, 2011) | two densities, for good trials () and for the rest; EI is maximized by maximizing | conditional and mixed spaces, cheap updates |
| Random forest, SMAC (Hutter, Hoos & Leyton-Brown, 2011) | from the spread of a forest's predictions | categorical and conditional hyperparameters, many observations |
Multi-fidelity: successive halving, Hyperband, population-based training
Most bad configurations reveal themselves early. Successive halving (Jamieson & Talwalkar, 2016, building on the sequential halving of Karnin, Koren & Somekh, 2013, for best-arm identification) starts configurations with a small budget each (epochs, say), keeps the best of them, multiplies their budget by , and repeats until one is left. Each round costs about , so the total is about instead of full runs.
| Rung | Configurations | Epochs each | Epochs in rung |
|---|---|---|---|
| 1 | 81 | 1 | 81 |
| 2 | 27 | 3 | 81 |
| 3 | 9 | 9 | 81 |
| 4 | 3 | 27 | 81 |
| 5 | 1 | 81 | 81 |
The catch is the choice of versus : many configurations with tiny budgets risk killing slow starters, few with large budgets waste effort on bad ones. Hyperband (Li, Jamieson, DeSalvo, Rostamizadeh & Talwalkar, 2018) hedges by running several brackets of successive halving that trade against ; with a maximum of 81 epochs and the brackets start , the last being plain random search. BOHB (Falkner, Klein & Hutter, 2018) replaces Hyperband's random sampling with a TPE-style model, aiming at Hyperband's fast start and Bayesian optimization's strong final performance.
Population-based training (Jaderberg et al., 2017) trains a population in parallel; periodically, poor members copy the weights and hyperparameters of better ones (exploit) and perturb them (explore). The result is a schedule of hyperparameters rather than fixed values.
Successive halving can discard the configuration that would have been best after full training. When is this most likely?
- When the validation set is large
- When early validation performance ranks configurations poorly, for example a low learning rate that starts slowly but finishes best
- When is small, such as 2
- When the number of configurations is a power of
Show the answer
B. When early validation performance ranks configurations poorly, for example a low learning rate that starts slowly but finishes best
Early stopping is safe only if low-fidelity results are correlated with final results. Configurations whose curves cross (slow starters, schedules with a late learning-rate drop) are exactly the ones a multi-fidelity method misjudges, which is why Hyperband also runs brackets with larger starting budgets.
Overfitting the validation set
The best validation score after a search is the minimum of many noisy estimates, so it is optimistic: by up to for configurations whose scores have noise , and by times the expected maximum of standard normals when the noise is independent and Gaussian (Cross-Validation: Theory & Pitfalls). With 1,000 configurations of equal quality and noise of half a point, the winner looks about 1.6 points better than it is. Searches that compare configurations on cheap, noisy, low-fidelity evaluations add further noise to that selection.
Tuning also confounds comparisons between methods. Melis, Dyer & Blunsom (2018) re-tuned several language-model architectures with large-scale black-box hyperparameter search and found that "standard LSTM architectures, when properly regularised, outperform more recent models." A new method tuned for weeks against a baseline tuned for an afternoon measures the tuning budget, not the method.
| Practice | Why |
|---|---|
| assess the final configuration on untouched data, or with nested cross-validation | the search's own best score is selection-biased |
| re-evaluate the top few configurations with more seeds or folds before choosing | separates real gains from lucky draws |
| give every compared method the same search space size and budget, and report them | otherwise the comparison measures tuning effort |
| use random search as a baseline for any tuner | it is simple, parallel and often hard to beat by much |
Key takeaways
- Hyperparameter optimization is noisy, expensive, mostly gradient-free bilevel optimization; search scale parameters on a log scale and keep final assessment separate.
- Random search beats grids under low effective dimensionality: trials hit the top fraction with probability , whatever the dimension.
- Bayesian optimization fits a surrogate (GP, TPE or random forest) and maximizes an acquisition such as , which rewards both low predicted loss and uncertainty.
- Successive halving starting at of a full budget screens configurations for the cost of about full runs, assuming early results rank configurations well; Hyperband hedges across brackets, BOHB adds a model, and population-based training learns schedules.
- The best validation score after a search is optimistic, and unequal tuning budgets confound method comparisons; assess on untouched data.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
You have 16 trials and two hyperparameters, of which only one actually affects the validation loss. How many distinct values of the important one do a grid and 16 random trials test?
- Grid 16, random 16
- Grid 4, random 16
- Grid 4, random 4
- Grid 16, random 4
Show the answer
B. Grid 4, random 16
The grid repeats each of its 4 values of the important hyperparameter 4 times, varying only the irrelevant one. Random trials almost surely give 16 different values of each. That is Bergstra and Bengio's argument for random search under low effective dimensionality.
In the expected-improvement formula (minimization), what happens to EI if a candidate's predicted mean stays the same but its predictive uncertainty grows?
- EI falls, because uncertain candidates are risky
- EI is unchanged, because it depends only on the mean
- EI grows, because
- EI becomes negative when the mean is worse than
Show the answer
C. EI grows, because
Improvement is , a convex function of , so spreading the distribution of raises its expectation. That built-in reward for uncertainty is what makes EI explore.
Why does Hyperband run several brackets of successive halving instead of one?
- To hedge between many configurations with small budgets and few with large budgets, since the right trade-off is unknown
- To average out the noise of a single bracket
- Because successive halving needs the number of configurations to be a power of
- To fit a Bayesian surrogate model across brackets
Show the answer
A. To hedge between many configurations with small budgets and few with large budgets, since the right trade-off is unknown
Aggressive brackets screen many configurations cheaply but may kill slow starters; conservative ones give fewer configurations more budget, down to plain random search. Running all of them costs only a logarithmic factor and protects against either failure mode.
After 500 trials of Bayesian optimization, your best configuration scores 91.8% on the validation set. What should you report as its expected accuracy on new data?
- 91.8%, the measured value
- 91.8% plus a correction, since validation sets are pessimistic
- The average validation score of all 500 trials
- Its score on a test set (or outer cross-validation folds) that the search never used
Show the answer
D. Its score on a test set (or outer cross-validation folds) that the search never used
91.8% is the maximum of 500 noisy estimates and therefore biased upward. An independent assessment, used once, is the honest number.
Further reading
- J. Bergstra & Y. Bengio, "Random search for hyper-parameter optimization," Journal of Machine Learning Research 13, 281–305 (2012).
- J. Snoek, H. Larochelle & R. P. Adams, "Practical Bayesian optimization of machine learning algorithms," Advances in Neural Information Processing Systems 25 (2012).
- L. Li, K. Jamieson, G. DeSalvo, A. Rostamizadeh & A. Talwalkar, "Hyperband: a novel bandit-based approach to hyperparameter optimization," Journal of Machine Learning Research 18(185), 1–52 (2018).
- S. Falkner, A. Klein & F. Hutter, "BOHB: robust and efficient hyperparameter optimization at scale," Proceedings of the 35th International Conference on Machine Learning, PMLR 80, 1437–1446 (2018).
- M. Feurer & F. Hutter, "Hyperparameter optimization," in F. Hutter, L. Kotthoff & J. Vanschoren (eds.), Automated Machine Learning: Methods, Systems, Challenges (Springer, 2019), 3–33.