2016-08-20 77 views
0

我開始玩弄theano,所以我試着計算一個簡單的函數並測試輸出,但是當我測試theano編譯版本與非theano版本時,輸出有一點不同的....計算輸出的差異theano,nonano

代碼:

import numpy as np 
import theano.tensor as T 
from theano import function 

np.random.seed(1) 
S = np.random.rand(4,3) 
Q = np.random.rand(4,3) 

def MSE(a, b): 
    n = min(a.shape[0], b.shape[0]) 
    fhat = T.dvector('fhat') 
    y = T.dvector('y') 
    mse = ((y - fhat)**2).sum()/n 
    mse_f = function([y, fhat], mse) 
    return mse_f(a,b) 

for row in range(S.shape[0]): 
    print(MSE(S[row], Q[row])) 

for i in range(S.shape[0]): 
    print(((S[i] - Q[i])**2).sum()/S.shape[0]) 

輸出:

# from MSE function 
0.0623486922837 
0.0652202301174 
0.151698460419 
0.187325204482 

# non theano output 
0.0467615192128 
0.0489151725881 
0.113773845314 
0.140493903362 

什麼我找過這裏?

回答

0

在表達這一說法

print(((S[i] - Q[i])**2).sum()/S.shape[0]) 

你應該S.shape[1],不S.shape[0]分。

您使用S = np.random.rand(4,3)創建了S,這意味着S具有形狀(4,3)。即,S.shape(4, 3)S中每行的長度爲S.shape[1]

+0

哦......這是正確的。儘管如此,現在我對這裏實際發生的事情有些困惑。 'n'應該等於4,這與'S.shape [0]'...相同...... OH NO,等等我看,在那一點它變成了一個行向量,所以形狀只是長度行。 – UberStuper

+0

我對答案增加了一條評論。這有幫助嗎? –

+0

yup!我知道了。謝謝! – UberStuper