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 4. 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)
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)
Trial 0 finished with value: 64.10244626979049 and parameters: {'x': -6.0064003315966215}. Best is trial 0 with value: 64.10244626979049.
Trial 1 finished with value: 1.47108163479192 and parameters: {'x': 3.2128815419454284}. Best is trial 1 with value: 1.47108163479192.
Trial 2 finished with value: 34.7720309545063 and parameters: {'x': 7.896781406369605}. Best is trial 1 with value: 1.47108163479192.

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)
Using an existing study with name 'example-study' instead of creating a new one.
Trial 3 finished with value: 7.408130118027405 and parameters: {'x': 4.721788036939579}. Best is trial 1 with value: 1.47108163479192.
Trial 4 finished with value: 1.5881481436285894 and parameters: {'x': 3.2602174985408627}. Best is trial 1 with value: 1.47108163479192.
Trial 5 finished with value: 42.5671572386491 and parameters: {'x': -4.52435109713212}. Best is trial 1 with value: 1.47108163479192.

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"))
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)
   number      value  params_x     state
0       0  64.102446 -6.006400  COMPLETE
1       1   1.471082  3.212882  COMPLETE
2       2  34.772031  7.896781  COMPLETE
3       3   7.408130  4.721788  COMPLETE
4       4   1.588148  3.260217  COMPLETE
5       5  42.567157 -4.524351  COMPLETE

A Study object also provides properties such as trials, best_value, best_params (see also 1. 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)
Best params:  {'x': 3.2128815419454284}
Best value:  1.47108163479192
Best Trial:  FrozenTrial(number=1, values=[1.47108163479192], datetime_start=datetime.datetime(2022, 9, 8, 9, 16, 41, 583301), datetime_complete=datetime.datetime(2022, 9, 8, 9, 16, 41, 600126), params={'x': 3.2128815419454284}, distributions={'x': FloatDistribution(high=10.0, log=False, low=-10.0, step=None)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=2, state=TrialState.COMPLETE, value=None)
Trials:  [FrozenTrial(number=0, values=[64.10244626979049], datetime_start=datetime.datetime(2022, 9, 8, 9, 16, 41, 528796), datetime_complete=datetime.datetime(2022, 9, 8, 9, 16, 41, 550522), params={'x': -6.0064003315966215}, distributions={'x': FloatDistribution(high=10.0, log=False, low=-10.0, step=None)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=1, state=TrialState.COMPLETE, value=None), FrozenTrial(number=1, values=[1.47108163479192], datetime_start=datetime.datetime(2022, 9, 8, 9, 16, 41, 583301), datetime_complete=datetime.datetime(2022, 9, 8, 9, 16, 41, 600126), params={'x': 3.2128815419454284}, distributions={'x': FloatDistribution(high=10.0, log=False, low=-10.0, step=None)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=2, state=TrialState.COMPLETE, value=None), FrozenTrial(number=2, values=[34.7720309545063], datetime_start=datetime.datetime(2022, 9, 8, 9, 16, 41, 616861), datetime_complete=datetime.datetime(2022, 9, 8, 9, 16, 41, 632242), params={'x': 7.896781406369605}, distributions={'x': FloatDistribution(high=10.0, log=False, low=-10.0, step=None)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=3, state=TrialState.COMPLETE, value=None), FrozenTrial(number=3, values=[7.408130118027405], datetime_start=datetime.datetime(2022, 9, 8, 9, 16, 41, 694780), datetime_complete=datetime.datetime(2022, 9, 8, 9, 16, 41, 716089), params={'x': 4.721788036939579}, distributions={'x': FloatDistribution(high=10.0, log=False, low=-10.0, step=None)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=4, state=TrialState.COMPLETE, value=None), FrozenTrial(number=4, values=[1.5881481436285894], datetime_start=datetime.datetime(2022, 9, 8, 9, 16, 41, 738202), datetime_complete=datetime.datetime(2022, 9, 8, 9, 16, 41, 754360), params={'x': 3.2602174985408627}, distributions={'x': FloatDistribution(high=10.0, log=False, low=-10.0, step=None)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=5, state=TrialState.COMPLETE, value=None), FrozenTrial(number=5, values=[42.5671572386491], datetime_start=datetime.datetime(2022, 9, 8, 9, 16, 41, 770282), datetime_complete=datetime.datetime(2022, 9, 8, 9, 16, 41, 786895), params={'x': -4.52435109713212}, distributions={'x': FloatDistribution(high=10.0, log=False, low=-10.0, step=None)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=6, state=TrialState.COMPLETE, value=None)]

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

Gallery generated by Sphinx-Gallery