Skip to content

Commit

Permalink
Add a Trace_ELBO wrapper to minipyro (pyro-ppl#1794)
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzo authored and neerajprad committed Mar 29, 2019
1 parent c98a2c2 commit f4bb4fe
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
11 changes: 6 additions & 5 deletions examples/minipyro.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import torch

# We use the pyro.generic interface to support dynamic choice of backend.
from pyro.generic import backend
from pyro.generic import pyro_backend
from pyro.generic import distributions as dist
from pyro.generic import infer, optim, pyro

Expand All @@ -37,11 +37,12 @@ def guide(data):

# Because the API in minipyro matches that of Pyro proper,
# training code works with generic Pyro implementations.
with backend(args.backend):
with pyro_backend(args.backend):
# Construct an SVI object so we can do variational inference on our
# model/guide pair. We work around small differences in elbo interface.
elbo = infer.elbo if args.backend == "minipyro" else infer.Trace_ELBO()
svi = infer.SVI(model, guide, optim.Adam({"lr": args.learning_rate}), elbo)
# model/guide pair.
elbo = infer.Trace_ELBO()
adam = optim.Adam({"lr": args.learning_rate})
svi = infer.SVI(model, guide, adam, elbo)

# Basic training loop
pyro.get_param_store().clear()
Expand Down
7 changes: 6 additions & 1 deletion pyro/contrib/minipyro.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def step(self, *args, **kwargs):
# fundamental objective in Variational Inference.
# See http://pyro.ai/examples/svi_part_i.html for details.
# This implementation has various limitations (for example it only supports
# random variablbes with reparameterized samplers), but all the ELBO
# random variables with reparameterized samplers), but all the ELBO
# implementations in Pyro share the same basic logic.
def elbo(model, guide, *args, **kwargs):
# Run the guide with the arguments passed to SVI.step() and trace the execution,
Expand Down Expand Up @@ -289,3 +289,8 @@ def elbo(model, guide, *args, **kwargs):
# Return (-elbo) since by convention we do gradient descent on a loss and
# the ELBO is a lower bound that needs to be maximized.
return -elbo


# This is a wrapper for compatibility with full Pyro.
def Trace_ELBO(*args, **kwargs):
return elbo
2 changes: 1 addition & 1 deletion pyro/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __getattribute__(self, name):


@contextmanager
def backend(*aliases, **new_backends):
def pyro_backend(*aliases, **new_backends):
"""
Context manager to set a custom backend for Pyro models.
Expand Down

0 comments on commit f4bb4fe

Please sign in to comment.