Sub topic 6
Samplers: how Optuna picks the next trial
This chapter assumes you can write an objective function and run a study, as covered earlier. It explains how Optuna chooses the parameter values for each new trial.
What a sampler does
A sampler is the object that proposes parameter values. When your objective calls trial.suggest_float("x", -10, 10), the study asks its sampler for a value. The sampler sees the search space and the finished trials, and it returns a value.
You choose the sampler with the sampler argument of create_study. If you omit it, Optuna uses TPESampler for single-objective studies and NSGAIISampler for multi-objective ones.
Built-in samplers
| Sampler | Strategy | Notes |
|---|---|---|
| `RandomSampler` | Independent random draws | A baseline. Ignores history. |
| `TPESampler` | Models good and bad regions | The default. Handles categorical and conditional spaces. |
| `GridSampler` | Every combination in a fixed grid | Cost grows multiplicatively with each parameter. |
| `CmaEsSampler` | Evolution strategy | Suited to continuous parameters. Needs the `cmaes` package. |
| `QMCSampler` | Low-discrepancy sequences | Covers the space more evenly than random draws. |
| `NSGAIISampler` | Genetic algorithm | Default for multi-objective studies. |
Comparing samplers
The script below runs the same objective with three samplers. Each has the same seed and the same budget of 50 trials. Install the extra dependency first with pip install cmaes.
import optuna
optuna.logging.set_verbosity(optuna.logging.WARNING)
def objective(trial):
x = trial.suggest_float("x", -10, 10)
y = trial.suggest_float("y", -10, 10)
return (x - 2) ** 2 + (y + 1) ** 2
samplers = {
"random": optuna.samplers.RandomSampler(seed=0),
"tpe": optuna.samplers.TPESampler(seed=0),
"cmaes": optuna.samplers.CmaEsSampler(seed=0),
}
for name, sampler in samplers.items():
study = optuna.create_study(direction="minimize", sampler=sampler)
study.optimize(objective, n_trials=50)
print(f"{name}: best value {study.best_value:.4f}")The guided samplers usually finish well below the random baseline on a smooth function like this one. The exact numbers depend on your Optuna version.
Grid search
GridSampler takes a dictionary that maps each parameter name to the values to try.
import optuna
def objective(trial):
x = trial.suggest_float("x", -10, 10)
y = trial.suggest_int("y", -3, 3)
return (x - 2) ** 2 + y ** 2
search_space = {"x": [-5.0, 0.0, 2.0, 5.0], "y": [-1, 0, 1]}
sampler = optuna.samplers.GridSampler(search_space)
study = optuna.create_study(sampler=sampler)
study.optimize(objective) # stops after all 12 combinations
print(study.best_params)Optuna stops the study once every grid point has run. The suggest_* ranges are ignored in favour of the grid values.
Common errors
- Expecting TPE to guide the first trials. TPE draws its first 10 trials at random by default (
n_startup_trials=10). With a budget of 15 trials, most of the run is random. - Forgetting that the sampler is not stored. A study loaded from a database starts with the default sampler. Pass the sampler again to
load_studyorcreate_study. - Using
CmaEsSamplerwith categorical parameters. CMA-ES does not model them well. Optuna falls back to random sampling for those parameters and warns you. - Defining a grid that does not match the objective.
GridSamplerexpects the grid keys to match the names passed tosuggest_*. - Seeding and parallelism. A seed makes a sequential run repeatable. Parallel workers finish in varying order, so results can still differ.
Exercise
A study uses TPESampler(seed=1) and n_trials=8. Why might its results look no better than random search?
Show answer
TPE uses random sampling for its first n_startup_trials trials, which is 10 by default. With only 8 trials, every trial is a random draw, so the model never guides the search.