Sub topic 17
Best practices and common pitfalls
This chapter collects habits that make Optuna results trustworthy. It assumes you can write an objective, define a search space, use cross-validation, and add pruning, as covered in earlier chapters.
Keep the validation data honest
Every trial scores the model on the same validation data, and the study keeps the best score. With enough trials, the winner is partly lucky. Its best score then overstates how well it will do on new data.
- Hold out a test set that the study never sees. Evaluate only the final chosen configuration on it, once.
- Use cross-validation inside the objective, as in the scikit-learn chapter, so one lucky split matters less.
- Prefer a moderate number of trials. Thousands of trials on a small dataset mostly fit the noise.
Design the search space with care
- Use
log=Truefor parameters that span orders of magnitude, such as learning rates and regularisation strengths. - Keep ranges wide enough to contain the optimum but not absurdly wide. If
best_paramssits at a range boundary, widen that range and rerun. - Suggest only the parameters a model uses. Conditional branches keep the sampler from learning from values that had no effect.
- Fix parameters you already know instead of searching them.
Use pruning where it fits
Pruning only helps when the objective produces intermediate scores, such as per-epoch validation accuracy. Two common mistakes:
- Calling
trial.reportwith a different metric from the one you return. The pruner compares reported values across trials, so they must mean the same thing. - Setting
n_startup_trialsandn_warmup_stepstoo low. The pruner then stops trials on noisy early scores, and good slow starters are lost.
Make runs reproducible and resumable
- Seed the sampler, for example
TPESampler(seed=42), and seed your model's own random state. - Seeding gives repeatable results only for sequential runs. Parallel workers finish in varying order, so the sampler sees results in a different order each time.
- Give the study a name and a database storage with
load_if_exists=True, so a crash does not lose completed trials.
Inspect before you trust
After a run, look at the history and parameter importances. A history that is still improving at the last trial means the budget was too small. A flat history from the first few trials suggests the search space or objective has a problem, such as a bug that returns a constant score.
| Symptom | Likely cause | Fix |
|---|---|---|
| Best value much higher than test score | Overfitting the validation data | Hold out a test set, use cross-validation |
| Best parameter at a range edge | Range too narrow | Widen the range |
| Many pruned trials, poor final result | Pruner too aggressive | Raise warmup settings |
| Different results on each run | No seed, or parallel workers | Seed the sampler, run sequentially to compare |
Exercise
A study of 500 trials reports a best cross-validation accuracy of 0.97, but the final model scores 0.91 on fresh data. Name the likely cause and two changes that would help.
Show answer
The search overfit the validation data, because the best of many trials benefits from chance. Hold out a test set that the study never uses and evaluate on it once. Also reduce the number of trials or use a more reliable cross-validation setup, such as more folds, so single lucky splits count for less.