optuna.study.MaxTrialsCallback

class optuna.study.MaxTrialsCallback(n_trials, states=(1,))[源]

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

虽然 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,则计算所有状态。