Sub topic 2
Studies and trials: the core objects
This chapter assumes you can write Python functions, train and validate a scikit-learn model, and tell parameters from hyperparameters. It introduces the two objects every Optuna program is built around: the study and the trial.
The two objects
A study is one optimisation session. It holds the direction (minimise or maximise), the sampler that proposes parameters, the pruner, and the full history of results. A trial is a single evaluation of your objective function with one set of hyperparameters.
Optuna runs a loop. The study creates a trial, your objective function asks the trial for parameter values and returns a score, and the study records the score. The sampler then uses the history to choose the next values.
A minimal study
The next example minimises a one-variable function. The best value is at x = 2.
import optuna
def objective(trial):
x = trial.suggest_float("x", -10, 10)
return (x - 2) ** 2
study = optuna.create_study(direction="minimize")
study.optimize(objective, n_trials=50)
print(study.best_params)
print(study.best_value)create_study builds the study. direction defaults to "minimize", so set "maximize" for scores such as accuracy. optimize runs the loop for n_trials trials. After 50 trials, best_params should be close to {"x": 2.0} and best_value close to 0. Exact numbers differ between runs because the sampler is random.
Reading results
A study keeps everything it has seen. These attributes are the ones you will use most.
| Attribute | Meaning |
|---|---|
| `study.best_trial` | The trial with the best value |
| `study.best_params` | Parameters of the best trial, as a dict |
| `study.best_value` | Objective value of the best trial |
| `study.trials` | List of all trials, in order |
| `study.trials_dataframe()` | All trials as a pandas DataFrame |
Each trial in study.trials has a number (its index, starting at 0), params, value and state.
Trial states
A trial is always in one state. Knowing them helps you read a study that has failures or pruned runs.
| State | Meaning |
|---|---|
| `RUNNING` | The objective is still executing |
| `COMPLETE` | The objective returned a value |
| `PRUNED` | The trial was stopped early (covered in a later chapter) |
| `FAIL` | The objective raised an exception |
| `WAITING` | The trial was enqueued and has not started |
best_trial only considers COMPLETE trials. If every trial fails, asking for best_value raises a ValueError.
Common mistakes
- Wrong direction. Maximising a loss, or minimising accuracy, makes the search work against you without any error. Check
directionfirst when results look odd. - Calling
optimizetwice. The second call continues the same study and adds trials. It does not restart. Create a new study for a fresh search. - Expecting one study to be saved. With no
storageargument, the study lives in memory and is lost when the process ends. Persistence is covered in a later chapter.
Exercise
You run study.optimize(objective, n_trials=20) and then run the same line again. How many trials does the study hold, and why?
Show answer
It holds 40 trials. A study keeps its history, and each call to optimize adds more trials to it. To start from scratch, create a new study with optuna.create_study.
Exercise
Your objective returns validation accuracy, but you created the study with the default arguments. What goes wrong, and how do you fix it?
Show answer
The default direction is "minimize", so Optuna searches for the lowest accuracy. Create the study with direction="maximize".