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\) 的最大资源参数。分配给试验的最大资源通常在目标函数内部进行限制(例如,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) – 用于指定分配给试验的最小资源的参数(在 论文 中,此参数被称为 \(r\))。此参数默认为 ‘auto’,其中值是根据启发式方法确定的,该方法查看第一个试验完成所需步骤的数量。试验在执行 \(\mathsf{min}\_\mathsf{resource} \times \mathsf{reduction}\_\mathsf{factor}^{ \mathsf{min}\_\mathsf{early}\_\mathsf{stopping}\_\mathsf{rate}}\) 步之前不会被修剪(即,第一个梯级的完成点)。当试验完成第一个梯级时,只有当试验的值位于已经达到该点的所有试验中的前 \({1 \over \mathsf{reduction}\_\mathsf{factor}}\) 部分时,它才会被提升到下一个梯级(否则它将被修剪)。如果试验赢得了竞争,它将运行到下一个完成点(即,\(\mathsf{min}\_\mathsf{resource} \times \mathsf{reduction}\_\mathsf{factor}^{ (\mathsf{min}\_\mathsf{early}\_\mathsf{stopping}\_\mathsf{rate} + \mathsf{rung})}\) 步)并重复相同的过程。 .. note:
reduction_factor (int) – 用于指定可提升试验的减少因子(在 论文 中,此参数被称为 \(\eta\))。 在每个阶段的完成点,大约 \({1 \over \mathsf{reduction}\_\mathsf{factor}}\) 的试验将被提升。
min_early_stopping_rate (int) – 用于指定最小早停率的参数(在 论文 中,此参数被称为 \(s\))。
bootstrap_count (int) – 在任何试验被考虑晋升到下一级之前,需要完成的最低试验次数。
方法
prune(study, trial)根据报告的值判断是否应该修剪试验。
- prune(study, trial)[源代码]
根据报告的值判断是否应该修剪试验。
请注意,此方法不应由库用户调用。相反,
optuna.trial.Trial.report()和optuna.trial.Trial.should_prune()提供了用户界面,用于在目标函数中实现剪枝机制。- 参数:
study (Study) – 目标研究的研究对象。
trial (FrozenTrial) – 目标试验的 FrozenTrial 对象。在修改此对象之前,请先复制一份。
- 返回:
一个布尔值,表示是否应该修剪试验。
- 返回类型: