提交 b0c8223d authored 作者: Iban Harlouchet's avatar Iban Harlouchet 提交者: Arnaud Bergeron

testcode for doc/tutorial/loop.txt

上级 be01a30d
......@@ -26,7 +26,7 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
**Scan Example: Computing tanh(x(t).dot(W) + b) elementwise**
.. code-block:: python
.. testcode::
import theano
import theano.tensor as T
......@@ -46,15 +46,21 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
b = np.ones((2), dtype=theano.config.floatX)
b[1] = 2
print compute_elementwise(x, w, b)[0]
print(compute_elementwise(x, w, b)[0])
# comparison with numpy
print np.tanh(x.dot(w) + b)
print(np.tanh(x.dot(w) + b))
.. testoutput::
[[ 0.96402758 0.99505475]
[ 0.96402758 0.99505475]]
[[ 0.96402758 0.99505475]
[ 0.96402758 0.99505475]]
**Scan Example: Computing the sequence x(t) = tanh(x(t - 1).dot(W) + y(t).dot(U) + p(T - t).dot(V))**
.. code-block:: python
.. testcode::
import theano
import theano.tensor as T
......@@ -84,18 +90,31 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
p[0, :] = 3
v = np.ones((2, 2), dtype=theano.config.floatX)
print compute_seq(x, w, y, u, p, v)[0]
print(compute_seq(x, w, y, u, p, v)[0])
# comparison with numpy
x_res = np.zeros((5, 2), dtype=theano.config.floatX)
x_res[0] = np.tanh(x.dot(w) + y[0].dot(u) + p[4].dot(v))
for i in range(1, 5):
x_res[i] = np.tanh(x_res[i - 1].dot(w) + y[i].dot(u) + p[4-i].dot(v))
print x_res
print(x_res)
.. testoutput::
[[-0.99505475 -0.99505475]
[ 0.96471973 0.96471973]
[ 0.99998585 0.99998585]
[ 0.99998771 0.99998771]
[ 1. 1. ]]
[[-0.99505475 -0.99505475]
[ 0.96471973 0.96471973]
[ 0.99998585 0.99998585]
[ 0.99998771 0.99998771]
[ 1. 1. ]]
**Scan Example: Computing norms of lines of X**
.. code-block:: python
.. testcode::
import theano
import theano.tensor as T
......@@ -108,14 +127,19 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
# test value
x = np.diag(np.arange(1, 6, dtype=theano.config.floatX), 1)
print compute_norm_lines(x)[0]
print(compute_norm_lines(x)[0])
# comparison with numpy
print np.sqrt((x ** 2).sum(1))
print(np.sqrt((x ** 2).sum(1)))
.. testoutput::
[ 1. 2. 3. 4. 5. 0.]
[ 1. 2. 3. 4. 5. 0.]
**Scan Example: Computing norms of columns of X**
.. code-block:: python
.. testcode::
import theano
import theano.tensor as T
......@@ -128,14 +152,19 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
# test value
x = np.diag(np.arange(1, 6, dtype=theano.config.floatX), 1)
print compute_norm_cols(x)[0]
print(compute_norm_cols(x)[0])
# comparison with numpy
print np.sqrt((x ** 2).sum(0))
print(np.sqrt((x ** 2).sum(0)))
.. testoutput::
[ 0. 1. 2. 3. 4. 5.]
[ 0. 1. 2. 3. 4. 5.]
**Scan Example: Computing trace of X**
.. code-block:: python
.. testcode::
import theano
import theano.tensor as T
......@@ -153,14 +182,20 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
# test value
x = np.eye(5, dtype=theano.config.floatX)
x[0] = np.arange(5, dtype=theano.config.floatX)
print compute_trace(x)[0]
print(compute_trace(x)[0])
# comparison with numpy
print np.diagonal(x).sum()
print(np.diagonal(x).sum())
.. testoutput::
4.0
4.0
**Scan Example: Computing the sequence x(t) = x(t - 2).dot(U) + x(t - 1).dot(V) + tanh(x(t - 1).dot(W) + b)**
.. code-block:: python
.. testcode::
import theano
import theano.tensor as T
......@@ -187,7 +222,7 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
n = 10
b = np.ones((2), dtype=theano.config.floatX)
print compute_seq2(x, u, v, w, b, n)
print(compute_seq2(x, u, v, w, b, n))
# comparison with numpy
x_res = np.zeros((10, 2))
......@@ -197,11 +232,35 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
for i in range(2, 10):
x_res[i] = (x_res[i - 2].dot(u) + x_res[i - 1].dot(v) +
np.tanh(x_res[i - 1].dot(w) + b))
print x_res
print(x_res)
.. testoutput::
[array([[ 1.40514825, 1.40514825],
[ 2.88898899, 2.38898899],
[ 4.34018291, 4.34018291],
[ 6.53463142, 6.78463142],
[ 9.82972243, 9.82972243],
[ 14.22203814, 14.09703814],
[ 20.07439936, 20.07439936],
[ 28.12291843, 28.18541843],
[ 39.1913681 , 39.1913681 ],
[ 54.28407732, 54.25282732]])]
[[ 1.40514825 1.40514825]
[ 2.88898899 2.38898899]
[ 4.34018291 4.34018291]
[ 6.53463142 6.78463142]
[ 9.82972243 9.82972243]
[ 14.22203814 14.09703814]
[ 20.07439936 20.07439936]
[ 28.12291843 28.18541843]
[ 39.1913681 39.1913681 ]
[ 54.28407732 54.25282732]]
**Scan Example: Computing the Jacobian of y = tanh(v.dot(A)) wrt x**
.. code-block:: python
.. testcode::
import theano
import theano.tensor as T
......@@ -221,13 +280,22 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
print compute_jac_t(w, x)[0]
# 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.
**Scan Example: Accumulate number of loop during a scan**
.. code-block:: python
.. testcode::
import theano
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
**Scan Example: Computing tanh(v.dot(W) + b) * d where d is binomial**
.. code-block:: python
.. testcode::
import theano
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
w = np.ones((2, 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.
**Scan Example: Computing pow(A, k)**
.. code-block:: python
.. testcode::
import theano
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
power = theano.function(inputs=[A, k], outputs=final_result,
updates=updates)
print power(range(10), 2)
#[ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81.]
print(power(range(10), 2))
.. testoutput::
[ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81.]
**Scan Example: Calculating a Polynomial**
.. code-block:: python
.. testcode::
import numpy
import theano
......@@ -329,7 +413,10 @@ Note that if you want to use a random variable ``d`` that will not be updated th
test_coeff = numpy.asarray([1, 0, 2], dtype=numpy.float32)
print calculate_polynomial(test_coeff, 3)
# 19.0
.. testoutput::
19.0
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论