Skip to content

Commit ef54f65

Browse files
Rename SN(P)E -> N(P)E in all tutorials
1 parent df4cb2c commit ef54f65

18 files changed

+119
-143
lines changed

tutorials/00_getting_started_flexible.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"import torch\n",
4343
"\n",
4444
"from sbi.analysis import pairplot\n",
45-
"from sbi.inference import SNPE, simulate_for_sbi\n",
45+
"from sbi.inference import NPE, simulate_for_sbi\n",
4646
"from sbi.utils import BoxUniform\n",
4747
"from sbi.utils.user_input_checks import (\n",
4848
" check_sbi_inputs,\n",
@@ -135,9 +135,9 @@
135135
"cell_type": "markdown",
136136
"metadata": {},
137137
"source": [
138-
"> Note: In the `sbi` toolbox, NPE is run by using the `SNPE` (Sequential NPE) class for only one iteration of simulation and training. \n",
138+
"> Note: In `sbi` version 0.23.0, we renamed all inference classes from, e.g., `SNPE`, to `NPE` (i.e., we removed the `S` prefix). The functionality of the classes remains the same. The `NPE` class handles both the amortized (as shown in this tutorial) and sequential (as shown [here](https://sbi-dev.github.io/sbi/tutorial/02_multiround_inference/)) versions of neural posterior estimation.\n",
139139
"\n",
140-
"> Note: This is where you could specify an alternative inference object such as (S)NRE for ratio estimation or (S)NLE for likelihood estimation. Here, you can see [all implemented methods](https://sbi-dev.github.io/sbi/tutorial/16_implemented_methods/)."
140+
"> Note: This is where you could specify an alternative inference object such as NRE for ratio estimation or NLE for likelihood estimation. Here, you can see [all implemented methods](https://sbi-dev.github.io/sbi/tutorial/16_implemented_methods/)."
141141
]
142142
},
143143
{
@@ -146,7 +146,7 @@
146146
"metadata": {},
147147
"outputs": [],
148148
"source": [
149-
"inference = SNPE(prior=prior)"
149+
"inference = NPE(prior=prior)"
150150
]
151151
},
152152
{
@@ -456,7 +456,7 @@
456456
"name": "python",
457457
"nbconvert_exporter": "python",
458458
"pygments_lexer": "ipython3",
459-
"version": "3.12.4"
459+
"version": "3.10.14"
460460
}
461461
},
462462
"nbformat": 4,

tutorials/01_gaussian_amortized.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"\n",
4242
"from sbi import analysis as analysis\n",
4343
"from sbi import utils as utils\n",
44-
"from sbi.inference import SNPE, simulate_for_sbi\n",
44+
"from sbi.inference import NPE, simulate_for_sbi\n",
4545
"from sbi.utils.user_input_checks import (\n",
4646
" check_sbi_inputs,\n",
4747
" process_prior,\n",
@@ -106,7 +106,7 @@
106106
],
107107
"source": [
108108
"# Create inference object. Here, NPE is used.\n",
109-
"inference = SNPE(prior=prior)\n",
109+
"inference = NPE(prior=prior)\n",
110110
"\n",
111111
"# generate simulations and pass to the inference object\n",
112112
"theta, x = simulate_for_sbi(simulator, proposal=prior, num_simulations=2000)\n",
@@ -287,7 +287,7 @@
287287
"name": "python",
288288
"nbconvert_exporter": "python",
289289
"pygments_lexer": "ipython3",
290-
"version": "3.12.4"
290+
"version": "3.10.14"
291291
}
292292
},
293293
"nbformat": 4,

tutorials/02_multiround_inference.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"import torch\n",
4545
"\n",
4646
"from sbi.analysis import pairplot\n",
47-
"from sbi.inference import SNPE, simulate_for_sbi\n",
47+
"from sbi.inference import NPE, simulate_for_sbi\n",
4848
"from sbi.utils import BoxUniform\n",
4949
"from sbi.utils.user_input_checks import (\n",
5050
" check_sbi_inputs,\n",
@@ -131,7 +131,7 @@
131131
"simulator = process_simulator(simulator, prior, prior_returns_numpy)\n",
132132
"check_sbi_inputs(simulator, prior)\n",
133133
"\n",
134-
"inference = SNPE(prior)\n",
134+
"inference = NPE(prior)\n",
135135
"\n",
136136
"posteriors = []\n",
137137
"proposal = prior\n",
@@ -204,7 +204,7 @@
204204
"metadata": {},
205205
"outputs": [],
206206
"source": [
207-
"inference = SNPE(prior=prior)"
207+
"inference = NPE(prior=prior)"
208208
]
209209
},
210210
{
@@ -366,7 +366,7 @@
366366
"name": "python",
367367
"nbconvert_exporter": "python",
368368
"pygments_lexer": "ipython3",
369-
"version": "3.12.4"
369+
"version": "3.10.14"
370370
}
371371
},
372372
"nbformat": 4,

