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 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在torch.sum() 中dim=-1 或-2 是什么意思?


基础教程推荐
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 求两个直方图的卷积 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 包装空间模型 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01