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
2.1k
ハイパーパラメータ最適化フレームワーク 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
PLaMoの事後学習を支える技術 / PFN LLMセミナー
pfn
PRO
9
3.8k
Optuna DashboardにおけるPLaMo2連携機能の紹介 / PFN LLM セミナー
pfn
PRO
1
860
PLaMo2シリーズのvLLM実装 / PFN LLM セミナー
pfn
PRO
2
950
Function calling機能をPLaMo2に実装するには / PFN LLMセミナー
pfn
PRO
0
880
Optuna MCPサーバ開発 - AI Engineering Decoded #10
pfn
PRO
4
280
Kubernetes における cgroup v2 でのOut-Of-Memory 問題の解決
pfn
PRO
0
540
Preferred Networks (PFN) とLLM Post-Training チームの紹介 / 第4回 関東Kaggler会 スポンサーセッション
pfn
PRO
1
420
AIベンダーにおけるAIセキュリティ・ガバナンスへの取組
pfn
PRO
1
130
オフィス環境及び機械学習向けKubernetesクラスタでのAkamai SIA(DNS ファイアウォール)活用事例
pfn
PRO
0
120
Other Decks in Technology
See All in Technology
Pure Goで体験するWasmの未来
askua
1
170
AI ReadyなData PlatformとしてのAutonomous Databaseアップデート
oracle4engineer
PRO
0
150
Sidekiq その前に:Webアプリケーションにおける非同期ジョブ設計原則
morihirok
17
7.2k
許しとアジャイル
jnuank
1
110
定期的な価値提供だけじゃない、スクラムが導くチームの共創化 / 20251004 Naoki Takahashi
shift_evolve
PRO
3
290
SOC2取得の全体像
shonansurvivors
1
360
How to achieve interoperable digital identity across Asian countries
fujie
0
110
From Prompt to Product @ How to Web 2025, Bucharest, Romania
janwerner
0
110
関係性が駆動するアジャイル──GPTに人格を与えたら、対話を通してふりかえりを習慣化できた話
mhlyc
0
130
研究開発部メンバーの働き⽅ / Sansan R&D Profile
sansan33
PRO
3
20k
extension 現場で使えるXcodeショートカット一覧
ktombow
0
200
Railsアプリケーション開発者のためのブックガイド
takahashim
14
6.1k
Featured
See All Featured
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Testing 201, or: Great Expectations
jmmastey
45
7.7k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Building Better People: How to give real-time feedback that sticks.
wjessup
368
20k
Making the Leap to Tech Lead
cromwellryan
135
9.5k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.7k
Documentation Writing (for coders)
carmenintech
75
5k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.1k
Fireside Chat
paigeccino
40
3.7k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
Scaling GitHub
holman
463
140k
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