FakeBrain FakeBrain - 1 year ago 117
Python Question

differences in computation outputs, theano, non theano

I'm starting to play around with theano, and so I tried computing a simple function and testing the output, however when I test a theano compiled version versus a non theano version the outputs are a bit different....

The code:

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

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

the outputs:

# from MSE function

# non theano output

What am I over looking here?

Answer Source

In the expression in this statement

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

you should divide by S.shape[1], not S.shape[0].

You created S using S = np.random.rand(4,3), which means S has shape (4, 3). That is, S.shape is (4, 3). The length of each row in S is S.shape[1].

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download