(ファイルベース)ジャーナルストレージ

Optuna は JournalStorage を提供します。この機能を使用すると、 RDB や Redis の設定を必要とせず、NFS を共有ストレージとしてネットワーク上で分散最適化を 簡単に実行できます。

import logging
import sys

import optuna


# 標準出力にログを出力するハンドラを追加
optuna.logging.get_logger("optuna").addHandler(logging.StreamHandler(sys.stdout))
study_name = "example-study"  # スタディの一意の識別子
file_path = "./optuna_journal_storage.log"
storage = optuna.storages.JournalStorage(
    optuna.storages.journal.JournalFileBackend(file_path),  # 分散最適化用の NFS パス
)

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)
A new study created in Journal with name: example-study
Trial 0 finished with value: 30.860514121222923 and parameters: {'x': 7.555224038796538}. Best is trial 0 with value: 30.860514121222923.
Trial 1 finished with value: 29.80482591287991 and parameters: {'x': -3.459379627107819}. Best is trial 1 with value: 29.80482591287991.
Trial 2 finished with value: 118.07618806670887 and parameters: {'x': -8.86628676534486}. Best is trial 1 with value: 29.80482591287991.

この例の最適化は並列実行には短すぎるため、並列実行可能な最適化スクリプトを 作成する例に拡張できます。

Note

Windows 環境では、”A required privilege is not held by the client” というエラーメッセージが 表示される場合があります。この場合、JournalFileOpenLock を 指定してストレージを作成することで問題を解決できます。詳細は JournalStorage のリファレンスを参照してください。

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

Gallery generated by Sphinx-Gallery