I am trying to use python function using theano’s @as_op wrapper.
I have below 4 input x(from file as numpy.ndarray(12,3)), t1,t2,t3 (from pymc3 as prior distribution). When I’m calling Theano function with all variables it’s throwing error below:
@as_op(itypes=[T.dmatrix, T.dscalar,T.dscalar,T.dscalar],
otypes=[T.dvector])
def predicta(xf, tf1,tf2,tf3):
(n, p) = xf.shape
tf = np.atleast_1d((tf1,tf2,tf3))
xt = np.concatenate((xf,np.tile(tf,(n,1))), axis=1) #broadcast t1,t2,t3 along matrix
yf_pred = gp.predict(xt) #pre-saved model
return np.array(yf_pred)
#create tensor variables
x = T.dmatrix()
t1 = T.dscalar()
t2 = T.dscalar()
t3 = T.dscalar()
#initialise the function with input [x,y] and output-predicta function return
yf_predict = function([x,t1,t2,t3], predicta(x,t1,t2,t3),on_unused_input='warn')
yf_flt = yf.values.reshape(1,12)
with pm.Model() as model:
Q1 = pm.Normal('Q1', mu=0.5, sd=0.5)
Q2 = pm.Normal('Q2', mu=0.5, sd=0.5)
Q3 = pm.Normal('Q3', mu=0.5, sd=0.5)
lambda_e = pm.Gamma('Noise',mu=10,sd=0.03)
yf_pred = yf_predict(xf,Q1,Q2,Q3)
# Likelihood
y = pm.MvNormal('likelihood', observed=yf_flt, mu=yf_pred, cov=lambda_e*tt.eye(n))
step = pm.NUTS()
trace_ = pm.sample(4, step,njobs=2, tune=1000,progressbar=True,discard_tuned_samples=False)
It’s throwing error like:
File "\tensorflow\lib\site-packages\theano\tensor\type.py", line 87, in filter
'Expected an array-like object, but found a Variable: '
TypeError: Bad input argument with name "Q1" to theano function with name "<ipython-input-8-9ec2aad3011b>:1" at index 1 (0-based).
Backtrace when that variable is created:
t1 = T.dscalar()
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?
I am assuming that Q1 from pm.Normal return will be T.dscalar here.