Core¶
-
optuna.create_study(storage=None, sampler=None, pruner=None, study_name=None, direction='minimize', load_if_exists=False)[源代码]¶ 创建新
Study.- 参数
storage --
数据库 URL. 如果该参数被设置成 None, 那么Optuna将使用 in-memory 存储。此时,
Study将无法被持久化。注解
当数据库 URL被传入该方法后,optuna 会在内部调用 SQLAlchemy 来处理数据库。具体细节见 SQLAlchemy's document 如果你想定制 SQLAlchemy Engine 的话,你可以在将
RDBStorage实例化时传入你想要的参数并且将该实例,而不是一个URL, 作为storage参数传进去。sampler -- Sampler 对象负责实现参数值采样。如果该参数设置为
None的话,默认 sampler 将是TPESampler. 具体见samplers.pruner -- Prunner 对象,决定是否提前终止无望的 trial. 具体见
pruners.study_name -- Study 名。如果该参数被设置成 None 的话,optuna 将自动生成一个唯一的 study name.
direction -- 优化的方向。设置成
minimize的话就是最小化,反之maximize则是最大化。load_if_exists -- 用于处理冲突的 study 名的选项。如果 load_if_exists` 是设置成
False的话,当storage中已经存在了一个名为study_name的 study 时DuplicatedStudyError异常就会被抛出。否则,创建 study 的过程将会被跳过,而已经存在的那个 study 会被加载。
- 返回
A
Studyobject.
参见
这是 optuna.study.create_study() 的一个别名。
-
optuna.load_study(study_name, storage, sampler=None, pruner=None)[源代码]¶ 加载一个已经指定名称的已存在的
Study.- 参数
study_name -- Study 名。每一个 study 都有一个作为标识符的唯一的名字。
storage -- 类似
sqlite:///example.db的数据库URL. 更多细节见create_study()的文档。sampler -- Sampler 对象负责实现参数值采样。如果该参数设置为
None的话,默认 sampler 将是TPESampler. 具体见samplers.pruner -- Prunner 对象,决定是否提前终止无望的 trial. 如果该选项设置成
None的话,MedianPruner将会作为默认 pruner 被启用。参见pruners.
参见
这是 optuna.study.load_study() 的一个别名。
-
optuna.delete_study(study_name, storage)[源代码]¶ 删除
Study对象。- 参数
study_name -- Study 名。
storage -- 类似
sqlite:///example.db的数据库URL. 更多细节见create_study()的文档。
参见
这是 optuna.study.delete_study() 的一个别名。
-
optuna.get_all_study_summaries(storage)[源代码]¶ 获取指定存储内的所有 study 历史记录。
- 参数
storage -- 类似
sqlite:///example.db的数据库URL. 更多细节见create_study()的文档。- 返回
以
StudySummary形式总结的 study 列表。
参见
这是 optuna.study.get_all_study_summaries() 的一个别名。
-
class
optuna.TrialPruned[源代码]¶ 被剪枝 trial 异常。
该报错告诉使用者当前
Trial已被剪枝。如下例所示,它会在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.exceptions.TrialPruned() 的一个别名。