Hi, I’m migrating from pymc2 to pymc3 and I’m getting an error when fitting a model that I don’t seem to understand. Below is the code of a simplified version of the model that is used for inverse 2D kinematics. In pymc2 the model worked fine, but I wanted to test the NUTS sampler and some of the other pymc3 new features.
import pymc3 as pm
import numpy as np
test data
q0 = np.array([ [20, 0], [20, 10], [40, 0], [40, 10] ])
theta = np.radians(20)
rot = np.array([[np.cos(theta), -np.sin(theta)] , [np.sin(theta), np.cos(theta)] ])
r = [1., 1.]
q1 = np.array([ np.dot(rot, q0[i] - r) for i in range(4) ])
q0n = q0 + np.random.normal(0, 1, size = q0.shape)
q1n = q1 + np.random.normal(0, 1, size = q0.shape)
with pm.Model() as model:
# priors
Theta = pm.Uniform('Theta', 0, np.pi/2)
r = pm.Uniform('r', -5, 5, shape = 2)
sigma = pm.HalfNormal('sigma', sigma = 2)
# forward kinematics
def forward(Theta, r, q0):
s, c = pm.math.sin(Theta), pm.math.cos(Theta)
R = np.array([[c,-s],[s,c]])
return pm.math.flatten(r + pm.math.dot(R, (q0 - r)))
# likelihood
obs = pm.Normal('obs', mu = forward(Theta, r, q0n), sigma= sigma,
observed = pm.math.flatten(q1n))
# sampling
trace = pm.sample()
This is the error that I get, it appears to be related to the trigonometric functions in the model. Any help with this will be much appreciated, thanks in advance!
Traceback (most recent call last):
File “”, line 15, in
obs = pm.Normal(‘obs’, mu = forward(Theta, r, q0n), sigma= sigma,
File “”, line 12, in forward
return pm.math.flatten(r + pm.math.dot(R, (q0 - r)))
File “/home/ben/anaconda3/lib/python3.7/site-packages/Theano-1.0.4-py3.7.egg/theano/tensor/basic.py”, line 6098, in dot
a, b = as_tensor_variable(a), as_tensor_variable(b)
File “/home/ben/anaconda3/lib/python3.7/site-packages/Theano-1.0.4-py3.7.egg/theano/tensor/basic.py”, line 200, in as_tensor_variable
raise AsTensorError(“Cannot convert %s to TensorType” % str_x, type(x))
AsTensorError: (‘Cannot convert [[Elemwise{cos,no_inplace}.0 Elemwise{neg,no_inplace}.0]\n [Elemwise{sin,no_inplace}.0 Elemwise{cos,no_inplace}.0]] to TensorType’, <class ‘numpy.ndarray’>)