2014-11-03 72 views
9

我不能完全理解theano.scan()的行爲。Python - Theano掃描()函數

下面是一個例子:

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) 

上面片斷輸出按照以下順序,它是完全合理的:

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

然而如果切換抽頭索引從-2至-1,即

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):

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

任何幫助將非常感激。

謝謝!

回答

10

當您使用taps = [ - 1]時,掃描假設輸出信息中的信息按原樣使用。這意味着addf函數將使用向量和非序列作爲輸入來調用。如果轉換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) 

這給這個輸出:

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

在你的情況,因爲它做ADDF(向量,標量),它廣播elemwise值。

另一種解釋是,如果tap是[-1],x0將按原樣傳遞給內部函數。如果水龍頭包含其他任何東西,傳遞給內部函數的內容將比X0小1,因爲x0必須提供許多初始步驟值(-2和-1)。