22
探索空間と目的関数の定義
def objective(trial: optuna.Trial)-> float:
# 探索空間の定義です。 Pythonの条件分岐やforループを使うことができます。
iou_thresh = trial.suggest_float("iou_thresh", 0, 1)
opt = trial.suggest_categorical("opt", ["SGD", "Adam"])
n_layers = trial.suggest_int("n_layers", 2, 5)
n_channels = [
trial.suggest_int(f"n_channels_{i}", 32, 256) for i in range(n_layers)
]
...
# 目的関数です。モデルの訓練と評価を行います。
ap = train_and_val(iou_thresh, opt, n_layers, n_channels, ...)
return ap
23.
23
最適化の実行
study.optimize(objective, n_trials=50)
# [I2021-06-14 14:17:41,256] A new study created in RDB with name: my-study
# [I 2021-06-14 14:31:09,376] Trial 0 finished with value: 0.4808...
# [I 2021-06-14 14:46:23,466] Trial 1 finished with value: 0.3574...
# [I 2021-06-14 15:01:29,615] Trial 2 finished with value: 0.5250...
# …
# 結果を分析することができます
len(study.trials) # == 50
best_trial = study.best_trial
best_params = best_trial.params # Or, study.best_params.
# {'iou_thresh': 0.34907190168024279, ...}
24.
24
分散最適化
$ python optimize.py
[I2021-06-14 16:53:04,039] A new study created in RDB with name: my-study
[I 2021-06-14 17:08:04,775] Trial 0 finished with...
[I 2021-06-14 17:27:43,012] Trial 2 finished with...
[I 2021-06-14 17:59:12,598] Trial 3 finished with...
[I 2021-06-14 18:14:55,981] Trial 6 finished with...
$ python optimize.py
[I 2021-06-14 16:53:04,580] Using an existing study with name 'my-study'
instead of creating a new one.
[I 2021-06-14 17:31:35,011] Trial 1 finished with...
[I 2021-06-14 17:40:02,211] Trial 4 finished with...
[I 2021-06-14 18:01:43,645] Trial 5 finished with...
[I 2021-06-14 18:17:59,447] Trial 7 finished with...
ターミナル
B
ターミナル
A
25.
25
分析
Web GUI
$ optuna-dashboardsqlite:///my-storage.db
...
Listening on http://localhost:8080/
https://github.com/optuna/optuna-dashboard
Python API
optuna.visualization.plot_...(study).show()
https://optuna.readthedocs.io/en/v2.8.0/reference/visualization/index.html