I am trying to understand how to build a maximum a posteriori (MAP) guide without using automatic guide generation. Below is a basic model and two potential guides (1 & 2). After estimation, I am interested in the values of both alpha
and theta
.
import pyro
import pyro.distributions as dist
import torch
from torch.distributions import constraints
M = 2
N = 3
def model():
# hyperparameter
alpha = torch.ones(M)
# theta ~ Dirichlet(alpha)
with pyro.plate('n', N):
pyro.sample('theta', dist.Dirichlet(alpha))
This guide does not look correct but it allows me to inspect both alpha
and theta
:
def map_guide1():
alpha = pyro.param('alpha', torch.ones(N, M), constraint=constraints.positive)
theta = dist.Dirichlet(alpha).sample()
with pyro.plate('n', N):
pyro.sample('theta', dist.Delta(x, event_dim=1))
This guide looks correct but it only gives me theta
, not alpha
:
def map_guide2():
theta = pyro.param('theta', torch.ones(N, M), constraint=constraints.simplex)
pyro.sample('theta', dist.Delta(theta))