FakeBrain - 3 months ago 27

Python Question

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

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

the outputs:

`# from MSE function`

0.0623486922837

0.0652202301174

0.151698460419

0.187325204482

# non theano output

0.0467615192128

0.0489151725881

0.113773845314

0.140493903362

What am I over looking here?

Answer

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

.