what does dim=-1 or -2 mean in torch.sum()?(在torch.sum() 中dim=-1 或-2 是什么意思?)
问题描述
让我以二维矩阵为例:
mat = torch.arange(9).view(3, -1)
tensor([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
torch.sum(mat, dim=-2)
tensor([ 9, 12, 15])
我发现 torch.sum(mat, dim=-2)
的结果等于 torch.sum(mat, dim=0)
和 dim=-1
等于 dim=1
.我的问题是如何理解这里的负面维度.如果输入矩阵有 3 个或更多维度怎么办?
I find the result of torch.sum(mat, dim=-2)
is equal to torch.sum(mat, dim=0)
and dim=-1
equal to dim=1
. My question is how to understand the negative dimension here. What if the input matrix has 3 or more dimensions?
推荐答案
减号本质上意味着您向后浏览维度.设 A 是一个 n 维矩阵.然后dim=n-1=-1,dim=n-2=-2,...,dim=1=-(n-1),dim=0=-n.有关详细信息,请参阅 numpy 文档,因为 pytorch 在很大程度上基于 numpy.
The minus essentially means you go backwards through the dimensions. Let A be a n-dimensional matrix. Then dim=n-1=-1, dim=n-2=-2, ..., dim=1=-(n-1), dim=0=-n. See the numpy doc for more information, as pytorch is heavily based on numpy.
这篇关于在torch.sum() 中dim=-1 或-2 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!