我开始玩theano,所以我试着计算一个简单的函数并测试输出,但是当我测试一个theano编译版本和一个非theano版本时,输出有点不同。。。。你知道吗
代码:
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
我在看什么?你知道吗
在这个语句的表达式中
你应该除以
S.shape[1]
,而不是S.shape[0]
。你知道吗您使用
S = np.random.rand(4,3)
创建了S
,这意味着S
具有形状(4,3)。也就是说,S.shape
就是(4, 3)
。S
中每行的长度是S.shape[1]
。你知道吗相关问题 更多 >
编程相关推荐