Sub topic 3
Choosing a Pruner
Assume you know Python, scikit-learn model validation, and the difference between parameters and hyperparameters. This chapter covers the operational choices that make Optuna studies useful and trustworthy.
Choosing a pruner
A pruner stops a trial before it finishes when its intermediate results look poor. Report a value at each training step, then ask whether Optuna should stop:
trial.report(validation_score, step)
if trial.should_prune():
raise optuna.TrialPruned()Choose a pruner based on how trials improve:
| Pruner | Good fit |
|---|---|
| `MedianPruner` | A simple baseline for comparable training steps |
| `SuccessiveHalvingPruner` | Many trials with a clear resource measure |
| `HyperbandPruner` | Several resource budgets and varied trial speeds |
Pruning saves time, but it can stop a trial that would improve later. Set warm-up steps and minimum startup trials when early scores are unreliable.
Persisting and resuming studies
Use database storage to resume a study after interruption:
import optuna
study = optuna.create_study(
study_name="model-tuning",
storage="sqlite:///optuna.db",
load_if_exists=True,
direction="maximize",
)
study.optimize(objective, n_trials=50)For several machines, use a shared server database rather than a local SQLite file. Each worker can load the same study and claim trials.
Running trials in parallel
Run trials in one process with n_jobs, or start multiple processes that share RDB storage:
study.optimize(objective, n_trials=100, n_jobs=4)Parallel execution reduces elapsed time, but workers make decisions using slightly stale results. It can also make runs less deterministic.
Inspecting results and plotting
Inspect the best trial and completed trials:
print(study.best_value)
print(study.best_params)
print(study.trials_dataframe())With Plotly installed, plot_optimization_history(study) shows progress, while plot_param_importances(study) estimates which parameters affected the result.
Making runs reproducible
Seed the sampler and every library that uses randomness:
sampler = optuna.samplers.TPESampler(seed=42)
study = optuna.create_study(direction="maximize", sampler=sampler)Also record data versions, cross-validation splits, package versions, and hardware. Parallel scheduling can still change results.
Optimising more than one objective
A multi-objective study returns a Pareto front: trials where improving one objective requires worsening another.
study = optuna.create_study(
directions=["maximize", "minimize"]
)
study.optimize(objective, n_trials=50)
for trial in study.best_trials:
print(trial.values, trial.params)Ask-and-tell and known trials
The ask-and-tell interface separates parameter selection from evaluation. enqueue_trial schedules a known starting point:
study.enqueue_trial({"x": 1.0})
trial = study.ask()
x = trial.suggest_float("x", -10, 10)
value = evaluate(x)
study.tell(trial, value)Best practices and common pitfalls
- Keep the final test set untouched until model selection ends.
- Use cross-validation or a fixed validation split consistently.
- Bound the search space with meaningful domain knowledge.
- Save the study, configuration, seeds, and environment details.
- Compare pruned and completed trials before changing the pruner.
- Do not treat the best validation score as an unbiased performance estimate.
Exercise
Why should a test set remain unused while Optuna chooses hyperparameters?
Show answer
Repeatedly selecting the best test score overfits the test set, so it no longer estimates performance on unseen data.