Visualizing posterior before, during and after training

HI all!

I’d like to show the shift of posterior of a Bayesian regression before, during and after training.

Is there some (pseudo) code on how to do this? I’m using an AutoGuide and I’m not sure how to sample from it before training.

When I do this:

from pyro.contrib.autoguide import AutoDiagonalNormal
guide = AutoDiagonalNormal(model)
optim = Adam({'lr': 1e-4})
elbo = JitTrace_ELBO()
svi = SVI(model, guide, optim, loss=elbo, num_samples=1000)
guide.sample_latent() # Sample posterior to visualize.

I get:

AttributeError: 'AutoDiagonalNormal' object has no attribute 'latent_dim'

Thanks,

Druhe

‘latent_dim’ is computed during the call to __setup_prototype method of the AutoContinuous class. You have to run guide function once, to execute call, which would in turn construct the internal variables. So, running one of these before the call to sample_latent would solve the issue

  1. guide(data, **kwargs)
  2. elbo.loss(model, guide, data, **kwargs)
  3. svi.step(data, **kwargs)
3 Likes

This works, thanks!