tutorials/03_density_estimators.ipynb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"cell_type": "markdown",
2626
"metadata": {},
2727
"source": [
28-
"One option is to use one of set of preconfigured density estimators by passing a string in the `density_estimator` keyword argument to the inference object (`SNPE` or `SNLE`), e.g., \"maf\" to use a Masked Autoregressive Flow, of \"nsf\" to use a Neural Spline Flow with default hyperparameters.\n"
28+
"One option is to use one of set of preconfigured density estimators by passing a string in the `density_estimator` keyword argument to the inference object (`NPE` or `NLE`), e.g., \"maf\" to use a Masked Autoregressive Flow, of \"nsf\" to use a Neural Spline Flow with default hyperparameters.\n"
2929
]
3030
},
3131
{
@@ -44,7 +44,7 @@
4444
"source": [
4545
"import torch\n",
4646
"\n",
47-
"from sbi.inference import SNPE, SNRE\n",
47+
"from sbi.inference import NPE, NRE\n",
4848
"from sbi.utils import BoxUniform"
4949
]
5050
},
@@ -55,14 +55,14 @@
5555
"outputs": [],
5656
"source": [
5757
"prior = BoxUniform(torch.zeros(2), torch.ones(2))\n",
58-
"inference = SNPE(prior=prior, density_estimator=\"maf\")"
58+
"inference = NPE(prior=prior, density_estimator=\"maf\")"
5959
]
6060
},
6161
{
6262
"cell_type": "markdown",
6363
"metadata": {},
6464
"source": [
65-
"In the case of `SNRE`, the argument is called `classifier`:\n"
65+
"In the case of `NRE`, the argument is called `classifier`:\n"
6666
]
6767
},
6868
{
@@ -71,7 +71,7 @@
7171
"metadata": {},
7272
"outputs": [],
7373
"source": [
74-
"inference = SNRE(prior=prior, classifier=\"resnet\")"
74+
"inference = NRE(prior=prior, classifier=\"resnet\")"
7575
]
7676
},
7777
{
@@ -87,7 +87,7 @@
8787
"source": [
8888
"Alternatively, you can use a set of utils functions to configure a density estimator yourself, e.g., use a MAF with hyperparameters chosen for your problem at hand.\n",
8989
"\n",
90-
"Here, because we want to use SN*P*E, we specifiy a neural network targeting the _posterior_ (using the utils function `posterior_nn`). In this example, we will create a neural spline flow (`'nsf'`) with `60` hidden units and `3` transform layers:\n"
90+
"Here, because we want to use N*P*E, we specifiy a neural network targeting the _posterior_ (using the utils function `posterior_nn`). In this example, we will create a neural spline flow (`'nsf'`) with `60` hidden units and `3` transform layers:\n"
9191
]
9292
},
9393
{
@@ -102,7 +102,7 @@
102102
"density_estimator_build_fun = posterior_nn(\n",
103103
" model=\"nsf\", hidden_features=60, num_transforms=3\n",
104104
")\n",
105-
"inference = SNPE(prior=prior, density_estimator=density_estimator_build_fun)"
105+
"inference = NPE(prior=prior, density_estimator=density_estimator_build_fun)"
106106
]
107107
},
108108
{
@@ -162,7 +162,7 @@
162162
"name": "python",
163163
"nbconvert_exporter": "python",
164164
"pygments_lexer": "ipython3",
165-
"version": "3.12.4"
165+
"version": "3.10.14"
166166
}
167167
},
168168
"nbformat": 4,

