I have a 3x2 tensor, and I do a column-sum. Then, I compare the results with a separate python sum and numpy.sum() of one of the columns. The results don’t agree.
x = torch.FloatTensor([1.1233445,1.52342352,-2.124345345,1.1233445,1.52342352,-2.124345345]).view(3,2)
y1 = torch.sum(x,dim=0,keepdim=True)*923430
y2 = sum(x[:,0])*923430
y3 = np.sum(x[:,0].numpy())*923430
print(y1[0,0]-y2,y1[0,0]-y3,y2-y3) # expected 0; observed not 0
>>> -0.0024566650390625 -0.0024566650390625 0.0
This discrepancy doesn’t show for a 3x1 tensor:
x = torch.FloatTensor([1.1233445,1.52342352,-2.124345345]).view(3,1)
y1 = torch.sum(x)*923430
y2 = sum(x)*923430
y3 = np.sum(x.numpy())*923430
print(y1-y2,y1-y3,y2-y3) # expected 0; observed 0
>>> 0 0.0 0
Furthermore, if I turn the 3x2 tensor into a Variable, the discrepancy again doesn’t appear (this may or may not be related to Sum of Variable not equal to sum of its data:
x = Variable(torch.FloatTensor([1.1233445,1.52342352,-2.124345345,1.1233445,1.52342352,-2.124345345])).view(3,2)
y1 = torch.sum(x,dim=0,keepdim=True)*923430
y2 = sum(x[:,0])*923430
y3 = np.sum(x.data.numpy(),axis=0)*923430
print(y1[0,0]-y2,y1[0,0]-y3[0],y2-y3[0]) # expected 0; observed 0
>>> 0 0 0
I ran the above on a Windows machine with Pytorch 0.3.0 (0.4.0 is not yet available). I used the floating point values shown because the discrepancy shows only for long decimals.