# Error when using pm.Potential with pm.Data + coordinates

**URL:** <https://discourse.pymc.io/t/error-when-using-pm-potential-with-pm-data-coordinates/7180>\
**Category:** Questions\
**Created:** [April 5, 2021, 4:11pm UTC](https://discourse.pymc.io/t/error-when-using-pm-potential-with-pm-data-coordinates/7180 "2021-04-05T16:11:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![juststarted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/juststarted/32/1997_2.png) [@juststarted](https://discourse.pymc.io/u/juststarted)\
**Post date:** [April 5, 2021, 4:11pm UTC](https://discourse.pymc.io/t/error-when-using-pm-potential-with-pm-data-coordinates/7180/1 "2021-04-05T16:11:04Z")

</div>

Hi everyone!

I am trying to use `pm.Data` and the `coords` keyword but I’m getting an error when sampling. Here’s the model:

```auto
coords = {
    'LoT': np.arange(len(L)),
    'cat': np.arange(L.shape[1]),
    'obs': np.arange(len(category_i))
}

with pm.Model(coords=coords) as model:
    
    L_data = pm.Data('L', L)#, dims=('LoT', 'cat'))
    cat_i_data = pm.Data('cat_i', category_i)#, dims='obs')
    out_i_data = pm.Data('outcome_i', outcome_i)#, dims='obs')
        
    L_dims = L.shape
    n_lots, n_cats = L.shape

    sigma = pm.HalfNormal('sigma', sigma=2, dims='cat')
    a_0 = pm.Normal('a_0', mu=0, sigma=2)
    a_1 = pm.Normal('a_1', mu=0, sigma=2)

    z_logp = tt.zeros((n_lots,), dtype='float')
    for z_i in range(n_lots):
        zi_logp = 0.
        for cat in range(n_cats):
            obs_idx = np.where(cat_i_data==cat)[0]
            muZ = a_0 + a_1 * L[z_i, cat]
            zi_logp = zi_logp + tt.sum(
                pm.Normal
                .dist(mu=muZ, sd=sigma[cat])
                .logp(out_i_data[obs_idx])
            )
        z_logp = tt.set_subtensor(z_logp[z_i], zi_logp)
    lp3 = pm.Deterministic('z_logp', z_logp, dims='LoT')
    tot_logp = pm.math.logsumexp(z_logp)
    pot = pm.Potential('e', tot_logp)

```

The model compiles fine, but when I try to sample:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/2/27fff4bd1ae6616a596a629ab09d5baea595a166.png)

And in the trace `zlog_p` is always 0, so clearly the sampling isn’t working.

However, when I use the arrays directly instead of using their `pm.Data` version, everything works fine, and when I comment out

```auto
    lp3 = pm.Deterministic('z_logp', z_logp, dims='LoT')
    tot_logp = pm.math.logsumexp(z_logp)
    pot = pm.Potential('e', tot_logp)

```

everything also works fine (well, except that it’s a different model, but I mean that it runs fine). Also, when I comment out just `pot = pm.Potential('e', tot_logp)` the problem is still there. So my guess is that it has something to do with the Deterministic `z_logp`, but I’m not sure what about it is problematic. Thanks in advance for any ideas!

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [April 5, 2021, 4:51pm UTC](https://discourse.pymc.io/t/error-when-using-pm-potential-with-pm-data-coordinates/7180/2 "2021-04-05T16:51:53Z")

</div>

I am not surprised the deterministic does not handle dims. It is not really supposed to do anything other than store the result of deterministic operations.

---

<div class="post-metadata">

**Author:** ![OriolAbril](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/oriolabril/32/2497_2.png) [@OriolAbril](https://discourse.pymc.io/u/OriolAbril)\
**Post date:** [April 5, 2021, 6:01pm UTC](https://discourse.pymc.io/t/error-when-using-pm-potential-with-pm-data-coordinates/7180/3 "2021-04-05T18:01:23Z")

</div>

Is the issue solved by using `dims` in `pm.Data` but not in `pm.Deterministic`?

I thought dims in deterministics were only stored and passed downstream to ArviZ at conversion but had no effect within PyMC3

---

<div class="post-metadata">

**Author:** ![juststarted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/juststarted/32/1997_2.png) [@juststarted](https://discourse.pymc.io/u/juststarted)\
**Post date:** [April 5, 2021, 6:09pm UTC](https://discourse.pymc.io/t/error-when-using-pm-potential-with-pm-data-coordinates/7180/4 "2021-04-05T18:09:35Z")

</div>

Unfortunately not! Even when changing the line to

```auto
    lp3 = pm.Deterministic('z_logp', z_logp)#, dims='LoT')

```

It gives the same error.

To be specific: if I use arrays directly instead of the `pm.Data` objects, everything works fine. So I don’t think the problem is about `Deterministic` and `dims`. It must have something to do with the combination of `Deterministic`, `dims`, and `pm.Data`.

(To generate fake data:)

```auto
sigma = 0.1
a_0, a_1 = 0, 1
z = 3 
L = np.array([
        [1, 2, 1],
        [3, 2, 1],
        [1, 4, 5],
        [5, 6, 8]
    ])

# category of the ith observation
category_i = np.repeat(np.arange(L.shape[1]), 1000)
mu_i = a_0 + a_1 * L[z,category_i]
outcome_i = np.random.normal(
    loc=mu_i,
    scale=[sigma]*len(mu_i)
)

```
