Aesara matmul shape NoneType error

I’m trying to use Aesara matmul but am having an issue due to the shape of some dimensions being None.

For example:

import numpy as np
import pymc as pm
import aesara.tensor as at
from aesara.tensor.basic import as_tensor_variable

a = np.random.random(size=(10, 5, 3))
b = np.random.random(size=(10, 3, 1))

# Works fine, prints (10, 5, 1)
print(at.matmul(a, b).eval().shape) 

# This gives an error
# TypeError: 'NoneType' object cannot be interpreted as an integer
with pm.Model() as m:
    c = pm.Normal('c', shape=(10, 3, 1))
    at.matmul(a, c)

# TensorType(float64, (10, 5, 3))

# TensorType(float64, (None, None, 1))

The issue seems to be with the shapes being None during broadcasting.
Any advise on the correct usage of aesara matmul would be appreciated, thanks!

at.matmul includes a bunch of checks on the inputs to determine the shape of the output. As a result, you can’t use it on tensors with undefined shapes. (aside: it’s surprising that PyMC RVs come out with None dims, even when the shape has been set by the user).

To circumvent this, you can specify the shape of c manually, using c = at.specify_shape(c, (10, 3, 1)).

Thanks! at.specify_shape did the trick :grinning:

That will change in the near future, but is indeed the case for now

1 Like