Slide 14
Slide 14 text
14
Optunaの基本的な使い方
1 目的関数を定義
2
サジェストAPI経由でハイパー
パラメーターをサンプル
3
最適化の状態を管理する
Studyオブジェクトを作成
4 試行回数を指定して最適化開始
5 結果の表示
import optuna
def objective(trial: optuna.Trial) -> float:
# ハイパーパラメーターのサンプル
x1 = trial.suggest_float("x1", -10, 10)
x2 = trial.suggest_float("x2", -10, 10)
# 評価値を計算してリターン
return (x1 - 1)**2 + (x2 + 5)**2
study = optuna.create_study()
study.optimize(objective, n_trials=100)
print(f"Best value: {study.best_value}")
print(f"Best params: {study.best_params}")
$ python example.py
Trial 0 finished with value: 83.8192 and parameters: {'x1': -8.1549, 'x2': -5.0722}
Trial 1 finished with value: 9.3275 and parameters: {'x1': -1.7273, 'x2': -3.6254}
Trial 2 finished with value: 79.0848 and parameters: {'x1': 9.8671, 'x2': -4.3235}
Trial 3 finished with value: 56.8358 and parameters: {'x1': -4.2930, 'x2': 0.3683}
Trial 4 finished with value: 198.6490 and parameters: {'x1': -4.0875, 'x2': 8.1440}
...
Best value: 0.02332568173253747
Best params: {'x1': 2.0699302180632904, 'x2': -4.864222806281179}
実行結果