**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