计算输出的差异,theano,非 theano
differences in computation outputs, theano, non theano
我开始使用 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
我在看什么?
在此语句中的表达式中
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]
.
我开始使用 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
我在看什么?
在此语句中的表达式中
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]
.