optuna.pruners.SuccessiveHalvingPruner

class optuna.pruners.SuccessiveHalvingPruner(min_resource='auto', reduction_factor=4, min_early_stopping_rate=0, bootstrap_count=0)[源]

使用异步连续减半算法的剪枝器。

Successive Halving 是一种基于赌徒算法,用于从多个配置中找出最佳配置。此类实现 Successive Halving 的异步版本。有关详细描述,请参阅 Asynchronous Successive Halving 的论文。

请注意,此类不处理最大资源参数,在论文中称为 \(R\)。分配给一个 trial 的最大资源通常在目标函数内部受到限制(例如,在 simple_pruning.py 中的 step 数,在 chainer_integration.py 中的 EPOCH 数)。

另请参阅

请参阅 report()

示例

我们使用 SuccessiveHalvingPruner 来最小化一个目标函数。

import numpy as np
from sklearn.datasets import load_iris
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import train_test_split

import optuna

X, y = load_iris(return_X_y=True)
X_train, X_valid, y_train, y_valid = train_test_split(X, y)
classes = np.unique(y)


def objective(trial):
    alpha = trial.suggest_float("alpha", 0.0, 1.0)
    clf = SGDClassifier(alpha=alpha)
    n_train_iter = 100

    for step in range(n_train_iter):
        clf.partial_fit(X_train, y_train, classes=classes)

        intermediate_value = clf.score(X_valid, y_valid)
        trial.report(intermediate_value, step)

        if trial.should_prune():
            raise optuna.TrialPruned()

    return clf.score(X_valid, y_valid)


study = optuna.create_study(
    direction="maximize", pruner=optuna.pruners.SuccessiveHalvingPruner()
)
study.optimize(objective, n_trials=20)
参数:
  • min_resource (str | int) –

    用于指定分配给 trial 的最小资源的参数(在 论文 中,此参数称为 \(r\))。此参数默认为 'auto',其值基于启发式方法确定,该方法会查看第一个 trial 完成所需的步数。

    一个 trial 在执行 \(\mathsf{min}\_\mathsf{resource} \times \mathsf{reduction}\_\mathsf{factor}^{ \mathsf{min}\_\mathsf{early}\_\mathsf{stopping}\_\mathsf{rate}}\) 步(即,第一轮的完成点)之前,永远不会被剪枝。当 trial 完成第一轮时,只有当 trial 的值排在前 \({1 \over \mathsf{reduction}\_\mathsf{factor}}\) 的比例(在所有已达到该点的 trial 中)时,它才会被提升到下一轮(否则将在那里被剪枝)。如果 trial 获胜,它将运行到下一个完成点(即 \(\mathsf{min}\_\mathsf{resource} \times \mathsf{reduction}\_\mathsf{factor}^{ (\mathsf{min}\_\mathsf{early}\_\mathsf{stopping}\_\mathsf{rate} + \mathsf{rung})}\) 步),并重复相同的过程。

    注意

    如果最后一个中间值的步数可能因 trial 而异,请手动指定最小可能步数给 min_resource

  • reduction_factor (int) – 用于指定可提升 trial 的缩减因子的参数(在 论文 中,此参数称为 \(\eta\))。在每一轮的完成点,大约会有 \({1 \over \mathsf{reduction}\_\mathsf{factor}}\) 的 trial 被提升。

  • min_early_stopping_rate (int) – 用于指定最小提前停止率的参数(在 论文 中,此参数称为 \(s\))。

  • bootstrap_count (int) – 在任何 trial 被考虑提升到下一轮之前,需要完成某轮的最小 trial 数量。

方法

prune(study, trial)

根据报告的值判断试验是否应被剪枝。

prune(study, trial)[源]

根据报告的值判断试验是否应被剪枝。

请注意,此方法不应由库用户调用。相反,optuna.trial.Trial.report()optuna.trial.Trial.should_prune() 提供了在目标函数中实现剪枝机制的用户界面。

参数:
  • study (Study) – 目标 study 的 study 对象。

  • trial (FrozenTrial) – 目标 trial 的 FrozenTrial 对象。修改此对象之前请复制一份。

返回:

一个布尔值,表示试验是否应被剪枝。

返回类型:

bool