# Confusion about shape/format of variable

**URL:** <https://discourse.pymc.io/t/confusion-about-shape-format-of-variable/2978>\
**Category:** Questions\
**Created:** [March 23, 2019, 7:55pm UTC](https://discourse.pymc.io/t/confusion-about-shape-format-of-variable/2978 "2019-03-23T19:55:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![T.J\_Gaffney](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/t.j_gaffney/32/1664_2.png) [@T.J\_Gaffney](https://discourse.pymc.io/u/T.J_Gaffney)\
**Post date:** [March 23, 2019, 7:55pm UTC](https://discourse.pymc.io/t/confusion-about-shape-format-of-variable/2978/1 "2019-03-23T19:55:53Z")

</div>

I’m trying to do a simple multivariate linear regression.

```auto
import pymc3 as pm
import theano.tensor as tt

# my_data has dimensions m x n, set elsewhere
# y has dimension n, set elsewhere
# Both are np.array
with pm.Model() as model:
  coeffs = pm.Uniform("coeffs", lower=-1, upper=1, shape=n)
  modeled_y = tt.tensordot(coeffs, my_data)
  obs = pm.Normal("obs", mu=modeled_y, sd=1, observed=y)
```

I think this works, but I feel uneasy about the way that I enter modeled\_y, which is a tensor. In the examples I’ve seen, they’ve passed mu as a np.array. It seems like if modeled\_y is a tensor, then I would want y to be a tensor as well. But when I pass observed=tt.as\_tensor\_variable(y), the results are strange and bad. I am confused what is going on.

Can somebody help me understand this better? coeffs is a set of trial values. modeled\_y is a tensor where each element is a set of trial points. Then what is obs; or what would it be if I hadn’t plugged in “observed=y”? Would that be a tensor of the same shape, but with a transformed set of points for each element? When I plug in “observed=y”, with y the same shape as the tensor but as an np.array, does it say (as I think it does) that the i-th element of y is said to be observed from the i-th element of modeled\_y? What is it doing if I pass y as a tensor?

Thank you.

---

<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 24, 2019, 9:47am UTC](https://discourse.pymc.io/t/confusion-about-shape-format-of-variable/2978/2 "2019-03-24T09:47:25Z")

</div>

Internally, you can kind of understand that everything is a tensor, `observed=tt.as_tensor_variable(y)` should also work, but sometimes there are shape problems. The easiest way to validate is check the logp - if the two ways are different, try making y into shape=(n, 1).

---

<div class="post-metadata">

**Author:** ![gbernstein](https://avatars.discourse-cdn.com/v4/letter/g/ac91a4/32.png) [@gbernstein](https://discourse.pymc.io/u/gbernstein)\
**Post date:** [March 24, 2019, 6:15pm UTC](https://discourse.pymc.io/t/confusion-about-shape-format-of-variable/2978/3 "2019-03-24T18:15:18Z")

</div>

Sorry, gotta run out and don’t have a chance to fully read/respond, but I coincidentally just posted a related topic that might be helpful

> [@Linear regression: Bias (intercept) term "baked into" covariate data?](https://discourse.pymc.io/t/linear-regression-bias-intercept-term-baked-into-covariate-data/2979/2):
>
> Eventually figured out the necessary theano manipulations with help from this [post](https://stackoverflow.com/questions/40644013/adding-a-row-of-ones-to-a-tensor-in-theano). Figured I’d post anyways in case it helps others. (Mods, feel free to edit/delete as necessary.) The ones column vector below needs to be created in a thean-ic way. Everything else is standard pymc3. Note that this specific code won’t work for k=1 because of the way pymc3 internally handles shape. This code can be modified as needed to handle that corner case, I’m just not posting it here for clarity. Also note …

---

<div class="post-metadata">

**Author:** ![T.J\_Gaffney](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/t.j_gaffney/32/1664_2.png) [@T.J\_Gaffney](https://discourse.pymc.io/u/T.J_Gaffney)\
**Post date:** [March 26, 2019, 6:56am UTC](https://discourse.pymc.io/t/confusion-about-shape-format-of-variable/2978/4 "2019-03-26T06:56:33Z")

</div>

I think I did have a shape issue that was causing a problem.
