Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Add progress bar for BMG MH #1321

Closed

Conversation

feynmanliang
Copy link
Contributor

@feynmanliang feynmanliang commented Jan 26, 2022

Motivation

Fixes #1314

Changes proposed

Adds a stdout progress bar to BMG. This is good for terminals, but I'm not sure how this will behave in Jupyter. Currently only reports for thread id 0.

Test Plan

Verify progress bar on

import beanmachine.ppl as bm
from beanmachine.ppl.inference.bmg_inference import BMGInference
from torch import tensor
from torch.distributions import Bernoulli, Beta


@bm.random_variable
def beta():
    return Beta(2.0, 2.0)


@bm.random_variable
def flip(n):
    return Bernoulli(beta())


queries = [beta()]
observations = {
    flip(0): tensor(0.0),
    flip(1): tensor(0.0),
    flip(2): tensor(1.0),
    flip(3): tensor(0.0),
}
num_samples = 10_000_000
inference = BMGInference()
mcsamples = inference.infer(queries, observations, num_samples, num_chains=4)
print(mcsamples[beta()].mean())

image

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • The title of my pull request is a short description of the requested changes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 26, 2022
@facebook-github-bot
Copy link
Collaborator

@feynmanliang has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@feynmanliang feynmanliang changed the title [WIP] Add progress bar for BMG MH Add progress bar for BMG MH Jan 27, 2022
@facebook-github-bot
Copy link
Collaborator

@feynmanliang has updated the pull request. You must reimport the pull request before landing.

1 similar comment
@facebook-github-bot
Copy link
Collaborator

@feynmanliang has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot
Copy link
Collaborator

@feynmanliang has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Summary:
### Motivation
Fixes facebookresearch#1314

### Changes proposed
Adds a stdout progress bar to BMG. This is good for terminals, but I'm not sure how this will behave in Jupyter. Currently only reports for thread id 0.

Pull Request resolved: facebookresearch#1321

Test Plan:
Verify progress bar on
```py
import beanmachine.ppl as bm
from beanmachine.ppl.inference.bmg_inference import BMGInference
from torch import tensor
from torch.distributions import Bernoulli, Beta

bm.random_variable
def beta():
    return Beta(2.0, 2.0)

bm.random_variable
def flip(n):
    return Bernoulli(beta())

queries = [beta()]
observations = {
    flip(0): tensor(0.0),
    flip(1): tensor(0.0),
    flip(2): tensor(1.0),
    flip(3): tensor(0.0),
}
num_samples = 10_000_000
inference = BMGInference()
mcsamples = inference.infer(queries, observations, num_samples, num_chains=4)
print(mcsamples[beta()].mean())
```

![image](https://user-images.githubusercontent.com/990069/151272888-5763b175-08e6-4a52-8cec-04c036ccd903.png)

### Types of changes
- [ ] Docs change / refactoring / dependency upgrade
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

### Checklist
- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have read the **[CONTRIBUTING](https://github.com/facebookresearch/beanmachine/blob/main/CONTRIBUTING.md)** document.
- [ ] I have added tests to cover my changes.
- [x] All new and existing tests passed.
- [x] The title of my pull request is a short description of the requested changes.

Differential Revision: D33803643

Pulled By: feynmanliang

fbshipit-source-id: 29cf7a50a0022e0e96b3639c30f455ba7515fe0d
@facebook-github-bot
Copy link
Collaborator

This pull request was exported from Phabricator. Differential Revision: D33803643

@feynmanliang feynmanliang deleted the bmg_progress_bar branch February 4, 2022 20:00
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request] add a progress bar for BMGInference
2 participants