optuna.copy_study

optuna.copy_study(*, from_study_name, from_storage, to_storage, to_study_name=None)[source]

Copy study from one storage to another.

The direction(s) of the objective(s) in the study, trials, user attributes and system attributes are copied.

Note

copy_study() copies a study even if the optimization is working on. It means users will get a copied study that contains a trial that is not finished.

Example

import optuna


def objective(trial):
    x = trial.suggest_float("x", -10, 10)
    return (x - 2) ** 2


study = optuna.create_study(
    study_name="example-study",
    storage="sqlite:///example.db",
)
study.optimize(objective, n_trials=3)

optuna.copy_study(
    from_study_name="example-study",
    from_storage="sqlite:///example.db",
    to_storage="sqlite:///example_copy.db",
)

study = optuna.load_study(
    study_name=None,
    storage="sqlite:///example_copy.db",
)
Parameters:
  • from_study_name (str) – Name of study.

  • from_storage (str | BaseStorage) – Source database URL such as sqlite:///example.db. Please see also the documentation of create_study() for further details.

  • to_storage (str | BaseStorage) – Destination database URL.

  • to_study_name (str | None) – Name of the created study. If omitted, from_study_name is used.

Raises:

DuplicatedStudyError – If a study with a conflicting name already exists in the destination storage.

Return type:

None