深度学习Theano中scan的使用方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/programmer_wei/article/details/51002379

版权声明:本文为原创文章:http://blog.csdn.net/programmer_wei/article/details/51002379

深度学习python库Theano中的函数scan是一种迭代形式,所以可以用于类似循环(looping)的场景。需要注意的是scan在计算的时候,可以访问以前n步的输出结果,所以比较适合RNN网络。


首先我们来看地一个例子1:



import theano
import theano.tensor as T

k = T.iscalar("k")
A = T.vector("A")

# Symbolic description of the result
result, updates = theano.scan(fn=lambda prior_result, A: prior_result * A,
                              outputs_info=T.ones_like(A),
                              non_sequences=A,
                              n_steps=k)

# We only care about A**k, but scan has provided us with A**1 through A**k.
# Discard the values that we don't care about. Scan is smart enough to
# notice this and not waste memory saving them.
final_result = result[-1]

# compiled function that returns A**k
power = theano.function(inputs=[A,k], outputs=final_result, updates=updates)

print(power(range(10),2))
print(power(range(10),4))

输出:

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

[ 0.00000000e+00 1.00000000e+00 1.60000000e+01 8.10000000e+01
2.56000000e+02 6.25000000e+02 1.29600000e+03 2.40100000e+03
4.09600000e+03 6.56100000e+03]

上述算法等同于:

result = 1
for i in range(k):
    result = result * A

在上述例子1中:
fn:定义了一个lamuda函数,实现累乘的功能 Ak
outputs_info:是scan输出在起始的状态。为result设置的初值,由outputs_info指定。这里为1,shape大小和A保持一致,这样才能实现累乘的目的。
non_sequences:描述了非序列的输入,即A是一个固定的输入,每次迭代加的A都是相同的。
n_steps:表示迭代的次数。


接下来我们再看第二个例子:

import numpy

coefficients = theano.tensor.vector("coefficients")
x = T.scalar("x")

max_coefficients_supported = 10000

# Generate the components of the polynomial
components, updates = theano.scan(fn=lambda coefficient, power, free_variable: coefficient * (free_variable ** power),
                                  outputs_info=None,
                                  sequences=[coefficients, theano.tensor.arange(max_coefficients_supported)],
                                  non_sequences=x)
# Sum them up
polynomial = components.sum()

# Compile a function
calculate_polynomial = theano.function(inputs=[coefficients, x], outputs=polynomial)

# Test
test_coefficients = numpy.asarray([1, 0, 2], dtype=numpy.float32)
test_value = 3
print(calculate_polynomial(test_coefficients, test_value))
print(1.0 * (3 ** 0) + 0.0 * (3 ** 1) + 2.0 * (3 ** 2))

输出:

19.0
19.0

这里例子2是实现了一个计算

f(x)=a0+a1x+a2x2++an1xn1+anxn
的功能,在上述例子2中:
fn:使用lamuda定义了每一项的计算过程,
coefficientfreevariablepower

outputs_info:因为没有累乘,所以这里是none。
sequences:就是需要迭代的序列,会变化。coefficients和T.arange(max_coefficients_supported)是在迭代中变化的(这里的变化指的是每次迭代使用的是向量中的每一项是不同的。)
non_sequences:x的值是不变的,所以这里是x。

这里需要注意:官方文档中有说明

The general order of function parameters to fn is:
sequences (if any), prior result(s) (if needed), non-sequences (if any)

所以说fn中参数的次序是有讲究的,不可以随便更换参数顺序!!!!


参考文献:
http://deeplearning.net/software/theano/library/scan.html
http://blog.csdn.net/lanchunhui/article/details/50256423
http://www.bubuko.com/infodetail-1232202.html

猜你喜欢

转载自blog.csdn.net/programmer_wei/article/details/51002379