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 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: 67.04316959265873 and parameters: {'x': -6.187989349813465}. Best is trial 0 with value: 67.04316959265873.
Trial 1 finished with value: 24.86602602235838 and parameters: {'x': -2.9865846049534124}. Best is trial 1 with value: 24.86602602235838.
Trial 2 finished with value: 29.858220178257294 and parameters: {'x': 7.464267579306242}. Best is trial 1 with value: 24.86602602235838.
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: 110.06357071896575 and parameters: {'x': -8.491118659083298}. Best is trial 1 with value: 24.86602602235838.
Trial 4 finished with value: 3.561852565963845 and parameters: {'x': 3.8872870915586333}. Best is trial 4 with value: 3.561852565963845.
Trial 5 finished with value: 25.598291431547274 and parameters: {'x': 7.0594754107068525}. Best is trial 4 with value: 3.561852565963845.
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 67.043170 -6.187989 COMPLETE
1 1 24.866026 -2.986585 COMPLETE
2 2 29.858220 7.464268 COMPLETE
3 3 110.063571 -8.491119 COMPLETE
4 4 3.561853 3.887287 COMPLETE
5 5 25.598291 7.059475 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.8872870915586333}
Best value: 3.561852565963845
Best Trial: FrozenTrial(number=4, values=[3.561852565963845], datetime_start=datetime.datetime(2022, 12, 1, 6, 17, 13, 102737), datetime_complete=datetime.datetime(2022, 12, 1, 6, 17, 13, 121116), params={'x': 3.8872870915586333}, 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)
Trials: [FrozenTrial(number=0, values=[67.04316959265873], datetime_start=datetime.datetime(2022, 12, 1, 6, 17, 12, 876534), datetime_complete=datetime.datetime(2022, 12, 1, 6, 17, 12, 900938), params={'x': -6.187989349813465}, 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=[24.86602602235838], datetime_start=datetime.datetime(2022, 12, 1, 6, 17, 12, 929502), datetime_complete=datetime.datetime(2022, 12, 1, 6, 17, 12, 947241), params={'x': -2.9865846049534124}, 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=[29.858220178257294], datetime_start=datetime.datetime(2022, 12, 1, 6, 17, 12, 966448), datetime_complete=datetime.datetime(2022, 12, 1, 6, 17, 12, 983237), params={'x': 7.464267579306242}, 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=[110.06357071896575], datetime_start=datetime.datetime(2022, 12, 1, 6, 17, 13, 57851), datetime_complete=datetime.datetime(2022, 12, 1, 6, 17, 13, 79202), params={'x': -8.491118659083298}, 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=[3.561852565963845], datetime_start=datetime.datetime(2022, 12, 1, 6, 17, 13, 102737), datetime_complete=datetime.datetime(2022, 12, 1, 6, 17, 13, 121116), params={'x': 3.8872870915586333}, 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=[25.598291431547274], datetime_start=datetime.datetime(2022, 12, 1, 6, 17, 13, 139541), datetime_complete=datetime.datetime(2022, 12, 1, 6, 17, 13, 156632), params={'x': 7.0594754107068525}, 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.885 seconds)