what does dim=-1 or -2 mean in torch.sum()?

我与影子孤独终老i 提交于 2020-02-22 07:31:00

问题


let me take a 2D matrix as example:

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])

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?


回答1:


The minus essentially means you go backwards through the dimensions. Let A be a n-dimensional matrix. Then dim=n=-1, dim=n-1=-2,...,dim=1=-(n-1),dim=0=-n. See the numpy doc for more information, as pytorch is heavily based on numpy.




回答2:


So, a tensor have multiple dimensions, ordered as in the following figure. This is a forward indexing, but there is a backward one. For backward indexing a minus is used. For example:

-1 will the last one, in our case it will be dim=2

-2 will be dim=-1

-3 will be dim=0



来源:https://stackoverflow.com/questions/59702785/what-does-dim-1-or-2-mean-in-torch-sum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!