# TensorFlow backend for PyMC4

**URL:** <https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409>\
**Category:** Development\
**Created:** [October 9, 2017, 2:44pm UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409 "2017-10-09T14:44:30Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![twiecki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/twiecki/32/6930_2.png) [@twiecki](https://discourse.pymc.io/u/twiecki)\
**Post date:** [October 9, 2017, 2:44pm UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/1 "2017-10-09T14:44:30Z")

</div>

Should we use TensorFlow for PyMC4? What would be the issues of doing so?

Pros are that some successful approaches already exist in the forms of (1) Edward ([http://edwardlib.org/](http://edwardlib.org/)) which will be moved into TensorFlow.contrib which might ensure that TensorFlow will have more capabilities to support Probabilistic Programming, and (2) ZhuSuan [GitHub - thu-ml/zhusuan: A probabilistic programming library for Bayesian deep learning, generative models, based on Tensorflow](https://github.com/thu-ml/zhusuan).

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [October 9, 2017, 4:09pm UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/2 "2017-10-09T16:09:06Z")

</div>

Discussion elsewhere, input welcomes  
Tensorflow(Backed by Google)

- Pros:
  - Strong support, large community
  - Tensorboard for visualization
  - parallel within graph execution that makes things fast
  - Convenient deployment

- Cons:
  - Slow?
  - Too many similar package already (e.g., Edward, ZhuSuan, Greta in R)

Other remarks:  
Compiler for graphs in development: XLA. It is supposed to support memory optimisations and Op fusion when it’s done.  
Custom Ops can be written in python, but C++ ops require us to play games with that strange build system (written in java, and probably not going to end up in any linux packages)

---

<div class="post-metadata">

**Author:** ![thjashin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/thjashin/32/285_2.png) [@thjashin](https://discourse.pymc.io/u/thjashin)\
**Post date:** [October 11, 2017, 7:51am UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/3 "2017-10-11T07:51:35Z")

</div>

To my knowledge of the PyMC3 code, an important issue is that TF doesn’t support a `theano.clone()` equivalent officially. This will make it hard to reuse model graphs. May need to come up with an inference scheme that doesn’t rely on graph copying.

---

<div class="post-metadata">

**Author:** ![jolespin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jolespin/32/297_2.png) [@jolespin](https://discourse.pymc.io/u/jolespin)\
**Post date:** [October 17, 2017, 4:50pm UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/4 "2017-10-17T16:50:11Z")

</div>

I agree with the Pros of TensorFlow and I see the concern of similar packages. Just curious, has there been any conversation about a potential merge with Edward or ZhuSuan? I believe NumPy started out in a similar way. . . I prefer PyMC over the others by far. I also think there are more resources and PyMC than Edward or ZhuSuan; that I have seen at least. I think if TensorFlow was in the backend it would open up a lot of the community to probabilistic programming which would mean more resources and knowledge flowing around.

---

<div class="post-metadata">

**Author:** ![semanticbeeng](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/semanticbeeng/32/501_2.png) [@semanticbeeng](https://discourse.pymc.io/u/semanticbeeng)\
**Post date:** [October 27, 2017, 8:01am UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/5 "2017-10-27T08:01:10Z")

</div>

Does this answer the question?

> **[Edward is officially moving into TensorFlow](https://discourse.edwardlib.org/t/edward-is-officially-moving-into-tensorflow/387)**
>
> Hello everyone, I have an exciting announcement to share: we’re officially merging Edward into TensorFlow. Specifically, Edward will be in the contrib module for now, and to avoid redundancy with other submodules, we’ll be merging its many features...

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [January 20, 2018, 1:48pm UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/6 "2018-01-20T13:48:05Z")

</div>

A few updates from Tensorflow and the Edward/Bayesflow team:

- Distribution class in Tensorflow, it is very well thought out and has a nice design:  
[tensorflow.contrib.distributions](https://www.tensorflow.org/api_guides/python/contrib.distributions) (see also their [recent paper](https://arxiv.org/pdf/1711.10604.pdf)).
- Edward 2.0 is in the pipeline [https://github.com/blei-lab/edward/pull/825](https://github.com/blei-lab/edward/pull/825)
- As I understand, this PR allows you to do copying of nodes that supports control flows [https://github.com/blei-lab/edward/pull/824](https://github.com/blei-lab/edward/pull/824)

---

<div class="post-metadata">

**Author:** ![sharan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/sharan/32/433_2.png) [@sharan](https://discourse.pymc.io/u/sharan)\
**Post date:** [February 23, 2018, 11:28am UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/7 "2018-02-23T11:28:20Z")

</div>

Hey,  
@twiecki @fonnesbeck @junpenglao  
Just came across this:

> **[tensorflow/probability](https://github.com/tensorflow/probability)**
>
> Probabilistic reasoning and statistical analysis in TensorFlow - tensorflow/probability

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [February 23, 2018, 12:32pm UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/8 "2018-02-23T12:32:45Z")

</div>

I saw it yesterday as well, maybe that’s where contrib.distribution and edward 2.0 would finally be?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [March 8, 2018, 4:29pm UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/9 "2018-03-08T16:29:24Z")

</div>

I can more or less see how MCMC is implemented in tensorflow (currently how it is done in tensorflow/probability and bayesflow):

1. a callable logp function written in tensorflow
2. a kernel that take current state and propose a new state, using a proposal function and the logp for MH accept
3. wrap the kernel into a tf.while\_loop, how ever the while\_loop only output the final state, so to get the whole MCMC trace:
4. wrap the while\_loop in 3 with a tf.scan, which output each step of the while\_loop

I did some experiment: [https://gist.github.com/junpenglao/05b0ed11219df25ff56ddc452fc1f6af](https://gist.github.com/junpenglao/05b0ed11219df25ff56ddc452fc1f6af). But I didnt use the tf.scan there. There are quite a lot of changes happening currently in tensorflow/probability and tensorflow.contrib.bayesflow, so I would probably wait a bit before doing more experiment.

So for PyMC4 to work using tensorflow, we need:

1. logp in tensorflow, relatively easy with tensorflow distribution and the pm.Model context available
2. kernels, the step\_methods need to rewrite but also not very difficult  
(2.5) compound step, making different kernels into one could be tricky, maybe compound step would becomes a big function that took different kernel as input?
3. (and 4) rewrite the pm.sampling with tf.while\_loop and tf.scan. Doable but there will be lots of painful debugging of the graph.

---

<div class="post-metadata">

**Author:** ![dustinvtran](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/dustinvtran/32/594_2.png) [@dustinvtran](https://discourse.pymc.io/u/dustinvtran)\
**Post date:** [March 15, 2018, 6:57pm UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/10 "2018-03-15T18:57:38Z")

</div>

Update on the TensorFlow end:

- TF Probability is in early stages. But we plan to launch in a few weeks(!).
- Edward2 is fairly low-level. One future is that PyMC4 is as a higher-level language on top, where PyMC4’s major value-adds are more automated fitting, non-TF prereqs for model-building, visualization, and many more. I suspect this also fits your audience.
- Edward2’s core should be in TF Probability in coming days.

Given you all are potential developers (not just users) of TF Probability, we’d love to set up a meeting so we can share more details. (No need for all core devs to attend this meeting if scheduling times are difficult; we can iterate fast and hopefully stay in regular contact.)

---

<div class="post-metadata">

**Author:** ![twiecki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/twiecki/32/6930_2.png) [@twiecki](https://discourse.pymc.io/u/twiecki)\
**Post date:** [March 15, 2018, 7:17pm UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/11 "2018-03-15T19:17:31Z")

</div>

Thanks for reaching out @dustinvtran, sounds like good progress is made. We’d be more than excited to chat more about basing PyMC4 on TF Probability / Edward. I’ll drop you an email for scheduling.

---

<div class="post-metadata">

**Author:** ![ericmjl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ericmjl/32/196_2.png) [@ericmjl](https://discourse.pymc.io/u/ericmjl)\
**Post date:** [March 15, 2018, 7:27pm UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/12 "2018-03-15T19:27:42Z")

</div>

@junpenglao you’re so prolific with the testing! Thank you for doing this 😄.

---

<div class="post-metadata">

**Author:** ![dustinvtran](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/dustinvtran/32/594_2.png) [@dustinvtran](https://discourse.pymc.io/u/dustinvtran)\
**Post date:** [March 16, 2018, 6:57pm UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/13 "2018-03-16T18:57:06Z")

</div>

Edward2 has just been pushed. 🙂

[https://github.com/tensorflow/probability/tree/master/tensorflow\_probability/python/edward2](https://github.com/tensorflow/probability/tree/master/tensorflow_probability/python/edward2)

The commit message:

> This initial commit for Edward2 adds three submodules. Together they comprise a minimal software for building flexible probabilistic programs alongside their flexible computation during training and testing. They are:
> 
> - `interceptor`, which is a low-level tool for intercepting execution of programs;
> - `random_variable`, which provides a primitive for building probabilistic programs;
> - `generated_random_variables`, which wraps TensorFlow Distributions as an easy way to write specific random variables.

---

<div class="post-metadata">

**Author:** ![ericmjl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ericmjl/32/196_2.png) [@ericmjl](https://discourse.pymc.io/u/ericmjl)\
**Post date:** [March 19, 2018, 3:22pm UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/14 "2018-03-19T15:22:00Z")

</div>

Thanks for the update, @dustinvtran !

---

<div class="post-metadata">

**Author:** ![dustinvtran](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/dustinvtran/32/594_2.png) [@dustinvtran](https://discourse.pymc.io/u/dustinvtran)\
**Post date:** [March 22, 2018, 5:32pm UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/15 "2018-03-22T17:32:39Z")

</div>

See the latest [commit which adds `make_log_joint_fn`](https://github.com/tensorflow/probability/commit/b49b3fa8ecf0609df195f5fd211ede4095d0df64). This function should be powerful enough to enable most-if-not-all algorithms in PyMC4 given an Edward program as a low-level representation of PyMC4 models.

---

<div class="post-metadata">

**Author:** ![bicepjai](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bicepjai/32/1495_2.png) [@bicepjai](https://discourse.pymc.io/u/bicepjai)\
**Post date:** [February 15, 2019, 11:51pm UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/16 "2019-02-15T23:51:27Z")

</div>

how can one help with pymc4 development ?

---

<div class="post-metadata">

**Author:** ![twiecki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/twiecki/32/6930_2.png) [@twiecki](https://discourse.pymc.io/u/twiecki)\
**Post date:** [February 16, 2019, 3:01pm UTC](https://discourse.pymc.io/t/tensorflow-backend-for-pymc4/409/17 "2019-02-16T15:01:08Z")

</div>

[https://github.com/pymc-devs/pymc4/issues](https://github.com/pymc-devs/pymc4/issues)
