Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
ハイパーパラメータ最適化フレームワーク Optunaの最新機能紹介 - 2023/10/28 ...
Search
Preferred Networks
PRO
October 27, 2023
Technology
2
2k
ハイパーパラメータ最適化フレームワーク Optunaの最新機能紹介 - 2023/10/28 PyCon APAC 2023
Optunaの最新リリースv3.4で導入された新機能について紹介いたします。
イベントサイト:
https://2023-apac.pycon.jp/
Preferred Networks
PRO
October 27, 2023
Tweet
Share
More Decks by Preferred Networks
See All by Preferred Networks
Kubernetes における cgroup v2 でのOut-Of-Memory 問題の解決
pfn
PRO
0
480
Preferred Networks (PFN) とLLM Post-Training チームの紹介 / 第4回 関東Kaggler会 スポンサーセッション
pfn
PRO
1
350
AIベンダーにおけるAIセキュリティ・ガバナンスへの取組
pfn
PRO
1
99
オフィス環境及び機械学習向けKubernetesクラスタでのAkamai SIA(DNS ファイアウォール)活用事例
pfn
PRO
0
100
Deploying PLaMo 2 with vLLM: A Practical Guide / vLLM roundup Community Meetup Tokyo
pfn
PRO
1
470
New Cache Hierarchy for Container Images and OCI Artifacts in Kubernetes Clusters using Containerd / KubeCon + CloudNativeCon Japan
pfn
PRO
0
290
Preferred Networks金融チームのご紹介
pfn
PRO
4
2k
KubeCon + CloudNativeCon Europe 2025 Recap: The GPUs on the Bus Go 'Round and 'Round / Kubernetes Meetup Tokyo #70
pfn
PRO
0
320
LLMの開発と社会実装の今と未来 / AI Builders' Community (ABC) vol.2
pfn
PRO
3
740
Other Decks in Technology
See All in Technology
企業の生成AIガバナンスにおけるエージェントとセキュリティ
lycorptech_jp
PRO
2
160
Obsidian応用活用術
onikun94
1
460
AIエージェント開発用SDKとローカルLLMをLINE Botと組み合わせてみた / LINEを使ったLT大会 #14
you
PRO
0
100
品質視点から考える組織デザイン/Organizational Design from Quality
mii3king
0
190
250905 大吉祥寺.pm 2025 前夜祭 「プログラミングに出会って20年、『今』が1番楽しい」
msykd
PRO
1
680
Terraformで構築する セルフサービス型データプラットフォーム / terraform-self-service-data-platform
pei0804
1
150
ハードウェアとソフトウェアをつなぐ全てを内製している企業の E2E テストの作り方 / How to create E2E tests for a company that builds everything connecting hardware and software in-house
bitkey
PRO
1
120
5分でカオスエンジニアリングを分かった気になろう
pandayumi
0
210
エラーとアクセシビリティ
schktjm
1
1.2k
開発者を支える Internal Developer Portal のイマとコレカラ / To-day and To-morrow of Internal Developer Portals: Supporting Developers
aoto
PRO
1
440
2025年になってもまだMySQLが好き
yoku0825
8
4.6k
20250910_障害注入から効率的復旧へ_カオスエンジニアリング_生成AIで考えるAWS障害対応.pdf
sh_fk2
3
220
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Bash Introduction
62gerente
615
210k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
31
2.2k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.6k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.4k
What's in a price? How to price your products and services
michaelherold
246
12k
Code Reviewing Like a Champion
maltzj
525
40k
Imperfection Machines: The Place of Print at Facebook
scottboms
268
13k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.1k
YesSQL, Process and Tooling at Scale
rocio
173
14k
Intergalactic Javascript Robots from Outer Space
tanoku
272
27k
A designer walks into a library…
pauljervisheath
207
24k
Transcript
ハイパーパラメータ最適化フレームワーク Optunaの最新機能紹介 Masashi Shibata PyCon APAC 2023 in Tokyo, Japan
2023.10.27-2023.10.28
MASASHI SHIBATA Preferred Networks, Inc. Release Manager of Optuna 3.3
and 3.4 Creator of Optuna Dashboard GitHub @c-bata / X @c_bata_
3 Optuna 3.4がリリースされました! https://x.com/OptunaAutoML/status/1714181590354739605
4 本発表の内容 1 Optuna / Optuna Dashboardの概要と基本的な使い方 2 Optuna Artifactを使った実験管理
3 Preferential Optimizationと生成AIへの活用 4 Jupyter Lab拡張とVS Code拡張
5 Optunaの概要と基本的な使い方
6 Optunaとは? ハイパラ最適化を表す絵 機械学習のハイパー パラメータ最適化 自律移動ロボット 3D CAD (Tunny) ※1
$ pip install optuna ハイパーパラメータ最適化フレームワーク ※1 詳細は下記Optuna公式ブログ記事を参照 https://medium.com/optuna/black-box-optimization-of-geometry-and-functionality-by-integrating-optuna-and-3d-cad-f2d2984d263e
7 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 - 2)**2 + (x2 + 5)**2 study = optuna.create_study( storage="sqlite:///db.sqlite3", study_name="optimize-quadratic-function" ) study.optimize(objective, n_trials=100) print(f"Best value: {study.best_value}") print(f"Best params: {study.best_params}")
8 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 - 2)**2 + (x2 + 5)**2 study = optuna.create_study( storage="sqlite:///db.sqlite3" study_name="optimize-quadratic-function" ) study.optimize(objective, n_trials=100) print(f"Best value: {study.best_value}") print(f"Best params: {study.best_params}") Trialオブジェクトを受け取り 評価値(float)を返す関数を定義
9 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 - 2)**2 + (x2 + 5)**2 study = optuna.create_study( storage="sqlite:///db.sqlite3" study_name="optimize-quadratic-function" ) study.optimize(objective, n_trials=100) print(f"Best value: {study.best_value}") print(f"Best params: {study.best_params}") ※この最小化問題の解は (x1, x2) = (2, -5)
10 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 - 2)**2 + (x2 + 5)**2 study = optuna.create_study( storage="sqlite:///db.sqlite3" study_name="optimize-quadratic-function" ) study.optimize(objective, n_trials=100) print(f"Best value: {study.best_value}") print(f"Best params: {study.best_params}") x1 および x2 の探索空間を定義 今回はどちらも区間 [-10, 10] の中で探索
11 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 - 2)**2 + (x2 + 5)**2 study = optuna.create_study( storage="sqlite:///db.sqlite3", study_name="optimize-quadratic-function" ) study.optimize(objective, n_trials=100) print(f"Best value: {study.best_value}") print(f"Best params: {study.best_params}") 最適化履歴をデータベースに保存
12 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 - 2)**2 + (x2 + 5)**2 study = optuna.create_study( storage="sqlite:///db.sqlite3" study_name="optimize-quadratic-function" ) study.optimize(objective, n_trials=100) print(f"Best value: {study.best_value}") print(f"Best params: {study.best_params}") 目的関数を100回呼び出し
13 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 - 2)**2 + (x2 + 5)**2 study = optuna.create_study( storage="sqlite:///db.sqlite3" study_name="optimize-quadratic-function" ) study.optimize(objective, n_trials=100) print(f"Best value: {study.best_value}") print(f"Best params: {study.best_params}")
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} 実行結果
15 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}") Optuna Dashboardによる履歴の確認 $ pip install optuna-dashboard $ optuna-dashboard sqlite:///db.sqlite3
16 より詳細な使い方は書籍をチェック! 初学者にとって最適な一冊です • 丁寧なチュートリアル • 様々な便利機能の紹介 • 様々な応用事例の紹介 •
アルゴリズムの詳細 好評発売中です!
17 最新機能紹介 ① Optuna Artifact を使った 実験管理
18 Artifactによるファイル管理 学習済みモデルや画像など大きな データもOptunaで管理が可能に! Optuna Artifactを使った実験管理 import optuna from optuna.artifacts
import FileSystemArtifactStore from optuna.artifacts import upload_artifact # この例では ./artifacts ディレクトリ以下に生成物を保存 artifact_store = FileSystemArtifactStore("./artifacts") def objective(trial: optuna.Trial) -> float: param = trial.suggest_float(...) file_path = generate_image(param, ...) # 生成物のアップロード (e.g. 学習済みモデル等) upload_artifact(trial, file_path, artifact_store) return ... 画像をArtifact Storeにアップロード ※ AWS S3やGoogle Cloud Storageにも対応
19 Optuna Dashboardでの確認 $ optuna-dashboard sqlite:///db.sqlite3 \ --artifact-dir ./artifacts コマンドラインでの起動方法
Python APIでの起動方法 AWS S3等にアップロードしたファイル を閲覧するにはPython APIを使用 from optuna.storages import RDBStorage from optuna.artifacts import Boto3ArtifactStore from optuna_dashboard import run_server storage = RDBStorage("sqlite:///db.sqlite3") artifact_store = Boto3ArtifactStore("my-bucket") run_server(storage, artifact_store=artifact_store)
20 Optuna Dashboardでの確認 $ optuna-dashboard sqlite:///db.sqlite3 \ --artifact-dir ./artifacts Optuna
Dashboardの起動方法 コマンドラインオプションの使用 Python APIでの起動方法 AWS S3等にアップロードしたファイル を閲覧するにはPython APIを使用 from optuna.storages import RDBStorage from optuna.artifacts import Boto3ArtifactStore from optuna_dashboard import run_server storage = RDBStorage("sqlite:///db.sqlite3") artifact_store = Boto3ArtifactStore("my-bucket") run_server(storage, artifact_store=artifact_store) 様々なファイル形式に対応! 3Dモデル 分子構造 音声
21 最新機能紹介 ② Preferential Optimization と生成AIへの活用
22 音声合成 生成AIにおけるハイパーパラメータ最適化 定量的に評価値を計算できず、人間による確認(主観評価)が重要 画像生成 自然言語生成 Optuna Dashboard はPythonのハイパー パラメーター最適化フ
レームワークです。。。
23 Preferential Optimization チュートリアル
24 お題:かわいいOptunaくんの生成 (Stable Diffusion) Hey! Please make me cuter 🙏
Optunaくん Sure. Let’s try using Stable Diffusion! Me
25 お題:かわいいOptunaくんの生成 (Stable Diffusion) 入力画像 Stable Diffusion プロンプト a mascot
character with two eyes and a mouth, smiling, charming, painting huggingface.co/stabilityai/ stable-diffusion-2-1 ? ナンカチガウ... 出力画像
26 お題:かわいいOptunaくんの生成 (Stable Diffusion) Hmm… 😫 Let me optimize a
prompt with Optuna. Optunaくん But wait… How am I supposed to score how cute I am? 🤔 It’s time to use Preferential Optimization! Me
27 Preferential Optimization による相対評価 2つのプログラムが協調的に動作しながら最適化を進める
28 Optuna Dashboardの操作画面 I prefer A over B! I prefer
B over A!
29 新 Optunaくん チュートリアルのお題:画像生成(img2img) 旧 Optunaくん Cool! The left one
is exactly I wanted 🥰 Me These images generated!
30 Preferential Optimizationのコード解説 generator.py ソースコードURL👇 https://gist.github.com/c-bata/449f2e90ac50a1285b7fe210ab51eae6 これから解説するコード Database & File
Storage Optuna Storage sqlite:///db.sqlite3 Artifact Store FileSystemBackend Optuna Dashboard 1. Studyの作成 2. 画面に表示するArtifactの指定 3. 新しいTrialの生成 4. パラメーターのサンプル 5. Stable Diffusionモデル実行 6. 画像アップロード 7. 以降、Step 3-6を繰り返す Optuna Dashboardの起動 $ optuna-dashboard …
31 公式チュートリアルもチェック! https://optuna-dashboard.readthedocs.io/en/latest /tutorials/preferential-optimization.html 夕焼け色(オレンジ色)のRGBカラーコードをOptunaで探索するチュートリアル
32 最新機能紹介 ③ Jupyter Lab拡張 VS Code拡張
33 $ pip install jupyterlab jupyterlab-optuna
34 Visual Studio Marketplace からインストール!
35 まとめ
36 本発表で話したこと • Optunaの基本的な使い方 • Artifact機能を使った実験管理 • Preferential Optimizationと生成AIへの活用 •
VS Code拡張やJupyter Lab拡張の紹介 最後におねがい • Optunaをみなさまのプロジェクトでご活用ください!気に入った方はGithub Starsもぜひおねがいします。 • Preferential OptimizationやArtifactなど最新機能の活用事例を ぜひブログやSNSでシェアしてください! おわりに
Making the real world computable