Aesara Error: Expected an array-like object, but found a Variable

Hi all,

I’m new to PyMC, so please excuse me if this is basic. I’ve been writing an Aesara function which calls another Aesara function. This is throwing an error and I havent found any answers in the docs or forms. Appreciate any help.

Here is a simplified version of the code:

x1 = at.dscalar(‘x’)
y1 = at.dscalar(‘y’)
z1 = x1 + y1
f1 = function([x1, y1], z1)

x2 = at.dscalar(‘x’)
y2 = at.dscalar(‘y’)
z2 = f1(x1, x2) + 5
f2 = function([x2, y2], z2)

When I compile I get this error:

TypeError Traceback (most recent call last)
Cell In [1469], line 9
7 x2 = at.dscalar(‘x’)
8 y2 = at.dscalar(‘y’)
----> 9 z2 = f1(x1, x2) + 5
10 f2 = function([x2, y2], z2)

File /opt/conda/lib/python3.10/site-packages/aesara/compile/function/types.py:859, in Function.call(self, *args, **kwargs)
857 else:
858 try:
→ 859 s.storage[0] = s.type.filter(
860 arg, strict=s.strict, allow_downcast=s.allow_downcast
861 )
863 except Exception as e:
864 function_name = “aesara function”

File /opt/conda/lib/python3.10/site-packages/aesara/tensor/type.py:139, in TensorType.filter(self, data, strict, allow_downcast)
136 # Explicit error message when one accidentally uses a Variable as
137 # input (typical mistake, especially with shared variables).
138 if isinstance(data, Variable):
→ 139 raise TypeError(
140 "Expected an array-like object, but found a Variable: "
141 "maybe you are trying to call a function on a (possibly "
142 “shared) variable instead of a numeric array?”
143 )
145 if isinstance(data, np.memmap) and (data.dtype == self.numpy_dtype):
146 # numpy.memmap is a “safe” subclass of ndarray,
147 # so we can use it wherever we expect a base ndarray.
148 # however, casting it would defeat the purpose of not
149 # loading the whole data into memory
150 pass

TypeError: Bad input argument with name “x” to aesara function with name “/tmp/ipykernel_92/1263410689.py:4” at index 0 (0-based).
Backtrace when that variable is created:

File “/opt/conda/lib/python3.10/site-packages/ipykernel/zmqshell.py”, line 528, in run_cell
return super().run_cell(*args, **kwargs)
File “/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py”, line 2885, in run_cell
result = self._run_cell(
File “/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py”, line 2940, in _run_cell
return runner(coro)
File “/opt/conda/lib/python3.10/site-packages/IPython/core/async_helpers.py”, line 129, in pseudo_sync_runner
coro.send(None)
File “/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py”, line 3139, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File “/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py”, line 3318, in run_ast_nodes
if await self.run_code(code, result, async
=asy):
File “/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py”, line 3378, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “/tmp/ipykernel_92/1263410689.py”, line 1, in
x1 = at.dscalar(‘x’)
Expected an array-like object, but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?
Blockquote

I see that it’s causing an issue when I pass the Variable rather than an array-like object, but I don’t know how to get around this.

Thanks in advance.

Once you compile the first function f1, the output (z2) will be a numpy array, not an Aesara tensor, so you can’t use that as an output of a compiled function (f2).

All you need to do is forego the first compile, wait until you’ve programmed all the computation you want do, then compile everything in one go:

    x1 = at.dscalar(‘x1’)
    y1 = at.dscalar(‘y1’)

    z1 = x1 + y1
    z2 = z1 + 5
    f2 = function([x1, y1], z2)

If you wanted f2 to return the intermediate computation z1, you could add that as an additional output of f2.

1 Like