是否可以将 scan 的迭代索引传递给我从 scan 调用的函数? 例如 -
def step(x,i):
# i is the current scan index. Use it for some conditional expressions
for i in range(0,10):
step(x,i)
我想用 theano 做一些类似的事情。有什么线索吗?
谢谢
请您参考如下方法:
这显示在 theano 教程中,例如 here .
函数的第一个参数自动取自 scan 的 sequences 参数(如果提供的话)。 例如,假设您要向向量 x 的每个元素添加其对应的索引。 你可以像这样用 theano.scan 来做:
import theano
import theano.tensor as T
x = T.dvector('x')
def step(idx, array):
return array[idx] + idx
results, updates = theano.scan(fn=step,
sequences=T.arange(x.shape[0]),
non_sequences=[x])
f = theano.function([x], results)
f([1., 0., 2.13])
# array([ 1. , 1. , 4.13])
所以基本上:注意你给 scan 的函数参数的顺序,因为它们是按特定顺序传递的。 你可以在相关documentation page of scan中看到具体是什么顺序.
