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 是什么意思?
基础教程推荐
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
