# Bayesian computation in PyTorch

**URL:** <https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755>\
**Category:** Uncategorized\
**Created:** [February 27, 2017, 7:01am UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755 "2017-02-27T07:01:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![junpenglao](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/junpenglao/32/188_2.png) [@junpenglao](https://discuss.pytorch.org/u/junpenglao)\
**Post date:** [February 27, 2017, 7:01am UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/1 "2017-02-27T07:01:27Z")

</div>

Hi all,

Just discover PyTorch yesterday, the dynamic graph idea is simply amazing! I am wondering if anybody is (or plans to) developing a Bayesian Computation package in PyTorch? Something like PyMC3 (theano) or Edward (tensorflow). I think the dynamic nature of PyTorch would be perfect for dirichlet process or mixture model, and Sequential Monte Carlo etc.

---

<div class="post-metadata">

**Author:** ![smth](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/32/13_2.png) [@smth](https://discuss.pytorch.org/u/smth)\
**Post date:** [February 27, 2017, 5:10pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/2 "2017-02-27T17:10:19Z")

</div>

from keeping tabs on the community, I’m not aware of anyone working on a PyMC3 / Edward -like package. Would love to see how far you get, let us know how it goes 🙂

---

<div class="post-metadata">

**Author:** ![stepelu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stepelu/32/202_2.png) [@stepelu](https://discuss.pytorch.org/u/stepelu)\
**Post date:** [February 28, 2017, 2:11pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/3 "2017-02-28T14:11:10Z")

</div>

I just recently started to work on this in my spare time.  
My plan is to:

- implement a selected number of variants of inference algorithms such as variational and MCMC inference (both traditional and scalable versions) separately from anything neural-network related
- make such framework easily applicable to the existing nn module
- implement basic functionalities for common statistical distributions  
I will post an update when I have something to show.

---

<div class="post-metadata">

**Author:** ![apaszke](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/apaszke/32/21_2.png) [@apaszke](https://discuss.pytorch.org/u/apaszke)\
**Post date:** [February 28, 2017, 3:02pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/4 "2017-02-28T15:02:02Z")

</div>

@stepelu great! let us know if you need any help.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/junpenglao/32/188_2.png) [@junpenglao](https://discuss.pytorch.org/u/junpenglao)\
**Post date:** [February 28, 2017, 3:43pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/5 "2017-02-28T15:43:17Z")

</div>

I would love to contribute. Did you start a repository somewhere?  
For the implementation of basic probability distribution, is it possible to just wrap the Scipy function: [https://github.com/pytorch/tutorials/blob/master/Creating%20extensions%20using%20numpy%20and%20scipy.ipynb](https://github.com/pytorch/tutorials/blob/master/Creating%20extensions%20using%20numpy%20and%20scipy.ipynb)

---

<div class="post-metadata">

**Author:** ![stepelu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stepelu/32/202_2.png) [@stepelu](https://discuss.pytorch.org/u/stepelu)\
**Post date:** [March 1, 2017, 1:51pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/6 "2017-03-01T13:51:34Z")

</div>

No yet, I’ll post the link here when I do.  
And unfortunately that approach wouldn’t work for CUDA.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/junpenglao/32/188_2.png) [@junpenglao](https://discuss.pytorch.org/u/junpenglao)\
**Post date:** [March 1, 2017, 5:06pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/7 "2017-03-01T17:06:13Z")

</div>

I see, thanks for the info - better implementing them from scratch then.

---

<div class="post-metadata">

**Author:** ![AjayTalati](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ajaytalati/32/176_2.png) [@AjayTalati](https://discuss.pytorch.org/u/AjayTalati)\
**Post date:** [May 29, 2017, 3:54pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/8 "2017-05-29T15:54:50Z")

</div>

Just bumping this old thread to see if anyone’s working on variational inference using PyTorch?

Been using Edward recently to do deep VI, and its great apart from having the usual TensorFlow disadvantages ☹

---

<div class="post-metadata">

**Author:** ![smth](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/32/13_2.png) [@smth](https://discuss.pytorch.org/u/smth)\
**Post date:** [May 29, 2017, 4:18pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/9 "2017-05-29T16:18:32Z")

</div>

the folks at Uber may be building out an Edward-like thing for PyTorch.  
Noah Goodman is at Uber ( he built [http://webppl.org/](http://webppl.org/) ).

---

<div class="post-metadata">

**Author:** ![AjayTalati](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ajaytalati/32/176_2.png) [@AjayTalati](https://discuss.pytorch.org/u/AjayTalati)\
**Post date:** [May 29, 2017, 4:36pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/10 "2017-05-29T16:36:30Z")

</div>

Wow, Noah Goodman sold out? Never thought that would happen? Guess every man has his price!

Then again I guess Uber will give him an army of engineers at his disposal 😄 Hope they open-source it?

Now that you mention Uber - yes I remember they’ve been working on Bayesian Optimization for their massive routing problems a long time. I see they signed up Zoubin Ghahramani as head scientist too.

I think principled Bayesian computation, overcomes many of the deficiencies of deep learning, and vice versa. Check out Shakir’s NIPs 2016 slides

[http://shakirm.com/slides/NIPS2016-Bayesian%20Agents.pdf](http://shakirm.com/slides/NIPS2016-Bayesian%20Agents.pdf)

---

<div class="post-metadata">

**Author:** ![AjayTalati](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ajaytalati/32/176_2.png) [@AjayTalati](https://discuss.pytorch.org/u/AjayTalati)\
**Post date:** [May 30, 2017, 11:25pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/11 "2017-05-30T23:25:40Z")

</div>

Hey @smth,

please post here/let me know, when Uber or anyone else make public some sort of Black-Box variational inference engine public for PyTorch.

Tensorflow is driving me nuts - once you’ve used PyTorch it’s painful to go back to TF!

It doesn’t look too unfriendly porting over some of the code from, [Edward](https://github.com/blei-lab/edward).

For example, the main KL inference routines, are well written and there’s not much TF dependency, see

> <https://github.com/blei-lab/edward/blob/master/edward/inferences/klpq.py>

---

<div class="post-metadata">

**Author:** ![yinhao](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/y/c0e974/32.png) [@yinhao](https://discuss.pytorch.org/u/yinhao)\
**Post date:** [June 1, 2017, 12:22pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/12 "2017-06-01T12:22:47Z")

</div>

There is another Bayesian generative models lib, [zhusuan](https://github.com/thu-ml/zhusuan), built on TF.

---

<div class="post-metadata">

**Author:** ![AjayTalati](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ajaytalati/32/176_2.png) [@AjayTalati](https://discuss.pytorch.org/u/AjayTalati)\
**Post date:** [June 1, 2017, 4:04pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/13 "2017-06-01T16:04:49Z")

</div>

Hi @yinhao,

thank you very much - I haven’t seen this library before, it looks very up to date and useful.

If you are working with Gaussian Processes, another very useful library is GPflow,

> **[GPflow/GPflow](https://github.com/GPflow/GPflow)**
>
> Gaussian processes in TensorFlow. Contribute to GPflow/GPflow development by creating an account on GitHub.

So it seems that there are now three variational inference libraries built upon Tensorflow by three different research groups, (Blei Lab, Tsinghua Machine Learning Group, and various contributors to GPflow)?

I guess it’s now only a matter of time before something is available in PyTorch 🙂

Kind regard,

Ajay

---

<div class="post-metadata">

**Author:** ![stepelu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stepelu/32/202_2.png) [@stepelu](https://discuss.pytorch.org/u/stepelu)\
**Post date:** [June 8, 2017, 9:52am UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/14 "2017-06-08T09:52:07Z")

</div>

I started working on different topics shortly after sending my last message so I didn’t make much progress yet.

Now, however, I am back on a project which involves generative models and inference so I expect I’ll have more time to be working on this.

I created [scilua](http://scilua.org) before, so I can capitalize on that and start with statistical distributions, followed by HMC (basic and NUTS) and then variational methods.

Before proceeding there are a few preliminary points I’d like to discuss:

**What is the plan for stochastic graphs?**

For unbiased gradients, pathwise-type estimators come for free.

Other than that, my understanding is that the currently supported approach is via `reinforce()`.

I also found [stochastic.py](https://github.com/pytorch/pytorch/blob/master/torch/autograd/_functions/stochastic.py) where the score-type estimators are implemented for some distributions.

However, both of these are “local” approaches, and it doesn’t seems to me that the current framework would allow for the automatic implementation of unbiased gradient estimators for more complex cases, say example 2 in section 2.3 of [stochastic computation graphs](https://arxiv.org/abs/1506.05254), without modifications.

**Classes for statistical distributions?**

Assuming that there is a plan to fully support stochastic graphs in the future it would make sense to implement distributions as classes instead of separate methods (`Normal().log_pdf()` vs `normal_log_pdf()`). Parameters would be passed to the constructor instead of passing them to every member function call.

**Separate gradient estimators?**

I would keep the logic related to gradient computations separated, via an option passed to the constructor or a wrapping class (`Normal(gradient_estimator=PathwiseEstimator())` or `PathwiseEstimator(Normal())`) to retain flexibility as there are many different ways to produce such estimators.

This can introduce some issues as [tensors are not currently promoted to autograd variables](https://github.com/pytorch/pytorch/issues/274) but I assume this will be done in the future.

A `cuda` named argument would be passed to the constructor of statistical classes to specify that operations like random number generation are done on the GPU (which).

**Assumptions on data shapes?**

It might be beneficial to assume `[batch, random_variable.size()]` dimensions: the first dimension has an iid samples meaning that gets averaged over log-likelyhood computations, and it gives space to generate multiple samples.

Basically I’m looking for the PyTorch’s core team comments / design suggestions to the points I mentioned above, and on the ones I failed to consider!

---

<div class="post-metadata">

**Author:** ![joh4n](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/2acd7d/32.png) [@joh4n](https://discuss.pytorch.org/u/joh4n)\
**Post date:** [June 12, 2017, 7:47am UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/15 "2017-06-12T07:47:13Z")

</div>

I am interested in contributing. I’m currently looking for a good package to test VI algorithms for big models and pytorch’s dynamic graph would make it a lot easier than working in theano or tensorflow.

---

<div class="post-metadata">

**Author:** ![stepelu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stepelu/32/202_2.png) [@stepelu](https://discuss.pytorch.org/u/stepelu)\
**Post date:** [June 23, 2017, 6:32am UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/16 "2017-06-23T06:32:28Z")

</div>

I started putting something online at [https://github.com/stepelu/ptstat](https://github.com/stepelu/ptstat), for now just some statistical distributions and a small number of related functions.

I’ll implement the Gamma, Beta and Dirichlet distributions too next week, sampling aside that would need a C/CUDA implementation (and it’s not trivial to do CUDA-efficiently and in a numerically stable way).

I am also working on MCMC: Metropolis-RW, Langevin-RW, HMC, NUTS.

Contributions are welcome, for instance:

- not yet implemented statistical distributions (let me know if you plan to work on any of the above)
- add missing kl-divergence implementations
- add mean and variance methods

Is there any timeline for issue #274? It would allow me to remove all these `Variable()` calls and facilitate the MCMC code I’m writing too.

---

<div class="post-metadata">

**Author:** ![emilemathieu](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/e/76d3ee/32.png) [@emilemathieu](https://discuss.pytorch.org/u/emilemathieu)\
**Post date:** [August 6, 2017, 6:50pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/17 "2017-08-06T18:50:28Z")

</div>

I’m also highly interested to contribute ! I have already contributed to [edward](https://github.com/blei-lab/edward) and [Turing](https://github.com/yebai/Turing.jl) by implementing various MCMC methods.

---

<div class="post-metadata">

**Author:** ![Andres\_Masegosa](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/858c86/32.png) [@Andres\_Masegosa](https://discuss.pytorch.org/u/Andres_Masegosa)\
**Post date:** [September 19, 2017, 12:07pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/18 "2017-09-19T12:07:26Z")

</div>

Any advances? Highly interested too.

---

<div class="post-metadata">

**Author:** ![ngoodman](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@ngoodman](https://discuss.pytorch.org/u/ngoodman)\
**Post date:** [November 6, 2017, 3:43pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/19 "2017-11-06T15:43:41Z")

</div>

Hi Bayes folks, It’s great to see people interested in this approach! We [recently released](https://eng.uber.com/pyro/) a probabilistic programming language built on top of PyTorch, called [Pyro PPL](http://pyro.ai/). Hopefully this will be of use / interest!

---

<div class="post-metadata">

**Author:** ![tom](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tom/32/3162_2.png) [@tom](https://discuss.pytorch.org/u/tom)\
**Post date:** [November 6, 2017, 4:19pm UTC](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755/20 "2017-11-06T16:19:48Z")

</div>

For those with an inclination towards gpflow and pytorch, I did some work on a port at [https://github.com/t-vi/candlegp](https://github.com/t-vi/candlegp) . Of course, it is much more modest in ambition than pyro.

Best regards

Thomas

[Next page](https://discuss.pytorch.org/t/bayesian-computation-in-pytorch/755.md?page=2)
