Optuna
optuna/optuna: A hyperparameter optimization framework
Optuna Dashboard — Optuna Dashboard documentation
Optuna - 株式会社Preferred Networks
Optuna - A hyperparameter optimization framework
AtsushiSakai/optuna_sample: optuna sample codes
02_Optuna_Advanced_Tutorial_ja.ipynb - Colab
code:sample.py
import optuna
def objective(trial):
x = trial.suggest_float("x", -5, 5)
y = trial.suggest_float("y", -5, 5)
return 2 * x ** 2 - 1.05 * x ** 4 + x ** 6 / 6 + x * y + y ** 2
study = optuna.create_study()
study.optimize(objective, n_trials=100)
print(study.best_value, study.best_params)
探索空間
optuna.trial.Trial — Optuna 4.0.0 documentation
suggest_categorical(name, choices)
suggest_discrete_uniform(name, low, high, q)
suggest_float(name, low, high, *[, step, log])
suggest_int(name, low, high, *[, step, log])
deprecated
_discrete_uniform, _uniform, _loguniform は deprecated
対数は log=True オプション suggest_float(..., log=True)
離散値は step=0.1 オプション suggest_float(..., step=0.2)
Study
optuna.study.create_study — Optuna 4.0.0 documentation
Storage への永続化
sqlite 使えるのでローカルでも大抵指定しておくとよさそう
code:storage.py
optuna.create_study(
study_name='test',
storage="sqlite:////content/test.db", # colab の content、絶対パスは slash 4 つになる
load_if_exists=True,
)
study.trials_dataframe() で DataFrame で得られる
レストア
失敗したもの取り出す
[t for t in study.trials if t.state != optuna.trial.TrialState.COMPLETE]
study.enqueue_trial(study.trials[-1].params) で続きからやるテクがあるけど sampler や pruner の状態は別途レストアしないといけない / プロセスがクラッシュするのにそなえた機構ないのかな?
Saving/Resuming Study with RDB Backend — Optuna 4.0.0 documentation
TPESampler は seed だけ合わせておけばよい?
Add explanation to restore sampler when calling Study.tell · Issue 3640 · optuna/optuna
Sampler
Efficient Optimization Algorithms — Optuna 4.0.0 documentation
optuna.samplers — Optuna 4.0.0 documentation
デフォルトは TPESampler
optuna.create_study(sampler=optuna.samplers.RandomSampler()) のように指定
visualization
Quick Visualization for Hyperparameter Optimization Analysis — Optuna 4.0.0 documentation
デフォルトは plotly が使われる、optuna.visualization.matplotlib 以下の同名のメソッドは plt 版
optuna.visualization.plot_optimization_history(study)
optuna.visualization.plot_param_importances(study)
optuna.visualization.plot_contour(study) ← かっこいい
https://gyazo.com/c749c89d302119abcd76c61b8bddd69b
optuna.visualization.plot_slice(study)
これどう読むか分かってない
https://gyazo.com/27729d559b012ddc63ff90e48372147b
optuna.visualization.plot_parallel_coordinate(study)
https://gyazo.com/73545dec3a57785db326f811991343fe