This might be a trivial thing, but could you please explain why a latent variable should be sampled in the guide and model separately? For example, can’t we use a latent variable we sampled in the guide later in the model?
Here is an example for making my question more concrete. The following code is inspired from SVI Part I: An Introduction to Stochastic Variational Inference in Pyro — Pyro Tutorials 1.8.1 documentation.
def model(data):
alpha0 = torch.tensor(15.0)
beta0 = torch.tensor(15.0)
f = pyro.sample("latent_fairness", dist.Beta(alpha0, beta0))
for i in range(len(data)):
pyro.sample("obs_{}".format(i), dist.Bernoulli(f), obs=data[i])
def guide(data):
alpha_q = pyro.param("alpha_q", torch.tensor(15.0),
constraint=constraints.positive)
beta_q = pyro.param("beta_q", torch.tensor(15.0),
constraint=constraints.positive)
pyro.sample("latent_fairness", dist.Beta(alpha_q, beta_q))
What would be the difference if this model/guide pair is implemented the following way instead?
def model(data):
for i in range(len(data)):
pyro.sample("obs_{}".format(i), dist.Bernoulli(latent_fairness), obs=data[i])
def guide(data):
alpha_q = pyro.param("alpha_q", torch.tensor(15.0),
constraint=constraints.positive)
beta_q = pyro.param("beta_q", torch.tensor(15.0),
constraint=constraints.positive)
latent_fairness = pyro.sample("latent_fairness", dist.Beta(alpha_q, beta_q))