(File-based) Journal Storage

Optuna provides JournalStorage. With this feature, you can easily run a distributed optimization over network using NFS as the shared storage, without need for setting up RDB or Redis.

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 = optuna.storages.JournalStorage(
    optuna.storages.JournalFileStorage("./journal.log"),  # NFS path for distributed optimization
)

study = optuna.create_study(study_name=study_name, storage=storage)


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


study.optimize(objective, n_trials=3)
/home/docs/checkouts/readthedocs.org/user_builds/optuna/checkouts/v3.4.0/tutorial/20_recipes/011_journal_storage.py:22: ExperimentalWarning:

JournalStorage is experimental (supported from v3.1.0). The interface can change in the future.

A new study created in Journal with name: example-study
Trial 0 finished with value: 13.747222221605702 and parameters: {'x': -1.7077246690666907}. Best is trial 0 with value: 13.747222221605702.
Trial 1 finished with value: 3.6240974522198868 and parameters: {'x': 3.903706241051882}. Best is trial 1 with value: 3.6240974522198868.
Trial 2 finished with value: 13.856794076477799 and parameters: {'x': 5.722471501096791}. Best is trial 1 with value: 3.6240974522198868.

Although the optimization in this example is too short to run in parallel, you can extend this example to write a optimization script which can be run in parallel.

Note

In a Windows environment, an error message “A required privilege is not held by the client” may appear. In this case, you can solve the problem with creating storage by specifying JournalFileOpenLock. See the reference of JournalStorage for any details.

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

Gallery generated by Sphinx-Gallery