Hyperparameter Optimization
Advanced 16 min read Hyperparameter Optimization

Bayesian Optimization

Choosing the next trial intelligently

Bayesian optimization builds a surrogate model of the score surface and picks the next config to maximize information per trial. It squeezes the most from expensive evaluations—ideal when each fit costs minutes or hours.

bayesian optimizationsurrogate modelacquisition functionGPTPEOptuna
Prerequisites: Random Search
Before we start

What you will learn

  • Describe the surrogate + acquisition loop
  • Explain exploration-exploitation in sampling
  • Use Optuna-style Bayesian search on expensive models
Introduction

What is this?

Bayesian optimization frames tuning as sequential decision making. It keeps a surrogate model — typically a Gaussian process or a Tree-structured Parzen Estimator (TPE) — that predicts score as a function of configuration, including uncertainty. An acquisition function (expected improvement, UCB, EI) scores candidate next configs by balancing 'where the surrogate says the best config lives' (exploitation) against 'where we're most uncertain' (exploration), and the winner is evaluated, folded back into the surrogate, and the loop repeats.

Motivation

Why do you need it?

  • Expensive models (deep nets, big GBDTs) can't afford thousands of trials — Bayesians make each one count.
  • Correlated knobs are exploited naturally (learning rate and depth interact).
  • Uncertainty is explicit: you know when you're guessing vs done.
  • It typically beats random search 2–5× in trials-to-optimum on smooth surfaces.
Think about it

Mental model

Imagine

A treasure hunt with noise on the metal detector

Think of

Bayesian optimization

You don't grid-search the whole beach. You map readings (surrogate), pick the most promising-but-uncertain spot (acquisition), dig there, update your mental map, and repeat — exploring new beaches only when signal is thin.

Mechanics

How it works

  • Evaluate a handful of random-start configs to seed surrogate uncertainty.
  • Fit the surrogate: score ≈ f(config) with mean and variance predictions.
  • Maximize the acquisition function (e.g., expected improvement) over the space to pick the next config.
  • Evaluate that config (CV or validation), record score, add to history.
  • Refit the surrogate and repeat until the budget or a tolerance bound is hit.

The Bayesian optimization loop.

Seed
Surrogate
Acquisition
Evaluate
Update
Real world

In production

  • A deep-learning team tunes lr, batch size, warmup and weight decay with Optuna — ~60 trials instead of grid's ~1,300.
  • An AutoML service uses TPE sampling because tree-based surrogates handle mixed categorical/continuous spaces.
  • A recurring monthly retrain never needs a full search: the warm-started surrogate re-tunes around the previous optimum.
Guidelines

Best practices

  • Start with the previous best config in the seed set — warm-starting beats cold search.
  • Bound the space to plausible regions; wide spaces just slow surrogate refinement.
  • Let the surrogate see log-transformed knobs so distances make sense (learning rate magnitudes).
  • Add a pruning racing layer (Hyperband-style) when evaluations are trainable iteratively.
Watch out

Common mistakes

  • Using Bayesian search on cheap models where random/grid would finish first — overhead dwarfs savings.
  • Forgetting to transform log-scale knobs, so the surrogate fits a much harder landscape.
  • Over-trusting EI with a tiny surrogate: sample variance collapses too fast and it exploits a false peak.
  • Interpreting the surrogate's predicted best as guaranteed — it's predicted, not measured.
Root cause analysis

Common issues

Error
Bayesian search hits a plateau lower than a known random-search result
Root cause

Surrogate exploited a spurious early peak; exploration didn't rescue

Fix

Lower exploration temperature (increase EI exploration weight), re-seed with more random starts, and verify ranges.

Error
Optimization takes longer than the model trains
Root cause

Surrogate refit overhead dominates the cheap evaluation

Fix

Use a cheaper surrogate (TPE over GP), or just use random search — the model is cheap enough.

When things break

Debugging tips

  • Plot history of observed scores — each trial should mostly land near-or-better than the previous best.
  • If acquisition keeps picking boundary values, your ranges are too tight; widen and re-seed.
  • Check surrogate predictions vs actuals on a holdout slice of trials to detect surrogate pathology.
  • Keep a fixed random-search comparison: if Bayesian isn't beating it, the surface may be noise-dominated — switch budget to folds.
Interviews

Interview checkpoint

Practice

Hands-on lab

Optuna: Fewer Trials, Same Winner
30 min · advanced
In progress
Objectives
  • Define a tuning study with log-uniform knobs
  • Let Optuna's TPE pick trials sequentially
  • Beat a random-search baseline at equal budget

Optuna: Fewer Trials, Same Winner

30 min · advanced

0/4 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

The acquisition function balances:

2

Bayesian optimization always beats random search.

Go deeper

Official documentation