Getting value from TensorVariable in DifferentialEquation

I am using DifferentialEquation and have defined function SIR which is form f(y,t,p) where t is a TensorVariable. I try to read its values (it should hold an integer) but I cannot do that.

t.eval() creates an error “MissingInputError: Undeclared input”. Any ideas how I could read tensor t and this way find out which integer t currently is?

sir_model = DifferentialEquation(
func=SIR,
times=timeline,
n_states=2,
n_theta=2,
t0=0,
)

you need to supply a dict to t.eval() with value replacing the free variables. Could you provide a full code you are trying to evaluate?

Thanks for your message, junpenglao!

My SIR-function is a little bit more complex than a normal one.

def SIR(y, t, p):
ds = -p[0] * y[0] * (y[2] + alpha * y[3]) / N
de = p[0] * y[0] * (y[2] + alpha * y[3]) / N - y[1] / D_e
di = p[1] * y[1] / D_e - y[2] / D_q - y[2] / D_i
da = (1 - p[1]) * y[1] / D_e - y[3] / D_i
dh = y[2] / D_q - y[4] / D_h
return [ds, de, di, da, dh]

The question is still simple. I would like to read an element of array arr using correct corresponding time index, meaning arr[t].

However, t is not an integer but a TensorVariable which is the reason why I want to convert it into integer. When i am using t.eval(), for example on line
ds = -p[0] * y[0] * (y[2] + alpha * y[3]) / N + t.eval()
I get an error MissingInputError: Undeclared input

It seems like I am reading uninitialized value…

usually, you dont need to read the value of t, as DifferentialEquation is a theano Ops that would do that for you, are you getting some error if you remove .eval()?

Also, if t is int type, arr[t] would work if arr is a theano tensor (if not you can do theano.shared(arr) to convert it).

Without eval() I cannot read arr (the code throws an error) because then t inside arr[t] is still a tensor.

The idea of arr_theano = theano.shared(arr) sounds great! Now, I am reading arr_theano[t] inside my SIR-function but it will throw an error TypeError: Expected an integer.
Funny because in my DifferentialEquation-implementation I have
timeline =[0,1,2,3,4,5,6,7,8,9,10,11,12,13] (integer type). I also tried timeline which is numpy-int64 type.

I think I got it right now!

t_int = t.astype('int32') was required at the beginning of my SIR-function. Thanks!

1 Like