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) – Tuple of the TrialState to be counted towards the max trials limit. Default value is (TrialState.COMPLETE,). If None, count all states.