TL;DR
Bayesian optimization looks for the input that gives the best output of a function that is slow or costly to evaluate, such as a model's validation score for one choice of settings. It keeps a model of the function, usually a Gaussian process, that gives a best guess and an uncertainty at every input. Each new trial is placed where an Acquisition Function is highest, which favours inputs that look good or are still unclear. The model is refitted after every trial, so each choice uses everything learned so far.
Builds on: Gaussian Process Regression, Acquisition Function, Bayes' Rule · Leads to: Expected Improvement, Global Optimization
Where it's used
Tuning machine learning models. Training a large model with one set of hyperparameters can take hours. Learning rate, weight decay and layer width interact in ways no formula gives. Snoek and colleagues (2012) applied Bayesian optimization to tune convolutional networks and latent Dirichlet allocation models, which is the pattern many hyperparameter tuning libraries now follow.
Designing experiments in chemistry and materials. Each synthesis run is expensive and slow, and the aim is a material with a target property. A model of the property over the recipe space proposes the next synthesis, so fewer experiments reach a good material. Many automated laboratory workflows use this loop, with each measurement added to the model before the next run.
Choosing a configuration of a costly simulator. Engineering simulations, such as a car body's drag or a reactor's yield, can take minutes to days per run. A model of the output, fitted to the few runs done so far, tells the engineer where the next run is most useful, and where the optimum probably lies.
How it works
Take a one-number input between 0 and 1. The unknown function is what we want to maximise, and each evaluation is expensive. Suppose the best value seen so far is , from one trial at .
-
Fit a model to the trials. A Gaussian process is fitted to all trials so far. At each candidate it gives a mean , the best guess for , and a standard deviation , how unsure it is. Far from any trial, is large. Near a trial, is small, because the model has seen that region.
Check: at the trial point itself the uncertainty is almost zero, so the model never proposes repeating a point it already knows well.
-
Predict at a few candidate points. Suppose the fitted model gives these numbers:
Candidate Mean Standard deviation 0.25 0.40 0.30 0.50 0.60 0.40 0.75 0.85 0.10 Check: the point at 0.75 has the best mean but the smallest uncertainty. The point at 0.25 is uncertain but its mean is low.
-
Score each candidate with an acquisition function. The expected-improvement score, which Expected Improvement explains in full, asks how much better than the value at is likely to be. Its formula is with , where is the standard normal distribution function and its density. Computed for each row, it gives about 0.006 at , about 0.052 at , and about 0.020 at .
Check: the best score is at 0.50, the point with a moderate mean and a moderate uncertainty. The point at 0.75 looks good but is already well known to be close to 0.85, so little is likely to be gained.
-
Run the trial with the highest score. The next evaluation is at . Suppose it returns . This is below , so the best value stays 0.9, but the model now knows more about the middle of the range.
Check: the trial count went from 1 to 2, and the best-so-far value did not change.
-
Refit and repeat. The Gaussian process is refitted with two trials. Its predictions change near 0.5, and the scores change with them. The loop continues until the budget of trials is used up, or until the scores are so small that further trials are unlikely to help.
Check: each pass costs one expensive evaluation plus a cheap model fit, which is the reason the method suits slow functions.
flowchart LR D[trials so far] --> GP[fit Gaussian process] GP --> AF[score candidates with acquisition function] AF --> T[run trial at best candidate] T --> D
The math
A Gaussian process gives a joint distribution over the function values at any set of inputs. Conditioning on the trials, as described in Gaussian Process Regression, gives a normal distribution at each new input :
The formula for and comes from the Kernel Function and the observed values, and it is what the table in step 2 reports.
Step 2 in symbols: the predictive mean and spread
The mean and the standard deviation are the two numbers the acquisition rule needs at each candidate. Write for the standard normal density and for the standard normal Cumulative Distribution Function. Both are standard functions, and the Gaussian (Normal) Distribution lesson gives their shape.
Step 3 in symbols: expected improvement
For maximisation, with normal with mean and standard deviation , the expected gain over the best value is
The first term rewards a high mean, and the second rewards a large spread. When is zero, the score is . Both terms are needed: a high mean with no spread is already known, and a large spread with a low mean is unlikely to beat the best value.
Step 4 in symbols: the loop
Let be the trials, where plus noise. The loop is
The means "the input at which the score is largest". In practice this inner maximisation is itself a cheap numerical search over many candidate points.
Worked examples
Take , and .
- Compute .
- Compute .
- Look up and .
- First term: .
- Second term: .
- Add them: , about 0.153.
- Check the sign: the expected gain cannot be negative, and 0.153 is positive. The uncertainty of 0.5 is what lets the candidate still promise a gain, even though its mean is below the best value.
Use the table from step 2, with .
- For : , , . Then .
- For : , , . Then .
- For : , , . Then .
- The largest score is 0.052, at . Choose that point for the next trial.
- Note what this shows. The point at 0.75 has the best mean, but its uncertainty is small, so it can gain little. The point at 0.50 is less promising on average but far more uncertain, so it has the better chance of a surprise. The score trades these off.
A team tunes two hyperparameters, the learning rate on a log scale and the weight decay, for a model that takes four hours to train. After 12 trials the best validation accuracy is 0.82. At a candidate pair of settings, the Gaussian process predicts mean accuracy 0.80 and standard deviation 0.05.
- Compute the gap .
- Compute .
- Look up and .
- Compute the first term: .
- Compute the second term: .
- Add: .
- Interpret. The expected gain from one more four-hour run at this pair is about 0.012 accuracy points, measured in the model's own belief. The team compares this with the score at other pairs before spending the run.
Try it: a candidate has , , and . Compute the expected improvement to two decimal places. Answer: 0.01, as in step 7 above, because , which rounds to 0.01.
Common mistakes and tradeoffs
It is tempting to think Bayesian optimization is always better than random search. In fact, with few input dimensions and smooth functions it usually beats random search in fewer trials. With many dimensions, or with trials that are cheap, the model's own cost and its weak fit can cancel the gain, and random search with a large budget is a strong baseline.
It is tempting to think the best predicted point is the best next trial. In fact the acquisition score must also value uncertainty, because a point the model already predicts well cannot teach it much. An optimizer that only follows the mean gets stuck near the first good point it finds.
It is tempting to think the Gaussian process model is correct. In fact it is a belief about smoothness, set by a kernel and its length scale. A wrong kernel gives confident but wrong uncertainty, and the search then explores the wrong places. Refitting the kernel parameters after each trial helps, and checking the predictions against held-out trials shows whether the model is credible.
Use Bayesian optimization when each evaluation is expensive, the number of inputs is small (up to a few dozen continuous dimensions), and the function is reasonably smooth. Do not use it when evaluations are cheap and plentiful, when inputs are mostly categorical, or when the function is very noisy with no repeated trials. Alternatives include random search, which scales well and is simple, and bandit methods such as successive halving for many cheap configurations.
History
In the 1960s, Harold Kushner proposed choosing the next trial of a noisy one-dimensional function by a probability-of-improvement rule. Jonas Mockus and colleagues, in the 1970s, framed the same problem as Bayesian decision-making and introduced expected improvement. For decades the idea was used mainly in engineering design and geostatistics.
It caught on in machine learning in the 2000s and early 2010s, when hyperparameter tuning became expensive. Jones, Schonlau and Welch (1998) gave the widely used "efficient global optimization" method, which pairs a Gaussian process with expected improvement. Snoek, Larochelle and Adams (2012) showed that the approach works for tuning neural networks in practice. The field now includes many variants, but the loop of model, acquisition and trial has stayed the same.
Recap
- The objective is unknown and costly, so it is modelled by a Gaussian process that gives a mean and an uncertainty at each input.
- The Acquisition Function scores each candidate by how promising it looks, combining high means with high uncertainty.
- Expected Improvement is one standard score: the expected gain over the best value seen so far.
- The loop maximises the score, runs one trial, adds it to the data, and refits.
- To read further, go to Expected Improvement for the score in detail, and to Global Optimization for the wider family of methods that search for the best input of a function.
Check yourself
QuestionWhy does the Gaussian process give an uncertainty as well as a mean, and why does Bayesian optimization need both?Show answer
The mean is the best guess for the function at an input, and the uncertainty says how far that guess can be trusted. The acquisition score needs both: a high mean on its own could be a point already well known, and a large uncertainty on its own could be a point that looks poor. The score balances the two, which is what lets the search both exploit and explore.
QuestionComputation: , , . What is the expected improvement to three decimal places?Show answer
Here . Then and . The first term is . The second term is . So the expected improvement is about 0.080.
QuestionWhy is random search a strong baseline for hyperparameter tuning in many dimensions, even though Bayesian optimization is efficient in a few?Show answer
A Gaussian process needs data to model a function, and in many dimensions the trials cover the space too thinly for the model to be reliable. Its cost also grows with the number of trials. Random search needs no model, scales to many dimensions, and covers the space evenly, so with a large budget it often finds settings as good as a poorly fitted model would propose.
QuestionA simulator costs one day per run and you can afford 15 runs over two continuous inputs. Is Bayesian optimization a reasonable choice, and what would you check first?Show answer
Yes, it is reasonable. Two continuous inputs are few, each run is expensive, and 15 runs is too few for a grid but enough for a Gaussian process to give useful predictions. Before starting, check that the output is smooth enough for a Gaussian process, for example by running a few points spread across the range and looking at whether nearby inputs give similar outputs.
Sources
- Snoek, Larochelle, Adams (2012). Practical Bayesian Optimization of Machine Learning Algorithms. https://arxiv.org/abs/1206.2944
- Wikipedia, Bayesian optimization. https://en.wikipedia.org/wiki/Bayesian_optimization