Skip to content

Commit aae2764

Browse files
committed
add to docs page
1 parent ffe8ddb commit aae2764

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

docs/how_to_guide.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Training
4646
how_to_guide/07_gpu_training.ipynb
4747
how_to_guide/07_save_and_load.ipynb
4848
how_to_guide/07_resume_training.ipynb
49+
how_to_guide/21_hyperparameter_tuning.ipynb
4950

5051

5152
Sampling

docs/how_to_guide/21_hyperparameter_tuning.ipynb

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,18 @@
1111
"cell_type": "markdown",
1212
"metadata": {},
1313
"source": [
14-
"This guide shows a minimal [`optuna`](https://optuna.org/) loop for hyperparameter tuning in `sbi`. It uses a toy simulator, `NPE`, an embedding network, and the `posterior_nn` helper. We tune just two hyperparameters: the embedding dimension and the number of flow transforms in an `nsf` density estimator."
15-
]
16-
},
17-
{
18-
"cell_type": "markdown",
19-
"metadata": {},
20-
"source": [
21-
"Optuna is not a dependency of `sbi`, you need to install it yourself in your\n",
14+
"This guide shows a minimal [`optuna`](https://optuna.org/) loop for hyperparameter\n",
15+
"tuning in `sbi`. Optuna is a lightweight hyperparameter optimization library. You define\n",
16+
"an objective function that trains a model (e.g., NPE) and returns a validation metric,\n",
17+
"and Optuna runs multiple trials to explore the search space and track the best\n",
18+
"configuration. As validation metric, we recommend using the negative log probability of\n",
19+
"a held-out validation set `(theta, x)` under the current posterior estimate (see\n",
20+
"Lueckmann et al. 2021 for details). \n",
21+
"\n",
22+
"Note that Optuna is not a dependency of `sbi`, you need to install it yourself in your\n",
2223
"environment. \n",
2324
"\n",
24-
"Optuna is a lightweight hyperparameter optimization library. You define an objective\n",
25-
"function that trains a model (e.g., NPE) and returns a validation metric, and Optuna runs multiple\n",
26-
"trials to explore the search space and track the best configuration. As validation\n",
27-
"metric, we recommend using the negative log probability of a held-out validation set\n",
28-
"`(theta, x)` under the current posterior estimate (see Lueckmann et al. 2021 for\n",
29-
"details). "
25+
"Here, we use a toy simulator and do `NPE` with an embedding network built using the `posterior_nn` helper. We tune just two hyperparameters: the embedding dimension and the number of flow transforms in an `nsf` density estimator."
3026
]
3127
},
3228
{
@@ -95,12 +91,27 @@
9591
},
9692
{
9793
"cell_type": "markdown",
94+
"id": "aad395b1",
9895
"metadata": {},
9996
"source": [
10097
"## Run the study and retrain\n",
10198
"\n",
99+
"Optuna defaults to the TPE sampler, which is a good starting point for many experiments.\n",
100+
"TPE (Tree-structured Parzen Estimator) is a Bayesian optimization method that\n",
101+
"models good vs. bad trials with nonparametric densities and samples new points\n",
102+
"that are likely to improve the objective. You can swap in other samplers (random\n",
103+
"search, GP-based, etc.) by passing a different sampler instance to `create_study`.\n",
104+
"\n",
105+
"The TPE sampler uses `n_startup_trials` random trials to seed the model. With\n",
106+
"`n_trials=25` and `n_startup_trials=10`, the first 10 trials are random and the\n",
107+
"remaining 15 are guided by the acquisition function. If you want to ensure to start at\n",
108+
"the default configuration, _enqueue_ it before optimization.\n",
109+
"\n",
102110
"```python\n",
103-
"study = optuna.create_study(direction=\"minimize\")\n",
111+
"sampler = optuna.samplers.TPESampler(n_startup_trials=10)\n",
112+
"study = optuna.create_study(direction=\"minimize\", sampler=sampler)\n",
113+
"# Optional: ensure the default config is evaluated\n",
114+
"study.enqueue_trial({\"embedding_dim\": 32, \"num_transforms\": 4})\n",
104115
"# This will run the above NPE training up to 25 times\n",
105116
"study.optimize(objective, n_trials=25)\n",
106117
"\n",
@@ -121,16 +132,6 @@
121132
"posterior = inference.build_posterior(final_estimator)\n",
122133
"```"
123134
]
124-
},
125-
{
126-
"cell_type": "markdown",
127-
"metadata": {},
128-
"source": [
129-
"## Notes\n",
130-
"\n",
131-
"- The toy simulator keeps the example short. Replace it with your simulator and prior.\n",
132-
"- You can expand the search space with additional `posterior_nn` arguments (e.g., `hidden_features`)."
133-
]
134135
}
135136
],
136137
"metadata": {

0 commit comments

Comments
 (0)