Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guide samples for AutoDelta don't contain deterministic variables #951

Closed
fbartolic opened this issue Mar 10, 2021 · 5 comments · Fixed by #1584
Closed

Guide samples for AutoDelta don't contain deterministic variables #951

fbartolic opened this issue Mar 10, 2021 · 5 comments · Fixed by #1584
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@fbartolic
Copy link

I noticed that samples from the AutoDelta guide don't contain deterministic transforms of random variables in the model. Here's a minimal example which reproduces the issue:

import numpyro
import numpyro.distributions as dist
from numpyro import optim
from numpyro.infer import SVI
from numpyro.infer.autoguide import AutoDelta
from numpyro.infer.elbo import Trace_ELBO
from jax import random

def model():
    x = numpyro.sample('x', dist.Normal(0, 1).expand([10]))
    numpyro.deterministic('x2', x**2)
    
guide = AutoDelta(model)
svi = SVI(model, guide, optim.Adam(0.01), Trace_ELBO())
svi_state = svi.init(random.PRNGKey(0))
svi_result = svi.run(random.PRNGKey(1), 1000)
guide_samples = guide.sample_posterior(random.PRNGKey(2), svi_result.params)
print(guide_samples['x2'])

Is this related to #946?

@fehiepsi fehiepsi added enhancement New feature or request discussion good first issue Good for newcomers and removed discussion labels Mar 10, 2021
@fehiepsi
Copy link
Member

Currently, AutoDelta.sample_posterior does not return deterministic sites. To collect those sites, we need to run Predictive on the model given those posterior samples.

@nikmich1
Copy link
Contributor

Since this is marked as a good first issue, I could give this a try as a first contribution.

To run Predictive on the model, I would pass the model's *args and **kwargs to AutoDelta.sample_posterior as well. My first attempt is something like this. If someone could take a brief look if this makes sense, before I create a pull request, this would be highly appreciated!

@fehiepsi
Copy link
Member

Hi @nikmich1, the solution looks good. To account for the non-trivial case of sample_shape, you can add batch_ndims=len(sample_shape) to the Predictive utility.

@nikmich1
Copy link
Contributor

@fehiepsi Thank you very much for the feedback! I added the sample_shape as suggested.

While running the unit tests, I noticed that sample_posterior should only pick up deterministic and not observed variables to be consistent e.g. with AutoNormal. Does it make sense to only return variables with AutoDelta.prototype_trace[variable]["type"] == "deterministic" (something like this)?

@fehiepsi
Copy link
Member

fehiepsi commented May 1, 2023

LGTM. You can obtain deterministic sites in advance so that we can

  • skip running Predictive if there are no deterministic sites
  • specify return_sites keywork in the Predictive constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants