>>> # the first two arguments are required and the last two are
outputs = x + y + z)
>>> # optional and initialized to 42 and 0, respectively.
# the first two arguments are required and the last two are
>>> # The last argument, w, is updated with w + x each time the
# optional and initialized to 42 and 0, respectively.
>>> # function is called.
# The last argument, w, is updated with w + x each time the
# function is called.
>>> fn(1) # illegal because there are two required arguments # doctest: +ELLIPSIS
Traceback (most recent call last):
fn(1) # illegal because there are two required arguments
...
fn(1, 2) # legal, z is 42, w goes 0 -> 1 (because w <- w + x), returns array(45.0)
TypeError: Missing required input: y
fn(1, y = 2) # legal, z is 42, w goes 1 -> 2, returns array(45.0)
>>> fn(1, 2) # legal, z is 42, w goes 0 -> 1 (because w <- w + x)
fn(x = 1, y = 2) # illegal because x was not named
array(45.0)
fn(1, 2, 3) # legal, z is 3, w goes 2 -> 3, returns array(6.0)
>>> fn(1, y=2) # legal, z is 42, w goes 1 -> 2
fn(1, z = 3, y = 2) # legal, z is 3, w goes 3 -> 4, returns array(6.0)
array(45.0)
fn(1, 2, w = 400) # legal, z is 42 again, w goes 400 -> 401, returns array(45.0)
>>> fn(x=1, y=2) # illegal because x was not named # doctest: +ELLIPSIS
fn(1, 2) # legal, z is 42, w goes 401 -> 402, returns array(45.0)
Traceback (most recent call last):
...
TypeError: Unknown input or state: x. The function has 3 named inputs (y, z, w), and 1 unnamed input which thus cannot be accessed through keyword argument (use 'name=...' in a variable's constructor to give it a name).
>>> fn(1, 2, 3) # legal, z is 3, w goes 2 -> 3
array(6.0)
>>> fn(1, z=3, y=2) # legal, z is 3, w goes 3 -> 4
array(6.0)
>>> fn(1, 2, w=400) # legal, z is 42 again, w goes 400 -> 401
array(45.0)
>>> fn(1, 2) # legal, z is 42, w goes 401 -> 402
array(45.0)
In the example above, ``z`` has value 42 when no value is explicitly given.
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
This default value is potentially used at every function invocation, because
...
@@ -282,20 +306,25 @@ If a single ``Variable`` or ``Out`` instance is given as argument, then the comp
...
@@ -282,20 +306,25 @@ If a single ``Variable`` or ``Out`` instance is given as argument, then the comp
If a list of ``Variable`` or ``Out`` instances is given as argument, then the compiled function will return a list of their values.
If a list of ``Variable`` or ``Out`` instances is given as argument, then the compiled function will return a list of their values.
HINT: Re-running with most Theano optimization disabled could give you a back-traces when this node was created. This can be done with by setting the Theano flags 'optimizer=fast_compile'. If that does not work, Theano optimization can be disabled with 'optimizer=None'.
HINT: Re-running with most Theano optimization disabled could give you a back-traces when this node was created. This can be done with by setting the Theano flags 'optimizer=fast_compile'. If that does not work, Theano optimization can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint of this apply node.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint of this apply node.
Arguably the most useful information is approximately half-way through
Arguably the most useful information is approximately half-way through
the error message, where the kind of error is displayed along with its
the error message, where the kind of error is displayed along with its
...
@@ -71,7 +67,7 @@ the faulty line, while ``exception_verbosity=high`` will display a
...
@@ -71,7 +67,7 @@ the faulty line, while ``exception_verbosity=high`` will display a
debugprint of the apply node. Using these hints, the end of the error
debugprint of the apply node. Using these hints, the end of the error
message becomes :
message becomes :
.. code-block:: bash
.. code-block:: none
Backtrace when the node is created:
Backtrace when the node is created:
File "test0.py", line 8, in <module>
File "test0.py", line 8, in <module>
...
@@ -101,7 +97,7 @@ following example. Here, we use ``exception_verbosity=high`` and
...
@@ -101,7 +97,7 @@ following example. Here, we use ``exception_verbosity=high`` and
``optimizer=None`` would and it could therefore be used instead of test values.
``optimizer=None`` would and it could therefore be used instead of test values.
.. code-block:: python
.. testcode:: testvalue
import numpy
import numpy
import theano
import theano
...
@@ -137,7 +133,7 @@ following example. Here, we use ``exception_verbosity=high`` and
...
@@ -137,7 +133,7 @@ following example. Here, we use ``exception_verbosity=high`` and
Running the above code generates the following error message:
Running the above code generates the following error message:
.. code-block:: bash
.. testoutput:: testvalue
Traceback (most recent call last):
Traceback (most recent call last):
File "test1.py", line 31, in <module>
File "test1.py", line 31, in <module>
...
@@ -185,7 +181,7 @@ of error can thus be identified with much more precision and much earlier in
...
@@ -185,7 +181,7 @@ of error can thus be identified with much more precision and much earlier in
the compilation pipeline. For example, running the above code yields the
the compilation pipeline. For example, running the above code yields the
following error message, which properly identifies *line 24* as the culprit.
following error message, which properly identifies *line 24* as the culprit.
.. code-block:: bash
.. code-block:: node
Traceback (most recent call last):
Traceback (most recent call last):
File "test2.py", line 24, in <module>
File "test2.py", line 24, in <module>
...
@@ -228,7 +224,10 @@ The ``compute_test_value`` mechanism works as follows:
...
@@ -228,7 +224,10 @@ The ``compute_test_value`` mechanism works as follows:
Theano provides a 'Print' op to do this.
Theano provides a 'Print' op to do this.
.. code-block:: python
.. testcode::
import numpy
import theano
x = theano.tensor.dvector('x')
x = theano.tensor.dvector('x')
...
@@ -243,6 +242,9 @@ Theano provides a 'Print' op to do this.
...
@@ -243,6 +242,9 @@ Theano provides a 'Print' op to do this.
#this runs the graph with the message, and value printed
#this runs the graph with the message, and value printed
**Scan Example: Computing the Jacobian of y = tanh(v.dot(A)) wrt x**
**Scan Example: Computing the Jacobian of y = tanh(v.dot(A)) wrt x**
.. code-block:: python
.. testcode::
import theano
import theano
import theano.tensor as T
import theano.tensor as T
...
@@ -221,13 +280,22 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
...
@@ -221,13 +280,22 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
print compute_jac_t(w, x)[0]
print compute_jac_t(w, x)[0]
# compare with numpy
# compare with numpy
print ((1 - np.tanh(x.dot(w)) ** 2) * w).T
print(((1 - np.tanh(x.dot(w)) ** 2) * w).T)
.. testoutput::
[[ 0.41997434 0. 0.41997434 0. 0. ]
[ 0. 1. 1. 0. 0. ]
[ 0. 0. 1. 0. 0. ]]
[[ 0.41997434 0. 0.41997434 0. 0. ]
[ 0. 1. 1. 0. 0. ]
[ 0. 0. 1. 0. 0. ]]
Note that we need to iterate over the indices of ``y`` and not over the elements of ``y``. The reason is that scan create a placeholder variable for its internal function and this placeholder variable does not have the same dependencies than the variables that will replace it.
Note that we need to iterate over the indices of ``y`` and not over the elements of ``y``. The reason is that scan create a placeholder variable for its internal function and this placeholder variable does not have the same dependencies than the variables that will replace it.
**Scan Example: Accumulate number of loop during a scan**
**Scan Example: Accumulate number of loop during a scan**
.. code-block:: python
.. testcode::
import theano
import theano
import theano.tensor as T
import theano.tensor as T
...
@@ -246,7 +314,7 @@ Note that we need to iterate over the indices of ``y`` and not over the elements
...
@@ -246,7 +314,7 @@ Note that we need to iterate over the indices of ``y`` and not over the elements
**Scan Example: Computing tanh(v.dot(W) + b) * d where d is binomial**
**Scan Example: Computing tanh(v.dot(W) + b) * d where d is binomial**
.. code-block:: python
.. testcode::
import theano
import theano
import theano.tensor as T
import theano.tensor as T
...
@@ -268,13 +336,26 @@ Note that we need to iterate over the indices of ``y`` and not over the elements
...
@@ -268,13 +336,26 @@ Note that we need to iterate over the indices of ``y`` and not over the elements
w = np.ones((2, 2), dtype=theano.config.floatX)
w = np.ones((2, 2), dtype=theano.config.floatX)
b = np.ones((2), dtype=theano.config.floatX)
b = np.ones((2), dtype=theano.config.floatX)
print compute_with_bnoise(x, w, b)
print(compute_with_bnoise(x, w, b))
.. testoutput::
[array([[ 0.96402758, 0. ],
[ 0. , 0.96402758],
[ 0. , 0. ],
[ 0.76159416, 0.76159416],
[ 0.76159416, 0. ],
[ 0. , 0.76159416],
[ 0. , 0.76159416],
[ 0. , 0.76159416],
[ 0. , 0. ],
[ 0.76159416, 0.76159416]])]
Note that if you want to use a random variable ``d`` that will not be updated through scan loops, you should pass this variable as a ``non_sequences`` arguments.
Note that if you want to use a random variable ``d`` that will not be updated through scan loops, you should pass this variable as a ``non_sequences`` arguments.
**Scan Example: Computing pow(A, k)**
**Scan Example: Computing pow(A, k)**
.. code-block:: python
.. testcode::
import theano
import theano
import theano.tensor as T
import theano.tensor as T
...
@@ -298,13 +379,16 @@ Note that if you want to use a random variable ``d`` that will not be updated th
...
@@ -298,13 +379,16 @@ Note that if you want to use a random variable ``d`` that will not be updated th
power = theano.function(inputs=[A, k], outputs=final_result,
power = theano.function(inputs=[A, k], outputs=final_result,
updates=updates)
updates=updates)
print power(range(10), 2)
print(power(range(10), 2))
#[ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81.]
.. testoutput::
[ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81.]
**Scan Example: Calculating a Polynomial**
**Scan Example: Calculating a Polynomial**
.. code-block:: python
.. testcode::
import numpy
import numpy
import theano
import theano
...
@@ -329,7 +413,10 @@ Note that if you want to use a random variable ``d`` that will not be updated th
...
@@ -329,7 +413,10 @@ Note that if you want to use a random variable ``d`` that will not be updated th