tutorials/04_embedding_networks.ipynb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"\n",
1616
"When an embedding network is used, the posterior approximation for a given observation $x_o$ can be denoted as $q_\\phi\\big(\\theta \\mid f_\\lambda(x_o)\\big)$ where $\\phi$ are the parameters of the conditional density estimator and $\\lambda$ the parameters of the embedding neural network. Note that the simulation outputs pass through the `embedding_net` before reaching the density estimator and that $\\phi$ and $\\lambda$ are **jointly learned** during training. `sbi` provides pre-configured embedding networks (MLP, CNN, and permutation-invariant networks) or allows to pass custom-written embedding networks.\n",
1717
"\n",
18-
"It is worth noting that only `SNPE` and `SNRE` methods can use an `embedding_net` to learn summary statistics from simulation outputs. `SNLE` does not offer such functionality because the simulation outputs are also the output of the neural density estimator. \n",
18+
"It is worth noting that only `NPE` and `NRE` methods can use an `embedding_net` to learn summary statistics from simulation outputs. `NLE` does not offer such functionality because the simulation outputs are also the output of the neural density estimator. \n",
1919
"\n",
2020
"## Main syntax"
2121
]
@@ -41,8 +41,8 @@
4141
"# instantiate the conditional neural density estimator\n",
4242
"neural_posterior = posterior_nn(model=\"maf\", embedding_net=embedding_net)\n",
4343
"\n",
44-
"# setup the inference procedure with the SNPE-C procedure\n",
45-
"inferer = SNPE(prior=prior, density_estimator=neural_posterior)\n",
44+
"# setup the inference procedure with NPE\n",
45+
"inferer = NPE(prior=prior, density_estimator=neural_posterior)\n",
4646
"\n",
4747
"# train the density estimator\n",
4848
"density_estimator = inference.append_simulations(theta, x).train()\n",
@@ -93,7 +93,7 @@
9393
"import torch.nn.functional as F\n",
9494
"\n",
9595
"from sbi import analysis, utils\n",
96-
"from sbi.inference import SNPE, simulate_for_sbi\n",
96+
"from sbi.inference import NPE, simulate_for_sbi\n",
9797
"from sbi.utils.user_input_checks import (\n",
9898
" check_sbi_inputs,\n",
9999
" process_prior,\n",
@@ -315,8 +315,8 @@
315315
"# instantiate the neural density estimator\n",
316316
"neural_posterior = posterior_nn(model=\"maf\", embedding_net=embedding_net)\n",
317317
"\n",
318-
"# setup the inference procedure with the SNPE-C procedure\n",
319-
"inferer = SNPE(prior=prior, density_estimator=neural_posterior)"
318+
"# setup the inference procedure with NPE\n",
319+
"inferer = NPE(prior=prior, density_estimator=neural_posterior)"
320320
]
321321
},
322322
{
@@ -490,7 +490,7 @@
490490
"name": "python",
491491
"nbconvert_exporter": "python",
492492
"pygments_lexer": "ipython3",
493-
"version": "3.12.4"
493+
"version": "3.10.14"
494494
}
495495
},
496496
"nbformat": 4,

