gather torch_torch.gather()的理解
函数定义
torch.gather(input, dim, index, out=None)
其中,
input(tensor): 待操作数,源张量
dim(int): 维度。
index(LongTensor): 索引下标。
out: 输出的张量
注意,输出的张量和index的size是一致的。
举例
dim=0
输入:
[code]a = torch.arange(0,16).view(4,4)
print(a)
index = torch.LongTensor([[0,1,2,3]]) # index的元素必须是int型数据,如果使用torch.Tensor会报错
print(a.gather(0, index))
输出:
[code]tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
tensor([[ 0, 5, 10, 15]])
解释:dim=0代表针对0维,即输出为行形式。不难看出,此时index的[0,1,2,3]分别指向源张量a的第0,1,2,3行的第0,1,2,3列的数据。被指向的数据分别为0,5,10,15,又因为dim=0代表“行”,故输出tensor([[ 0, 5, 10, 15]])
dim=1
输入:
[code]index3 = torch.LongTensor([[0,1,2,3]]).t() # 添加上t.()将index3变成一列的数据
print(a.gather(1,index3))
输出:
[code]tensor([[ 0],
[ 5],
[10],
[15]])
解释:dim=0代表针对0维,即输出为行形式。不难看出,此时index的[0,1,2,3]分别指向源张量a的第0,1,2,3列的第0,1,2,3行的数据。被指向的数据分别为0,5,10,15,又因为dim=1代表“列”,故有上述输出。
dim=2
输入:
[code]a = torch.randint(0, 30, (2, 3, 5))
print("------a------")
print(a)
index = torch.LongTensor([[[0,1,2,0,2],
[0,0,0,0,0],
[1,1,1,1,1]],
[[1,2,2,2,2],
[0,0,0,0,0],
[2,2,2,2,2]]])
print("----index----")
print(index)
c = torch.gather(a, 2,index) # 除了使用a.gather(1,index)外,也可以torch.gather(a,1,index)
print("------c------")
print(c)
输出:
[code]------a------
tensor([[[ 2, 0, 28, 28, 12],
[28, 28, 13, 26, 13],
[16, 7, 26, 21, 3]],
[[19, 16, 29, 20, 23],
[10, 26, 4, 24, 26],
[26, 14, 28, 3, 25]]])
----index----
tensor([[[0, 1, 2, 0, 2],
[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1]],
[[1, 2, 2, 2, 2],
[0, 0, 0, 0, 0],
[2, 2, 2, 2, 2]]])
------c------
tensor([[[ 2, 0, 28, 2, 28],
[28, 28, 28, 28, 28],
[ 7, 7, 7, 7, 7]],
[[16, 29, 29, 29, 29],
[10, 10, 10, 10, 10],
[28, 28, 28, 28, 28]]])
解释:
我们可以看index[0][0],为[0,1,2,0,2],其中的“0 1 2 0 2”五个数字分别指向源张量第0页第0行的第“0 1 2 0 2”列数据,故输出张量c[0][0]=[2,0,28,2,28]。类推即可理解dim=2的输出情况。
参考链接:
点赞
收藏
分享
文章举报
野草.
发布了6 篇原创文章 · 获赞 0 · 访问量 143
私信
关注
gather torch_torch.gather()的理解相关推荐
- Pytorch中gather函数的个人理解方法
之前一直理解不了Pytorch中gather的用法,看了官方的文档也是一头雾水.然后自己琢磨,找规律,用以下方法进行理解. 一.官方文档 torch.gather(input, dim, index, ...
- gather torch_我对torch中的gather函数的一点理解
本文首发于公众号[拇指笔记] 官方文档的解释 torch.gather(input,dim,index,out=None) → Tensor torch.gather(input, dim, inde ...
- 006_similarsites
title 006<SimilarSites> 一键查找姊妹网站 SimilarSites 当你浏览一个很棒的站点的时候, 或许你会想到, 和它"差不多"的站点有哪些, ...
- 理解 PyTorch 中的 gather 函数
好久没更新博客了,最近一直在忙,既有生活上的也有工作上的.道阻且长啊. 今天来水一文,说一说最近工作上遇到的一个函数:torch.gather() . 文字理解 我遇到的代码是 NLP 相关的,代码中 ...
- pytorch之expand,gather,squeeze,sum,contiguous,softmax,max,argmax
目录 gather squeeze expand sum contiguous softmax max argmax gather torch.gather(input,dim,index,out=N ...
- pytorch新手需要注意的隐晦操作Tensor,max,gather
文章移到:https://oldpan.me/archives/pytorch-rookie-care-tensor-max-gather pytorch中有很多操作比较隐晦,需要仔细研究结合一些例子 ...
- gather torch_gather函数
Python Python开发 Python语言 gather函数 gather(input, dim, index):根据 index,在 dim 维度上选取数据,输出的 size 与 ...
- pytorch中的gather()函数
首先,给出官方文档的链接: https://pytorch.org/docs/stable/generated/torch.gather.html?highlight=gather#torch.gat ...
- yii2的model数据库配置以及应用(主从数据库配置)
2019独角兽企业重金招聘Python工程师标准>>> 1.多数据库配置 'db' => require(__DIR__ . '/db.php'),'gdb' => re ...
最新文章
- 爬虫与浏览器的区别,爬虫产生(出自简书)
- 论文笔记:Deep Residual Learning
- css实现垂直居中定位
- iPhoneX 序列适配方案
- 调用oracle常用方法,区分Oracle和SQL Server常用函数调用方法
- 转----ASP.NET中常用的优化性能方法
- 力扣--349两个数组的交集I-II
- Java EE 各个版本简介
- (转)如何看待美国监管机构要求文艺复兴基金提交源码?
- FP-growth算法原理解析
- 笔记本此计算机到网络出现一个叉,笔记本电脑无线网络不可用并显示红叉的解决方...
- 为什么 MySQL 唯一索引会导致死锁,“有心杀贼,无力回天”?
- fflush(stdout)作用
- JAVA并发编程的艺术-读书笔记
- Spark并行度的设定
- ThinkPad X201 笔记本通过硬盘安装 Ubuntu 双系统
- pdf.js改成base64方式加载
- 2011年的工作总结
- GD32F407之LWIP+RTL8201F
- CATIA 3DEXPERIENCE R2019x VBA开发介绍
热门文章
- PyQt5--TableWidget中插入PushButton
- 让Yahoo开通POP3和SMTP
- web小游戏开发:蜘蛛纸牌(一)
- dedecms标签一 : 首页模板常用标签
- 【数据库设计】概念设计-数据库ER图基础概念
- JavaScript 根据指定年月获取该月的第一天和最后一天、获取上个月的年月、上个月月底日期
- 百分百全开源的ERP项目,太赞了
- adb devices unauthorized问题解决
- 【HBuilder】wap2app禁用原生标题栏,解决 “双标题” 的问题
- 车辆网络安全的未来(上):车辆开发中的威胁分析、风险评估和安全设计、漏洞分析