Sub topic 7
How the TPE sampler works
This chapter assumes you can write an Optuna objective function, define a search space with suggest_* methods, and explain what a trial is. It explains what Optuna's default sampler does between your study.optimize call and the parameters your objective receives.
The idea behind TPE
TPE stands for Tree-structured Parzen Estimator. It is a Bayesian-style optimiser, but it does not model the score as a function of the parameters. It models the parameters as a function of the score.
After each finished trial, TPE splits the history into two groups:
- Good trials: the best fraction, ranked by objective value.
- Bad trials: all the others.
For each group it fits a density estimate over the parameter values. Call the good-group density l(x) and the bad-group density g(x). The sampler then favours values of x where l(x) / g(x) is large. Those are values that appear often among good trials and rarely among bad ones.
In practice, TPE draws several candidate values from l(x). It scores each by the ratio and returns the best one as the next suggestion.
The "tree-structured" part
The tree refers to the search space itself. In a define-by-run objective, some parameters exist only when an earlier choice selects them. For example, svc_c is only suggested when the classifier is "svc". TPE handles this by modelling each parameter using only the trials in which it was suggested. That is why conditional spaces work without extra configuration.
What happens at the start
A density estimate needs data. For the first trials, TPE samples at random. The default is 10 random startup trials, controlled by n_startup_trials. After that, it uses the two-group model.
import optuna
sampler = optuna.samplers.TPESampler(
seed=42,
n_startup_trials=10,
)
def objective(trial):
x = trial.suggest_float("x", -10, 10)
y = trial.suggest_float("y", 1e-4, 1, log=True)
return (x - 2) ** 2 + (y - 0.01) ** 2
study = optuna.create_study(direction="minimize", sampler=sampler)
study.optimize(objective, n_trials=60)
print(study.best_params)
print(study.best_value)You can see the switch in the trial history. The first 10 trials are spread across the space. Later trials cluster near good regions.
Tuning TPE
The defaults work for most problems. These options are the ones worth knowing.
| Option | Effect |
|---|---|
| `n_startup_trials` | Number of random trials before modelling starts. |
| `n_ei_candidates` | Number of candidates drawn from `l(x)` per suggestion. |
| `multivariate` | Models parameters jointly, so interactions between them are captured. |
| `seed` | Makes the sampler's random choices repeatable. |
Setting multivariate=True helps when parameters interact, such as learning rate and batch size. The default models each parameter independently.
Gotchas
- TPE is sequential by nature. In parallel runs, workers start trials before earlier ones finish, so the model sees less history. Gains per trial are somewhat smaller.
- Noisy objectives blur the good/bad split. A lucky trial can pull the good-group density toward a poor region.
- The method needs a meaningful ordering of values. Categorical parameters work, but they are modelled by frequency among good trials rather than by distance.
Exercise
A study has run 5 trials with the default TPESampler. Is the sixth trial chosen by the good/bad density ratio? Why or why not?
Show answer
No. The default sampler uses 10 random startup trials, so the sixth trial is still sampled at random. Density modelling begins once the startup trials are complete.