LoginSignup
13
19

More than 5 years have passed since last update.

ハイパーパラメータを最適化するOptunaの使い方

Last updated at Posted at 2019-04-16

Optunaとは

Preferred Networks社が開発したハイパーパラメータ自動最適化ツールです。
機械学習プログラムを作成する際、手動でパラメータの調整するのはかなりの時間がかかります。
Optunaを使用することで、最適なパラメータを算出することが可能です。

使用環境

OS: Ubuntu16.04(WSLによる構築)
Python: 3.7.1

使い方

公式チュートリアルにある情報を参考にしています。

以下のメソッドを作成したとします。

main.py
#(x-2)を二乗するメソッド
def objective()
    return (x-2) ** 2

まずは適当にxに値を入れてみます。
xが5であれば返り値は9、xが3であれば返り値は1です。

しかしこのように人間が適当に値を入れて返り値を確認するのでは、時間がかかります。

ここでOptunaの登場です。
先程のメソッドを以下のように書き換えてみます。

main.py
# (x-2)を二乗するメソッド(最適化する対象のメソッドの引数にtrialを入れる)
def objective(trial):

    # 変数xに-10から10までの値の中から最適な値を算出してもらう
    x = trial.suggest_uniform('x', -10, 10)

    return (x - 2) ** 2

if __name__ == "__main__" :
    # optimizeの第一引数に対象のメソッドを指定、n_trialsにプログラムが試行錯誤する回数を指定
    study = optuna.create_study()
    study.optimize(objective, n_trials=10)

    # プログラムが試行錯誤した結果最も良いパラメータを表示
    print("params_{}".format(study.best_params))
    # 最も良いパラメータで実行したときの結果(返り値)を表示
    print("value_{}".format(study.best_value))

コメントをつけたので説明は不要かと思います。

実行すると以下の結果になりました。

[I 2019-04-16 21:03:32,576] Finished trial#0 resulted in value: 0.20119113879600378. Current best value is 0.20119113879600378 with parameters: {'x': 1.5514566478075906}.
[I 2019-04-16 21:03:32,578] Finished trial#1 resulted in value: 10.435502578888205. Current best value is 0.20119113879600378 with parameters: {'x': 1.5514566478075906}.
[I 2019-04-16 21:03:32,580] Finished trial#2 resulted in value: 3.571036101481987. Current best value is 0.20119113879600378 with parameters: {'x': 1.5514566478075906}.
[I 2019-04-16 21:03:32,580] Finished trial#3 resulted in value: 97.82212786563248. Current best value is 0.20119113879600378 with parameters: {'x': 1.5514566478075906}.
[I 2019-04-16 21:03:32,581] Finished trial#4 resulted in value: 1.3704269910753695. Current best value is 0.20119113879600378 with parameters: {'x': 1.5514566478075906}.
[I 2019-04-16 21:03:32,582] Finished trial#5 resulted in value: 20.918380466509028. Current best value is 0.20119113879600378 with parameters: {'x': 1.5514566478075906}.
[I 2019-04-16 21:03:32,583] Finished trial#6 resulted in value: 2.5374665783762294. Current best value is 0.20119113879600378 with parameters: {'x': 1.5514566478075906}.
[I 2019-04-16 21:03:32,583] Finished trial#7 resulted in value: 37.28904976220985. Current best value is 0.20119113879600378 with parameters: {'x': 1.5514566478075906}.
[I 2019-04-16 21:03:32,584] Finished trial#8 resulted in value: 101.48804583756308. Current best value is 0.20119113879600378 with parameters: {'x': 1.5514566478075906}.
[I 2019-04-16 21:03:32,585] Finished trial#9 resulted in value: 35.60409961501467. Current best value is 0.20119113879600378 with parameters: {'x': 1.5514566478075906}.
params_{'x': 1.5514566478075906}
value_0.20119113879600378

10回試行錯誤した結果、

変数xの最適なパラメータは1.5514566478075906
返り値は0.20119113879600378

という結果になりました。

今度はn_trials20にしてみます。

