Bayesian Optimization for Hyperparameter Tuning

How Bayesian optimization uses surrogate models and acquisition functions to find good hyperparameters with far fewer trials than grid search.

Fundamentals

Surrogate Models

Acquisition Functions

Batching and Parallelism

Use q-EI or fantasizing for batch selection; prefer asynchronous schedulers to avoid stragglers.

Warm Starts and Space Design

Seed with random/Sobol samples, use log scales for rates, and encode conditionals explicitly.

How the Algorithm Works

Algorithm

  1. Initialize with random/Sobol samples
  2. Fit surrogate model to observed (x, y)
  3. Optimize acquisition to propose new x
  4. Evaluate objective; update dataset
  5. Repeat until budget or convergence

Acquisition Choices

Batching and Async

Use q-EI or fantasizing; asynchronous BO avoids idle workers by proposing without waiting for all evaluations.

Real-World Applications

Domains

Constraints and KPIs

Latency, memory, and fairness constraints; KPIs include accuracy, calibration, and cost.

Best Practices

Checklist

Anti-Patterns

Evaluation

Protocols

Worked Examples

Optuna + TPE

study = optuna.create_study(direction="maximize", sampler=optuna.samplers.TPESampler())
study.optimize(objective, n_trials=200)

BoTorch + q-EI

# Build GP, use q-EI to propose batch candidates

Implementation

Optuna

study = optuna.create_study(direction="maximize", sampler=optuna.samplers.TPESampler())
study.optimize(objective, n_trials=100)

BoTorch

# GP surrogate with q-EI acquisition and batch suggestions

SMAC

# Tree-based surrogates for categorical-heavy spaces

Patterns

The Math Behind It

Gaussian Process Posterior

Given prior f ~ GP(m, k) and noisy observations y = f(x)+ε, the posterior mean and variance admit closed forms; kernels (Matern, RBF) control smoothness.

TPE

TPE models p(x|y) via l(x) and g(x) densities over good and bad observations, choosing x to maximize l(x)/g(x).

Acquisitions

EI integrates expected improvement over the posterior; UCB uses mean+β·std; PI integrates tail probability beyond best f*.

Batch Selection

q-EI and fantasizing enable parallel proposals by integrating over pending evaluations.

Training Strategy

Budgets

Stability

Frequently Asked Questions

When does BO shine?

Expensive evaluations and moderate noise.

When to avoid?

Very high-d spaces with heavy noise.

What about categorical variables?

Prefer TPE or tree-aware surrogates.

How to de-duplicate?

Enforce minimum distance between suggestions.

How to parallelize?

Batch acquisitions or async BO.

How to stop?

Stabilized best and narrow uncertainty.

How to implement?

Optuna/BoTorch/SMAC provide robust frameworks.