How do I sum over the columns of a tensor?
torch.Size([10, 100]) ---> torch.Size()
The simplest and best solution is to use
To sum all elements of a tensor:
torch.sum(x) # gives back a scalar
To sum over all rows (i.e. for each column):
torch.sum(x, dim=0) # size = [ncol]
To sum over all columns (i.e. for each row):
torch.sum(x, dim=1) # size = [nrow]
It should be noted that the dimension summed over is eliminated from the resulting tensor.
Alternatively, you can use
1 for summing over rows and columns respectively, for a 2D tensor.
In : X Out: tensor([[ 1, -3, 0, 10], [ 9, 3, 2, 10], [ 0, 3, -12, 32]]) In : X.sum(1) Out: tensor([ 8, 24, 23]) In : X.sum(0) Out: tensor([ 10, 3, -10, 52])
As, we can see from the above outputs, in both cases, the output is a 1D tensor. If you, on the other hand, wish to retain the dimension of the original tensor in the output as well, then you’ve set the boolean kwarg
True as in:
In : X.sum(0, keepdim=True) Out: tensor([[ 10, 3, -10, 52]]) In : X.sum(1, keepdim=True) Out: tensor([[ 8], , ])
If you have tensor
my_tensor, and you wish to sum across the second array dimension (that is, the one with index 1, which is the column-dimension, if the tensor is 2-dimensional, as yours is), use
torch.sum(my_tensor,1) or equivalently
my_tensor.sum(1) see documentation here.
One thing that is not mentioned explicitly in the documentation is: you can sum across the last array-dimension by using
-1 (or the second-to last dimension, with
So, in your example, you could use:
torch.sum(outputs,1), or, equivalently,
torch.sum(outputs,-1). All of these would give the same result, an output tensor of size
torch.Size(), with each entry being the sum over the all rows in a given column of the tensor
To illustrate with a 3-dimensional tensor:
In : my_tensor = torch.arange(24).view(2, 3, 4) Out: tensor([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]) In : my_tensor.sum(2) Out: tensor([[ 6, 22, 38], [54, 70, 86]]) In : my_tensor.sum(-1) Out: tensor([[ 6, 22, 38], [54, 70, 86]])
it should be
dim (int or tuple of python:ints) – the dimension or dimensions to reduce.
dim=0 means reduce row dimensions: condense all rows = sum by col
dim=1 means reduce col dimensions: condense cols= sum by row
Just for the sake of completeness (I could not find it easily) I include how to sum along multiple dimensions with
torch.sum which is heavily used in computer vision tasks where you have to reduce along
If you have an image
x with shape
C x H x W and want to compute the average pixel intensity value per channel you could do:
avg = torch.sum(x, dim=(1,2)) / (H*W) # Sum along (H,W) and norm