Exceptions¶
-
class
optuna.exceptions.
TrialPruned
[源代码]¶ 被剪枝 trial 异常。
该 error 表明:当前
Trial
已被剪枝。这种 error 一般会在optuna.trial.Trial.should_prune()
之后被抛出。具体见下面的例子。示例
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_uniform('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') study.optimize(objective, n_trials=20)
参见
optuna.TrialPruned
是该异常的一个别名。