Python - Theano scan() 函数 [英] Python - Theano scan() function

查看:27
本文介绍了Python - Theano scan() 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法完全理解 theano.scan() 的行为.

I cannot fully understand the behaviour of 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)

上面的代码片段打印了以下序列,这是完全合理的:

The above snippet prints the following sequence, which is perfectly reasonable:

[ 3  3  5  5  7  7  9  9 11 11]

但是,如果我将抽头索引从 -2 切换到 -1,即

However if I switch the tap index from -2 to -1, i.e.

outputs_info=[{'initial':x0, 'taps':[-1]}]

结果变成:

[[ 3  3]
 [ 5  5]
 [ 7  7]
 [ 9  9]
 [11 11]
 [13 13]
 [15 15]
 [17 17]
 [19 19]
 [21 21]]

而不是我认为合理的(只需取向量的最后一个值并添加 2):

instead of what would seem reasonable to me (just take the last value of the vector and add 2):

[ 3  5  7  9 11 13 15 17 19 21]

任何帮助将不胜感激.

谢谢!

推荐答案

当您使用 taps=[-1] 时,假设输出信息中的信息按原样使用.这意味着将使用向量和 non_sequence 作为输入调用 addf 函数.如果将 x0 转换为标量,它将按预期工作:

When you use taps=[-1], scan suppose that the information in the output info is used as is. That mean the addf function will be called with a vector and the non_sequence as inputs. If you convert x0 to a scalar, it will work as you expect:

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)

这给出了这个输出:

TensorType(int32, scalar)
TensorType(int32, scalar)
[ 3  5  7  9 11 13 15 17 19 21]

在你的情况下,当它做 addf(vector,scalar) 时,它会广播 elemwise 值.

In your case as it do addf(vector,scalar), it broadcast the elemwise value.

换一种方式解释,如果 taps 为 [-1],x0 将按原样"传递给内部函数.如果 taps 包含其他任何内容,则传递给内部函数的内容将比 x0 小 1 维,因为 x0 必须提供许多初始步长值(-2 和 -1).

Explained in another way, if taps is [-1], x0 will be passed "as is" to the inner function. 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).

这篇关于Python - Theano scan() 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