# Expected an array-like object, but found a Variable

**URL:** <https://discourse.pymc.io/t/expected-an-array-like-object-but-found-a-variable/8445>\
**Category:** Questions\
**Tags:** theano\
**Created:** [December 13, 2021, 2:22pm UTC](https://discourse.pymc.io/t/expected-an-array-like-object-but-found-a-variable/8445 "2021-12-13T14:22:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![HG1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/hg1/32/1669_2.png) [@HG1](https://discourse.pymc.io/u/HG1)\
**Post date:** [December 13, 2021, 2:22pm UTC](https://discourse.pymc.io/t/expected-an-array-like-object-but-found-a-variable/8445/1 "2021-12-13T14:22:19Z")

</div>

Hi all,

When executing the code below I

```auto
theano.config.compute_test_value='warn'
f = theano.function([x], theano.tensor.extra_ops.Unique(False, False, True)(x))

with pm.Model() as model:
           
    gamma = pm.DiscreteUniform('gamma',lower=413,upper=1000)
    delta = pm.DiscreteUniform('delta',lower=1,upper=gamma, shape=(900))
    
    st = f(delta)
    
    step = pm.Metropolis()
    trace = pm.sample(10000, tune=2500, chains=4, cores=4, step=step)

```

It fails with the following errors

```auto
TypeError: Bad input argument with name "delta" to theano function with name "/tmp/ipykernel_4862/3750763553.py:2" at index 0 (0-based).  
Backtrace when that variable is created:

and 

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?

```

What am I doing wrong?

Regards Hans

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [December 13, 2021, 5:29pm UTC](https://discourse.pymc.io/t/expected-an-array-like-object-but-found-a-variable/8445/2 "2021-12-13T17:29:23Z")

</div>

There is no need to compile the operation into a `theano.function`, apply the ops on the tensor directly like:

```python
with pm.Model() as model:
           
    gamma = pm.DiscreteUniform('gamma',lower=413,upper=1000)
    delta = pm.DiscreteUniform('delta',lower=1,upper=gamma, shape=(900))
    
    st = theano.tensor.extra_ops.Unique(False, False, True)(delta)
    ...

```

---

<div class="post-metadata">

**Author:** ![HG1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/hg1/32/1669_2.png) [@HG1](https://discourse.pymc.io/u/HG1)\
**Post date:** [December 13, 2021, 5:59pm UTC](https://discourse.pymc.io/t/expected-an-array-like-object-but-found-a-variable/8445/3 "2021-12-13T17:59:53Z")

</div>

Thanks Junpenglao  
I struggle a lot with Theano/Pymc3.  
E.g. I do not understand why the function does not work

Could I have found out it myself somewhere?

Just to avoid asking questions the next time.

Regards Hans

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [December 14, 2021, 1:57pm UTC](https://discourse.pymc.io/t/expected-an-array-like-object-but-found-a-variable/8445/4 "2021-12-14T13:57:28Z")

</div>

`theano.function` is meant for compiling a tensor expression into a callable, so if you have a series of tensor operation `outputs = theano.tensor.XX(*inputs)`, the computation is not actually done as we only build out the rule how you might do the computation when you see the data. `theano.function([*inputs], outputs)` returns a function like object you can call like a NumPy function. More information is in the doc: [function - defines aesara.function — Aesara 2.3.2+17.g79961a625.dirty documentation](https://aesara.readthedocs.io/en/latest/library/compile/function.html)

More specific, during modeling with PyMC you rarely need to use `theano.function`, as in the model only tensor are passing around.

---

<div class="post-metadata">

**Author:** ![HG1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/hg1/32/1669_2.png) [@HG1](https://discourse.pymc.io/u/HG1)\
**Post date:** [December 14, 2021, 2:59pm UTC](https://discourse.pymc.io/t/expected-an-array-like-object-but-found-a-variable/8445/5 "2021-12-14T14:59:42Z")

</div>

One thing I find difficult is understanding the types the pymc3 functions like DiscreteUniform return.

E.g. I can subtract a number from a numpy array which subtracts it from all elements.

When I try to do the same in my model it fails.  
How can I do that?

For simple things like these it is hard to find resources.

Thanks Hans

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [December 14, 2021, 3:33pm UTC](https://discourse.pymc.io/t/expected-an-array-like-object-but-found-a-variable/8445/6 "2021-12-14T15:33:50Z")

</div>

DiscreteUniform is not a function, so the usage is a bit different - in general you use it to create a random variable within a model context (whereas theano/aesara operation works outside the model context as well).

---

<div class="post-metadata">

**Author:** ![HG1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/hg1/32/1669_2.png) [@HG1](https://discourse.pymc.io/u/HG1)\
**Post date:** [December 14, 2021, 5:26pm UTC](https://discourse.pymc.io/t/expected-an-array-like-object-but-found-a-variable/8445/7 "2021-12-14T17:26:45Z")

</div>

Yes I understand  
That makes it so difficult 😝

This worked as per your advice:  
st = theano.tensor.extra\_ops.Unique(False, False, True)(delta)  
But now I want to subtract 1 of the values

a = st - 1  
Gives:

TypeError: unsupported operand type(s) for -: ‘list’ and ‘int’

Can you give me a clue?  
Do I need theano.scan??

I promise that there will be no more questions in this post 😊

Regards Hans

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [December 14, 2021, 6:13pm UTC](https://discourse.pymc.io/t/expected-an-array-like-object-but-found-a-variable/8445/8 "2021-12-14T18:13:13Z")

</div>

No worries! Hopefully you find the answer useful.  
`theano.tensor.extra_ops.Unique(False, False, True)` return unique value and count, so you should do:

```auto
unique_value, count = theano.tensor.extra_ops.Unique(False, False, True)(delta)

```

And subtract the value accordingly.

However, I am not completely sure what you want to do, but since `unique_value` change length, you probably will have some problem building model on top of it (and sample from this model)

---

<div class="post-metadata">

**Author:** ![HG1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/hg1/32/1669_2.png) [@HG1](https://discourse.pymc.io/u/HG1)\
**Post date:** [December 14, 2021, 7:03pm UTC](https://discourse.pymc.io/t/expected-an-array-like-object-but-found-a-variable/8445/9 "2021-12-14T19:03:10Z")

</div>

Oh no  
That was a no brainer.  
Works the same with numpy☺️

---

<div class="post-metadata">

**Author:** ![HG1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/hg1/32/1669_2.png) [@HG1](https://discourse.pymc.io/u/HG1)\
**Post date:** [December 14, 2021, 7:08pm UTC](https://discourse.pymc.io/t/expected-an-array-like-object-but-found-a-variable/8445/10 "2021-12-14T19:08:21Z")

</div>

I understand your point.  
Will rethink it
