Mark Seygan Mark Seygan - 1 year ago 67
Python Question

theano.function - how does it work?

I've read the official documenation and read the comments here, and one man told me that it tells Theano to compile the symbolic computation graph into an actual program that you can run.

However, I still cannot figure out how does it know, for example in this code:

self.update_fun = theano.function(
inputs=[self.input_mat, self.output_mat],

how to compute all that, if it has no body? I mean all those things are computed in some code above these pasted lines, but... is theano.function actually looking to source code to find out how to compute those things? I'm just guessing and would really like to know how it works.

Maybe the problem I have in the explanation that "it tells Theano to compile the symbolic computation graph into an actual program" is that I have no clue what symbolic computation graph is, so that would be another question very related to the previous.

Explanation would be appreciated.

Answer Source

I'm no expert but here's my take at explaining it:

Yes the 'body' is defined in the code above. But theano doesn't 'interpret' that code directly like the python interpreter would. The code in question is just creating theano objects that will allow theano to compile the desired function. Let's take a simple example: how you would create a function f(x) = 2x + x**3.

You first create a symbolic input variable x. Then you define the 'body' of the function by building the symbolic expression of f(x):

y = 2 * x + x**3 # defines a new symbolic variable which depends on x

This y object is equivalent to a graph representing the formula. Something like Plus(Times(2,x), Power(x,3)). You finally call theano.function with input=x and output=y. Then theano does its magic and compiles the actual function f(x) = y = 2 * x + x**3 from the information (the graph) 'contained' in y.

Does it make things clearer?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download