# \[Edit\] Trouble with "aesara.tensor.as\_tensor\_variable" when building custom fitting function

**URL:** <https://discourse.pymc.io/t/edit-trouble-with-aesara-tensor-as-tensor-variable-when-building-custom-fitting-function/7169>\
**Category:** Questions\
**Tags:** theano\
**Created:** [April 3, 2021, 9:26pm UTC](https://discourse.pymc.io/t/edit-trouble-with-aesara-tensor-as-tensor-variable-when-building-custom-fitting-function/7169 "2021-04-03T21:26:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![corcoted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/corcoted/32/3895_2.png) [@corcoted](https://discourse.pymc.io/u/corcoted)\
**Post date:** [April 3, 2021, 9:27pm UTC](https://discourse.pymc.io/t/edit-trouble-with-aesara-tensor-as-tensor-variable-when-building-custom-fitting-function/7169/1 "2021-04-03T21:27:00Z")

</div>

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?

```python
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

```python
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-46-5345c0bfe47f> in <module>
     11 amp = pm.Deterministic('amp',a-c)
     12 
---> 13 fit = tt.outer(x,b) # not my real function. Only the broken part.
     14 
     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))
   2285 
   2286 

~/.conda/envs/colorfits-mkl/lib/python3.9/site-packages/aesara/tensor/math.py in dot(l, r)
   1982 
   1983 if not isinstance(l, Variable):
-> 1984 l = as_tensor_variable(l)
   1985 
   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)
     36 
     37 """
---> 38 return _as_tensor_variable(x, name, ndim, **kwargs)
     39 
     40 

~/.conda/envs/colorfits-mkl/lib/python3.9/functools.py in wrapper(*args, **kw)
    875 '1 positional argument')
    876 
--> 877 return dispatch(args[0]. __class__ )(*args, **kw)
    878 
    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("")
     46 
     47 

NotImplementedError: 

```

```auto
%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]

---

<div class="post-metadata">

**Author:** ![aakhmetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aakhmetz/32/1500_2.png) [@aakhmetz](https://discourse.pymc.io/u/aakhmetz)\
**Post date:** [May 16, 2021, 11:34am UTC](https://discourse.pymc.io/t/edit-trouble-with-aesara-tensor-as-tensor-variable-when-building-custom-fitting-function/7169/2 "2021-05-16T11:34:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![corcoted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/corcoted/32/3895_2.png) [@corcoted](https://discourse.pymc.io/u/corcoted)\
**Post date:** [May 16, 2021, 2:07pm UTC](https://discourse.pymc.io/t/edit-trouble-with-aesara-tensor-as-tensor-variable-when-building-custom-fitting-function/7169/3 "2021-05-16T14:07:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aakhmetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aakhmetz/32/1500_2.png) [@aakhmetz](https://discourse.pymc.io/u/aakhmetz)\
**Post date:** [May 17, 2021, 3:55am UTC](https://discourse.pymc.io/t/edit-trouble-with-aesara-tensor-as-tensor-variable-when-building-custom-fitting-function/7169/4 "2021-05-17T03:55:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [May 17, 2021, 4:49am UTC](https://discourse.pymc.io/t/edit-trouble-with-aesara-tensor-as-tensor-variable-when-building-custom-fitting-function/7169/5 "2021-05-17T04:49:10Z")

</div>

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
