Completely confused about the guide function

Looking at the docs, we have:

We also need to make sure that every pyro.sample() statement from the model has a matching pyro.sample() statement in the guide. In our case, we include z in both the model and the guide.

However, in the example we have:

def model(data):
    prior_loc = torch.tensor([0.])
    prior_scale = torch.tensor([5.])
    z = pyro.sample('z', dist.Normal(prior_loc, prior_scale))
    scale = torch.tensor([0.1])

    with pyro.plate('data', len(data)):
        pyro.sample('x', dist.Normal(z*z, scale), obs=data)

However, in the guide we have:

def guide(data, index):
    scale_q = pyro.param('scale_{}'.format(index), torch.tensor([1.0]), constraints.positive)
    loc_q = pyro.param('loc_{}'.format(index), torch.tensor([0.0]))
    pyro.sample("z", dist.Normal(loc_q, scale_q))

What about the x variable?


You only need to match unobserved sample statements, since those are the ones you need to optimize. Any sample statement that has the optional obs=... keyword assigned is an observed sample and doesn’t need to be optimized.

If you think about Bayes’ theorem, your regular sample statements are the priors, and your observed sample statements are the likelihood.