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 AutoDelta Guide

From Leeroopedia


Metadata

Field Value
Sources Repo: Pyro
Domains Bayesian_Inference, Optimization
Updated 2026-02-09
Type Class

Overview

AutoDelta is an automatic guide that constructs Delta (point mass) distributions for each latent variable in a Pyro model, enabling Maximum A Posteriori (MAP) estimation. Each latent site receives a single learnable point estimate stored as a PyroParam with the appropriate constraint from the model's prior support.

Description

The AutoDelta class inherits from AutoGuide and implements MAP inference by placing all variational mass at a single point per latent site. When first called, it traces the model to discover stochastic latent sites and creates a PyroParam for each one, constrained to lie within the support of the site's prior distribution.

During the forward pass, each latent value is sampled from a Delta distribution centered at the current parameter value. Because Delta distributions have zero variance, optimizing the ELBO with this guide is equivalent to maximizing the log joint probability log p(x, z) at the point estimates, which yields the MAP solution.

The class operates in constrained space, meaning the point estimates are directly stored in the space where the prior distribution is defined (e.g., positive reals for a LogNormal prior). This differs from AutoNormal, which works in unconstrained space via bijective transforms.

Latent variables are initialized using init_loc_fn, which defaults to init_to_median. The guide handles plates automatically by repeating initial values to full plate sizes when subsampling is used.

Usage

Import this class when you need fast point estimation with prior regularization for a Pyro model, or when you want to initialize parameters before applying a richer variational family.

Code Reference

Source Location

Repository
pyro-ppl/pyro
File
pyro/infer/autoguide/guides.py
Lines
L319--413

Signature

class AutoDelta(AutoGuide):

    def __init__(self, model, init_loc_fn=init_to_median, *, create_plates=None):

Import

from pyro.infer.autoguide import AutoDelta

I/O Contract

Inputs

Parameter Type Required Description
model callable Yes A Pyro model function containing pyro.sample statements for latent variables
init_loc_fn callable No Per-site initialization function for point estimates (default: init_to_median)
create_plates callable or None No Optional function returning pyro.plate contexts for data subsampling

Outputs

Name Type Description
return value dict Dictionary mapping sample site names (str) to point-estimate values (Tensor) in constrained space

Internal Parameters Created

Attribute Type Description
self.{name} PyroParam One learnable parameter per latent site, constrained to the prior's support via site["fn"].support

Usage Examples

Basic MAP Estimation

import torch
import pyro
import pyro.distributions as dist
from pyro.infer import SVI, Trace_ELBO
from pyro.infer.autoguide import AutoDelta

# Define a Bayesian linear regression model
def model(X, y=None):
    weight = pyro.sample("weight", dist.Normal(0.0, 10.0).expand([X.shape[1]]).to_event(1))
    bias = pyro.sample("bias", dist.Normal(0.0, 10.0))
    sigma = pyro.sample("sigma", dist.LogNormal(0.0, 1.0))
    mean = X @ weight + bias
    with pyro.plate("data", X.shape[0]):
        pyro.sample("obs", dist.Normal(mean, sigma), obs=y)

# Construct the MAP guide
guide = AutoDelta(model)

# Set up SVI for MAP estimation
optimizer = pyro.optim.Adam({"lr": 0.01})
svi = SVI(model, guide, optimizer, loss=Trace_ELBO())

# Training loop
for step in range(1000):
    loss = svi.step(X_train, y_train)

Retrieving MAP Estimates

# After training, get the MAP point estimates
median = guide.median(X_train, y_train)
print(median)
# {'weight': tensor([0.52, -0.31, 1.20]), 'bias': tensor(0.08), 'sigma': tensor(0.95)}

Custom Initialization

from pyro.infer.autoguide import AutoDelta
from pyro.infer.autoguide.initialization import init_to_sample

# Initialize from prior samples instead of median
guide = AutoDelta(model, init_loc_fn=init_to_sample)

Related Pages

Page Connections

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