Feras Almasri
2017-10-16 17:08:41 UTC
I'm trying to implement a new node in Theano, and in order in test every
step I'm building everything aside and testing output results. One of these
steps is to test sum function in two dimensions. the output is good for the
first dimension but it get strange result when I apply the second
dimension. I compared the output with Numpy. Please see the code and the
results below.
arr = np.array([[[.1,.2],[.3,.4]],[[.2,.5],[.6,.7]],[[.2,.6],[.7,.4]]]).astype(np.float32)
x = T.ftensor3('x')
y = T.max(x)
z2 = T.sum(T.exp(x), axis=(2,1))
tester1 = T.exp(x)
tester2 = T.sum(tester1, axis=2)
tester3 = T.sum(tester2, axis=1)
s1 = theano.function([x], tester1)
s2 = theano.function([x], tester2)
s3 = theano.function([x], tester3)
final = theano.function([x], z2)
firstValue = s1(arr)print firstValueprint "\n"
secValue = s2(arr)print secValueprint "\n"
thirdValue = s3(arr)print thirdValueprint '\n'print final(arr)print "-----------------\n"print firstValue[0,:,:]print "\n"# print firstValue[0,:,:].sum()
firstSum = np.sum(firstValue, axis=2)print firstSumprint np.sum(firstSum, axis=1)
and here is the output.
[[[ 1.10517097 1.22140276]
[ 1.34985888 1.49182475]]
[[ 1.22140276 1.64872122]
[ 1.82211888 2.01375294]]
[[ 1.22140276 1.82211888]
[ 2.01375294 1.49182475]]]
[[ 2.32657385 2.84168363]
[ 2.87012386 3.8358717 ]
[ 3.04352164 3.50557756]]
[ 3.67643261 4.69224262 5.05727482]
[ 3.67643261 4.69224262 5.05727482]-----------------
[[ 1.10517097 1.22140276]
[ 1.34985888 1.49182475]]
[[ 2.32657385 2.84168363]
[ 2.87012386 3.8358717 ]
[ 3.04352164 3.50557756]][ 5.16825771 6.70599556 6.54909897]
AS you can see first dimension with axis=2 get the results summed properly
but then when I sum over axis one the results doesn't mach any of the
numbers. I tested summing twice by 2 and then by 1 and tested the function
by giving the two axis (2,1).
Update. When the sum function applied by using keepdims=True sequentially
the results summed up good but when using One function giving the two
axis(1,2) even with keepdims=True the results are wrong.
step I'm building everything aside and testing output results. One of these
steps is to test sum function in two dimensions. the output is good for the
first dimension but it get strange result when I apply the second
dimension. I compared the output with Numpy. Please see the code and the
results below.
arr = np.array([[[.1,.2],[.3,.4]],[[.2,.5],[.6,.7]],[[.2,.6],[.7,.4]]]).astype(np.float32)
x = T.ftensor3('x')
y = T.max(x)
z2 = T.sum(T.exp(x), axis=(2,1))
tester1 = T.exp(x)
tester2 = T.sum(tester1, axis=2)
tester3 = T.sum(tester2, axis=1)
s1 = theano.function([x], tester1)
s2 = theano.function([x], tester2)
s3 = theano.function([x], tester3)
final = theano.function([x], z2)
firstValue = s1(arr)print firstValueprint "\n"
secValue = s2(arr)print secValueprint "\n"
thirdValue = s3(arr)print thirdValueprint '\n'print final(arr)print "-----------------\n"print firstValue[0,:,:]print "\n"# print firstValue[0,:,:].sum()
firstSum = np.sum(firstValue, axis=2)print firstSumprint np.sum(firstSum, axis=1)
and here is the output.
[[[ 1.10517097 1.22140276]
[ 1.34985888 1.49182475]]
[[ 1.22140276 1.64872122]
[ 1.82211888 2.01375294]]
[[ 1.22140276 1.82211888]
[ 2.01375294 1.49182475]]]
[[ 2.32657385 2.84168363]
[ 2.87012386 3.8358717 ]
[ 3.04352164 3.50557756]]
[ 3.67643261 4.69224262 5.05727482]
[ 3.67643261 4.69224262 5.05727482]-----------------
[[ 1.10517097 1.22140276]
[ 1.34985888 1.49182475]]
[[ 2.32657385 2.84168363]
[ 2.87012386 3.8358717 ]
[ 3.04352164 3.50557756]][ 5.16825771 6.70599556 6.54909897]
AS you can see first dimension with axis=2 get the results summed properly
but then when I sum over axis one the results doesn't mach any of the
numbers. I tested summing twice by 2 and then by 1 and tested the function
by giving the two axis (2,1).
Update. When the sum function applied by using keepdims=True sequentially
the results summed up good but when using One function giving the two
axis(1,2) even with keepdims=True the results are wrong.
--
---
You received this message because you are subscribed to the Google Groups "theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to theano-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
---
You received this message because you are subscribed to the Google Groups "theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to theano-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.