Skip to content

The PAVI guide

pavi.core.PAVIGuide is a Pyro guide: the parameterized variational family that approximates the posterior of your model. It is used like any Pyro guide, together with the model, in a Pyro ELBO (see training).

PAVIGuide(
    model,
    transform_modules,
    transform_scheme,
    encoding_manager,
    hidden_sites=None,
    create_plates=None,
    device=None,
    **model_kwargs,
)

The examples below use the hierarchical Gaussian model of Getting started: a global latent site z_2, a latent site z_1 in plate P_1, and an observed site X in plates P_1 and P_0.

Arguments

model

The generative model: a Pyro function or a pyro.nn.PyroModule. The guide calls it as model(data, idx, ...), so it must accept an idx argument and pass it to its plates (see Precisions about PAVI guide instantiation).

def model(data=None, idx=None):
    idx = idx or {}
    z_2 = pyro.sample("z_2", dist.Normal(torch.zeros(DIM), 1.0).to_event(1))
    with pyro.plate("P_1", NUM_GROUPS, subsample=idx.get("P_1"), dim=-1):
        ...

transform_modules

The normalizing flow of each latent site: a dict mapping each site name to a list of transform factories, applied in order. Sites in a plate typically use a conditional transform, which receives the site's encodings.

transform_modules = {
    "z_2": [affine(scale_init=1.0)],                     # global site: unconditional
    "z_1": [conditional_affine(hidden_layers=[32, 32])],  # site in plate P_1: conditioned on its encodings
}

A latent site missing from this dict is not inferred: the guide samples it from its prior. See Transforms for the available transforms.

transform_scheme

The base distribution of the flows, given as a class:

  • NormalizingFlowScheme: a standard normal base (the mean-field approximation with affine transforms);
  • CascadingScheme: the prior of each site given its parents (cascading flows).
transform_scheme = NormalizingFlowScheme

See Schemes.

encoding_manager

Creates the encodings of the plate instances and gives each site its context vector: JointEM (one encoding per instance) or FactorizedEM (one encoding per plate, combined for instances in several plates). encoding_size sets the size of the encodings, per plate or plate combination.

encoding_manager = JointEM(encoding_size={"P_1": 8})  # one encoding of size 8 per group

See Encodings.

hidden_sites

Sites the guide must ignore: observed sites, and discrete sites summed out by enumeration.

hidden_sites = ["X"]

device

Device of the guide's parameters, e.g. "cpu" or "cuda:0". By default: cuda:0 if available, then mps:0, then cpu.

create_plates

Optional, advanced: a function returning the plates the guide should use, forwarded to Pyro's AutoGuide. Not needed when the model passes idx to its plates.

**model_kwargs

Example arguments of the model. The guide runs the model once with them, when it is created, to discover its latent sites, their plates and their shapes.

guide = PAVIGuide(model, transform_modules, transform_scheme, encoding_manager, hidden_sites=["X"], device="cpu", data=X)

Calling the guide

guide(data, idx) draws one sample of every latent site from the approximate posterior. It returns a dict with the value of every latent site (guide.all_sites); pass return_all=True to also get the other sites visited, such as plate indices. With idx, only the selected plate elements are sampled:

sample = guide(X)                                       # sample["z_1"]: (5, 2)
sample = guide(X[:, [0, 3]], {"P_1": torch.tensor([0, 3])})  # sample["z_1"]: (2, 2)

Note

Guide parameters live in Pyro's global parameter store, under names derived from the sites. Call pyro.clear_param_store() before building a new guide in the same Python session. Otherwise the new guide silently reuses the parameters of the previous one.

Inspecting a guide

Attribute Content
guide.all_sites latent sites handled by the guide
guide.sample_plate {site: plate names}
guide.plate_size, guide.plate_dim plate sizes and tensor dimensions
guide.encodings the encoding tables, e.g. guide.encodings.P_1.weight (see parameter names)
guide.<site> the learnable transforms of a site, e.g. guide.z_1
guide.transform_modules the full transform chain of each site