optuna.storages.JournalStorage
- class optuna.storages.JournalStorage(log_storage)[source]
Journalストレージバックエンド用のストレージクラス。
ライブラリユーザーはこのクラスをインスタンス化できますが、このクラスが提供する属性は直接アクセスすることを想定していません。
Journalストレージは、操作が実行されるたびにデータベースに記録を書き込み、同時にデータベースの最新スナップショットをメモリ上に保持します。データベースが何らかの理由でクラッシュした場合、このストレージは最初から保存された操作を再生することで、メモリ上の内容を再現できます。
Journalストレージは、従来の値ロギングストレージに比べていくつかの利点があります。
ログの粒度が大きいため、I/O操作の回数を削減できます。
Journalストレージは、値ロギングストレージよりもシンプルなバックエンドAPIを提供します。
Journalストレージはスナップショットをメモリ上に保持するため、追加のキャッシュを必要としません。
使用例
import optuna def objective(trial): ... storage = optuna.storages.JournalStorage( optuna.storages.journal.JournalFileBackend("./optuna_journal_storage.log") ) study = optuna.create_study(storage=storage) study.optimize(objective)
Windows環境では、「A required privilege is not held by the client」というエラーメッセージが表示される場合があります。この場合、以下のように
JournalFileOpenLock
を指定してストレージを作成することで問題を解決できます。file_path = "./optuna_journal_storage.log" lock_obj = optuna.storages.journal.JournalFileOpenLock(file_path) storage = optuna.storages.JournalStorage( optuna.storages.journal.JournalFileBackend(file_path, lock_obj=lock_obj), )
メソッド
check_trial_is_updatable
(trial_id, trial_state)Check whether a trial state is updatable.
create_new_study
(directions[, study_name])Create a new study from a name.
create_new_trial
(study_id[, template_trial])Create and add a new trial to a study.
delete_study
(study_id)Delete a study.
Read a list of
FrozenStudy
objects.get_all_trials
(study_id[, deepcopy, states])Read all trials in a study.
get_best_trial
(study_id)Return the trial with the best value in a study.
get_n_trials
(study_id[, state])Count the number of trials in a study.
get_study_directions
(study_id)Read whether a study maximizes or minimizes an objective.
get_study_id_from_name
(study_name)Read the ID of a study.
get_study_name_from_id
(study_id)Read the study name of a study.
get_study_system_attrs
(study_id)Read the optuna-internal attributes of a study.
get_study_user_attrs
(study_id)Read the user-defined attributes of a study.
get_trial
(trial_id)Read a trial.
Read the trial ID of a trial.
get_trial_number_from_id
(trial_id)Read the trial number of a trial.
get_trial_param
(trial_id, param_name)Read the parameter of a trial.
get_trial_params
(trial_id)Read the parameter dictionary of a trial.
get_trial_system_attrs
(trial_id)Read the optuna-internal attributes of a trial.
get_trial_user_attrs
(trial_id)Read the user-defined attributes of a trial.
Clean up all connections to a database.
restore_replay_result
(snapshot)set_study_system_attr
(study_id, key, value)Register an optuna-internal attribute to a study.
set_study_user_attr
(study_id, key, value)Register a user-defined attribute to a study.
set_trial_intermediate_value
(trial_id, step, ...)Report an intermediate value of an objective function.
set_trial_param
(trial_id, param_name, ...)Set a parameter to a trial.
set_trial_state_values
(trial_id, state[, values])Update the state and values of a trial.
set_trial_system_attr
(trial_id, key, value)Set an optuna-internal attribute to a trial.
set_trial_user_attr
(trial_id, key, value)Set a user-defined attribute to a trial.
- Parameters:
log_storage (BaseJournalBackend)
- check_trial_is_updatable(trial_id, trial_state)
トライアルの状態が更新可能かどうかを確認します。
- Parameters:
trial_id (int) – トライアルのID。 エラーメッセージの表示にのみ使用されます。
trial_state (TrialState) – チェックするトライアルの状態。
- Raises:
UpdateFinishedTrialError – トライアルが既に終了している場合。
- Return type:
None
- create_new_study(directions, study_name=None)[source]
名前から新しいスタディを作成します。
名前が指定されていない場合、ストレージクラスが名前を生成します。 返されるスタディIDは、現在および削除されたすべてのスタディの中で一意です。
- Parameters:
directions (Sequence[StudyDirection]) – 要素が
MAXIMIZE
またはMINIMIZE
のいずれかである方向のシーケンス。study_name (str | None) – 作成する新しいスタディの名前。
- Returns:
作成されたスタディのID。
- Raises:
optuna.exceptions.DuplicatedStudyError – 同じ
study_name
を持つスタディが既に存在する場合。- Return type:
- create_new_trial(study_id, template_trial=None)[source]
スタディに新しいトライアルを作成して追加します。
返されるトライアルIDは、現在および削除されたすべてのトライアルの中で一意です。
- Parameters:
study_id (int) – スタディのID。
template_trial (FrozenTrial | None) – デフォルトのユーザー属性、システム属性、中間値、および状態を持つテンプレート
FrozenTrial
。
- Returns:
作成されたトライアルのID。
- Raises:
KeyError – 一致する
study_id
を持つスタディが存在しない場合。- Return type:
- get_all_studies()[source]
FrozenStudy
オブジェクトのリストを読み込みます。- Returns:
study_id
でソートされたFrozenStudy
オブジェクトのリスト。- Return type:
list[FrozenStudy]
- get_all_trials(study_id, deepcopy=True, states=None)[source]
スタディ内のすべてのトライアルを読み込みます。
- Parameters:
- Returns:
trial_id
でソートされたスタディ内のトライアルのリスト。- Raises:
KeyError – 一致する
study_id
を持つスタディが存在しない場合。- Return type:
- get_best_trial(study_id)
スタディ内で最も良い値を持つトライアルを返します。
このメソッドは単一目的最適化の場合にのみ有効です。
- Parameters:
study_id (int) – スタディのID。
- Returns:
スタディ内のすべての終了したトライアルの中で最良の目的値を持つトライアル。
- Raises:
KeyError – 一致する
study_id
を持つスタディが存在しない場合。RuntimeError – スタディに複数の方向がある場合。
ValueError – トライアルが完了していない場合。
- Return type:
- get_n_trials(study_id, state=None)
スタディ内のトライアルの数をカウントします。
- Parameters:
study_id (int) – スタディのID。
state (tuple[TrialState, ...] | TrialState | None) – フィルタリングするトライアルの状態。
None
の場合、すべての状態を含めます。
- Returns:
スタディ内のトライアルの数。
- Raises:
KeyError – 一致する
study_id
を持つスタディが存在しない場合。- Return type:
- get_study_directions(study_id)[source]
スタディが目的を最大化または最小化するかを読み取ります。
- get_trial(trial_id)[source]
トライアルを読み取ります。
- get_trial_number_from_id(trial_id)
トライアルの番号を読み取ります。
Note
トライアル番号はスタディ内で一意であり、連番です。
- get_trial_param(trial_id, param_name)
トライアルのパラメータを読み取ります。
- Parameters:
- Returns:
パラメータの内部表現。
- Raises:
KeyError – 一致する
trial_id
を持つトライアルが存在しない場合。- Return type:
パラメータが存在しない場合。
- get_trial_params(trial_id)
トライアルのパラメータ辞書を読み取ります。
- get_trial_system_attrs(trial_id)
トライアルの内部属性を読み取ります。
- get_trial_user_attrs(trial_id)
トライアルのユーザー定義属性を読み取ります。
- remove_session()
データベースへのすべての接続を切断します。
- Return type:
None
- set_trial_intermediate_value(trial_id, step, intermediate_value)[source]
目的関数の中間値を報告します。
このメソッドは、指定されたステップに関連付けられた既存の中間値を上書きします。
- Parameters:
- Raises:
KeyError – 一致する
trial_id
を持つトライアルが存在しない場合。UpdateFinishedTrialError – トライアルが既に終了している場合。
- Return type:
None
- set_trial_param(trial_id, param_name, param_value_internal, distribution)[source]
トライアルにパラメータを設定します。
- Parameters:
- Raises:
KeyError – 一致する
trial_id
を持つトライアルが存在しない場合。UpdateFinishedTrialError – トライアルが既に終了している場合。
- Return type:
None
- set_trial_state_values(trial_id, state, values=None)[source]
トライアルの状態と値を更新します。
目的関数の戻り値をvalues引数に設定します。 values引数が
None
でない場合、このメソッドは既存のトライアル値を上書きします。- Parameters:
trial_id (int) – トライアルのID。
state (TrialState) – トライアルの新しい状態。
- Returns:
状態が正常に更新された場合は
True
。 状態が変更されなかった場合はFalse
。 この場合、このメソッドがRUNNING
のトライアルの状態をRUNNING
に更新しようとした場合に発生します。- Raises:
KeyError – 一致する
trial_id
を持つトライアルが存在しない場合。UpdateFinishedTrialError – トライアルが既に終了している場合。
- Return type:
- set_trial_system_attr(trial_id, key, value)[source]
トライアルに optuna 内部属性を設定します。
このメソッドは既存の属性を上書きします。
- Parameters:
- Raises:
KeyError – 一致する
trial_id
を持つトライアルが存在しない場合。UpdateFinishedTrialError – トライアルが既に終了している場合。
- Return type:
None
- set_trial_user_attr(trial_id, key, value)[source]
トライアルにユーザー定義属性を設定します。
このメソッドは既存の属性を上書きします。
- Parameters:
- Raises:
KeyError – 一致する
trial_id
を持つトライアルが存在しない場合。UpdateFinishedTrialError – トライアルが既に終了している場合。
- Return type:
None