mirror of
https://github.com/apachecn/ailearning.git
synced 2026-05-11 11:07:31 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
import theano, time
|
||||
import theano.tensor as T
|
||||
import numpy as np
|
||||
@@ -12,7 +12,7 @@ def floatX(X):
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Using gpu device 1: Tesla C2075 (CNMeM is disabled)
|
||||
|
||||
```
|
||||
@@ -35,7 +35,7 @@ Using gpu device 1: Tesla C2075 (CNMeM is disabled)
|
||||
|
||||
函数的用法如下:
|
||||
|
||||
```
|
||||
```py
|
||||
theano.scan(fn,
|
||||
sequences=None,
|
||||
outputs_info=None,
|
||||
@@ -76,14 +76,14 @@ theano.scan(fn,
|
||||
|
||||
这里实现一个简单的 `map` 操作,将向量 $\mathbf x$ 中的所有元素变成原来的两倍:
|
||||
|
||||
```
|
||||
```py
|
||||
map(lambda t: t * 2, x)
|
||||
|
||||
```
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = T.vector()
|
||||
|
||||
results, _ = theano.scan(fn = lambda t: t * 2,
|
||||
@@ -94,7 +94,7 @@ print x_double_scan(range(10))
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
[ 0\. 2\. 4\. 6\. 8\. 10\. 12\. 14\. 16\. 18.]
|
||||
|
||||
```
|
||||
@@ -107,7 +107,7 @@ print x_double_scan(range(10))
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
result, _ = theano.map(fn = lambda t: t * 2,
|
||||
sequences = x)
|
||||
x_double_map = theano.function([x], result)
|
||||
@@ -116,7 +116,7 @@ print x_double_map(range(10))
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
[ 0\. 2\. 4\. 6\. 8\. 10\. 12\. 14\. 16\. 18.]
|
||||
|
||||
```
|
||||
@@ -125,14 +125,14 @@ print x_double_map(range(10))
|
||||
|
||||
这里一个简单的 `reduce` 操作,求和:
|
||||
|
||||
```
|
||||
```py
|
||||
reduce(lambda a, b: a + b, x)
|
||||
|
||||
```
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
result, _ = theano.scan(fn = lambda t, v: t + v,
|
||||
sequences = x,
|
||||
outputs_info = floatX(0.))
|
||||
@@ -145,7 +145,7 @@ print x_sum_scan(range(10))
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
45.0
|
||||
|
||||
```
|
||||
@@ -154,7 +154,7 @@ print x_sum_scan(range(10))
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
result, _ = theano.reduce(fn = lambda t, v: t + v,
|
||||
sequences = x,
|
||||
outputs_info = 0.)
|
||||
@@ -166,7 +166,7 @@ print x_sum_reduce(range(10))
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
45.0
|
||||
|
||||
```
|
||||
@@ -198,7 +198,7 @@ print x_sum_reduce(range(10))
|
||||
|
||||
第二部分则是更新规则的字典,告诉我们如何对 `scan` 中使用到的一些共享的变量进行更新:
|
||||
|
||||
```
|
||||
```py
|
||||
return [y1_t, y2_t], {x:x+1}
|
||||
|
||||
```
|
||||
@@ -211,7 +211,7 @@ return [y1_t, y2_t], {x:x+1}
|
||||
|
||||
例如,在我们的第一个例子中
|
||||
|
||||
```
|
||||
```py
|
||||
theano.scan(fn = lambda t: t * 2,
|
||||
sequences = x)
|
||||
|
||||
@@ -221,7 +221,7 @@ theano.scan(fn = lambda t: t * 2,
|
||||
|
||||
再如,在我们的第二个例子中:
|
||||
|
||||
```
|
||||
```py
|
||||
theano.scan(fn = lambda t, v: t + v,
|
||||
sequences = x,
|
||||
outputs_info = floatX(0.))
|
||||
@@ -236,7 +236,7 @@ theano.scan(fn = lambda t, v: t + v,
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
# 变量 x
|
||||
x = T.scalar("x")
|
||||
|
||||
@@ -262,7 +262,7 @@ print polynomial(floatX(10),
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
2301.0
|
||||
|
||||
```
|
||||
@@ -280,7 +280,7 @@ print polynomial(floatX(10),
|
||||
|
||||
传入 `fn` 的参数也会按照 `taps` 中的顺序来排列,我们考虑下面这个例子:
|
||||
|
||||
```
|
||||
```py
|
||||
scan(fn, sequences = [ dict(input= Sequence1, taps = [-3,2,-1])
|
||||
, Sequence2
|
||||
, dict(input = Sequence3, taps = 3) ]
|
||||
@@ -293,7 +293,7 @@ scan(fn, sequences = [ dict(input= Sequence1, taps = [-3,2,-1])
|
||||
|
||||
首先是 `Sequence1` 的 `[-3, 2, -1]` 被传入,然后 `Sequence2` 不是 `dict`, 所以传入默认值 `[0]`,`Sequence3` 传入的参数是 `3`,所以 `fn` 在第 `t` 步接受的前几个参数是:
|
||||
|
||||
```
|
||||
```py
|
||||
Sequence1[t-3]
|
||||
Sequence1[t+2]
|
||||
Sequence1[t-1]
|
||||
@@ -303,7 +303,7 @@ Sequence3[t+3]
|
||||
|
||||
然后 `Output1` 传入的是 `[-3, -5]`(**传入的初始值的形状应为 `shape (5,)+`**),`Output2` 不作为参数传入,`Output3` 传入的是 `[-1]`,所以接下的参数是:
|
||||
|
||||
```
|
||||
```py
|
||||
Output1[t-3]
|
||||
Output1[t-5]
|
||||
Output3[t-1]
|
||||
@@ -313,7 +313,7 @@ Argument2
|
||||
|
||||
总的说来上面的例子中,`fn` 函数按照以下顺序最多接受这样 10 个参数:
|
||||
|
||||
```
|
||||
```py
|
||||
Sequence1[t-3]
|
||||
Sequence1[t+2]
|
||||
Sequence1[t-1]
|
||||
@@ -330,7 +330,7 @@ Argument2
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
X = T.matrix("X")
|
||||
Y = T.vector("y")
|
||||
|
||||
@@ -353,7 +353,7 @@ Y_seq = theano.function(inputs = [X, Y, W_1, W_2, W_3],
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
# 测试
|
||||
t = 1001
|
||||
x_dim = 10
|
||||
@@ -386,7 +386,7 @@ print "the max difference of the first 10 results is", np.max(np.abs(y_res_thean
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
theano running time 0.0537 s
|
||||
numpy running time 0.0197 s
|
||||
the max difference of the first 10 results is 1.25780650354e-06
|
||||
@@ -397,7 +397,7 @@ the max difference of the first 10 results is 1.25780650354e-06
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
# 测试
|
||||
t = 1001
|
||||
x_dim = 100
|
||||
@@ -430,7 +430,7 @@ print "the max difference of the first 10 results is", np.max(np.abs(y_res_thean
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
theano running time 0.0754 s
|
||||
numpy running time 0.1334 s
|
||||
the max difference of the first 10 results is 0.000656997077348
|
||||
@@ -441,14 +441,14 @@ the max difference of the first 10 results is 0.000656997077348
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
for i in xrange(20):
|
||||
print "iter {:03d}, max diff:{:.6f}".format(i + 1,
|
||||
np.max(np.abs(y_res_numpy[i + 1,:] - y_res_theano[i,:])))
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
iter 001, max diff:0.000002
|
||||
iter 002, max diff:0.000005
|
||||
iter 003, max diff:0.000007
|
||||
@@ -478,7 +478,7 @@ iter 020, max diff:0.123678
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
A = T.matrix("A")
|
||||
k = T.iscalar("k")
|
||||
|
||||
@@ -504,7 +504,7 @@ print a_k
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
[[ 107616\. -152192.]
|
||||
[ -76096\. 107616.]]
|
||||
[[ 107616\. -152192.]
|
||||
@@ -518,7 +518,7 @@ print a_k
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
n = theano.shared(floatX(0))
|
||||
k = T.iscalar("k")
|
||||
|
||||
@@ -539,7 +539,7 @@ print n.get_value()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
0.0
|
||||
10.0
|
||||
20.0
|
||||
@@ -558,7 +558,7 @@ print n.get_value()
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
max_value = T.scalar()
|
||||
|
||||
results, _ = theano.scan(fn = lambda v_pre, max_v: (v_pre * 2, theano.scan_module.until(v_pre * 2 > max_v)),
|
||||
@@ -575,7 +575,7 @@ print power_of_2(40)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
[ 2\. 4\. 8\. 16\. 32.]
|
||||
|
||||
```
|
||||
Reference in New Issue
Block a user