# Tensorflow probability help: \*\*Irrelevant to PyMC3\*\*

**URL:** <https://discourse.pymc.io/t/tensorflow-probability-help-irrelevant-to-pymc3/6043>\
**Category:** Questions\
**Created:** [October 19, 2020, 6:46pm UTC](https://discourse.pymc.io/t/tensorflow-probability-help-irrelevant-to-pymc3/6043 "2020-10-19T18:46:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mahdi\_Kooshkbaghi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/mahdi_kooshkbaghi/32/2897_2.png) [@Mahdi\_Kooshkbaghi](https://discourse.pymc.io/u/Mahdi_Kooshkbaghi)\
**Post date:** [October 19, 2020, 6:46pm UTC](https://discourse.pymc.io/t/tensorflow-probability-help-irrelevant-to-pymc3/6043/1 "2020-10-19T18:46:49Z")

</div>

Hi All,  
Sorry for posting here, but frustrated for a long time, and will appreciate for any help.

Consider I have observations (Y,Z) \in \mathbb{R}^2 from the following fake data:

X \in [0,1], \\Y = w\_1X+b\_1, \\ Z = w\_2X+b\_2

```python
X_np = np.linspace(0,1,100)
# w1=2, b1=1.7, w2=3.2, b2= -0.2
Y_np = 2*X_np + 1.7
Z_np = 3.2*X_np - 0.2

```

I want to infer (w\_1,w\_2, b\_1, b\_2). Using `PyMC3`, the task was super easy since we have an `observed` argument in building a model.

But in `Tensorflow probability` I have difficulties due batch/event/sample shapes issues I dealing for weeks now.

My issues:

- Is the code implementation corresponds to the mathematical description I gave below, in terms of shape sizes.

- How I can calculate the `target_log_prob_fn` function? Is it suppose to give me a scalar or two values (since I have two observations).

Here is my implementation:

w\_1 \sim \mathcal{N}(0,1) \\ w\_2 \sim \mathcal{N}(0,1) \\ b\_1 \sim \mathcal{N}(0,1) \\ b\_2 \sim \mathcal{N}(0,1) \\ Y \sim \mathcal{N}(\mu = b\_1+ w\_1X, \sigma=1)\\ Z \sim \mathcal{N}(\mu = b\_2+ w\_2X, \sigma=1)

```auto
Root = tfd.JointDistributionCoroutine.Root # Convenient alias.
def model():
    b2 = yield Root(tfd.Normal(loc=tf.cast(0, dtype), scale=1.))    
    w2 = yield Root(tfd.Normal(loc=tf.cast(0, dtype), scale=1.))
    b1 = yield Root(tfd.Normal(loc=tf.cast(0, dtype), scale=1.))
    w1 = yield Root(tfd.Normal(loc=tf.cast(0, dtype), scale=1.))
    yhat = b1[...,tf.newaxis]+w1[...,tf.newaxis]*X_np
    zhat = b2[...,tf.newaxis]+w2[...,tf.newaxis]*X_np
    
    likelihood = yield tfd.Independent(tfd.Normal(loc=[yhat,zhat], scale=1.),
                                       reinterpreted_batch_ndims=2)

mdl_ols_coroutine = tfd.JointDistributionCoroutine(model)
target_log_prob_fn = lambda *x: mdl_ols_coroutine.log_prob(x + (Y_np,Z_np ))

```

---

<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 20, 2020, 7:57am UTC](https://discourse.pymc.io/t/tensorflow-probability-help-irrelevant-to-pymc3/6043/2 "2020-10-20T07:57:51Z")

</div>

I think in your case it is much easier to use `tfd.JointDistributionCoroutineAutoBatched` that batch a model for you, it goes something like:

```python
@tfd.JointDistributionCoroutineAutoBatched
def mdl_ols_coroutine():
    b2 = yield tfd.Normal(loc=tf.cast(0, dtype), scale=1.)   
    w2 = yield tfd.Normal(loc=tf.cast(0, dtype), scale=1.)
    b1 = yield tfd.Normal(loc=tf.cast(0, dtype), scale=1.)
    w1 = yield tfd.Normal(loc=tf.cast(0, dtype), scale=1.)
    yhat = b1 + w1 * X_np
    zhat = b2 + w2 * X_np
    likelihood1 = yield tfd.Normal(loc=yhat, scale=1.)
    likelihood2 = yield tfd.Normal(loc=yhat, scale=1.)

val = mdl_ols_coroutine.sample(5)
mdl_ols_coroutine.log_prob_parts(val) # <= validate shape is correct
target_log_prob_fn = lambda *x: mdl_ols_coroutine.log_prob(*x, Y_np, Z_np)
target_log_prob_fn(*val[:-2])

```

Also, FYI tensorflow\_probability also have a user forum: [https://groups.google.com/a/tensorflow.org/forum/#!forum/tfprobability](https://groups.google.com/a/tensorflow.org/forum/#!forum/tfprobability)

---

<div class="post-metadata">

**Author:** ![Mahdi\_Kooshkbaghi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/mahdi_kooshkbaghi/32/2897_2.png) [@Mahdi\_Kooshkbaghi](https://discourse.pymc.io/u/Mahdi_Kooshkbaghi)\
**Post date:** [October 20, 2020, 10:23am UTC](https://discourse.pymc.io/t/tensorflow-probability-help-irrelevant-to-pymc3/6043/3 "2020-10-20T10:23:16Z")

</div>

Thank you very much.

I have asked this question on tensorflow github issue section several months ago. Yesterday I figured it out there is a Google group as well and they helping me out.

Again, thanks mate.
