Python 火炬和沿轴的张量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44790670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:28:31  来源:igfitidea点击:

Torch sum a tensor along an axis

pythonsumpytorchtorchtensor

提问by Abhishek Bhatia

ipdb> outputs.size()
torch.Size([10, 100])
ipdb> print sum(outputs,0).size(),sum(outputs,1).size(),sum(outputs,2).size()
(100L,) (100L,) (100L,)

How do I sum over the columns instead?

我如何对列求和?

回答by mexmex

The simplest and best solution is to use torch.sum().

最简单和最好的解决方案是使用torch.sum().

To sum all elements of a tensor:

对张量的所有元素求和:

torch.sum(outputs) # gives back a scalar

To sum over all rows (i.e. for each column):

对所有行(即每列)求和:

torch.sum(outputs, dim=0) # size = [1, ncol]

To sum over all columns (i.e. for each row):

对所有列(即每一行)求和:

torch.sum(outputs, dim=1) # size = [nrow, 1]

回答by kmario23

Alternatively, you can use tensor.sum(axis)where axisindicates 0and 1for summing over rows and columns respectively, for a 2D tensor.

或者,对于二维张量,您可以使用tensor.sum(axis)whereaxis指示01分别对行和列求和。

In [210]: X
Out[210]: 
tensor([[  1,  -3,   0,  10],
        [  9,   3,   2,  10],
        [  0,   3, -12,  32]])

In [211]: X.sum(1)
Out[211]: tensor([ 8, 24, 23])

In [212]: X.sum(0)
Out[212]: 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 keepdimto Trueas in:

从上面的输出我们可以看出,在这两种情况下,输出都是一维张量。另一方面,如果您还希望在输出中保留原始张量的维度,那么您已将布尔 kwargkeepdim设置True为:

In [217]: X.sum(0, keepdim=True)
Out[217]: tensor([[ 10,   3, -10,  52]])

In [218]: X.sum(1, keepdim=True)
Out[218]: 
tensor([[ 8],
        [24],
        [23]])

回答by postylem

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.

如果您有 tensor my_tensor,并且您希望对第二个数组维度求和(即,索引为 1 的那个维度,即列维度,如果张量是二维的,就像您的一样),请使用torch.sum(my_tensor,1)或等效地my_tensor.sum(1)参见文档在这里

One thing that is not mentioned explicitly in the documentation is: you can sum across the lastarray-dimension by using -1(or the second-to last dimension, with -2, etc.)

文档中没有明确提到的一件事是:您可以通过使用(或倒数第二个维度,with等)对最后一个数组维度求和-1-2

So, in your example, you could use: outputs.sum(1)or torch.sum(outputs,1), or, equivalently, outputs.sum(-1)or torch.sum(outputs,-1). All of these would give the same result, an output tensor of size torch.Size([10]), with each entry being the sum over the all rows in a given column of the tensor outputs.

因此,在您的示例中,您可以使用:outputs.sum(1)torch.sum(outputs,1),或,等价地, outputs.sum(-1)torch.sum(outputs,-1)。所有这些都会给出相同的结果,一个大小为 的输出张量torch.Size([10]),每个条目是 tensor 给定列中所有行的总和outputs

To illustrate with a 3-dimensional tensor:

用 3 维张量来说明:

In [1]: my_tensor = torch.arange(24).view(2, 3, 4) 
Out[1]: 
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 [2]: my_tensor.sum(2)
Out[2]:
tensor([[ 6, 22, 38],
        [54, 70, 86]])

In [3]: my_tensor.sum(-1)
Out[3]:
tensor([[ 6, 22, 38],
        [54, 70, 86]])