我无法完全理解theano.scan()的行为.
这是一个例子:
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)
Run Code Online (Sandbox Code Playgroud)
上面的代码段打印出以下序列,这是完全合理的:
[ 3 3 5 5 7 7 9 9 11 11]
Run Code Online (Sandbox Code Playgroud)
但是如果我将tap索引从-2切换到-1,即
outputs_info=[{'initial':x0, 'taps':[-1]}]
Run Code Online (Sandbox Code Playgroud)
结果变成:
[[ 3 3]
[ 5 5]
[ 7 7]
[ 9 9]
[11 11]
[13 13]
[15 15]
[17 17]
[19 19]
[21 21]]
Run Code Online (Sandbox Code Playgroud)
而不是对我来说似乎合理的东西(只需取向量的最后一个值并加2):
[ 3 5 7 9 11 13 15 17 19 21]
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激.
谢谢!
nou*_*uiz 10
当您使用taps = [ - 1]时,扫描假设输出信息中的信息按原样使用.这意味着将使用向量调用addf函数,并将non_sequence作为输入调用.如果将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)
Run Code Online (Sandbox Code Playgroud)
这给出了这个输出:
TensorType(int32, scalar)
TensorType(int32, scalar)
[ 3 5 7 9 11 13 15 17 19 21]
Run Code Online (Sandbox Code Playgroud)
在你的情况下,因为它做addf(矢量,标量),它广播elemwise值.
用另一种方式解释,如果抽头是[-1],x0将"按原样"传递给内部函数.如果taps包含其他任何东西,传递给内部函数的内容将比x0小1维,因为x0必须提供许多初始步骤值(-2和-1).
| 归档时间: |
|
| 查看次数: |
5663 次 |
| 最近记录: |