Skip to content

Commit ebb8c6f

Browse files
abelabajanfb
andauthored
docs: deprecation warnings for build_posterior stringly typed parameters (#1627)
* refactor: add deprecation warnings for deprecated build_posterior arguments * test: update tests to work with updated build_posterior logic * docs: update tutorials to use strongly typed build_posterior logic * Update logic for raising warning for deprecated build_posterior parameters * test: update mcmc configuration dict fixtures to MCMCPosteriorParameters * docs: add how to guide for using PosteriorParameters * chore: move posterior parameters resolution to a new method * Revert "refactor: add deprecation warnings for deprecated build_posterior arguments" This reverts commit be39800. * test: update mcmc_params_fast test initialization * update deprecation warning message and warning type on test * Apply suggestion from @janfb Co-authored-by: Jan <janfb@users.noreply.github.com> * Apply suggestion from @janfb Co-authored-by: Jan <janfb@users.noreply.github.com> * docs: remove max_num_epochs from posterior parameters tutorial --------- Co-authored-by: Jan <janfb@users.noreply.github.com>
1 parent 671f596 commit ebb8c6f

22 files changed

+479
-170
lines changed

docs/advanced_tutorials/12_iid_data_and_permutation_invariant_embeddings.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"\n",
6363
"from sbi.analysis import pairplot\n",
6464
"from sbi.inference import NLE, NPE, simulate_for_sbi\n",
65+
"from sbi.inference.posteriors.posterior_parameters import MCMCPosteriorParameters\n",
6566
"from sbi.simulators.linear_gaussian import (\n",
6667
" linear_gaussian,\n",
6768
" true_posterior_linear_gaussian_mvn_prior,\n",
@@ -220,17 +221,16 @@
220221
"nle_samples = []\n",
221222
"num_samples = 1000\n",
222223
"\n",
223-
"mcmc_parameters = dict(\n",
224+
"mcmc_parameters = MCMCPosteriorParameters(\n",
225+
" method=\"slice_np_vectorized\",\n",
224226
" num_chains=50,\n",
225227
" thin=5,\n",
226228
" warmup_steps=30,\n",
227229
" init_strategy=\"proposal\",\n",
228230
")\n",
229-
"mcmc_method = \"slice_np_vectorized\"\n",
230231
"\n",
231232
"posterior = inferer.build_posterior(\n",
232-
" mcmc_method=mcmc_method,\n",
233-
" mcmc_parameters=mcmc_parameters,\n",
233+
" posterior_parameters=mcmc_parameters,\n",
234234
")\n",
235235
"\n",
236236
"# Generate samples with MCMC given the same set of x_os as above.\n",
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "898a1f9b",
6+
"metadata": {},
7+
"source": [
8+
"# How to use `PosteriorParameters` in sbi\n",
9+
"\n",
10+
"The `PosteriorParameters` classes in SBI are dataclasses used to initialize parameters for different types of posterior algorithms, when using the `build_posterior` method. This guide shows how to configure and use PosteriorParameters in your workflow.\n",
11+
"\n",
12+
"`sbi` currently implements the following PosteriorParameters subclasses. Each subclass of `PosteriorParameters` holds parameters for a specific posterior sampling method. \n",
13+
"\n",
14+
"- `DirectPosteriorParameters`: used with DirectPosterior\n",
15+
"\n",
16+
"- `ImportanceSamplingPosteriorParameters`: used with ImportanceSamplingPosterior\n",
17+
"\n",
18+
"- `MCMCPosteriorParameters`: used with MCMCPosterior\n",
19+
"\n",
20+
"- `RejectionPosteriorParameters`: used with RejectionPosterior\n",
21+
"\n",
22+
"- `VectorFieldPosteriorParameters`: : used with VectorFieldPosterior\n",
23+
"\n",
24+
"- `VIPosteriorParameters`: used with VIPosterior"
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"id": "8c4df01a",
30+
"metadata": {},
31+
"source": [
32+
"# Usage"
33+
]
34+
},
35+
{
36+
"cell_type": "markdown",
37+
"id": "a7c6d86e",
38+
"metadata": {},
39+
"source": [
40+
"The example below uses `MCMCPosteriorParameters` for passing MCMC options to a posterior sampling via MCMC. You can follow a similar approach as the example below with your `sbi` workflow when using the other `PosteriorParameters` types."
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": null,
46+
"id": "5c1d2e5a",
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"import torch\n",
51+
"\n",
52+
"from sbi.inference import NRE\n",
53+
"from sbi.utils.torchutils import BoxUniform\n",
54+
"\n",
55+
"\n",
56+
"def simulator(theta):\n",
57+
" return 1.0 + theta + torch.randn(theta.shape, device=theta.device) * 0.1\n",
58+
"\n",
59+
"num_dim = 3\n",
60+
"prior = BoxUniform(low=-2 * torch.ones(num_dim), high=2 * torch.ones(num_dim))\n",
61+
"theta = prior.sample((300,))\n",
62+
"x = simulator(theta)\n",
63+
"\n",
64+
"inference = NRE(prior=prior)\n",
65+
"inference.append_simulations(theta, x)\n",
66+
"inference.train()"
67+
]
68+
},
69+
{
70+
"cell_type": "markdown",
71+
"id": "894a2aa3",
72+
"metadata": {},
73+
"source": [
74+
"Create an instance of `MCMCPosteriorParameters` with desired values to configure MCMC sampling."
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"id": "d41d021b",
81+
"metadata": {},
82+
"outputs": [],
83+
"source": [
84+
"from sbi.inference.posteriors.posterior_parameters import MCMCPosteriorParameters\n",
85+
"\n",
86+
"params = MCMCPosteriorParameters(\n",
87+
" method=\"slice_np_vectorized\",\n",
88+
" thin=1,\n",
89+
" warmup_steps=100,\n",
90+
" num_chains=4,\n",
91+
" init_strategy=\"sir\",\n",
92+
" init_strategy_parameters={\"num_candidate_samples\": 1000},\n",
93+
" num_workers=2,\n",
94+
" mp_context=\"spawn\"\n",
95+
")"
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"id": "b61e8eac",
101+
"metadata": {},
102+
"source": [
103+
"Now pass the instance to `build_posterior()` using the `posterior_parameters` argument."
104+
]
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": null,
109+
"id": "86c1a6cf",
110+
"metadata": {},
111+
"outputs": [],
112+
"source": [
113+
"inference.build_posterior(posterior_parameters=params)"
114+
]
115+
},
116+
{
117+
"cell_type": "markdown",
118+
"id": "eaaf79ce",
119+
"metadata": {},
120+
"source": [
121+
"## Modifying Parameters with `.with_param()`\n",
122+
"You can create a new copy with updated fields without mutating the original.\n"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": null,
128+
"id": "b62cd717",
129+
"metadata": {},
130+
"outputs": [],
131+
"source": [
132+
"new_params = params.with_param(num_chains=8, warmup_steps=200)\n",
133+
"\n",
134+
"inference.build_posterior(posterior_parameters=new_params)"
135+
]
136+
}
137+
],
138+
"metadata": {
139+
"kernelspec": {
140+
"display_name": "sbi",
141+
"language": "python",
142+
"name": "python3"
143+
},
144+
"language_info": {
145+
"name": "python",
146+
"version": "3.10.16"
147+
}
148+
},
149+
"nbformat": 4,
150+
"nbformat_minor": 5
151+
}

docs/tutorials/16_implemented_methods.ipynb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,17 @@
250250
"outputs": [],
251251
"source": [
252252
"from sbi.inference import NLE\n",
253+
"from sbi.inference.posteriors.posterior_parameters import MCMCPosteriorParameters\n",
253254
"\n",
254255
"inference = NLE(prior)\n",
255256
"proposal = prior\n",
256257
"for _ in range(num_rounds):\n",
257258
" theta = proposal.sample((num_sims,))\n",
258259
" x = simulator(theta)\n",
259260
" _ = inference.append_simulations(theta, x).train()\n",
260-
" posterior = inference.build_posterior(mcmc_method=\"slice_np_vectorized\",\n",
261-
" mcmc_parameters={\"num_chains\": 20,\n",
262-
" \"thin\": 5})\n",
261+
" posterior = inference.build_posterior(posterior_parameters=MCMCPosteriorParameters(method=\"slice_np_vectorized\", num_chains=20,\n",
262+
" thin=5))\n",
263+
"\n",
263264
" proposal = posterior.set_default_x(x_o)"
264265
]
265266
},
@@ -279,15 +280,15 @@
279280
"outputs": [],
280281
"source": [
281282
"from sbi.inference import NLE\n",
283+
"from sbi.inference.posteriors.posterior_parameters import VIPosteriorParameters\n",
282284
"\n",
283285
"inference = NLE(prior)\n",
284286
"proposal = prior\n",
285287
"for _ in range(num_rounds):\n",
286288
" theta = proposal.sample((num_sims,))\n",
287289
" x = simulator(theta)\n",
288290
" _ = inference.append_simulations(theta, x).train()\n",
289-
" posterior = inference.build_posterior(sample_with=\"vi\",\n",
290-
" vi_method=\"fKL\").set_default_x(x_o)\n",
291+
" posterior = inference.build_posterior(posterior_parameters=VIPosteriorParameters(vi_method=\"fKL\")).set_default_x(x_o)\n",
291292
" proposal = posterior.train() # Train VI posterior on given x_o."
292293
]
293294
},
@@ -364,16 +365,17 @@
364365
"outputs": [],
365366
"source": [
366367
"from sbi.inference import NRE\n",
368+
"from sbi.inference.posteriors.posterior_parameters import MCMCPosteriorParameters\n",
367369
"\n",
368370
"inference = NRE(prior)\n",
369371
"proposal = prior\n",
370372
"for _ in range(num_rounds):\n",
371373
" theta = proposal.sample((num_sims,))\n",
372374
" x = simulator(theta)\n",
373375
" _ = inference.append_simulations(theta, x).train()\n",
374-
" posterior = inference.build_posterior(mcmc_method=\"slice_np_vectorized\",\n",
375-
" mcmc_parameters={\"num_chains\": 20,\n",
376-
" \"thin\": 5})\n",
376+
" posterior = inference.build_posterior(posterior_parameters=MCMCPosteriorParameters(method=\"slice_np_vectorized\", num_chains=20,\n",
377+
" thin=5))\n",
378+
"\n",
377379
" proposal = posterior.set_default_x(x_o)"
378380
]
379381
},

docs/tutorials/Example_01_DecisionMakingModel.ipynb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"\n",
8787
"from sbi.analysis import pairplot\n",
8888
"from sbi.inference import MNLE, MCMCPosterior\n",
89+
"from sbi.inference.posteriors.posterior_parameters import MCMCPosteriorParameters\n",
8990
"from sbi.inference.potentials.likelihood_based_potential import LikelihoodBasedPotential\n",
9091
"from sbi.neural_nets import likelihood_nn\n",
9192
"from sbi.utils import BoxUniform, MultipleIndependent, mcmc_transform\n",
@@ -220,7 +221,7 @@
220221
"outputs": [],
221222
"source": [
222223
"# Build posterior from the trained estimator and prior.\n",
223-
"mnle_posterior = trainer.build_posterior(prior=prior, mcmc_parameters=mcmc_kwargs)\n",
224+
"mnle_posterior = trainer.build_posterior(prior=prior, posterior_parameters=MCMCPosteriorParameters(**mcmc_kwargs))\n",
224225
"\n",
225226
"mnle_samples = mnle_posterior.sample((num_samples,), x=x_o)"
226227
]

0 commit comments

Comments
 (0)