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: 0.0037369134179836942 and parameters: {'x': 2.0611302986904505}. Best is trial 0 with value: 0.0037369134179836942.
Trial 1 finished with value: 3.7231333459786713 and parameters: {'x': 3.9295422633305215}. Best is trial 0 with value: 0.0037369134179836942.
Trial 2 finished with value: 0.0008903861881741019 and parameters: {'x': 1.9701606603931303}. Best is trial 2 with value: 0.0008903861881741019.

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: 10.910701798569782 and parameters: {'x': -1.3031351468823953}. Best is trial 2 with value: 0.0008903861881741019.
Trial 4 finished with value: 11.634857533307791 and parameters: {'x': -1.4109906967489358}. Best is trial 2 with value: 0.0008903861881741019.
Trial 5 finished with value: 34.16534301614867 and parameters: {'x': -3.845112746230706}. Best is trial 2 with value: 0.0008903861881741019.

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   0.003737  2.061130  COMPLETE
1       1   3.723133  3.929542  COMPLETE
2       2   0.000890  1.970161  COMPLETE
3       3  10.910702 -1.303135  COMPLETE
4       4  11.634858 -1.410991  COMPLETE
5       5  34.165343 -3.845113  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': 1.9701606603931303}
Best value:  0.0008903861881741019
Best Trial:  FrozenTrial(number=2, values=[0.0008903861881741019], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 46, 624209), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 46, 635489), params={'x': 1.9701606603931303}, 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=[0.0037369134179836942], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 46, 554786), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 46, 569507), params={'x': 2.0611302986904505}, 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=[3.7231333459786713], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 46, 594389), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 46, 606557), params={'x': 3.9295422633305215}, 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.0008903861881741019], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 46, 624209), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 46, 635489), params={'x': 1.9701606603931303}, 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=[10.910701798569782], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 46, 684493), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 46, 697608), params={'x': -1.3031351468823953}, 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=[11.634857533307791], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 46, 720099), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 46, 731930), params={'x': -1.4109906967489358}, 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=[34.16534301614867], datetime_start=datetime.datetime(2021, 10, 18, 2, 52, 46, 749384), datetime_complete=datetime.datetime(2021, 10, 18, 2, 52, 46, 760574), params={'x': -3.845112746230706}, 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.452 seconds)

Gallery generated by Sphinx-Gallery