|
11 | 11 | "cell_type": "markdown", |
12 | 12 | "metadata": {}, |
13 | 13 | "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", |
22 | 23 | "environment. \n", |
23 | 24 | "\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." |
30 | 26 | ] |
31 | 27 | }, |
32 | 28 | { |
|
95 | 91 | }, |
96 | 92 | { |
97 | 93 | "cell_type": "markdown", |
| 94 | + "id": "aad395b1", |
98 | 95 | "metadata": {}, |
99 | 96 | "source": [ |
100 | 97 | "## Run the study and retrain\n", |
101 | 98 | "\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", |
102 | 110 | "```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", |
104 | 115 | "# This will run the above NPE training up to 25 times\n", |
105 | 116 | "study.optimize(objective, n_trials=25)\n", |
106 | 117 | "\n", |
|
121 | 132 | "posterior = inference.build_posterior(final_estimator)\n", |
122 | 133 | "```" |
123 | 134 | ] |
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 | | - ] |
134 | 135 | } |
135 | 136 | ], |
136 | 137 | "metadata": { |
|
0 commit comments