|
136 | 136 | "x = T.vector()\n",
|
137 | 137 | "\n",
|
138 | 138 | "results, _ = theano.scan(fn = lambda t: t * 2,\n",
|
139 |
| - " sequences = x)\n", |
| 139 | + " sequences = x)\n", |
140 | 140 | "x_double_scan = theano.function([x], results)\n",
|
141 | 141 | "\n",
|
142 | 142 | "print x_double_scan(range(10))"
|
|
170 | 170 | ],
|
171 | 171 | "source": [
|
172 | 172 | "result, _ = theano.map(fn = lambda t: t * 2,\n",
|
173 |
| - " sequences = x)\n", |
| 173 | + " sequences = x)\n", |
174 | 174 | "x_double_map = theano.function([x], result)\n",
|
175 | 175 | "\n",
|
176 | 176 | "print x_double_map(range(10))"
|
|
211 | 211 | ],
|
212 | 212 | "source": [
|
213 | 213 | "result, _ = theano.scan(fn = lambda t, v: t + v,\n",
|
214 |
| - " sequences = x,\n", |
215 |
| - " outputs_info = floatX(0.))\n", |
| 214 | + " sequences = x,\n", |
| 215 | + " outputs_info = floatX(0.))\n", |
216 | 216 | "\n",
|
217 | 217 | "# 因为每一步的输出值都会被记录到最后的 result 中,所以最后的和是 result 的最后一个元素。\n",
|
218 | 218 | "x_sum_scan = theano.function([x], result[-1])\n",
|
|
245 | 245 | ],
|
246 | 246 | "source": [
|
247 | 247 | "result, _ = theano.reduce(fn = lambda t, v: t + v,\n",
|
248 |
| - " sequences = x,\n", |
249 |
| - " outputs_info = 0.)\n", |
| 248 | + " sequences = x,\n", |
| 249 | + " outputs_info = 0.)\n", |
250 | 250 | "\n",
|
251 | 251 | "x_sum_reduce = theano.function([x], result)\n",
|
252 | 252 | "\n",
|
|
384 | 384 | "\n",
|
385 | 385 | "# a 对应的是 A, n 对应 N,v 对应 x\n",
|
386 | 386 | "components, _ = theano.scan(fn = lambda a, n, v: a * (v ** n),\n",
|
387 |
| - " sequences = [A, N],\n", |
388 |
| - " non_sequences = x)\n", |
| 387 | + " sequences = [A, N],\n", |
| 388 | + " non_sequences = x)\n", |
389 | 389 | "\n",
|
390 | 390 | "result = components.sum()\n",
|
391 | 391 | "\n",
|
|
436 | 436 | "Sequence3[t+3]\n",
|
437 | 437 | "```\n",
|
438 | 438 | "\n",
|
439 |
| - "然后 `Output1` 传入的是 `[-5,-3,-1]`(**传入的初始值的形状应为 `shape (5,)+`**),`Output2` 不作为参数传入,`Output3` 传入的是 `[-1]`,所以接下的参数是:\n", |
| 439 | + "然后 `Output1` 传入的是 `[-3, -5]`(**传入的初始值的形状应为 `shape (5,)+`**),`Output2` 不作为参数传入,`Output3` 传入的是 `[-1]`,所以接下的参数是:\n", |
440 | 440 | "```\n",
|
441 | 441 | "Output1[t-3]\n",
|
442 | 442 | "Output1[t-5]\n",
|
|
479 | 479 | "\n",
|
480 | 480 | "# W_yy 和 W_xy 作为不变的参数可以直接使用\n",
|
481 | 481 | "results, _ = theano.scan(fn = lambda x, x_pre, y: T.tanh(T.dot(W_1, y) + T.dot(W_2, x) + T.dot(W_3, x_pre)), \n",
|
482 |
| - " # 0 对应 x,-1 对应 x_pre\n", |
483 |
| - " sequences = dict(input=X, taps=[0, -1]), \n", |
484 |
| - " outputs_info = Y)\n", |
| 482 | + " # 0 对应 x,-1 对应 x_pre\n", |
| 483 | + " sequences = dict(input=X, taps=[0, -1]), \n", |
| 484 | + " outputs_info = Y)\n", |
485 | 485 | "\n",
|
486 | 486 | "Y_seq = theano.function(inputs = [X, Y, W_1, W_2, W_3], \n",
|
487 | 487 | " outputs = results)"
|
|
683 | 683 | "k = T.iscalar(\"k\")\n",
|
684 | 684 | "\n",
|
685 | 685 | "results, _ = theano.scan(fn = lambda P, A: P.dot(A),\n",
|
686 |
| - " # 初始值设为单位矩阵\n", |
687 |
| - " outputs_info = T.eye(A.shape[0]),\n", |
688 |
| - " # 乘 k 次\n", |
689 |
| - " non_sequences = A,\n", |
690 |
| - " n_steps = k)\n", |
| 686 | + " # 初始值设为单位矩阵\n", |
| 687 | + " outputs_info = T.eye(A.shape[0]),\n", |
| 688 | + " # 乘 k 次\n", |
| 689 | + " non_sequences = A,\n", |
| 690 | + " n_steps = k)\n", |
691 | 691 | "\n",
|
692 | 692 | "A_k = theano.function(inputs = [A, k], outputs = results[-1])\n",
|
693 | 693 | "\n",
|
|
740 | 740 | "\n",
|
741 | 741 | "# 这里 lambda 的返回值是一个 dict,因此这个值会被传入 updates 中\n",
|
742 | 742 | "_, updates = theano.scan(fn = lambda n: {n:n+1},\n",
|
743 |
| - " non_sequences = n,\n", |
744 |
| - " n_steps = k)\n", |
| 743 | + " non_sequences = n,\n", |
| 744 | + " n_steps = k)\n", |
745 | 745 | "\n",
|
746 | 746 | "counter = theano.function(inputs = [k], \n",
|
747 | 747 | " outputs = [],\n",
|
|
0 commit comments