[I 2019-04-16 21:01:12,109] Finished trial#0 resulted in value: 129.62121766714577. Current best value is 129.62121766714577 with parameters: {'x': -9.385131429506897}.
[I 2019-04-16 21:01:12,110] Finished trial#1 resulted in value: 1.3700292573060269. Current best value is 1.3700292573060269 with parameters: {'x': 3.170482489106961}.
[I 2019-04-16 21:01:12,110] Finished trial#2 resulted in value: 2.4729398257825888. Current best value is 1.3700292573060269 with parameters: {'x': 3.170482489106961}.
[I 2019-04-16 21:01:12,111] Finished trial#3 resulted in value: 1.230744347958422. Current best value is 1.230744347958422 with parameters: {'x': 0.8906108221375053}.
[I 2019-04-16 21:01:12,112] Finished trial#4 resulted in value: 23.706190192052336. Current best value is 1.230744347958422 with parameters: {'x': 0.8906108221375053}.
[I 2019-04-16 21:01:12,113] Finished trial#5 resulted in value: 14.32215168666239. Current best value is 1.230744347958422 with parameters: {'x': 0.8906108221375053}.
[I 2019-04-16 21:01:12,114] Finished trial#6 resulted in value: 15.947993730614547. Current best value is 1.230744347958422 with parameters: {'x': 0.8906108221375053}.
[I 2019-04-16 21:01:12,115] Finished trial#7 resulted in value: 57.72935205282507. Current best value is 1.230744347958422 with parameters: {'x': 0.8906108221375053}.
[I 2019-04-16 21:01:12,116] Finished trial#8 resulted in value: 45.95103606701841. Current best value is 1.230744347958422 with parameters: {'x': 0.8906108221375053}.
[I 2019-04-16 21:01:12,117] Finished trial#9 resulted in value: 32.641155150040326. Current best value is 1.230744347958422 with parameters: {'x': 0.8906108221375053}.
[I 2019-04-16 21:01:12,119] Finished trial#10 resulted in value: 73.24873119946368. Current best value is 1.230744347958422 with parameters: {'x': 0.8906108221375053}.
[I 2019-04-16 21:01:12,121] Finished trial#11 resulted in value: 0.034037571494250074. Current best value is 0.034037571494250074 with parameters: {'x': 1.8155072589659689}.
[I 2019-04-16 21:01:12,123] Finished trial#12 resulted in value: 62.04197401290096. Current best value is 0.034037571494250074 with parameters: {'x': 1.8155072589659689}.
[I 2019-04-16 21:01:12,125] Finished trial#13 resulted in value: 22.900663626024777. Current best value is 0.034037571494250074 with parameters: {'x': 1.8155072589659689}.
[I 2019-04-16 21:01:12,127] Finished trial#14 resulted in value: 1.8656904796930245. Current best value is 0.034037571494250074 with parameters: {'x': 1.8155072589659689}.
[I 2019-04-16 21:01:12,129] Finished trial#15 resulted in value: 73.90198631741467. Current best value is 0.034037571494250074 with parameters: {'x': 1.8155072589659689}.
[I 2019-04-16 21:01:12,131] Finished trial#16 resulted in value: 1.2165258419418383. Current best value is 0.034037571494250074 with parameters: {'x': 1.8155072589659689}.
[I 2019-04-16 21:01:12,133] Finished trial#17 resulted in value: 2.2149363493299687. Current best value is 0.034037571494250074 with parameters: {'x': 1.8155072589659689}.
[I 2019-04-16 21:01:12,136] Finished trial#18 resulted in value: 17.569131479308282. Current best value is 0.034037571494250074 with parameters: {'x': 1.8155072589659689}.
[I 2019-04-16 21:01:12,139] Finished trial#19 resulted in value: 7.534211277306568. Current best value is 0.034037571494250074 with parameters: {'x': 1.8155072589659689}.
params_{'x': 1.8155072589659689}
value_0.034037571494250074

20回試行錯誤した結果、

変数xの最適なパラメータは1.8155072589659689
返り値は0.034037571494250074

になりました。

suggest_uniformsuggest_intにすると整数のみになります。

投稿時点(2019/04/16)でのOptunaは返り値を最小にする最適化のみ対応しているようです。

まとめ

Optunaを使えば機械学習に限らず、様々なパラメータの最適化が可能になります。

また、返り値を最大にするようにするには返り値にマイナスをかけてあげれば実現すると思います。

13
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
19