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

[FR]: Example of Predictive with MCMC sampling #2803

Closed
fonnesbeck opened this issue Apr 13, 2021 · 8 comments
Closed

[FR]: Example of Predictive with MCMC sampling #2803

fonnesbeck opened this issue Apr 13, 2021 · 8 comments
Labels
Examples help wanted Issues suitable for, and inviting external contributions

Comments

@fonnesbeck
Copy link

There are currently no examples in the docs and tutorials of Predictive being used in MCMC sampling. Given its importance in returning deterministic values of interest from models, it would probably be useful for users to be able to see how they are used.

@fritzo fritzo added Examples help wanted Issues suitable for, and inviting external contributions labels Apr 13, 2021
@mtsokol
Copy link
Contributor

mtsokol commented Apr 13, 2021

Hi!
Is it open as a good-first-issue? If yes then I would like to try it.
I haven't used pyro (only TFP) so I would be grateful if you could point out which examples from http://pyro.ai/examples I can refer to and follow while learning and writing.

@fritzo
Copy link
Member

fritzo commented Apr 13, 2021

Hi @mtsokol, yes this would be a good first issue.

But wait, it looks like the baseball example and the Bayesian Regression II tutorial use MCMC and Predictive. @fonnesbeck can you suggest how we might improve these tutorials and examples to make Predictive easier to understand?

EDIT my mistake, it looks like Bayesian Regression II uses Predictive with SVI rather than with MCMC. @mtsokol WDYT of making a simple tutorial kind of like Bayesian Regression but with a model with pyro.deterministic sites in it? For example I'm working with a regression model that looks like

def model(features, counts):
    N, P = features.shape
    assert counts.shape == (N,)
    scale = pyro.sample("scale", dist.LogNormal(0, 1))
    coef = pyro.sample("coef", dist.Normal(0, scale).expand([P]).to_event(1))
    rate = pyro.deterministic("rate", torch.nn.functional.softplus(coef @ features.T))
    concentration = pyro.sample("concentration", dist.LogNormal(0, 1))
    with pyro.plate("bins", N):
        pyro.sample("counts", dist.GammaPoisson(concentration, rate),
                    obs=counts)

where you could examine samples from the deterministic rate variable.

@fonnesbeck
Copy link
Author

My mistake--did not notice it in the baseball example. One minor suggestion is for important classes like Predictive to have links in the API docs to where it is used in the examples. I think Tensorflow does this (not sure if its done automatically or manually).

@mtsokol
Copy link
Contributor

mtsokol commented May 7, 2021

Hi @fritzo! Sorry for the delay and thank you for a guidance!

I did a short (draft with possible errors and misunderstanding) notebook in a "tutorial" way that utilizes deterministic, Predictive and MCMC with the model you provided. But it's pretty short and it repeats content from existing tutorials so I don't think it's worth it. (However writing it was a great way to familiarize myself with the library!)

I can search for another issue in pyro or numpyro or continue this notebook according to your tips if you consider it worthwhile. What do you think?

Links to the notebook:
https://nbviewer.jupyter.org/gist/mtsokol/fe00f78bb0b925f986d241622c2ec019
https://gist.github.com/mtsokol/fe00f78bb0b925f986d241622c2ec019

(I focused on explaining deterministic, rate variable and GammaPoisson distribution)

@fritzo
Copy link
Member

fritzo commented May 10, 2021

Hi @mtsokol your notebook looks great! Even though it is simple, I still think it is worth publishing it and adding to the index, so feel free to submit a PR.

I think the following additions would also help users:

  • Include both words "Predictive" and "Deterministic" in the notebook title, to help users find content.
  • Add a paragraph linking to other tutorials and examples that use Predictive and pyro.deterministic, so users can see other uses.
  • Maybe also add a section on SVI, so users can see the different way to construct a Predictive object.
  • Maybe in a follow-up PR add an Advanced Usage section showing how simple uses of Predictive could be replaced with either replay+trace as in
    guide_trace = poutine.trace(guide).get_trace(*args, **kwargs)
    model_trace = poutine.trace(poutine.replay(model, guide_trace)).get_trace(*args, **kwargs)
    or condition+trace as in
    samples = mcmc.get_samples()
    vectorize = pyro.plate("particles", num_particles, dim=-model_specific_dim)
    trace = poutine.trace(poutine.condition(vectorize(model), samples)).get_trace(*args, **kwargs)
    (I find that in complex situations I often need to write custom code like this).

Anyway, don't hesitate to put up a PR even for a small notebook, and thanks for writing this!

@khsibr
Copy link

khsibr commented May 25, 2021

Hi @mtsokol
Don't you need to add a bias term in the regression?
Also, it would nice to add a line explaining why GammaPoisson is appropriate for this example.

Thanks!

@mtsokol
Copy link
Contributor

mtsokol commented May 30, 2021

Hi @khsibr!

The model I used here was suggested by @fritzo a few comments before so I can't really elaborate why it's designed that way.

I've just opened a PR with updated notebook #2852 where we can continue discussion.

@fritzo
Copy link
Member

fritzo commented Jun 17, 2021

Completed in #2857

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Examples help wanted Issues suitable for, and inviting external contributions
Projects
None yet
Development

No branches or pull requests

4 participants