Sample does not run if I use pm.math.dot with shape more than 6

Hi,

I hope this is a right place to ask. I am using PyMC v5.0.2.
If I try to run the following code, it starts but does not sample or tune. If I separate weights and do w1*feature1 and so on it works what I am not doing correctly with dot product?

import pymc as pm

with pm.Model() as unpooled_model:
    features = pm.MutableData("features", data_full_feat2)   
    weights = pm.Normal("weights", 0, sigma=1, shape=(1, 7))     
    intercept = pm.Normal("intercept", 3, sigma=1)
    sigma = pm.HalfCauchy("sigma", beta=1)
    
    miu = pm.Deterministic("miu", intercept + pm.math.dot(weights,features.T).reshape(-1,1) )
    
   pm.Normal("Y", mu=miu, sigma=sigma, observed=y)
with unpooled_model:
    
    idata_unpooled_model = pm.sample(2000)

Hi Paulius,

One super useful trick for debugging shape problems / matrix transpose stuff is to take your model out of the pymc context and sample from the components independently. To pull this off, you delete the context wrapper and all the name strings and add .dist at the end of the pm object. Then you can use pm.draw() and .eval(). That way you can isolate when the shape is misbehaving.

For me, this code works and I get draws from priors just fine. So maybe the data isn’t actually shaped as (50,7) once it gets through pm.MutableData?

features = np.random.random(size=(50,7)) # just a guess as to what your data is like
weights = pm.Normal.dist(0, sigma=1, shape=(1,7))
intercept = pm.Normal.dist(3, sigma=1)
sigma = pm.HalfCauchy.dist(beta=1)

miu = intercept + pm.math.dot(weights,features.T).reshape(-1,1)
pm.draw(miu)
6 Likes

@daniel-saunders-phil that’s great advice.

You can also call draw or eval on model variables, so you don’t need to create them outside of a model just for debugging.

2 Likes

Thank you for suggestions. It was helpful to check shapes.

I was passing pandas dataframe to pm.MutableData and pymc probably did not like column names. Passing dataframe.values works as expected.

does pm.math.dot alias pytensor.tensor.dot?

Yes

1 Like