Torch.sum not return a Tensor

>>> a = torch.randn(1,2)
>>> a

 0.4613 -0.6274
[torch.FloatTensor of size 1x2]

>>> torch.sum(a)

According to the official doc, torch.sum should return a Tensor, so why I got a float number?

There are two versions of torch.sum: one which sums all elements in a Tensor and returns a float, and one with sums all elements in each dimension of a Tensor and returns a Tensor.

>>> a = torch.randn(1,2)
>>> a

 1.1424  0.5776
[torch.FloatTensor of size 1x2]

>>> torch.sum(a)

>>> torch.sum(a,0)

[torch.FloatTensor of size 2]

>>> torch.sum(a,1)

[torch.FloatTensor of size 1]

But the official docs says they will both return a Tensor

Ah, interesting! It looks like a recent change. You linked to the docs for the master branch, but the same docs for v0.3.1 show the two different return types. I guess you’re using an older version of PyTorch, but looking at the latest docs.

>>> torch.__version__

I prefer they both return a Tensor… Why to change that?


In master, Tensor and Variable are now the same thing. So the behaviour is the same as what Variable used to be: It will return a Tensor with a single element in it.

In 0.3.1, Tensor sum was returning a python float while a Variable sum was returning another Variable with a single element. This weird behaviour is now fixed in master.

1 Like