计算输出的差异,theano,non thean

2024-10-16 17:29:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我开始玩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

我在看什么?你知道吗


Tags: fromimport版本asnpfunctionrandomtheano
1条回答
网友
1楼 · 发布于 2024-10-16 17:29:03

在这个语句的表达式中

    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]。你知道吗

相关问题 更多 >