Skip to content

Transform schemes

PAVI approximates the posterior of each latent site with a normalizing flow: a base distribution mapped by a pipeline of invertible transforms. The scheme chooses the base distribution. You pass its class to the guide: transform_scheme=NormalizingFlowScheme.

NormalizingFlowScheme

\[z = f(u), \qquad u \sim \mathcal{N}(0, I)\]

The base is a standard normal distribution (on the unconstrained space of the site), and the transforms push it to the posterior. This is the classical normalizing-flow construction, used in the PAVI paper and in the brain application.

  • With a single diagonal affine transform, this is exactly the mean-field Gaussian approximation. Positive variables get a log-normal posterior, through the final exp.
  • Each site is approximated independently of the others. Instances of a plate are only related through the shared transform parameters and their encodings.

CascadingScheme

\[z = f(u), \qquad u \sim p(z \mid \text{parents})\]

The base is the site's prior given its parents, as written in the model. This is the cascading flows algorithm of Ambrogioni, Silvestri & van Gerven (2021), Automatic Variational Inference with Cascading Flows (ICML 2021).

  • The prior of a site is evaluated at the guide's samples of its parents. The variational family therefore keeps the hierarchical dependencies of the model, e.g. \(q(z_1 \mid z_2)\), which the mean-field approximation cannot represent.
  • With identity transforms, \(q\) is the prior. Training starts from the prior, and the transforms learn a correction.
  • Near a perfect fit (identity transforms), the gradient estimates remain noisy: decay the learning rate to avoid drifting around the optimum.

Comparison

NormalizingFlowScheme CascadingScheme
base distribution \(\mathcal{N}(0, I)\) the prior \(p(z \mid \text{parents})\)
dependencies between sites none inherited from the model
at initialization set by the transforms' initialization the prior
typical use the default; mean-field approximation of large models when the hierarchical structure of the prior is informative

Example

import pyro
import pyro.distributions as dist
import torch

from pavi.core import CascadingScheme, JointEM, NormalizingFlowScheme, PAVIGuide
from pavi.distributions import affine


def model(data=None, idx=None):
    # A positive variable: the chain is support⁻¹ (log) → affine → support (exp)
    pyro.sample("kappa", dist.Gamma(2.0, 2.0))


for scheme in (NormalizingFlowScheme, CascadingScheme):
    pyro.clear_param_store()
    guide = PAVIGuide(model, {"kappa": [affine(scale_init=1.0)]}, scheme, JointEM(1), device="cpu")
    with torch.no_grad():
        samples = torch.stack([guide()["kappa"] for _ in range(5000)])
    print(f"{scheme.__name__:22} mean of q at initialization: {samples.mean():.2f}")
# NormalizingFlowScheme  ≈ 1.65 (log-normal: exp of N(0, 1))
# CascadingScheme        ≈ 1.00 (the Gamma(2, 2) prior)

Writing a scheme

A scheme is a pyro.poutine.messenger.Messenger whose constructor accepts transform_modules (a dict of site → list of ready-to-use transforms) and device. It should:

  • in _pyro_sample, replace msg["fn"] for the targeted sites (skipping subsample sites);
  • in _pyro_post_sample, record msg["value"] in self.results, which the guide returns.