Optuna
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)
探索空間
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
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 の状態は別途レストアしないといけない / プロセスがクラッシュするのにそなえた機構ないのかな?
TPESampler は seed だけ合わせておけばよい?
Sampler
デフォルトは TPESampler
optuna.create_study(sampler=optuna.samplers.RandomSampler()) のように指定
visualization
デフォルトは 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