optuna.storages.InMemoryStorage
- class optuna.storages.InMemoryStorage[source]
Pythonプロセスのメモリ上にデータを保存するストレージクラス。
使用例
InMemoryStorage
インスタンスの作成例。import optuna def objective(trial): x = trial.suggest_float("x", -100, 100) return x**2 storage = optuna.storages.InMemoryStorage() study = optuna.create_study(storage=storage) study.optimize(objective, n_trials=10)
メソッド一覧
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.
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.
- 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)[source]
スタディ内で最も良い値を持つトライアルを返します。
このメソッドは単一目的最適化の場合のみ有効です。
- 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_param(trial_id, param_name)[source]
トライアルのパラメータを取得します。
- 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]
トライアルに内部属性を設定します。
このメソッドは既存の属性を上書きします。
- 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