How to get value from a theano tensor variable backed by a shared variable? How to get value from a theano tensor variable backed by a shared variable? numpy numpy

How to get value from a theano tensor variable backed by a shared variable?


get_value only works for shared variables. TensorVariables are general expressions and thus potentially need extra input in order to be able to determine their value (Imagine you set y = x + z, where z is another tensor variable. You would need to specify z before being able to calculate y). You can either create a function to provide this input or provide it in a dictionary using the eval method.

In your case, y only depends on x, so you can do

import theanoimport theano.tensor as Tx = theano.shared(numpy.asarray([1, 2, 3], dtype='float32'))y = T.cast(x, 'int32')y.eval()

and you should see the result

array([1, 2, 3], dtype=int32)

(And in the case y = x + z, you would have to do y.eval({z : 3.}), for example)