Matrix multiplication in pymc3

Hi I am new to pymc3. I want to do a simple matrix multiplication involving a tensor X and coefficients beta.

If all in numpy, I could do beta * X, or np.matmul(X, beta), but I don’t know the equivalent in pymc3, but I have beta declared in pm

alpha = pm.Normal('alpha', 0, sd=10)
beta = pm.Normal('beta', 0, sd=0.5, shape=(D)) 

X itself is declared in numpy

X = np.random.normal(size=(N, D)) * 10

How do I implement

Y = alpha + beta * X

?

You can do: Y = alpha + pm.math.dot(X, beta)

Thank you. That’s affirmative!

in tensorflow, I did a tensordot between a X tensor with shape (N, T, D) and a beta with shape (S, D). The idea is to dot alone the D axis on both tensors. in tensorflow this is expressed as

tf.tensordot(X, beta, [[2], [1]])

what would be the equivalent thing in pymc3 / theano?

See http://deeplearning.net/software/theano/library/tensor/basic.html#theano.tensor.tensordot

Thank, it looks like this should work just like tensorflow, but it doesn’t. I have

x_logit = np.tensordot(X, beta, [[1], [0]])

where X has shape (1000, 9), and beta has shape (9, ), this line crashes with
if as_[axes_a[k]] != bs[axes_b[k]]:
IndexError: tuple index out of range

Then I changed to x_logit = tt.tensordot(X, beta, [[1], [0]])

now I get x_logit shape TensorVariable: Shape.0

I am of course expecting (1000,)

why doesn’t this work?

You are displaying the symbolic shape. You can check the shape conditioned on the current input (under PyMC3 at least) by doing x_logit.tag.test_value.shape

Ok, got it. Another reason I was confused is that the data exceeded memory capacity of the laptop, and the program simply quit silently without any error. Finally, I moved it to the server and started to see execution messages.

It helps to specify cores=1 in pm.sample when you are testing large model, it makes the error easier to read as well.

1 Like