I’m not sure, but is this the same error:
n=2
def odefunc(y, t, p):
dy = -p[0]*y
return dy
pm.ode.DifferentialEquation(odefunc, times=[1], n_states=n, n_theta=1)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-7-63b1db971a54> in <module>
5 return dy
6
----> 7 pm.ode.DifferentialEquation(odefunc, times=[1], n_states=n, n_theta=1)
~/software/pymc3/pymc3/ode/ode.py in __init__(self, func, times, n_states, n_theta, t0)
72 # Private
73 self._augmented_times = np.insert(times, 0, t0).astype(floatX)
---> 74 self._augmented_func = utils.augment_system(func, self.n_states, self.n_theta)
75 self._sens_ic = utils.make_sens_ic(self.n_states, self.n_theta, floatX)
76
~/software/pymc3/pymc3/ode/utils.py in augment_system(ode_func, n_states, n_theta)
96
97 # Now compute gradients
---> 98 J = tt.jacobian(t_yhat, t_y)
99
100 Jdfdy = tt.dot(J, dydp)
~/miniconda3/envs/spols191122/lib/python3.7/site-packages/theano/gradient.py in jacobian(expression, wrt, consider_constant, disconnected_inputs)
1876 "tensor.jacobian expects a Variable as `expression`"
1877 assert expression.ndim < 2, \
-> 1878 ("tensor.jacobian expects a 1 dimensional variable as "
1879 "`expression`. If not use flatten to make it a vector")
1880
AssertionError: tensor.jacobian expects a 1 dimensional variable as `expression`. If not use flatten to make it a vector
And I came up with this ugly workaround:
n=3
def odefunc(y, t, p):
y1 = y[:n]
y2 = y[n:]
dy1 = -p[0]*y1*y2
dy2 = p[0]*y1*y2
return_string = '['
for i in range(n):
return_string += f'dy1[{i}], '
for i in range(n):
return_string += f'dy2[{i}], '
return_string += ']'
print(return_string)
return eval(return_string)
pm.ode.DifferentialEquation(odefunc, times=[1], n_states=2*n, n_theta=1)
Output:
[dy1[0], dy1[1], dy1[2], dy2[0], dy2[1], dy2[2], ]
<pymc3.ode.ode.DifferentialEquation at 0x7fe4bb280860>