You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: myst_nbs/variational_inference/variational_api_quickstart.myst.md
+24-18
Original file line number
Diff line number
Diff line change
@@ -6,18 +6,20 @@ jupytext:
6
6
format_version: 0.13
7
7
jupytext_version: 1.13.7
8
8
kernelspec:
9
-
display_name: Python (PyMC3 Dev)
9
+
display_name: pymc
10
10
language: python
11
-
name: pymc3-dev
11
+
name: pymc
12
12
---
13
13
14
14
# Variational API quickstart
15
15
16
+
:tags: variational inference
17
+
16
18
The variational inference (VI) API is focused on approximating posterior distributions for Bayesian models. Common use cases to which this module can be applied include:
17
19
18
20
* Sampling from model posterior and computing arbitrary expressions
19
21
* Conduct Monte Carlo approximation of expectation, variance, and other statistics
20
-
* Remove symbolic dependence on PyMC3 random nodes and evaluate expressions (using `eval`)
22
+
* Remove symbolic dependence on PyMC random nodes and evaluate expressions (using `eval`)
21
23
* Provide a bridge to arbitrary Theano code
22
24
23
25
Sounds good, doesn't it?
@@ -26,14 +28,14 @@ The module provides an interface to a variety of inference methods, so you are f
26
28
27
29
```{code-cell} ipython3
28
30
%matplotlib inline
31
+
import aesara
29
32
import arviz as az
30
33
import matplotlib.pyplot as plt
31
34
import numpy as np
32
-
import pymc3 as pm
33
-
import theano
35
+
import pymc as pm
34
36
35
37
np.random.seed(42)
36
-
pm.set_tt_rng(42)
38
+
pm.set_at_rng(42)
37
39
```
38
40
39
41
## Basic setup
@@ -46,7 +48,7 @@ mu = pm.floatX([-0.3, 0.5])
46
48
sd = pm.floatX([0.1, 0.1])
47
49
48
50
with pm.Model() as model:
49
-
x = pm.NormalMixture("x", w=w, mu=mu, sigma=sd, dtype=theano.config.floatX)
51
+
x = pm.NormalMixture("x", w=w, mu=mu, sigma=sd)
50
52
x2 = x**2
51
53
sin_x = pm.math.sin(x)
52
54
```
@@ -79,7 +81,7 @@ Let's use the same model:
79
81
```{code-cell} ipython3
80
82
with pm.Model() as model:
81
83
82
-
x = pm.NormalMixture("x", w=w, mu=mu, sigma=sd, dtype=theano.config.floatX)
Again, we see that ADVI is not able to cope with multimodality; we can instead use SVGD, which generates an approximation based on a large number of particles.
Every call yields a different value from the same theano node. This is because it is **stochastic**.
275
281
276
-
By applying replacements, we are now free of the dependence on the PyMC3 model; instead, we now depend on the approximation. Changing it will change the distribution for stochastic nodes:
282
+
By applying replacements, we are now free of the dependence on the PyMC model; instead, we now depend on the approximation. Changing it will change the distribution for stochastic nodes:
277
283
278
284
```{code-cell} ipython3
279
285
sns.kdeplot(np.array([a_sample.eval() for _ in range(2000)]))
@@ -373,7 +379,7 @@ with pm.Model() as iris_model:
373
379
```
374
380
375
381
### Applying replacements in practice
376
-
PyMC3 models have symbolic inputs for latent variables. To evaluate an espression that requires knowledge of latent variables, one needs to provide fixed values. We can use values approximated by VI for this purpose. The function `sample_node` removes the symbolic dependenices.
382
+
PyMC models have symbolic inputs for latent variables. To evaluate an espression that requires knowledge of latent variables, one needs to provide fixed values. We can use values approximated by VI for this purpose. The function `sample_node` removes the symbolic dependenices.
377
383
378
384
`sample_node` will use the whole distribution at each step, so we will use it here. We can apply more replacements in single function call using the `more_replacements` keyword argument in both replacement functions.
379
385
@@ -459,7 +465,7 @@ So, `Tracker` allows us to monitor our approximation and choose good training sc
459
465
## Minibatches
460
466
When dealing with large datasets, using minibatch training can drastically speed up and improve approximation performance. Large datasets impose a hefty cost on the computation of gradients.
461
467
462
-
There is a nice API in pymc3 to handle these cases, which is available through the `pm.Minibatch` class. The minibatch is just a highly specialized Theano tensor:
468
+
There is a nice API in pymc to handle these cases, which is available through the `pm.Minibatch` class. The minibatch is just a highly specialized Theano tensor:
@@ -504,7 +510,7 @@ Now let's use minibatches. At every iteration, we will draw 500 random values:
504
510
505
511
> Remember to set `total_size` in observed
506
512
507
-
**total_size** is an important parameter that allows pymc3 to infer the right way of rescaling densities. If it is not set, you are likely to get completely wrong results. For more information please refer to the comprehensive documentation of `pm.Minibatch`.
513
+
**total_size** is an important parameter that allows pymc to infer the right way of rescaling densities. If it is not set, you are likely to get completely wrong results. For more information please refer to the comprehensive documentation of `pm.Minibatch`.
0 commit comments