Theano error with element wise multiplication

Hi everyone,

I am trying to work for higher dimensional inputs in Thomas Wiecki’s example . After taking the log of the ‘adjwt’ column,here is the new model:

county_idx = data['county_code'].values
data_set = np.hstack((np.ones((len(data),1)), data[['floor','log_adjwt']].values))
#data_set.shape = (919,3)

x,y = T.dmatrices('x', 'y') 
out = x * y
f = theano.function([x,y], out)

with pm.Model() as hierarchical_model:
	mu_b = pm.Normal('mu_beta', mu=0., sd=100**2)
	sigma_b = pm.HalfCauchy('sigma_beta', beta=1)
	b = pm.Normal('beta', mu=mu_b, sd=sigma_b, shape=(n_counties,3) )
	eps = pm.HalfNormal('eps', sd=100)
	radon_est =  f( b[county_idx] , data_set ).sum(axis=1)
	radon_like = pm.Normal('radon_like', mu=radon_est, sd=eps, observed=target_var)

I get this theano error:
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?

f works fine on this regular matrix:
z=np.ones((n_counties,3))*np.arange(1,86).reshape(-1,1)
z = z[county_idx]
f(z,data_set) = good output

So, I am guessing it has something to do with the pymc3 object (b in this case). Is there something I need to do to make a pymc3 object work in a theano function? Or am I way off base in what I think is the problem?

Thanks in advance!

  • Charles

This line works:
radon_est = (b[county_idx] * data_set).sum(axis=1)

So, using a theano function is probably just not the right way.

The variables that are returned by pm.Normal and co are theano tensors already, so there is not reason to use a theano function. The input to a theano function would have to be a numpy array.

Thanks, I’m new to theano and thought that you had to make functions to perform operations with tensors.

I know where you are coming from. This is confusing at the beginning. If you want to play guinea pig, you can try to read our new theano overview: https://github.com/pymc-devs/pymc3/blob/1db42f786cdadaa53214fb89eedc6829c6b1d018/docs/source/theano.rst

1 Like

Thanks for the link with extra insights. Looks like the last bit is most pertinent to my question, aka what I was confused about. I just need to put in more practice. Much appreciated.