前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Theano学习笔记(一)——scan函数

Theano学习笔记(一)——scan函数

作者头像
深度学习思考者
发布2018-01-02 17:35:36
1.1K0
发布2018-01-02 17:35:36
举报

一 scan的介绍

       函数scan是Theano中迭代的一般形式,所以可以用于类似循环(looping)的场景。Reduction和map都是scan的特殊形式,即将某函数依次作用一个序列的每个元素上。但scan在计算的时候,可以访问以前n步的输出结果,所以比较适合RNN网络。        看起来scan完全可以用for… loop来代替,然而scan有其自身的优点:        ① Number of iterations to be part of the symbolic graph.        ② Minimizes GPU transfers (if GPU is involved).        ③ Computes gradients through sequential steps.     ④Slightly faster than using a for loop in Python with a compiled Theano function.        ⑤ Can lower the overall memory usage by detecting the actual amount of memory needed.

二 scan的一般形式

theano.scan()

代码语言:javascript
复制
results, updates = theano.scan(fn = lambda y, p, x_tm2, x_tm1,A: y+p+x_tm2+xtm1+A,
sequences=[Y, P[::-1]], 
outputs_info=[dict(initial=X, taps=[-2, -1])]), 
non_sequences=A)

其中的参数

fn:函数可以在外部定义好,也可以在内部再定义。在内部在定义的fn一般用lambda来定义需要用到的参数,在外部就def好的函数,fn直接函数名即可。        构造出描述一步迭代的输出的变量。同样还需要看成是 theano 的输入变量,表示输入序列的所有分片和过去的输出值,以及所有赋给 scan 的 non_sequences 的这些其他参数。

sequences:scan进行迭代的变量;序列是 Theano 变量或者字典的列表,告诉程序 scan 必须迭代的序列,scan会在T.arange()生成的list上遍历。        任何在 sequence 列表的 Theano 变量都会自动封装成一个字典,其 taps 被设置为 [0]

outputs_info:初始化fn的输出变量,描述了需要用到的初始化值,以及是否需要用到前几次迭代输出的结果,dict(initial=X, taps=[-2, -1])表示使用序列x作为初始化值,taps表示会用到前一次和前两次输出的结果。如果当前迭代输出为x(t),则计算中使用了(x(t-1)和x(t-2)。

non_sequences:fn函数用到的其他变量,迭代过程中不可改变(unchange),即A是一个固定的输入,每次迭代加的A都是相同的。如果Y是一个向量,A就是一个常数。总之,A比Y少一个维度。

n_steps:fn的迭代次数。

三 具体实例

例子1:

代码语言:javascript
复制
# A的K次方
k = T . iscalar('k')
A = T . vector( 'A')
outputs, updates = theano.scan(lambda result, A : result * A,
             non_sequences = A, outputs_info=T.ones_like(A), n_steps = k)
result = outputs [-1]
fn_Ak = theano . function([A,k ], result, updates=updates )
print fn_Ak( range(10 ), 2 )

Result:
[  0.   1.   4.   9.  16.  25.  36.  49.  64.  81.]

例子2:tanh(wx+b)

代码语言:javascript
复制
import theano
import theano.tensor as T
import numpy as np

# defining the tensor variables
X = T.matrix("X")
W = T.matrix("W")
b_sym = T.vector("b_sym")

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])

# test values
x = np.eye(2, dtype=theano.config.floatX)
w = np.ones((2, 2), dtype=theano.config.floatX)
b = np.ones((2), dtype=theano.config.floatX)
b[1] = 2

print(compute_elementwise(x, w, b)[0])

# comparison with numpy
print(np.tanh(x.dot(w) + b))

[[ 0.96402758  0.99505475]
 [ 0.96402758  0.99505475]]
[[ 0.96402758  0.99505475]
 [ 0.96402758  0.99505475]]

参考文献

  1. http://deeplearning.net/software/theano/library/scan.html#lib-scan-shared-variables
  2. http://deeplearning.net/software/theano/tutorial/loop.html
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一 scan的介绍
  • 二 scan的一般形式
  • 三 具体实例
  • 参考文献
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档