optuna.study.MaxTrialsCallback

class optuna.study.MaxTrialsCallback(n_trials, states=(TrialState.COMPLETE,))[源代码]

在研究结束前设置最大试验次数。

虽然 optuna.study.Study.optimize()n_trials 参数设置了要运行的试验次数,但您可能希望继续运行,直到达到一定数量的成功完成的试验,或者在达到一定数量的失败试验时停止研究。这个 MaxTrialsCallback 类允许您为特定的 TrialState 设置最大试验次数,然后停止研究。

示例

import optuna
from optuna.study import MaxTrialsCallback
from optuna.trial import TrialState


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


study = optuna.create_study()
study.optimize(
    objective,
    callbacks=[MaxTrialsCallback(10, states=(TrialState.COMPLETE,))],
)
参数:
  • n_trials (int) – 最大试验次数。必须设置为整数。

  • states (Container[TrialState] | None) – 用于计入最大试验次数限制的 TrialState 的元组。默认值为 (TrialState.COMPLETE,)。如果为 None,则计算所有状态。