提交 6c77667d authored 作者: Joseph Turian's avatar Joseph Turian

updated function documentation

上级 56d344a0
...@@ -29,38 +29,54 @@ Inputs ...@@ -29,38 +29,54 @@ Inputs
The ``inputs`` argument to ``theano.function`` is a list, containing the ``Result`` instances for which values will be specified at the time of the function call. But inputs can be more than just Results. The ``inputs`` argument to ``theano.function`` is a list, containing the ``Result`` instances for which values will be specified at the time of the function call. But inputs can be more than just Results.
``In`` instances let us attach properties to ``Results`` to tell function more about how to use them. ``In`` instances let us attach properties to ``Results`` to tell function more about how to use them.
.. code-block:: python **In(result, name=None, value=None, update=None, mutable=False)** returns an ``In`` instance:
- ``result``: a Result instance.
This will be assigned a value before running the function,
not computed from its owner.
- ``name``: Any type. (If autoname_input=True, defaults to result.name).
If name is a valid Python identifier, this input can be set by
``kwarg``, and its value can be accessed by ``self.<name>``.
Default: ``None``
- ``value``: literal or Container
This is the default value of the Input.
Default: ``None``
- ``update``: Result instance
This expression Result will replace ``value`` after each function call.
Default: ``None``
- ``mutable``: Bool (requires value)
If ``True``, permit the compiled function to modify the python object being used as the default value.
class In(obect): Default: ``False``
def __init__(self, result, name=None, value=None, update=None, mutable=False):
""" - ``autoname``: Bool
result: a Result instance.
This will be assigned a value before running the function, ``True``: if ``name`` is None and the Result has a name, it will be taken
not computed from its owner. as the input's name.
name: Any type. (If autoname_input=True, defaults to result.name). ``False``: the name is the exact value passed as the name parameter
If name is a valid Python identifier, this input can be set by kwarg, and its value (possibly ``None``).
can be accessed by self.<name>.
Default: ???
value: literal or Container
This is the default value of the Input.
update: Result instance
value (see previous) will be replaced with this expression result after each function call.
mutable: Bool (requires value)
True: permit the compiled function to modify the python object being used as the default value.
False: do not permit the compiled function to modify the python object being used as the default value.
autoname: Bool
True: if the name parameter is None and the Result has a name, it will be taken as the input's name
False: the name is the exact value passed as the name parameter (possibly None)
"""
Value: initial and default values Value: initial and default values
--------------------------------- ---------------------------------
A non-None `value` argument makes an In() instance an optional parameter. For example, in the following code we are defining an arity-2 function ``inc``. A non-None `value` argument makes an In() instance an optional parameter
of the compiled function. For example, in the following code we are
defining an arity-2 function ``inc``.
>>> u, x, s = T.scalars('uxs') >>> u, x, s = T.scalars('uxs')
>>> inc = function([u, In(x, value=3), In(s, update=(s+x*u), value=10.0)], []) >>> inc = function([u, In(x, value=3), In(s, update=(s+x*u), value=10.0)], [])
...@@ -68,28 +84,43 @@ A non-None `value` argument makes an In() instance an optional parameter. For e ...@@ -68,28 +84,43 @@ A non-None `value` argument makes an In() instance an optional parameter. For e
Since we provided a ``value`` for ``s`` and ``x``, we can call it with just a value for ``u`` like this: Since we provided a ``value`` for ``s`` and ``x``, we can call it with just a value for ``u`` like this:
>>> inc(5) # update s with 10+3*5 >>> inc(5) # update s with 10+3*5
>>> print inc[s] # print 25 []
>>> print inc[s]
25.0
The effect of this call is to increment the storage associated to ``s`` in ``inc`` by 15. The effect of this call is to increment the storage associated to ``s`` in ``inc`` by 15.
If we pass two arguments to ``inc``, then we over-ride the value associated to ``x``, but only for the one function call. If we pass two arguments to ``inc``, then we override the value associated to
``x``, but only for this one function call.
>>> inc(3, 4) # update s with 25 + 3*4 >>> inc(3, 4) # update s with 25 + 3*4
>>> print inc[s] # print 37 []
>>> print inc[x] # print 3 <--- the over-ride value of 4 was only temporary >>> print inc[s]
37.0
>>> print inc[x] # the override value of 4 was only temporary
3.0
If we pass three arguments to ``inc``, then we over-ride the value associated If we pass three arguments to ``inc``, then we override the value associated
with ``x`` and ``u`` and ``s``. with ``x`` and ``u`` and ``s``.
Since ``s``'s value is updated on every call, the old value of ``s`` will be ignored and then replaced. Since ``s``'s value is updated on every call, the old value of ``s`` will be ignored and then replaced.
>>> inc(3, 4, 7) # update s with 7 + 3*4 >>> inc(3, 4, 7) # update s with 7 + 3*4
>>> print inc[s] # print 19 []
>>> print inc[s]
19.0
We can also assign to ``inc[s]`` directly:
>>> inc[s] = 10
>>> inc[s]
array(10.0)
Advanced: Sharing Storage Between Functions Advanced: Sharing Storage Between Functions
------------------------------------------- -------------------------------------------
The ``value`` can be a :api:`theano.gof.Container` as well as a literal. ``value`` can be a :api:`theano.gof.Container` as well as a literal.
This permits linking a value of a Result in one function to the value of a Result in another function. This permits linking a value of a Result in one function to the value of a Result in another function.
By using a ``Container`` as a value we can implement shared variables between functions. By using a ``Container`` as a value we can implement shared variables between functions.
...@@ -98,8 +129,14 @@ For example, consider the following program. ...@@ -98,8 +129,14 @@ For example, consider the following program.
>>> x, s = T.scalars('xs') >>> x, s = T.scalars('xs')
>>> inc = function([x, In(s, update=(s+x), value=10.0)], []) >>> inc = function([x, In(s, update=(s+x), value=10.0)], [])
>>> dec = function([x, In(s, update=(s-x), value=inc.container[s])], []) >>> dec = function([x, In(s, update=(s-x), value=inc.container[s])], [])
>>> dec(3) >>> dec(3)
>>> print inc[s] # print 7 []
>>> print inc[s]
7.0
>>> inc(2)
[]
>>> print dec[s]
9.0
The functions ``inc`` and ``dec`` operate on a shared internal value for ``s``. The functions ``inc`` and ``dec`` operate on a shared internal value for ``s``.
Theano's Module system uses this mechanism to share storage between Methods. Theano's Module system uses this mechanism to share storage between Methods.
...@@ -110,48 +147,62 @@ but that's usually how this mechanism is used. ...@@ -110,48 +147,62 @@ but that's usually how this mechanism is used.
Input Argument Restrictions Input Argument Restrictions
--------------------------- ---------------------------
- Every input list element must be (or be upgradable to) a valid ``In`` instance (see the shortcut rules below). The following restrictions apply to the inputs to ``theano.function``:
- Every input list element must be a valid ``In`` instance, or must be
upgradable to a valid ``In`` instance. See the shortcut rules below.
- The same restrictions apply as in Python function definitions: default arguments and keyword arguments have to come at the end of the list. Un-named mandatory arguments must come at the beginning of the list. - The same restrictions apply as in Python function definitions:
default arguments and keyword arguments must come at the end of
the list. Un-named mandatory arguments must come at the beginning of
the list.
- Names have to be unique within an input list. If multiple inputs have the same name, then the function will raise an exception. [***Which exception?] - Names have to be unique within an input list. If multiple inputs
have the same name, then the function will raise an exception. [***Which
exception?**]
- Two ``In`` instances may not name the same Result. (You can't give the same parameter multiple times.) - Two ``In`` instances may not name the same Result. I.e. you cannot
give the same parameter multiple times.
If no name is specified explicitly for an In instance, then it will be taken from the Result's name. If no name is specified explicitly for an In instance, then its name
Note that this feature can cause harmless-looking input lists to not satisfy the two conditions above. will be taken from the Result's name. Note that this feature can cause
In such cases, Inputs should be named explicitly to avoid problems such as duplicate names, and named arguments preceding unnamed ones. harmless-looking input lists to not satisfy the two conditions above.
This automatic naming feature can be disabled by instantiating an In instance explicitly with the ``autoname`` flag set to False. In such cases, Inputs should be named explicitly to avoid problems
such as duplicate names, and named arguments preceding unnamed ones.
This automatic naming feature can be disabled by instantiating an In
instance explicitly with the ``autoname`` flag set to False.
Access to function values and containers Access to function values and containers
---------------------------------------- ----------------------------------------
For each input, ``theano.function`` will create a ``Container`` if the value wasn't already a ``Contanier``. For each input, ``theano.function`` will create a ``Container`` if the
At the time of a function call, each of these containers must be filled with a value. value was not already a ``Container``. At the time of a function call,
Each input (but especially ones with a default value or an update expression) may have a value between calls too. each of these containers must be filled with a value. Each input (but
The function interface defines a way to get at both the current value associated with an input, as well as the container which will contain all future values. especially ones with a default value or an update expression) may have a
value between calls. The function interface defines a way to get at
both the current value associated with an input, as well as the container
which will contain all future values:
The ``value`` property accesses the current values. It is both readable and writable, but - The ``value`` property accesses the current values. It is both readable
assignments (writes) may be implemented by an internal copy and/or casts. and writable, but assignments (writes) may be implemented by an internal
copy and/or casts.
The ``container`` property accesses the corresponding container. - The ``container`` property accesses the corresponding container.
This property accesses is a read-only dictionary-like interface. It is useful This property accesses is a read-only dictionary-like interface. It is
for fetching the container associated with a particular input to share useful for fetching the container associated with a particular input to
containers between functions, or to have a sort of pointer to an always share containers between functions, or to have a sort of pointer to an
up-to-date value. always up-to-date value.
(It is illegal to try to get a function to use the same container in two or
more places.)
Both ``value`` and ``container`` properties provide dictionary-like access based on three types of keys: Both ``value`` and ``container`` properties provide dictionary-like access based on three types of keys:
- ''integer keys'': you can look up a value/container by its position in the input list; - integer keys: you can look up a value/container by its position in the input list;
- ''name keys'': you can look up a value/container by its name; - name keys: you can look up a value/container by its name;
- ''Result keys'': you can look up a value/container by the Result it corresponds to. - Result keys: you can look up a value/container by the Result it corresponds to.
In addition to these access mechanisms, there is an even more convenient method In addition to these access mechanisms, there is an even more convenient
to access values by simply indexing a Function directly, as in the method to access values by indexing a Function directly by typing
examples above, by typing ``fn[<name>]``. ``fn[<name>]``, as in the examples above.
To show some examples of these access methods... To show some examples of these access methods...
...@@ -159,7 +210,7 @@ To show some examples of these access methods... ...@@ -159,7 +210,7 @@ To show some examples of these access methods...
a, b, c = T.scalars('xys') # set the internal names of graph nodes a, b, c = T.scalars('xys') # set the internal names of graph nodes
# Note that the name of c is 's', not 'c'! # Note that the name of c is 's', not 'c'!
fn = theano.function([a, b, ((c, c+a+b), 10.0)], []) fn = function([a, b, ((c, c+a+b), 10.0)], [])
#the value associated with c is accessible in 3 ways #the value associated with c is accessible in 3 ways
assert fn['s'] is fn.value[c] assert fn['s'] is fn.value[c]
...@@ -215,15 +266,17 @@ Example: ...@@ -215,15 +266,17 @@ Example:
# function is called. # function is called.
fn(1) # illegal because there are two required arguments fn(1) # illegal because there are two required arguments
fn(1, 2) # legal, z is 42, w goes 0 -> 1 (because w <- w + x) fn(1, 2) # legal, z is 42, w goes 0 -> 1 (because w <- w + x), returns array(45.0)
fn(1, y = 2) # legal, z is 42, w goes 1 -> 2 fn(1, y = 2) # legal, z is 42, w goes 1 -> 2, returns array(45.0)
fn(x = 1, y = 2) # illegal because x was not named fn(x = 1, y = 2) # illegal because x was not named
fn(1, 2, 3) # legal, z is 3, w goes 2 -> 3 fn(1, 2, 3) # legal, z is 3, w goes 2 -> 3, returns array(6.0)
fn(1, z = 3, y = 2) # legal, z is 3, w goes 3 -> 4 fn(1, z = 3, y = 2) # legal, z is 3, w goes 3 -> 4, returns array(6.0)
fn(1, 2, w = 400) # legal, z is 42 again, w goes 400 -> 401 fn(1, 2, w = 400) # legal, z is 42 again, w goes 400 -> 401, returns array(45.0)
fn(1, 2) # legal, z is 42, w goes 401 -> 402 fn(1, 2) # legal, z is 42, w goes 401 -> 402, returns array(45.0)
In the example above, ``z`` has value 42 when no value is explicitly given.
This default value is potentially used at every function invocation, because
``z`` has no ``update`` or storage associated with it.
Outputs Outputs
...@@ -231,26 +284,22 @@ Outputs ...@@ -231,26 +284,22 @@ Outputs
The ``outputs`` argument to function can be one of The ``outputs`` argument to function can be one of
- None, or - ``None``, or
- a Result, or ``Out`` instance, or - a Result or ``Out`` instance, or
- a list of Results or ``Out`` instances. - a list of Results or ``Out`` instances.
An ``Out`` instance is a structure that lets us attach options to individual output ``Result`` instances, An ``Out`` instance is a structure that lets us attach options to individual output ``Result`` instances,
similarly to how ``In`` lets us attach options to individual input ``Result`` instances. similarly to how ``In`` lets us attach options to individual input ``Result`` instances.
.. code-block:: python **Out(result, borrow=False)** returns an ``Out`` instance:
class Out(object):
def __init__(self, result, borrow=False):
""" * ``borrow``
If ``True``, a reference to function's internal storage
is OK. A value returned for this output might be clobbered by running
the function again, but the function might be faster.
borrow: set this to True to indicate that a reference to Default: ``False``
function's internal storage is OK. A value returned
for this output might be clobbered by running the
function again, but the function might be faster.
"""
....
...@@ -281,14 +330,15 @@ If a list of ``Result`` or ``Out`` instances is given as argument, then the comp ...@@ -281,14 +330,15 @@ If a list of ``Result`` or ``Out`` instances is given as argument, then the comp
Modes Modes
===== =====
The ``mode`` parameter to ``theano.function`` controls how the inputs-to-outputs graph is transformed into a callable object. The ``mode`` parameter to ``theano.function`` controls how the
inputs-to-outputs graph is transformed into a callable object.
Currently, theano defines the following modes by name: Theano defines the following modes by name:
- ``FAST_COMPILE``: Apply just a few optimizations, but use C op implementations where possible. - ``FAST_COMPILE``: Apply just a few optimizations, but use C op implementations where possible.
- ``FAST_RUN``: Apply all optimizations, use C op implementations where possible. - ``FAST_RUN``: Apply all optimizations, and use C op implementations where possible.
- ``DEBUG_MODE``: Verify the correctness of all optimizations, and compare C and python - ``DEBUG_MODE``: Verify the correctness of all optimizations, and compare C and python
implementations. This mode can take much longer than the other modes, implementations. This mode can take much longer than the other modes,
but can identify many kinds of problems. but can identify many kinds of problems.
For a finer level of control over which optimizations are applied, and whether For a finer level of control over which optimizations are applied, and whether
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论