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_float("x", -10, 10)
return (x - 2) ** 2
study.optimize(objective, n_trials=3)
Out:
Trial 0 finished with value: 2.5076953997086076 and parameters: {'x': 0.4164295406554821}. Best is trial 0 with value: 2.5076953997086076.
Trial 1 finished with value: 6.690113159688054 and parameters: {'x': 4.586525306214508}. Best is trial 0 with value: 2.5076953997086076.
Trial 2 finished with value: 32.02327308391347 and parameters: {'x': 7.6589109450417645}. Best is trial 0 with value: 2.5076953997086076.
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: 5.330134038319179 and parameters: {'x': 4.308708305160957}. Best is trial 0 with value: 2.5076953997086076.
Trial 4 finished with value: 31.341384665788983 and parameters: {'x': 7.598337669861383}. Best is trial 0 with value: 2.5076953997086076.
Trial 5 finished with value: 92.6177988473662 and parameters: {'x': -7.6238141527861085}. Best is trial 0 with value: 2.5076953997086076.
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 2.507695 0.416430 COMPLETE
1 1 6.690113 4.586525 COMPLETE
2 2 32.023273 7.658911 COMPLETE
3 3 5.330134 4.308708 COMPLETE
4 4 31.341385 7.598338 COMPLETE
5 5 92.617799 -7.623814 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.4164295406554821}
Best value: 2.5076953997086076
Best Trial: FrozenTrial(number=0, values=[2.5076953997086076], datetime_start=datetime.datetime(2021, 4, 5, 3, 40, 53, 316118), datetime_complete=datetime.datetime(2021, 4, 5, 3, 40, 53, 331856), params={'x': 0.4164295406554821}, distributions={'x': UniformDistribution(high=10.0, low=-10.0)}, user_attrs={}, system_attrs={}, intermediate_values={}, trial_id=1, state=TrialState.COMPLETE, value=None)
Trials: [FrozenTrial(number=0, values=[2.5076953997086076], datetime_start=datetime.datetime(2021, 4, 5, 3, 40, 53, 316118), datetime_complete=datetime.datetime(2021, 4, 5, 3, 40, 53, 331856), params={'x': 0.4164295406554821}, 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=[6.690113159688054], datetime_start=datetime.datetime(2021, 4, 5, 3, 40, 53, 361936), datetime_complete=datetime.datetime(2021, 4, 5, 3, 40, 53, 376935), params={'x': 4.586525306214508}, 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=[32.02327308391347], datetime_start=datetime.datetime(2021, 4, 5, 3, 40, 53, 397610), datetime_complete=datetime.datetime(2021, 4, 5, 3, 40, 53, 409843), params={'x': 7.6589109450417645}, 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=[5.330134038319179], datetime_start=datetime.datetime(2021, 4, 5, 3, 40, 53, 468341), datetime_complete=datetime.datetime(2021, 4, 5, 3, 40, 53, 483528), params={'x': 4.308708305160957}, 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=[31.341384665788983], datetime_start=datetime.datetime(2021, 4, 5, 3, 40, 53, 507785), datetime_complete=datetime.datetime(2021, 4, 5, 3, 40, 53, 521519), params={'x': 7.598337669861383}, 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=[92.6177988473662], datetime_start=datetime.datetime(2021, 4, 5, 3, 40, 53, 542024), datetime_complete=datetime.datetime(2021, 4, 5, 3, 40, 53, 554991), params={'x': -7.6238141527861085}, 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.508 seconds)