AnyLearn
All lessons
AIadvanced

Optimizing an Expensive Black Box

Some functions cost hours or dollars to evaluate once, give no gradient, and must be optimized in as few tries as possible. This lesson sets up that problem, shows why grid and random search waste the budget, and introduces the surrogate-model loop that Bayesian optimization is built on.

Updated · AI-authored, review-gated · how lessons are made

Not signed in: your progress and quiz score won't be saved.
Progress1 / 9

When one evaluation is expensive

Most optimization you meet assumes evaluations are cheap and gradients are available: run the function millions of times, follow the slope downhill. A large class of real problems breaks both assumptions.

Training a neural network to score one hyperparameter setting can take hours on a GPU. Measuring the yield of one chemical recipe means running a physical experiment. Simulating one aircraft wing takes a compute cluster overnight. In each case a single evaluation is expensive, so your entire budget might be fifty or a hundred tries, not millions.

These functions are also black boxes. You put in a configuration and get out a number, with no formula to differentiate and no gradient to follow. Bayesian optimization is the standard method for exactly this regime: derivative-free, expensive, and evaluation-limited.

Full lesson text

All 9 steps on one page, for reading, reference, and search.

Show

1. When one evaluation is expensive

Most optimization you meet assumes evaluations are cheap and gradients are available: run the function millions of times, follow the slope downhill. A large class of real problems breaks both assumptions.

Training a neural network to score one hyperparameter setting can take hours on a GPU. Measuring the yield of one chemical recipe means running a physical experiment. Simulating one aircraft wing takes a compute cluster overnight. In each case a single evaluation is expensive, so your entire budget might be fifty or a hundred tries, not millions.

These functions are also black boxes. You put in a configuration and get out a number, with no formula to differentiate and no gradient to follow. Bayesian optimization is the standard method for exactly this regime: derivative-free, expensive, and evaluation-limited.

2. Why grid search wastes the budget

The reflex for tuning a few knobs is grid search: pick several values per knob and try every combination. It has two fatal properties when evaluations are expensive.

First, it is exponential. Five values across four hyperparameters is 625 evaluations before you have looked anywhere in between. Second, and less obvious, it spends the budget uniformly even though most knobs barely matter.

James Bergstra and Yoshua Bengio showed this precisely in their 2012 paper "Random Search for Hyper-Parameter Optimization". Because typically only a few hyperparameters actually affect the result, a grid wastes most of its trials varying the ones that do not. Random search, drawing each configuration at random, covers the important dimensions far better for the same budget, and became the recommended baseline overnight.

3. The ceiling that random search hits

Random search fixes grid search's worst habit, but it has a ceiling of its own: every query is chosen with no memory of the previous ones. The hundredth sample is drawn exactly as blindly as the first, ignoring everything you have learned about where good regions lie.

That is the opening Bayesian optimization exploits. If evaluations are expensive, it is worth spending real computation between them to decide where to look next. A few seconds of modelling is negligible next to a six-hour training run, so you can afford to think hard about each query.

The idea is to build a model of the objective from the evaluations so far, and let that model propose the next point. The model is cheap to query; the true function is not. Every observation makes the model sharper.

4. The surrogate loop

Bayesian optimization is a loop with two moving parts.

The surrogate is a cheap probabilistic model fitted to the evaluations so far. It predicts the objective everywhere, and crucially it also reports how uncertain that prediction is.

The acquisition function turns those predictions into a score for every candidate point, encoding a policy for what to try next. It is cheap, so you can optimize it thoroughly to pick the single most promising point. You then pay for one expensive evaluation there, add the result, and repeat.

This structure is what Bobak Shahriari and colleagues called "taking the human out of the loop" in their 2016 review: the loop itself decides where to sample, replacing a human squinting at results between runs.

flowchart LR
A["Evaluations so far"] --> B["Fit a surrogate model"]
B --> C["Acquisition function scores candidate points"]
C --> D["Pick the best candidate"]
D --> E["Evaluate the true expensive function"]
E --> A

5. Why uncertainty is the whole point

A plain regression model would predict the objective and stop there. That is not enough, because a point predicted to be mediocre might be somewhere the model has simply never looked, and could hide the true optimum.

So the surrogate must be probabilistic: at every point it returns not just a best guess but a measure of how unsure it is. Uncertainty is high in unexplored regions and low near points you have already evaluated.

That single extra quantity is what lets the loop balance two competing urges. Exploitation samples where the predicted value is best. Exploration samples where the uncertainty is highest, in case something better is hiding. An optimizer that only exploits gets stuck in the first decent region it finds; one that only explores never converges. The next two lessons are, in effect, how to model uncertainty and how to trade it off.

6. A concrete walk through the loop

Suppose you are tuning a single hyperparameter, a learning rate, and each trial is one training run.

You start with three random rates and their validation scores. The surrogate fits a curve through them with a confidence band: narrow at the three points, wide in the gaps. The acquisition function scans the whole range and finds that a rate between two of your points has a promising predicted score and meaningful uncertainty, so it scores highest there. You run that one trial.

