Saving/Resuming Study with RDB Backend

An RDB backend enables persistent experiments (i.e., to save and resume a study) as well as access to history of studies. In addition, we can run multi-node optimization tasks with this feature, which is described in Easy Parallelization.

In this section, let’s try simple examples running on a local environment with SQLite DB.

Note

You can also utilize other RDB backends, e.g., PostgreSQL or MySQL, by setting the storage argument to the DB’s URL. Please refer to SQLAlchemy’s document for how to set up the URL.

New Study

We can create a persistent study by calling create_study() function as follows. An SQLite file example.db is automatically initialized with a new study record.

import logging
import sys

import optuna

# Add stream handler of stdout to show the messages
optuna.logging.get_logger("optuna").addHandler(logging.StreamHandler(sys.stdout))
study_name = "example-study"  # Unique identifier of the study.
storage_name = "sqlite:///{}.db".format(study_name)
study = optuna.create_study(study_name=study_name, storage=storage_name)

Out:

A new study created in RDB with name: example-study

To run a study, call optimize() method passing an objective function.

def objective(trial):
    x = trial.suggest_float("x", -10, 10)
    return (x - 2) ** 2


study.optimize(objective, n_trials=3)

Out:

Trial 0 finished with value: 15.025436006709292 and parameters: {'x': 5.876265729630683}. Best is trial 0 with value: 15.025436006709292.
Trial 1 finished with value: 20.730602955504025 and parameters: {'x': 6.553087189534594}. Best is trial 0 with value: 15.025436006709292.
Trial 2 finished with value: 0.4606045910025517 and parameters: {'x': 2.678678562356696}. Best is trial 2 with value: 0.4606045910025517.

Resume Study

To resume a study, instantiate a Study object passing the study name example-study and the DB URL sqlite:///example-study.db.

study = optuna.create_study(study_name=study_name, storage=storage_name, load_if_exists=True)
study.optimize(objective, n_trials=3)

Out:

Using an existing study with name 'example-study' instead of creating a new one.
Trial 3 finished with value: 11.512565862764529 and parameters: {'x': -1.3930172211122844}. Best is trial 2 with value: 0.4606045910025517.
Trial 4 finished with value: 37.988468476428345 and parameters: {'x': -4.163478601928325}. Best is trial 2 with value: 0.4606045910025517.
Trial 5 finished with value: 9.19889092075396 and parameters: {'x': -1.0329673458106932}. Best is trial 2 with value: 0.4606045910025517.

Experimental History

We can access histories of studies and trials via the Study class. For example, we can get all trials of example-study as:

study = optuna.create_study(study_name=study_name, storage=storage_name, load_if_exists=True)
df = study.trials_dataframe(attrs=("number", "value", "params", "state"))

Out:

Using an existing study with name 'example-study' instead of creating a new one.

The method trials_dataframe() returns a pandas dataframe like:

print(df)

Out:

   number      value  params_x     state
0       0  15.025436  5.876266  COMPLETE
1       1  20.730603  6.553087  COMPLETE
2       2   0.460605  2.678679  COMPLETE
3       3  11.512566 -1.393017  COMPLETE
4       4  37.988468 -4.163479  COMPLETE
5       5   9.198891 -1.032967  COMPLETE

A Study object also provides properties such as trials, best_value, best_params (see also Lightweight, versatile, and platform agnostic architecture).

print("Best params: ", study.best_params)
print("Best value: ", study.best_value)
print("Best Trial: ", study.best_trial)
print("Trials: ", study.trials)

Out:

Best params:  {'x': 2.678678562356696}
Best value:  0.4606045910025517
Best Trial:  FrozenTrial(number=2, values=[0.4606045910025517], datetime_start=datetime.datetime(2022, 7, 1, 1, 27, 4, 640448), datetime_complete=datetime.datetime(2022, 7, 1, 1, 27, 4, 654413), params={'x': 2.678678562356696}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=3, state=TrialState.COMPLETE, value=None)
Trials:  [FrozenTrial(number=0, values=[15.025436006709292], datetime_start=datetime.datetime(2022, 7, 1, 1, 27, 4, 559966), datetime_complete=datetime.datetime(2022, 7, 1, 1, 27, 4, 577653), params={'x': 5.876265729630683}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=1, state=TrialState.COMPLETE, value=None), FrozenTrial(number=1, values=[20.730602955504025], datetime_start=datetime.datetime(2022, 7, 1, 1, 27, 4, 606581), datetime_complete=datetime.datetime(2022, 7, 1, 1, 27, 4, 620070), params={'x': 6.553087189534594}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=2, state=TrialState.COMPLETE, value=None), FrozenTrial(number=2, values=[0.4606045910025517], datetime_start=datetime.datetime(2022, 7, 1, 1, 27, 4, 640448), datetime_complete=datetime.datetime(2022, 7, 1, 1, 27, 4, 654413), params={'x': 2.678678562356696}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=3, state=TrialState.COMPLETE, value=None), FrozenTrial(number=3, values=[11.512565862764529], datetime_start=datetime.datetime(2022, 7, 1, 1, 27, 4, 710909), datetime_complete=datetime.datetime(2022, 7, 1, 1, 27, 4, 740716), params={'x': -1.3930172211122844}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=4, state=TrialState.COMPLETE, value=None), FrozenTrial(number=4, values=[37.988468476428345], datetime_start=datetime.datetime(2022, 7, 1, 1, 27, 4, 766551), datetime_complete=datetime.datetime(2022, 7, 1, 1, 27, 4, 780719), params={'x': -4.163478601928325}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=5, state=TrialState.COMPLETE, value=None), FrozenTrial(number=5, values=[9.19889092075396], datetime_start=datetime.datetime(2022, 7, 1, 1, 27, 4, 801004), datetime_complete=datetime.datetime(2022, 7, 1, 1, 27, 4, 815031), params={'x': -1.0329673458106932}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=6, state=TrialState.COMPLETE, value=None)]

Total running time of the script: ( 0 minutes 0.568 seconds)

Gallery generated by Sphinx-Gallery