Sub topic 10
Choosing a pruner
This chapter assumes you can write an Optuna objective that calls trial.report and trial.should_prune, and that you know the difference between a hyperparameter and a model parameter. Here you will compare the three common pruners and pick one for a given job.
What a pruner decides
A pruner is the object that trial.should_prune() asks. It looks at the intermediate values your running trial has reported. It compares them with the values from earlier trials in the same study. If the trial looks worse than the others at the same step, the pruner says yes, and you raise optuna.TrialPruned.
You pass the pruner to create_study. The sampler and the pruner work independently. The sampler proposes parameters, and the pruner stops weak trials early.
The three pruners
| Pruner | Rule | Best when |
|---|---|---|
| `MedianPruner` | Stops a trial if its best value so far is worse than the median of earlier trials at the same step. | You want a simple default. |
| `SuccessiveHalvingPruner` | Gives trials a small budget, keeps the top fraction, then gives the survivors more budget. | You can report steps cheaply and want aggressive pruning. |
| `HyperbandPruner` | Runs several successive-halving brackets with different starting budgets. | You do not know how early a trial's score predicts its final score. |
Configuring the median pruner
Two arguments matter most. n_startup_trials is the number of trials that must finish before any pruning starts. n_warmup_steps is the number of steps each trial runs before it can be pruned.
import optuna
pruner = optuna.pruners.MedianPruner(
n_startup_trials=5,
n_warmup_steps=5,
interval_steps=1,
)
study = optuna.create_study(direction="maximize", pruner=pruner)Without startup trials, the first trial would have nothing to be compared with. Without warm-up steps, a slow starter such as a model with a small learning rate may be stopped before it has a fair chance.
Hyperband
Hyperband needs to know the range of steps your objective uses. Set min_resource and max_resource to match.
import optuna
pruner = optuna.pruners.HyperbandPruner(
min_resource=1,
max_resource=20,
reduction_factor=3,
)
study = optuna.create_study(direction="maximize", pruner=pruner)If max_resource does not match the number of steps you actually report, the brackets are sized wrongly. Pruning then happens at the wrong moments.
How to choose
Start with MedianPruner. It has few settings and is easy to reason about. Move to Hyperband when you have many cheap steps and suspect that some good trials start slowly.
Common errors
- Reporting a value that is not comparable across steps, for example training loss at one step and validation loss at the next.
- Reusing the same step number within one trial. Each
trial.reportcall needs its own step. - Forgetting to raise
optuna.TrialPruned()aftershould_prune()returns true. The trial then keeps running and wastes the time you meant to save. - Setting
n_warmup_stepshigher than the number of steps you report. No trial is ever pruned.
Exercise
Your objective reports a validation score at steps 0 to 19. Some good trials score poorly for the first 8 steps and then improve quickly. Which pruner setting would you change first on a MedianPruner, and why?
Show answer
Raise n_warmup_steps to about 8 or 10. Trials are not pruned during warm-up, so slow starters get time to improve before they are compared with other trials. If the slow start is common, switch to HyperbandPruner.