pythontheanotensor

How to get the value of a Theano.tensor variable?


I have to do something like this:

import Theano.tensor as tt

a = 2
b = 3
c = tt.arctan2(a,b)

c has now as output Elemwise{arctan2,no_inplace}.0. How can I get the computed value of the function? Have already read here that I need to compile the Theano function, but didn't really understood how... Can someone please help me?

Thanks in advance


Solution

  • In theano you first need to define your variables as symbols. Then you define a function with those symbols. A theano.function has a List of input parameters and the function which should be executed as parameters.

    Once that is done, you can compute your function by substituting the symbols with actual values.

    from theano import function
    import theano.tensor as tt
    
    a = tt.dscalar('a')
    b = tt.dscalar('b')
    f = theano.function([a,b], tt.arctan2(a, b))
    f(2, 3)
    

    This will output:

    array(0.5880026)