tutorials/05_conditional_distributions.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26211,7 +26211,7 @@
2621126211
"cell_type": "markdown",
2621226212
"metadata": {},
2621326213
"source": [
26214-
"In this tutorial, we will use SNPE, but the same also works for SNLE and SNRE. First, we define the prior and the simulator and train the deep neural density estimator:\n"
26214+
"In this tutorial, we will use NPE, but the same also works for NLE and NRE. First, we define the prior and the simulator and train the deep neural density estimator:\n"
2621526215
]
2621626216
},
2621726217
{
@@ -26231,7 +26231,7 @@
2623126231
"import torch\n",
2623226232
"\n",
2623326233
"from sbi.inference import (\n",
26234-
" SNPE,\n",
26234+
" NPE,\n",
2623526235
" MCMCPosterior,\n",
2623626236
" posterior_estimator_based_potential,\n",
2623726237
" simulate_for_sbi,\n",
@@ -26254,7 +26254,7 @@
2625426254
"theta = prior.sample((num_simulations,))\n",
2625526255
"x = linear_gaussian(theta)\n",
2625626256
"\n",
26257-
"inference = SNPE()\n",
26257+
"inference = NPE()\n",
2625826258
"posterior_estimator = inference.append_simulations(theta, x).train()"
2625926259
]
2626026260
},
@@ -26392,7 +26392,7 @@
2639226392
"name": "python",
2639326393
"nbconvert_exporter": "python",
2639426394
"pygments_lexer": "ipython3",
26395-
"version": "3.12.4"
26395+
"version": "3.10.14"
2639626396
},
2639726397
"toc": {
2639826398
"base_numbering": 1,

tutorials/06_restriction_estimator.ipynb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"\n",
99
"For many simulators, the output of the simulator can be ill-defined or it can have non-sensical values. For example, in neuroscience models, if a specific parameter set does not produce a spike, features such as the spike shape can not be computed. When using `sbi`, such simulations that have `NaN` or `inf` in their output are discarded during neural network training. This can lead to inefficetive use of simulation budget: we carry out many simulations, but a potentially large fraction of them is discarded.\n",
1010
"\n",
11-
"In this tutorial, we show how we can use `sbi` to learn regions in parameter space that produce `valid` simulation outputs, and thereby improve the sampling efficiency. The key idea of the method is to use a classifier to distinguish parameters that lead to `valid` simulations from regions that lead to `invalid` simulations. After we have obtained the region in parameter space that produes `valid` simulation outputs, we train the deep neural density estimator used in `SNPE`. The method was originally proposed in [Lueckmann, Goncalves et al. 2017](https://arxiv.org/abs/1711.01861) and later used in [Deistler et al. 2021](https://www.biorxiv.org/content/10.1101/2021.07.30.454484v3.abstract).\n"
11+
"In this tutorial, we show how we can use `sbi` to learn regions in parameter space that produce `valid` simulation outputs, and thereby improve the sampling efficiency. The key idea of the method is to use a classifier to distinguish parameters that lead to `valid` simulations from regions that lead to `invalid` simulations. After we have obtained the region in parameter space that produes `valid` simulation outputs, we train the deep neural density estimator used in `NPE`. The method was originally proposed in [Lueckmann, Goncalves et al. 2017](https://arxiv.org/abs/1711.01861) and later used in [Deistler et al. 2021](https://www.biorxiv.org/content/10.1101/2021.07.30.454484v3.abstract).\n"
1212
]
1313
},
1414
{
@@ -23,7 +23,7 @@
2323
"metadata": {},
2424
"source": [
2525
"```python\n",
26-
"from sbi.inference import SNPE\n",
26+
"from sbi.inference import NPE\n",
2727
"from sbi.utils import RestrictionEstimator\n",
2828
"\n",
2929
"restriction_estimator = RestrictionEstimator(prior=prior)\n",
@@ -40,7 +40,7 @@
4040
"\n",
4141
"all_theta, all_x, _ = restriction_estimator.get_simulations()\n",
4242
"\n",
43-
"inference = SNPE(prior=prior)\n",
43+
"inference = NPE(prior=prior)\n",
4444
"density_estimator = inference.append_simulations(all_theta, all_x).train()\n",
4545
"posterior = inference.build_posterior()\n",
4646
"```\n"
@@ -70,7 +70,7 @@
7070
"import torch\n",
7171
"\n",
7272
"from sbi.analysis import pairplot\n",
73-
"from sbi.inference import SNPE, simulate_for_sbi\n",
73+
"from sbi.inference import NPE, simulate_for_sbi\n",
7474
"from sbi.utils import BoxUniform, RestrictionEstimator\n",
7575
"\n",
7676
"_ = torch.manual_seed(2)"
@@ -293,7 +293,7 @@
293293
"cell_type": "markdown",
294294
"metadata": {},
295295
"source": [
296-
"We can now use **all** simulations and run `SNPE` as always:\n"
296+
"We can now use **all** simulations and run `NPE` as always:\n"
297297
]
298298
},
299299
{
@@ -350,7 +350,7 @@
350350
" _,\n",
351351
") = restriction_estimator.get_simulations() # Get all simulations run so far.\n",
352352
"\n",
353-
"inference = SNPE(prior=prior)\n",
353+
"inference = NPE(prior=prior)\n",
354354
"density_estimator = inference.append_simulations(all_theta, all_x).train()\n",
355355
"posterior = inference.build_posterior()\n",
356356
"\n",
@@ -386,7 +386,7 @@
386386
"name": "python",
387387
"nbconvert_exporter": "python",
388388
"pygments_lexer": "ipython3",
389-
"version": "3.12.4"
389+
"version": "3.10.14"
390390
},
391391
"toc": {
392392
"base_numbering": 1,

tutorials/07_sensitivity_analysis.ipynb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@
104104
" )\n",
105105
"\n",
106106
"\n",
107-
"posterior = infer(simulator, prior, num_simulations=2000, method=\"SNPE\").set_default_x(\n",
108-
" torch.zeros(2)\n",
109-
")"
107+
"theta, x = simulate_for_sbi(simulator, prior, 2000)\n",
108+
"inference = NPE(prior)\n",
109+
"_ = inference.append_simulations(theta, x).train()\n",
110+
"posterior = inference.build_posterior().set_default_x(torch.zeros(2))"
110111
]
111112
},
112113
{
@@ -295,9 +296,10 @@
295296
" return linear_gaussian(theta, -0.8 * torch.ones(2), torch.eye(2))\n",
296297
"\n",
297298
"\n",
298-
"posterior = infer(simulator, prior, num_simulations=2000, method=\"SNPE\").set_default_x(\n",
299-
" torch.zeros(2)\n",
300-
")"
299+
"theta, x = simulate_for_sbi(simulator, prior, 2000)\n",
300+
"inference = NPE(prior)\n",
301+
"_ = inference.append_simulations(theta, x).train()\n",
302+
"posterior = inference.build_posterior().set_default_x(torch.zeros(2))"
301303
]
302304
},
303305
{
@@ -479,7 +481,7 @@
479481
"name": "python",
480482
"nbconvert_exporter": "python",
481483
"pygments_lexer": "ipython3",
482-
"version": "3.12.4"
484+
"version": "3.10.14"
483485
},
484486
"toc": {
485487
"base_numbering": 1,

0 commit comments

Comments
 (0)