简体   繁体   中英

What is wrong in Theano.Tensor.Sum function?

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.

I did another experminet so I'll divide it into two problems:

  1. First problem:

     arr = np.ones((2, 100, 100)).astype(np.float32) x = T.ftensor3('x') tester = T.sum(x) tester2k = T.sum(x, axis=2) s1 = theano.function([x], tester) s2k = theano.function([x], tester2k) print s1(arr) print s2k(arr) The first gives 5625 instead of 20000 The second gives 30 instead of 100 [30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30................... 
  2. Second problem

     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 firstValue print "\\n" secValue = s2(arr) print secValue print "\\n" thirdValue = s3(arr) print thirdValue print '\\n' print final(arr) print "-----------------\\n" print firstValue[0,:,:] print "\\n" # print firstValue[0,:,:].sum() firstSum = np.sum(firstValue, axis=2) print firstSum print 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:

first problem, result doesn't represent the real value

second problem, 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.

This problem was related to the version below Theano.0.10, Once it is updated this problem is solved. In case someone is facing the same problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM