提交 c69e32dd authored 作者: Frederic's avatar Frederic

pep8

上级 e945851a
...@@ -36,12 +36,12 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`. ...@@ -36,12 +36,12 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
W = T.matrix("W") W = T.matrix("W")
b_sym = T.vector("b_sym") b_sym = T.vector("b_sym")
results, updates = theano.scan(lambda v:T.tanh(T.dot(v,W)+b_sym), sequences=X) results, updates = theano.scan(lambda v:T.tanh(T.dot(v, W)+b_sym), sequences=X)
compute_elementwise = theano.function(inputs = [X, W, b_sym], outputs=[results]) compute_elementwise = theano.function(inputs = [X, W, b_sym], outputs=[results])
# test values # test values
x = np.eye(2) x = np.eye(2)
w = np.ones((2,2)) w = np.ones((2, 2))
b = np.ones((2)) b = np.ones((2))
b[1] = 2 b[1] = 2
...@@ -67,28 +67,28 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`. ...@@ -67,28 +67,28 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
V = T.matrix("V") V = T.matrix("V")
P = T.matrix("P") P = T.matrix("P")
results, updates = theano.scan(lambda results, updates = theano.scan(lambda
y,p,x_tm1:T.tanh(T.dot(x_tm1,W)+T.dot(y,U)+T.dot(p,V)), y,p,x_tm1:T.tanh(T.dot(x_tm1, W)+T.dot(y, U)+T.dot(p, V)),
sequences=[Y,P[::-1]], outputs_info=[X]) sequences=[Y, P[::-1]], outputs_info=[X])
compute_seq = theano.function(inputs = [X, W, Y, U, P, V], outputs=[results]) compute_seq = theano.function(inputs = [X, W, Y, U, P, V], outputs=[results])
# test values # test values
x = np.zeros((2)) x = np.zeros((2))
x[1] = 1 x[1] = 1
w = np.ones((2,2)) w = np.ones((2, 2))
y = np.ones((5,2)) y = np.ones((5, 2))
y[0,:] = -3 y[0, :] = -3
u = np.ones((2,2)) u = np.ones((2, 2))
p = np.ones((5,2)) p = np.ones((5, 2))
p[0,:] = 3 p[0, :] = 3
v = np.ones((2,2)) v = np.ones((2, 2))
print compute_seq(x,w,y,u,p,v)[0] print compute_seq(x, w, y, u, p, v)[0]
# comparison with numpy # comparison with numpy
x_res = np.zeros((5,2)) x_res = np.zeros((5, 2))
x_res[0] = np.tanh(x.dot(w) + y[0].dot(u) + p[4].dot(v)) x_res[0] = np.tanh(x.dot(w) + y[0].dot(u) + p[4].dot(v))
for i in range(1,5): 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)) x_res[i] = np.tanh(x_res[i-1].dot(w) + y[i].dot(u) + p[4-i].dot(v))
**Scan Example: Computing norms of lines of X** **Scan Example: Computing norms of lines of X**
...@@ -104,7 +104,7 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`. ...@@ -104,7 +104,7 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
compute_norm_lines = theano.function(inputs = [X], outputs=[results]) compute_norm_lines = theano.function(inputs = [X], outputs=[results])
# test value # test value
x = np.diag(np.arange(1,6),1) x = np.diag(np.arange(1, 6), 1)
print compute_norm_lines(x)[0] print compute_norm_lines(x)[0]
# comparison with numpy # comparison with numpy
...@@ -123,7 +123,7 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`. ...@@ -123,7 +123,7 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
compute_norm_cols = theano.function(inputs = [X], outputs=[results]) compute_norm_cols = theano.function(inputs = [X], outputs=[results])
# test value # test value
x = np.diag(np.arange(1,6),1) x = np.diag(np.arange(1, 6), 1)
print compute_norm_cols(x)[0] print compute_norm_cols(x)[0]
# comparison with numpy # comparison with numpy
...@@ -139,7 +139,7 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`. ...@@ -139,7 +139,7 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
# define tensor variable # define tensor variable
X = T.matrix("X") X = T.matrix("X")
results, updates = theano.scan(lambda i, j, t_f:T.cast(X[i,j]+t_f, floatX), \ results, updates = theano.scan(lambda i, j, t_f:T.cast(X[i, j]+t_f, floatX), \
sequences=[T.arange(X.shape[0]), T.arange(X.shape[1])], \ sequences=[T.arange(X.shape[0]), T.arange(X.shape[1])], \
outputs_info=np.asarray(0., dtype=floatX)) outputs_info=np.asarray(0., dtype=floatX))
result = results[-1] result = results[-1]
...@@ -168,29 +168,29 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`. ...@@ -168,29 +168,29 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
V = T.matrix("V") V = T.matrix("V")
n_sym = T.iscalar("n_sym") n_sym = T.iscalar("n_sym")
results, updates = theano.scan(lambda x_tm2,x_tm1:T.dot(x_tm2,U) + T.dot(x_tm1,V) \ results, updates = theano.scan(lambda x_tm2, x_tm1:T.dot(x_tm2, U) + T.dot(x_tm1, V) \
+ T.tanh(T.dot(x_tm1,W) + b_sym), \ + T.tanh(T.dot(x_tm1, W) + b_sym), \
n_steps=n_sym, outputs_info=[dict(initial = X, taps = [-2,-1])]) n_steps=n_sym, outputs_info=[dict(initial = X, taps = [-2, -1])])
compute_seq2 = theano.function(inputs = [X, U, V, W, b_sym, n_sym], outputs=[results]) compute_seq2 = theano.function(inputs = [X, U, V, W, b_sym, n_sym], outputs=[results])
# test values # test values
x = np.zeros((2,2)) # the initial value must be able to return x[-2] x = np.zeros((2, 2)) # the initial value must be able to return x[-2]
x[1,1] = 1 x[1, 1] = 1
w = 0.5*np.ones((2,2)) w = 0.5*np.ones((2, 2))
u = 0.5*(np.ones((2,2))-np.eye(2)) u = 0.5*(np.ones((2, 2))-np.eye(2))
v = 0.5*np.ones((2,2)) v = 0.5*np.ones((2, 2))
n = 10 n = 10
b = np.ones((2)) b = np.ones((2))
print compute_seq2(x,u,v,w,b,n) print compute_seq2(x, u, v, w, b, n)
# comparison with numpy # comparison with numpy
x_res = numpy.zeros((10,2)) x_res = numpy.zeros((10, 2))
x_res[0] = x[0].dot(u) + x[1].dot(v) + numpy.tanh(x[1].dot(w) + b) x_res[0] = x[0].dot(u) + x[1].dot(v) + numpy.tanh(x[1].dot(w) + b)
x_res[1] = x[1].dot(u) + x_res[0].dot(v) + numpy.tanh(x_res[0].dot(w) + b) x_res[1] = x[1].dot(u) + x_res[0].dot(v) + numpy.tanh(x_res[0].dot(w) + b)
x_res[2] = x_res[0].dot(u) + x_res[1].dot(v) \ x_res[2] = x_res[0].dot(u) + x_res[1].dot(v) \
+ numpy.tanh(x_res[1].dot(w) + b) + numpy.tanh(x_res[1].dot(w) + b)
for i in range(2,10): for i in range(2, 10):
x_res[i] = (x_res[i-2].dot(u) + x_res[i-1].dot(v) \ x_res[i] = (x_res[i-2].dot(u) + x_res[i-1].dot(v) \
+ numpy.tanh(x_res[i-1].dot(w) + b)) + numpy.tanh(x_res[i-1].dot(w) + b))
...@@ -204,20 +204,20 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`. ...@@ -204,20 +204,20 @@ The full documentation can be found in the library: :ref:`Scan <lib_scan>`.
# define tensor variables # define tensor variables
v = T.vector() v = T.vector()
A = T.matrix() A = T.matrix()
y = T.tanh(T.dot(v,A)) y = T.tanh(T.dot(v, A))
results, updates = theano.scan(lambda i:T.grad(y[i], v), sequences = [T.arange(y.shape[0])]) results, updates = theano.scan(lambda i:T.grad(y[i], v), sequences = [T.arange(y.shape[0])])
compute_jac_t = theano.function([A,v], [results], allow_input_downcast = True) # shape (d_out, d_in) compute_jac_t = theano.function([A, v], [results], allow_input_downcast = True) # shape (d_out, d_in)
# test values # test values
x = np.eye(5)[0] x = np.eye(5)[0]
w = np.eye(5,3) w = np.eye(5, 3)
w[2] = np.ones((3)) w[2] = np.ones((3))
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
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**
...@@ -253,18 +253,18 @@ Note that we need to iterate over the indices of ``y`` and not over the elements ...@@ -253,18 +253,18 @@ Note that we need to iterate over the indices of ``y`` and not over the elements
trng = T.shared_randomstreams.RandomStreams(1234) trng = T.shared_randomstreams.RandomStreams(1234)
d=trng.binomial(size=W[1].shape) d=trng.binomial(size=W[1].shape)
results, updates = theano.scan(lambda v:T.tanh(T.dot(v,W)+b_sym)*d, sequences=X) results, updates = theano.scan(lambda v:T.tanh(T.dot(v, W)+b_sym)*d, sequences=X)
compute_with_bnoise = theano.function(inputs = [X, W, b_sym], outputs=[results], \ compute_with_bnoise = theano.function(inputs = [X, W, b_sym], outputs=[results], \
updates=updates, allow_input_downcast = True) updates=updates, allow_input_downcast = True)
x = np.eye(10,2) x = np.eye(10, 2)
w = np.ones((2,2)) w = np.ones((2, 2))
b = np.ones((2)) b = np.ones((2))
print compute_with_bnoise(x, w, b) print compute_with_bnoise(x, w, b)
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 .. code-block:: python
...@@ -290,7 +290,7 @@ Note that if you want to use a random variable ``d`` that will not be updated th ...@@ -290,7 +290,7 @@ 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.] #[ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81.]
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论