重新使用最佳值

有时候, 在完成超参数优化以后, 你可能要用最佳的超参数对目标函数重新求值.

比如,

  • 你已经用 Optuna 发现了好的超参数, 并且想用已经发现的最佳超参数来运行一个类似的 objective 函数以进一步分析结果, 或者

  • 为节省时间, 你已经用 Optuna 来优化了一个部分数据集. 在超参数调整以后, 你想在整个数据集上用你找到的最佳超参数来训练模型.

best_trial 提供了一个接口, 用当前的最佳超参数值对目标函数进行重新求值.

像上面的第一个例子一样, 本教程展示了利用当前最佳值来重新运行一个不同的 objective 函数的例子.

进一步研究最佳模型

考虑一个如下的采用了 Optuna 的经典有监督分类问题

from sklearn import metrics
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split


import optuna


def objective(trial):
    X, y = make_classification(n_features=10, random_state=1)
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

    C = trial.suggest_loguniform("C", 1e-7, 10.0)

    clf = LogisticRegression(C=C)
    clf.fit(X_train, y_train)

    return clf.score(X_test, y_test)


study = optuna.create_study(direction="maximize")
study.optimize(objective, n_trials=10)

print(study.best_trial.value)  # Show the best value.

Out:

0.92

假设在超参数优化之后, 你想计算出同一个数据集上的其他的测度, 比如召回率, 精确度和 f1-score. 你可以定义另一个目标函数, 令其和 objective 高度相似, 以用最佳超参数来重现模型.

def detailed_objective(trial):
    # Use same code objective to reproduce the best model
    X, y = make_classification(n_features=10, random_state=1)
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

    C = trial.suggest_loguniform("C", 1e-7, 10.0)

    clf = LogisticRegression(C=C)
    clf.fit(X_train, y_train)

    # calculate more evaluation metrics
    pred = clf.predict(X_test)

    acc = metrics.accuracy_score(pred, y_test)
    recall = metrics.recall_score(pred, y_test)
    precision = metrics.precision_score(pred, y_test)
    f1 = metrics.f1_score(pred, y_test)

    return acc, f1, recall, precision

study.best_trial 传递给 detailed_objective 做参数.

detailed_objective(study.best_trial)  # calculate acc, f1, recall, and precision

Out:

(0.92, 0.9285714285714286, 0.9285714285714286, 0.9285714285714286)

best_trial 和普通的 trials 的不同之处

这里使用了 best_trial, 它返回一个 best_trial, 属于 FrozenTrial 类型. FrozenTrial 与活跃的 trial 不同, 而且在某些情况下它的行为和 Trial 也不一样. 比如, 剪枝没法使用, 因为 should_prune 总是返回 False.

Total running time of the script: ( 0 minutes 0.062 seconds)

Gallery generated by Sphinx-Gallery