Mark Seygan - 10 months ago 37

Python Question

I've read the official documenation and read the comments here https://github.com/Theano/theano/blob/ddfd7d239a1e656cee850cdbc548da63f349c37d/theano/compile/function.py#L74-L324, 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],

outputs=self.cost,

updates=updates,

allow_input_downcast=True)

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 "

Explanation would be appreciated.

Answer

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?

Source (Stackoverflow)