Generate samples using model function itself?

Thanks for posting this question @pookie. I need to do something similar where I want to sample from the model itself.

Hi @martinjankowiak,
I need to sample from unconditioned model using MCMC-NUTS. So I modified the code available at Pyro-NUTS-docs based on poutine.uncondition(model) comment in this question thread.

def model(data):
      coefs_mean = torch.zeros(dim)
      coefs = pyro.sample('beta', dist.Normal(coefs_mean, torch.ones(3)))
      logits = coefs * data
      logits = logits.sum(-1)
      y = pyro.sample('y', dist.Bernoulli(logits=logits), obs=labels)
      return y

def uncondition_model(data):
      return poutine.uncondition(model)

I need the ‘y’ samples. Is it possible? If I use below code, I don’t get any value for beta or y.

nuts_kernel = NUTS(uncondition_model, adapt_step_size=True)
mcmc = MCMC(nuts_kernel, num_samples=50, warmup_steps=30)
mcmc.run(data)
mcmc.get_samples()[‘y’].mean(0)
mcmc.get_samples()[‘beta’].mean(0)

However, if I use NUTS(model, adapt_step_size=True) then mcmc.get_samples()['beta'].mean(0) does start to work (as in original Pyro-NUTS-docs example). But what I actually need is y with uncondition_model.

I guess poutine.trace(...).get_trace(...) can give me the ‘y’ samples (as discussed in this pyro-forum-post), but I am assuming that through trace function what I get are random samples from the distribution. What if I want samples via ‘NUTS’, is that possible?

Thanks for all the help!!