KeyError: 'object' TypeError: Unsupported dtype for TensorType: object

I have the following function:

import aesara.tensor as at

def carryover(x, strength, length=21):
    w = at.as_tensor_variable(
        [at.power(strength, i) for i in range(length)]
    )
    
    x_lags = at.stack(
        [at.concatenate([
            at.zeros(i),
            x[:x.shape[0]-I]
        ]) for i in range(length)]
    )

The channel_data is literally a column taken from a data frame:

channel_data = X_[channel].values

And the strength parameter is defined like so:

import pymc3 as pm
car_alpha = pm.Exponential("car_alpha", lam=0.01)
car_beta = pm.Exponential("car_beta", lam=0.01)
strength = pm.Beta(f"car_{channel}_{acc}", alpha=car_alpha, beta=car_beta)

When I try to call the function like so:

carryover(channel_data, strength)

I get the following error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File ~/opt/anaconda3/lib/python3.9/site-packages/aesara/tensor/type.py:281, in TensorType.dtype_specs(self)
    280 try:
--> 281     return self.dtype_specs_map[self.dtype]
    282 except KeyError:

KeyError: 'object'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Input In [52], in <cell line: 1>()
----> 1 carryover(channel_data, strength)

Input In [36], in carryover(x, strength, length)
      6 def carryover(x, strength, length=21):
----> 7     w = at.as_tensor_variable(
      8         [at.power(strength, i) for i in range(length)]
      9     )
     11     x_lags = at.stack(
     12         [at.concatenate([
     13             at.zeros(i),
     14             x[:x.shape[0]-I]
     15         ]) for i in range(length)]
     16     )
     18     return at.dot(w, x_lags)

File ~/opt/anaconda3/lib/python3.9/site-packages/aesara/tensor/__init__.py:49, in as_tensor_variable(x, name, ndim, **kwargs)
     17 def as_tensor_variable(
     18     x: TensorLike, name: Optional[str] = None, ndim: Optional[int] = None, **kwargs
     19 ) -> "TensorVariable":
     20     """Convert `x` into an equivalent `TensorVariable`.
     21 
     22     This function can be used to turn ndarrays, numbers, `ScalarType` instances,
   (...)
     47 
     48     """
---> 49     return _as_tensor_variable(x, name, ndim, **kwargs)

File ~/opt/anaconda3/lib/python3.9/functools.py:888, in singledispatch.<locals>.wrapper(*args, **kw)
    884 if not args:
    885     raise TypeError(f'{funcname} requires at least '
    886                     '1 positional argument')
--> 888 return dispatch(args[0].__class__)(*args, **kw)

File ~/opt/anaconda3/lib/python3.9/site-packages/aesara/tensor/basic.py:159, in _as_tensor_Sequence(x, name, ndim, dtype, **kwargs)
    154     # In this case, we have at least one non-`Constant` term, so we
    155     # couldn't get an underlying non-symbolic sequence of objects and we to
    156     # symbolically join terms.
    157     return stack(x)
--> 159 return constant(x, name=name, ndim=ndim, dtype=dtype)

File ~/opt/anaconda3/lib/python3.9/site-packages/aesara/tensor/basic.py:220, in constant(x, name, ndim, dtype)
    214             raise ValueError(
    215                 f"ndarray could not be cast to constant with {int(ndim)} dimensions"
    216             )
    218     assert x_.ndim == ndim
--> 220 ttype = TensorType(dtype=x_.dtype, shape=x_.shape)
    222 return TensorConstant(ttype, x_, name=name)

File ~/opt/anaconda3/lib/python3.9/site-packages/aesara/tensor/type.py:110, in TensorType.__init__(self, dtype, shape, name, broadcastable)
    107         return s
    109 self.shape = tuple(parse_bcast_and_shape(s) for s in shape)
--> 110 self.dtype_specs()  # error checking is done there
    111 self.name = name
    112 self.numpy_dtype = np.dtype(self.dtype)

File ~/opt/anaconda3/lib/python3.9/site-packages/aesara/tensor/type.py:283, in TensorType.dtype_specs(self)
    281     return self.dtype_specs_map[self.dtype]
    282 except KeyError:
--> 283     raise TypeError(
    284         f"Unsupported dtype for {self.__class__.__name__}: {self.dtype}"
    285     )

TypeError: Unsupported dtype for TensorType: object

I tried converting channel_data into a theano tensor before passing it to the function:

import theano
channel_data = theano.shared(channel_data.astype("float64"))

But it still produces the same error. Any help is appreciated.

There’s not enough info here to figure out what’s going on. Could you provide a minimal example that reproduces this error?

Here’s a working minimum viable example to reproduce the issue:

import aesara.tensor as at
import pymc3 as pm

channel_data = [    0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
       89256.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
           0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.]

def carryover(x, strength, length=21):
    w = at.as_tensor_variable(
        [at.power(strength, i) for i in range(length)]
    )
    
    x_lags = at.stack(
        [at.concatenate([
            at.zeros(i),
            x[:x.shape[0]-I]
        ]) for i in range(length)]
    )
    
with pm.Model():
    car_alpha = pm.Exponential("car_alpha", lam=0.01)
    car_beta = pm.Exponential("car_beta", lam=0.01)
    strength = pm.Beta(f"strength", alpha=car_alpha, beta=car_beta)
    
    carryover(channel_data, strength)

If you remove the function call at the end and run the code, it does not produce any error. If you include the function call, it throws the error described above.

You shouldn’t be mixing aesara and pymc v3. If you want v3, then you need to use theano. If you want aesara, you need to use pymc v4. I’m not sure that is the source of your error, but it’s definitely going to cause problems at some point.

I see. I am new to PyMC3, and am just experimenting around.

In that case, could you please suggest an alternate way to define w, that’s not going to produce the error:

    w = at.as_tensor_variable(
        [at.power(strength, i) for i in range(length)]
    )

If you are new to pymc, I would strongly recommend v4 (installation instructions are here). But here how I might do what (I think) you are trying:

import pymc as pm
import aesara.tensor as at
import numpy as np

with pm.Model() as model:
    strength = pm.Beta("strength", alpha=1, beta=1)
    w = at.power(strength, np.arange(10))