[英]Python - Theano scan() function
I cannot fully understand the behaviour of theano.scan(). 我无法完全理解theano.scan()的行为。
Here's an example: 这是一个例子:
import numpy as np
import theano
import theano.tensor as T
def addf(a1,a2):
return a1+a2
i = T.iscalar('i')
x0 = T.ivector('x0')
step= T.iscalar('step')
results, updates = theano.scan(fn=addf,
outputs_info=[{'initial':x0, 'taps':[-2]}],
non_sequences=step,
n_steps=i)
f=theano.function([x0,i,step],results)
print f([1,1],10,2)
The above snippet prints the following sequence, which is perfectly reasonable: 上面的代码段打印出以下序列,这是完全合理的:
[ 3 3 5 5 7 7 9 9 11 11]
However if I switch the tap index from -2 to -1, ie 但是,如果我将点击索引从-2切换到-1,即
outputs_info=[{'initial':x0, 'taps':[-1]}]
The result becomes: 结果变成:
[[ 3 3]
[ 5 5]
[ 7 7]
[ 9 9]
[11 11]
[13 13]
[15 15]
[17 17]
[19 19]
[21 21]]
instead of what would seem reasonable to me (just take the last value of the vector and add 2): 而不是对我来说似乎合理的东西(只需取向量的最后一个值并加2):
[ 3 5 7 9 11 13 15 17 19 21]
Any help would be much appreciated. 任何帮助将非常感激。
Thanks! 谢谢!
When you use taps=[-1], scan suppose that the information in the output info is used as is. 当您使用taps = [ - 1]时,扫描假设输出信息中的信息按原样使用。 That mean the addf function will be called with a vector and the non_sequence as inputs. 这意味着将使用向量调用addf函数,并将non_sequence作为输入调用。 If you convert x0 to a scalar, it will work as you expect: 如果将x0转换为标量,它将按预期工作:
import numpy as np
import theano
import theano.tensor as T
def addf(a1,a2):
print a1.type
print a2.type
return a1+a2
i = T.iscalar('i')
x0 = T.iscalar('x0')
step= T.iscalar('step')
results, updates = theano.scan(fn=addf,
outputs_info=[{'initial':x0, 'taps':[-1]}],
non_sequences=step,
n_steps=i)
f=theano.function([x0,i,step],results)
print f(1,10,2)
This give this output: 这给出了这个输出:
TensorType(int32, scalar)
TensorType(int32, scalar)
[ 3 5 7 9 11 13 15 17 19 21]
In your case as it do addf(vector,scalar), it broadcast the elemwise value. 在你的情况下,因为它做addf(矢量,标量),它广播elemwise值。
Explained in another way, if taps is [-1], x0 will be passed "as is" to the inner function. 用另一种方式解释,如果抽头是[-1],x0将“按原样”传递给内部函数。 If taps contain anything else, what is passed to the inner function will have 1 dimension less then x0, as x0 must provide many initial steps value (-2 and -1). 如果taps包含其他任何东西,传递给内部函数的内容将比x0小1维,因为x0必须提供许多初始步骤值(-2和-1)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.