# 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?

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

def SIR(y, t, p):
ds = -p * y * (y + alpha * y) / N
de = p * y * (y + alpha * y) / N - y / D_e
di = p * y / D_e - y / D_q - y / D_i
da = (1 - p) * y / D_e - y / D_i
dh = y / D_q - y / 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 * y * (y + alpha * y) / 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