The new result collapses the uncertainty around that rate. Maybe it was excellent, and the model now concentrates there; maybe it was poor, and the model rules the region out and sends the next query elsewhere. Either way you spent one expensive run and learned the most any single run could have told you.

7. Where this actually gets used

The technique earns its keep wherever evaluations are the bottleneck.

The best-known application is tuning machine-learning models: learning rates, regularization strengths, architecture choices, where each configuration costs a full training run. It also drives experimental design in the physical sciences, choosing the next chemical, alloy, or material to synthesize when each experiment takes days in a lab. Engineers use it to tune simulators, and roboticists to tune control policies where each trial risks hardware.

The common thread is never the number of dimensions or the shape of the function. It is the cost per evaluation. If a single measurement is slow, costly, or risky, and you have no gradient, this is the family of methods to reach for.

8. What Bayesian optimization is not for

The method is a specialist, and using it in the wrong regime just adds overhead.

If evaluations are cheap, the surrogate machinery is pure waste. Running the true function a million times is fine, so random search or an evolutionary method will beat the modelling cost. If you have gradients, as you do inside a differentiable model, gradient descent is enormously more efficient than treating the function as a black box.

SituationReach for
Cheap evaluations, no gradientRandom or evolutionary search
Gradient availableGradient descent
Expensive, no gradient, few dimensionsBayesian optimization
Thousands of dimensionsSpecialized methods; standard BO struggles

Standard Bayesian optimization also fades as dimensionality climbs into the hundreds, because uncertainty becomes hard to estimate when data is sparse in a huge space. The comfortable zone is roughly up to a few dozen dimensions with a tight evaluation budget.

9. The two questions ahead

You now have the shape of the method: a loop that fits a probabilistic surrogate to past evaluations and uses an acquisition function to choose the next expensive query. Everything specific reduces to two design choices.

First, what surrogate models the objective and its uncertainty? The dominant answer is the Gaussian process, which gives a principled predicted mean and variance at every point, and is the subject of the next lesson. It assumes some familiarity with distributions; the probability-and-statistics-for-ml cursus covers that groundwork.

Second, how does the acquisition function convert mean and uncertainty into a single decision about where to look? That is the third lesson, and it is where the exploration-exploitation trade-off, familiar from the bandit setting in reinforcement-learning-foundations, gets made concrete.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. What defines the regime Bayesian optimization is designed for?
    • Functions with millions of cheap evaluations and known gradients
    • Functions where each evaluation is expensive, no gradient is available, and the budget is small
    • Functions with thousands of input dimensions
    • Convex functions with a single global optimum
  2. According to Bergstra and Bengio (2012), why does random search beat grid search for hyperparameter tuning?
    • Random search evaluates fewer points in total
    • Grid search cannot handle continuous hyperparameters
    • Random search follows the gradient of the objective
    • Only a few hyperparameters usually matter, and a grid wastes trials varying the ones that do not
  3. What ceiling does random search hit that Bayesian optimization is meant to break?
    • Each query is chosen with no memory of previous results
    • It cannot evaluate expensive functions
    • It requires the gradient of the objective
    • It only works in one dimension
  4. Why must the surrogate model report uncertainty, not just a predicted value?
    • To make the surrogate cheaper to evaluate
    • Because the true function has no gradient
    • So the loop can explore regions it has not sampled, where the true optimum might hide
    • To reduce the number of hyperparameters
  5. In which situation is Bayesian optimization the wrong tool?
    • Tuning a neural network where each trial is a full training run
    • Choosing the next expensive chemistry experiment
    • Optimizing a differentiable function where the gradient is available
    • Optimizing a black-box simulator with a budget of 60 runs

Related lessons

AI
intermediate

Streams, Actions, Rewards, and Thinking That Is Not Ours

The paper is concrete about what an experiential agent would differ on, and names four: it lives in a continuous stream rather than episodes, acts in the world rather than emitting text, takes rewards from grounded signals rather than human judgement, and plans in terms it worked out rather than imitating human chain of thought. This lesson works through each.

8 steps·~12 min
AI
intermediate

The Argument: Why Learning From Us Runs Out

David Silver and Richard Sutton argue that the current approach has a ceiling built into it, because a system trained to predict what humans wrote is aiming at human performance by construction. This lesson works through their three eras, the claim about data exhaustion, why they think superhuman performance needs a different learning signal, and the honest counter-arguments.

8 steps·~12 min
AI
advanced

Outliers: Why Large Models Resist Naive Quantization

Round-to-nearest works on small models and falls apart on large ones, because beyond a certain scale transformers grow systematic activation outliers in a few fixed channels, a hundred times larger than everything else. This lesson shows arithmetically why one outlier destroys a tensor, then works through the three families of fix: decompose, smooth, and rotate.

10 steps·~15 min
AI
advanced

What Quantization Actually Does to a Number

Decoding is memory-bandwidth-bound, so fewer bits per weight means more tokens per second, not fewer. This lesson builds the mechanism from the arithmetic up: the affine mapping, a worked example done by hand, why group size costs fractional bits, the difference between W4A16 and W8A8, and which formats the hardware actually accelerates.

10 steps·~15 min