This module provides function(), commonly accessed as theano.function, the interface for compiling graphs into callable objects.
You’ve already seen example usage in the basic tutorial... something like this:
>>> x = theano.tensor.dscalar()
>>> f = theano.function([x], 2*x)
>>> print f(4) # prints 8.0
The idea here is that we’ve compiled the symbolic graph (2*x) into a function that can be called on a number and will do some computations.
The behaviour of function can be controlled in several ways, such as Param, mode, updates, and givens. These are covered in the tutorial examples and tutorial on modes.
A class for attaching information to function outputs
A variable in an expression graph to use as a compiled-function output
True indicates that a reference to internal storage may be returned, and that the caller is aware that subsequent function evaluations might overwrite this memory.
Initialize attributes from arguments.
A class for attaching information to function inputs.
A variable in an expression graph to use as a compiled-function parameter
The default value to use at call-time (can also be a Container where the function will find a value at call-time.)
A string to identify an argument for this parameter in keyword arguments.
True means the compiled-function is allowed to modify this argument. False means it is not allowed.
If False, a function argument may be copied or cast to match the type required by the parameter variable. If True, a function argument must exactly match the type required by variable.
Initialize object attributes.
Return a callable object that will calculate outputs from inputs.
Parameters: |
|
---|---|
Return type: | Function instance |
Returns: | a callable object that will compute the outputs (given the inputs) and update the implicit function arguments according to the updates. |
Inputs can be given as variables or Param instances. Param instances also have a variable, but they attach some extra information about how call-time arguments corresponding to that variable should be used. Similarly, Out instances can attach information about how output variables should be returned.
The default is typically ‘FAST_RUN’ but this can be changed in theano.config. The mode argument controls the sort of optimizations that will be applied to the graph, and the way the optimized graph will be evaluated.
After each function evaluation, the updates mechanism can replace the value of any SharedVariable [implicit] inputs with new values computed from the expressions in the updates list. An exception will be raised if you give two update expressions for the same SharedVariable input (that doesn’t make sense).
If a SharedVariable is not given an update expression, but has a default_update member containing an expression, this expression will be used as the update expression for this variable. Passing no_default_updates=True to function disables this behavior entirely, passing no_default_updates=[sharedvar1, sharedvar2] disables it for the mentioned variables.
Regarding givens: Be careful to make sure that these substitutions are independent, because behaviour when Var1 of one pair appears in the graph leading to Var2 in another expression is undefined (e.g. with {a: x, b: a + 1}). Replacements specified with givens are different from optimizations in that Var2 is not expected to be equivalent to Var1.