Concepts¶
This page summarizes the ideas behind PAVI: hierarchical Bayesian models, variational inference with normalizing flows, plate amortization and stochastic training. The method is described in detail in Rouillard et al. (2023).
Hierarchical Bayesian models and plates¶
A Bayesian model explains observations \(X\) with latent variables \(Z\) through a joint distribution \(p(X, Z) = p(X \mid Z)\, p(Z)\). Inference targets the posterior \(p(Z \mid X) = p(X, Z) / p(X)\), whose normalizing constant \(p(X)\) is usually intractable.
A hierarchical Bayesian model (HBM) stacks levels of latent variables, each level conditioning the next. Variables repeated identically at a level, and conditionally independent given their parents, are grouped in a plate. In the toy model used throughout these docs:
- \(z_2 \sim p(z_2)\) is a global variable;
- \(z_1^i \sim p(z_1^i \mid z_2)\), for \(i = 1, \dots, N_1\), are group-level variables (plate \(\mathcal{P}_1\));
- \(X^{i,j} \sim p(X^{i,j} \mid z_1^i)\), for \(j = 1, \dots, N_0\), are observations (plate \(\mathcal{P}_0\)).
The compact description with plates is the template \(\mathcal{T}\). Replicating each variable according to the plate sizes gives the grounded model \(\mathcal{M}\):

Within a plate, instances are exchangeable: given their parent, they are i.i.d. PAVI exploits this symmetry. In Pyro, plates are pyro.plate contexts, random variables are sites (pyro.sample), and the template is your model function.
Variational inference¶
Variational inference (VI) approximates \(p(Z \mid X)\) by the closest member \(q(Z; \Phi)\) of a parametric family, the variational family. In Pyro, this family is called the guide.

The fit maximizes the evidence lower bound (ELBO):
The first term rewards explaining the data. The second keeps \(q\) close to the prior.
Mean-field approximation¶
The usual mean-field family factorizes over all latent instances:
Each instance has its own parameters \(\Phi_i^n\), for example the mean and standard deviation of a Gaussian in \(\mathbb{R}^d\). The number of parameters therefore grows as \(2dN\). For the brain model (about 60 million vertex-level variables, about 20 networks), that is more than 2 billion parameters.
Normalizing flows¶
A normalizing flow writes each variational distribution as a simple base distribution mapped by a learnable invertible transform \(f\):
The transform is usually a chain of simple invertible transforms, \(f = f_k \circ \dots \circ f_1\). The richer the chain, the more expressive the variational family. Samples are a differentiable function of \(\Phi\) (the reparameterization trick), so unbiased ELBO gradients are obtained by backpropagating through \(f\).
The mean-field Gaussian approximation is the simplest particular case: a standard normal base \(u \sim \mathcal{N}(0, I)\) and a diagonal affine transform \(f(u) = \beta + \alpha \odot u\).
The base distribution does not have to be a standard normal. Using the prior of each variable given its parents as the base gives the cascading flows of Ambrogioni et al. (2021), which keep the hierarchical structure of the model in the variational family. In pavi, the transforms are chosen per site (see transforms), and the base distribution is set by the scheme.
Plate amortization¶
PAVI shares part of the variational parameters among the instances of a plate. Each instance's parameters \(\Phi_1^n\) split into:
- a component \(\phi_1\) shared by all instances of the plate;
- an instance-specific encoding \(E_1^n \in \mathbb{R}^m\), which encodes the statistics of the instance's posterior.

In general, the posterior of each instance is a normalizing flow whose transform is shared across the plate and conditioned on the encoding:
Mean-field case. With a diagonal Gaussian family, \(f\) is an affine transform whose location and scale are produced from the encoding by two small networks \(\alpha\) and \(\beta\) with shared parameters \(\phi_1\):
This is the conditional_affine transform in pavi, used in the brain application. Two benefits:
- Far fewer parameters: \(mN + M\) instead of \(2dN\), with \(M\) the size of the shared networks. For large \(N\), the number of parameters is reduced by a factor \(O(m/d)\), which is beneficial for \(m \ll d\).
- Regularization: the shared \(\phi_1\) captures what instances have in common, which improves generalization.
In pavi, the encodings are created by an encoding manager, and the shared networks are the conditional transforms of each site.
Stochastic variational inference¶
Even amortized, the full model may not fit in GPU memory. Stochastic VI estimates the ELBO on a random sub-model at each iteration \(t\). It samples a mini-batch of observations \(\mathcal{B}_X^{[t]}\) and keeps only their ancestors \(\mathcal{B}_i^{[t]}\):

Rescaling each term by the inverse sampling ratio keeps the estimate unbiased:
and likewise for \(\log q^{[t]}\). In Pyro this rescaling is automatic when plates are subsampled. In pavi, the subsample indices are passed as idx, and only the encodings of the sampled instances are used. See mini-batches.
Inference on new subjects¶
After training, the shared parameters \(\phi\) encode what instances have in common. For new data (e.g. unseen subjects), the efficient strategy is to:
- freeze \(\phi\), and optionally the posteriors of population-level variables;
- optimize only the encodings \(E^n\) of the new instances.
This transfers the knowledge learned on a large dataset to small ones, as in the brain application. See refitting encodings for code.
Discrete variables: enumeration¶
Discrete latent variables, such as the network label of each brain vertex, cannot be reparameterized: sampling them is not differentiable. Gradient estimators that do not need reparameterization (score-function estimators such as REINFORCE) exist, but their variance is high. In the brain application they did not converge, and neither did the Gumbel-softmax relaxation.
The solution is to marginalize discrete variables out of the joint distribution, i.e. to sum over all their values instead of sampling them. For a label \(l\) with \(L\) categories:
The ELBO then only involves continuous variables, which are reparameterized, and gradients are exact with respect to \(l\). The cost grows with \(L\), which is affordable for a few tens of categories (17 networks in the brain model). Pyro performs this summation efficiently by exploiting the plate structure of the model, a technique called enumeration (Obermeyer et al., 2019). In pavi, enumerated sites are hidden from the guide and the model is trained with TraceEnum_ELBO (see training).
KL annealing¶
Early in training, the KL term of the ELBO can dominate and keep \(q\) at the prior, preventing it from learning from the data. KL annealing scales this term by a factor \(\beta(t)\) that grows from 0 to 1 during training:
The model first fits the data, then gradually takes the prior into account. See AnnealingScheduleCallback.
References¶
- Rouillard, L., Le Bris, A., Moreau, T., & Wassermann, D. (2023). PAVI: Plate-Amortized Variational Inference. Transactions on Machine Learning Research.
- Ambrogioni, L., Silvestri, G., & van Gerven, M. (2021). Automatic Variational Inference with Cascading Flows. International Conference on Machine Learning (ICML).
- Obermeyer, F., Bingham, E., Jankowiak, M., Chiu, J., Pradhan, N., Rush, A., & Goodman, N. (2019). Tensor Variable Elimination for Plated Factor Graphs. International Conference on Machine Learning (ICML).