Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Pyro ppl Pyro Pyro Plate

From Leeroopedia


Knowledge Sources
Domains Probabilistic_Programming, Bayesian_Inference
Last Updated 2026-02-09 00:00 GMT

Overview

Concrete tool for declaring conditional independence in probabilistic models provided by the Pyro library.

Description

pyro.plate is a context manager that declares a batch of conditionally independent random variables. It enables vectorized computation over independent observations, correct ELBO scaling for mini-batch training, and proper tensor dimension management for parallel enumeration.

Internally, pyro.plate creates a PlateMessenger that annotates sample sites with conditional independence information. When subsample_size is less than size, the plate automatically handles subsampling and scales log-probabilities by the ratio N/M.

Usage

Use pyro.plate around any group of conditionally independent sample sites — most commonly observations in a dataset. Required for mini-batch SVI training, parallel enumeration of discrete variables, and correct posterior predictive sampling.

Code Reference

Source Location

  • Repository: pyro
  • File: pyro/primitives.py
  • Lines: L283-389

Signature

def plate(
    name: str,
    size: Optional[int] = None,
    subsample_size: Optional[int] = None,
    subsample: Optional[torch.Tensor] = None,
    dim: Optional[int] = None,
    use_cuda: Optional[bool] = None,
    device: Optional[str] = None,
) -> PlateMessenger:
    """
    Construct a plate context manager for conditional independence.

    Args:
        name: Name of the plate.
        size: Total size of the plate (full dataset size).
        subsample_size: Size of the subsample (mini-batch size).
        subsample: Optional tensor of indices for subsampling.
        dim: Optional batch dimension for this plate (negative integer).
        use_cuda: DEPRECATED. Use device instead.
        device: Optional device for subsample indices.
    Returns:
        PlateMessenger context manager
    """

Import

import pyro
# Used as: pyro.plate(name, size, ...)

I/O Contract

Inputs

Name Type Required Description
name str Yes Unique name for the plate
size Optional[int] No Total number of elements (full dataset size)
subsample_size Optional[int] No Mini-batch size for subsampling
subsample Optional[torch.Tensor] No Explicit indices tensor
dim Optional[int] No Batch dimension (negative integer, e.g., -1)

Outputs

Name Type Description
context PlateMessenger Context manager that annotates sample sites with independence info
indices torch.Tensor When iterated, yields subsample indices

Usage Examples

Basic Observation Plate

import pyro
import pyro.distributions as dist

def model(data):
    mu = pyro.sample("mu", dist.Normal(0., 1.))

    # Vectorized plate over observations
    with pyro.plate("data", size=len(data)):
        pyro.sample("obs", dist.Normal(mu, 1.), obs=data)

Mini-batch Subsampling

def model(data):
    mu = pyro.sample("mu", dist.Normal(0., 1.))

    # Subsample 256 from full dataset of 10000
    with pyro.plate("data", size=10000, subsample_size=256) as ind:
        pyro.sample("obs", dist.Normal(mu, 1.), obs=data[ind])

Nested Plates

def topic_model(data, num_topics=10, num_docs=1000, num_words=5000):
    with pyro.plate("topics", num_topics):
        topic_words = pyro.sample("topic_words",
                                   dist.Dirichlet(torch.ones(num_words)))

    with pyro.plate("documents", num_docs, dim=-1):
        doc_topics = pyro.sample("doc_topics",
                                  dist.Dirichlet(torch.ones(num_topics)))

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment