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 是什么意思?
- 我如何透明地重定向一个Python导入? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
