I’d like to use scan to iteratively multiply a matrix onto a vector. At each step, the matrix elements are first constructed from the values of the scan
routine’s sequences
and non_sequences
, and then the matrix is multiplied onto the vector. The state of the vector is updated from one iteration to the next via outputs_info
(scan tutorial e.g. 4).
An attempt at the code is pasted below. This attempt yields TypeError: Unsupported dtype for TensorType: object
, thrown at the call to pt.tensor.as_tensor_variable()
.
def step(t,
tau_01, tau_10, a_01, a_10,
p):
"""
step(sequences,
non_sequences,
outputs_info)
"""
# Transition rates in effect at time `t` within `times`
gamma_01 = a_01 * pt.tensor.math.exp(-t/tau_01)
gamma_10 = a_10 * pt.tensor.math.exp(-t/tau_10)
# Transition matrix
T = pt.tensor.as_tensor_variable(
np.array([[-gamma_01, gamma_10],
[ gamma_01, -gamma_10]])
)
return pt.tensor.dot(T,p)
# sequences
times = pt.tensor.dvector('times')
# non_sequences
tau_01 = pt.tensor.dscalar('tau_01')
tau_10 = pt.tensor.dscalar('tau_10')
a_01 = pt.tensor.dscalar('a_01')
a_10 = pt.tensor.dscalar('a_10')
# outputs_info
p = pt.tensor.dvector('p')
output, updates = pt.scan(fn=step,
sequences=[times],
non_sequences=[tau_01, tau_10, a_01, a_10],
outputs_info=[p])
f = pt.function(inputs=[times,
tau_01, tau_10, a_01, a_10,
p],
outputs=output,
updates=updates)
# sequences
times_value = np.arange(0, 100+1, 1)
# non_sequences
tau_01_value = 40
tau_10_value = 20
a_01_value = 5
a_10_value = 50
# outputs_info
p_value = np.asarray([1, 0]) # [state-0, state-1]
result = f(times_value,
tau_01_value, tau_10_value, a_01_value, a_10,
p_value)
This question is similar to Learning pytensor scan function utilizing a simple population model in the sense that I’m attempting to do something like forward simulation of a population model, but with the added complication of computing new elements of the transition matrix at each step.
What might I have wrong here? Is there a recommended way of constructing a matrix within the step
function of a scan
routine?