[Edit] Trouble with "aesara.tensor.as_tensor_variable" when building custom fitting function

I’m dusting off some old code to do Bayesian MCMC fits to some experimental data. After upgrading pymc3 and aesara (version info below) my model no longer builds. The line below defining fit throws a “Not Implemented” error in aesara.tensor.as_tensor_variable. The code worked as shown for pymc3 version 3.8 and whichever version of theano was contemporary to that.

Update: the trouble seems to come from aesara v.2.0.1 and up. I suspect this change (from the release notes):

aesara.tensor.as_tensor_function is now a functools.singledispatch function

I’m not familiar with singledispatch and what it does. How can I make my code compatible?

with pm.Model() as model61:
    x = pm.Data('x',xdata) # xdata is ndarray with shape=samples, dtype=np.float32
    y = pm.Data('y',ydata) # ydata is ndarray with shape=(samples,Ncolor), dtype=np.float32
    b = pm.HalfNormal('b', 0.2,shape=Ncolor)
    sigma = pm.HalfNormal('sigma',0.03,shape=Ncolor) 
    fit = tt.outer(x,b) # not my real function. Only the broken part.
    yobs = pm.Normal('yobs',fit,sigma,observed=y)

The resulting errors are

NotImplementedError                       Traceback (most recent call last)
<ipython-input-46-5345c0bfe47f> in <module>
     11     amp = pm.Deterministic('amp',a-c)
---> 13     fit = tt.outer(x,b) # not my real function. Only the broken part.
     15     yobs = pm.Normal('yobs',fit,sigma,observed=y)

~/.conda/envs/colorfits-mkl/lib/python3.9/site-packages/aesara/tensor/math.py in outer(x, y)
   2282     if y.ndim != 1:
   2283         y = y.flatten()
-> 2284     return dot(x.dimshuffle(0, "x"), y.dimshuffle("x", 0))

~/.conda/envs/colorfits-mkl/lib/python3.9/site-packages/aesara/tensor/math.py in dot(l, r)
   1983     if not isinstance(l, Variable):
-> 1984         l = as_tensor_variable(l)
   1986     if not isinstance(r, Variable):

~/.conda/envs/colorfits-mkl/lib/python3.9/site-packages/aesara/tensor/__init__.py in as_tensor_variable(x, name, ndim, **kwargs)
     37     """
---> 38     return _as_tensor_variable(x, name, ndim, **kwargs)

~/.conda/envs/colorfits-mkl/lib/python3.9/functools.py in wrapper(*args, **kw)
    875                             '1 positional argument')
--> 877         return dispatch(args[0].__class__)(*args, **kw)
    879     funcname = getattr(func, '__name__', 'singledispatch function')

~/.conda/envs/colorfits-mkl/lib/python3.9/site-packages/aesara/tensor/__init__.py in _as_tensor_variable(x, name, ndim, **kwargs)
     43     x, name: Optional[str], ndim: Optional[int], **kwargs
     44 ) -> NoReturn:
---> 45     raise NotImplementedError("")

%watermark -iv -v

Python implementation: CPython
Python version       : 3.9.2
IPython version      : 7.22.0

pandas    : 1.2.3
pymc3     : 3.11.2
aesara    : 2.0.4
numpy     : 1.20.2

[Edit: additional info]
[Edit: found the breaking change in aesara, trimmed code to minimum NWE]

I have the same trouble, maybe someone has some insight in this question?

I never identified the problem, but was able to work around it by being explicit with the tensor shapes and avoiding broadcasting (i.e. being sure that everything has the same shape). This was tedious, but it worked. There is probably a simpler solution.

It looks like the package is quite under development. I updated it from github, and I have got the message for NotImplemented error. In the meantime, I switched back to theano.tensor, which looks more reliable

PyMC will be using aesara but this version is still in development and unstable/ missing features. The latest stable version is v3.11 which relies on theano-pymc. That’s the recommended version

1 Like