Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Pyro ppl Pyro Autoname Scoping: Difference between revisions

From Leeroopedia
Auto-imported from implementations/Pyro_ppl_Pyro_Autoname_Scoping.md
 
Sync from local file
 
Line 90: Line 90:
== Related Pages ==
== Related Pages ==


* [[Pyro_ppl_Pyro_Autoname_Mixture]] - Mixture model using <code>named.Object</code> approach
* [[Implementation:Pyro_ppl_Pyro_Autoname_Mixture]] - Mixture model using <code>named.Object</code> approach
* [[Pyro_ppl_Pyro_Autoname_TreeData]] - Hierarchical model using named objects
* [[Implementation:Pyro_ppl_Pyro_Autoname_TreeData]] - Hierarchical model using named objects


[[Category:Implementations]]
[[Category:Implementations]]


[[Category:Implementations]]
[[Category:Implementations]]

Latest revision as of 10:49, 27 September 2026


Property Value
Implementation Type Pattern Doc
Source File examples/contrib/autoname/scoping_mixture.py
Module pyro.contrib.autoname
Pyro Features pyro.contrib.autoname.scope, config_enumerate, TraceEnum_ELBO, pyro.plate, discrete enumeration
Pattern Gaussian Mixture Model using scope decorator for site name prefixing

Overview

This file demonstrates using the @scope decorator from pyro.contrib.autoname to build a Gaussian Mixture Model with modular local model/guide functions. The @scope(prefix="local") decorator automatically prefixes all pyro.sample and pyro.param site names within the decorated function, enabling clean separation of global and local model components without manual name management.

Unlike the named.Object approach in the companion mixture example, the scoping approach works with discrete enumeration via config_enumerate and TraceEnum_ELBO, allowing exact marginalization of the discrete mixture assignments.

Code Reference

def model(K, data):
    weights = pyro.param("weights", torch.ones(K) / K, constraint=constraints.simplex)
    locs = pyro.param("locs", 10 * torch.randn(K))
    scale = pyro.param("scale", torch.tensor(0.5), constraint=constraints.positive)

    with pyro.plate("data"):
        return local_model(weights, locs, scale, data)

@scope(prefix="local")
def local_model(weights, locs, scale, data):
    assignment = pyro.sample("assignment",
        dist.Categorical(weights).expand_by([len(data)]))
    return pyro.sample("obs", dist.Normal(locs[assignment], scale), obs=data)

def guide(K, data):
    assignment_probs = pyro.param("assignment_probs",
        torch.ones(len(data), K) / K, constraint=constraints.unit_interval)
    with pyro.plate("data"):
        return local_guide(assignment_probs)

@scope(prefix="local")
def local_guide(probs):
    return pyro.sample("assignment", dist.Categorical(probs))

I/O Contract

Parameter Type Description
K int Number of mixture components
data torch.Tensor 1D tensor of observed data points
-n / --num-epochs int Number of training epochs (default: 200)

Named sites (after scoping):

  • local/assignment: Categorical component assignment (auto-prefixed)
  • local/obs: Normal observation (auto-prefixed)
  • weights, locs, scale: Global parameters
  • assignment_probs: Guide parameters

Usage Examples

import pyro
from pyro.infer import SVI, TraceEnum_ELBO, config_enumerate

pyro.set_rng_seed(0)
pyro.clear_param_store()
K = 2
data = torch.tensor([0.0, 1.0, 2.0, 20.0, 30.0, 40.0])

optim = pyro.optim.Adam({"lr": 0.1})
inference = SVI(model, config_enumerate(guide), optim,
                loss=TraceEnum_ELBO(max_plate_nesting=1))

for step in range(200):
    loss = inference.step(K, data)

Related Pages