Note
Click here to download the full example code
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_uniform("x", -10, 10)
return (x - 2) ** 2
study.optimize(objective, n_trials=3)
Out:
Trial 0 finished with value: 1.2064608709485383 and parameters: {'x': 0.9016098730648849}. Best is trial 0 with value: 1.2064608709485383.
Trial 1 finished with value: 76.80548227608128 and parameters: {'x': -6.763873702654624}. Best is trial 0 with value: 1.2064608709485383.
Trial 2 finished with value: 61.153727127696364 and parameters: {'x': -5.82008485425167}. Best is trial 0 with value: 1.2064608709485383.
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: 19.404838504974748 and parameters: {'x': -2.4050923378488616}. Best is trial 0 with value: 1.2064608709485383.
Trial 4 finished with value: 3.4154346902013577 and parameters: {'x': 3.8480894702912405}. Best is trial 0 with value: 1.2064608709485383.
Trial 5 finished with value: 6.431332276211713 and parameters: {'x': 4.536007152239858}. Best is trial 0 with value: 1.2064608709485383.
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 1.206461 0.901610 COMPLETE
1 1 76.805482 -6.763874 COMPLETE
2 2 61.153727 -5.820085 COMPLETE
3 3 19.404839 -2.405092 COMPLETE
4 4 3.415435 3.848089 COMPLETE
5 5 6.431332 4.536007 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': 0.9016098730648849}
Best value: 1.2064608709485383
Best Trial: FrozenTrial(number=0, value=1.2064608709485383, datetime_start=datetime.datetime(2020, 12, 4, 4, 15, 21, 798218), datetime_complete=datetime.datetime(2020, 12, 4, 4, 15, 21, 874075), params={'x': 0.9016098730648849}, distributions={'x': UniformDistribution(high=10, low=-10)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=1, state=TrialState.COMPLETE)
Trials: [FrozenTrial(number=0, value=1.2064608709485383, datetime_start=datetime.datetime(2020, 12, 4, 4, 15, 21, 798218), datetime_complete=datetime.datetime(2020, 12, 4, 4, 15, 21, 874075), params={'x': 0.9016098730648849}, distributions={'x': UniformDistribution(high=10, low=-10)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=1, state=TrialState.COMPLETE), FrozenTrial(number=1, value=76.80548227608128, datetime_start=datetime.datetime(2020, 12, 4, 4, 15, 21, 931130), datetime_complete=datetime.datetime(2020, 12, 4, 4, 15, 21, 964018), params={'x': -6.763873702654624}, distributions={'x': UniformDistribution(high=10, low=-10)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=2, state=TrialState.COMPLETE), FrozenTrial(number=2, value=61.153727127696364, datetime_start=datetime.datetime(2020, 12, 4, 4, 15, 22, 71571), datetime_complete=datetime.datetime(2020, 12, 4, 4, 15, 22, 103566), params={'x': -5.82008485425167}, distributions={'x': UniformDistribution(high=10, low=-10)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=3, state=TrialState.COMPLETE), FrozenTrial(number=3, value=19.404838504974748, datetime_start=datetime.datetime(2020, 12, 4, 4, 15, 22, 200925), datetime_complete=datetime.datetime(2020, 12, 4, 4, 15, 22, 295092), params={'x': -2.4050923378488616}, distributions={'x': UniformDistribution(high=10, low=-10)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=4, state=TrialState.COMPLETE), FrozenTrial(number=4, value=3.4154346902013577, datetime_start=datetime.datetime(2020, 12, 4, 4, 15, 22, 352253), datetime_complete=datetime.datetime(2020, 12, 4, 4, 15, 22, 383256), params={'x': 3.8480894702912405}, distributions={'x': UniformDistribution(high=10, low=-10)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=5, state=TrialState.COMPLETE), FrozenTrial(number=5, value=6.431332276211713, datetime_start=datetime.datetime(2020, 12, 4, 4, 15, 22, 436235), datetime_complete=datetime.datetime(2020, 12, 4, 4, 15, 22, 471140), params={'x': 4.536007152239858}, distributions={'x': UniformDistribution(high=10, low=-10)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=6, state=TrialState.COMPLETE)]
Total running time of the script: ( 0 minutes 2.594 seconds)