浅谈Pytorch中的torch.gather函数的含义


Posted in Python onAugust 18, 2019

pytorch中的gather函数

pytorch比tensorflow更加编程友好,所以准备用pytorch试着做最近要做的一些实验。

立个flag开始学习pytorch,新开一个分类整理学习pytorch中的一些踩到的泥坑。

浅谈Pytorch中的torch.gather函数的含义

今天刚开始接触,读了一下documentation,写一个一开始每太搞懂的函数gather

b = torch.Tensor([[1,2,3],[4,5,6]])
print b
index_1 = torch.LongTensor([[0,1],[2,0]])
index_2 = torch.LongTensor([[0,1,1],[0,0,0]])
print torch.gather(b, dim=1, index=index_1)
print torch.gather(b, dim=0, index=index_2)

观察它的输出结果:

1 2 3
 4 5 6
[torch.FloatTensor of size 2x3]


 1 2
 6 4
[torch.FloatTensor of size 2x2]


 1 5 6
 1 2 3
[torch.FloatTensor of size 2x3]

这里是官方文档的解释

torch.gather(input, dim, index, out=None) → Tensor

 Gathers values along an axis specified by dim.

 For a 3-D tensor the output is specified by:

 out[i][j][k] = input[index[i][j][k]][j][k] # dim=0
 out[i][j][k] = input[i][index[i][j][k]][k] # dim=1
 out[i][j][k] = input[i][j][index[i][j][k]] # dim=2

 Parameters: 

  input (Tensor) ? The source tensor
  dim (int) ? The axis along which to index
  index (LongTensor) ? The indices of elements to gather
  out (Tensor, optional) ? Destination tensor

 Example:

 >>> t = torch.Tensor([[1,2],[3,4]])
 >>> torch.gather(t, 1, torch.LongTensor([[0,0],[1,0]]))
  1 1
  4 3
 [torch.FloatTensor of size 2x2]

可以看出,gather的作用是这样的,index实际上是索引,具体是行还是列的索引要看前面dim 的指定,比如对于我们的栗子,【1,2,3;4,5,6,】,指定dim=1,也就是横向,那么索引就是列号。index的大小就是输出的大小,所以比如index是【1,0;0,0】,那么看index第一行,1列指的是2, 0列指的是1,同理,第二行为4,4 。这样就输入为【2,1;4,4】,参考这样的解释看上面的输出结果,即可理解gather的含义。

gather在one-hot为输出的多分类问题中,可以把最大值坐标作为index传进去,然后提取到每一行的正确预测结果,这也是gather可能的一个作用。

以上这篇浅谈Pytorch中的torch.gather函数的含义就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python获取豆瓣电影简介代码分享
Jan 16 Python
低版本中Python除法运算小技巧
Apr 05 Python
构建Python包的五个简单准则简介
Jun 15 Python
MySQL中表的复制以及大型数据表的备份教程
Nov 25 Python
对Python的Django框架中的项目进行单元测试的方法
Apr 11 Python
Django+uni-app实现数据通信中的请求跨域的示例代码
Oct 12 Python
Python Print实现在输出中插入变量的例子
Dec 25 Python
python爬虫开发之PyQuery模块详细使用方法与实例全解
Mar 09 Python
Python基于yield遍历多个可迭代对象
Mar 12 Python
在 Pycharm 安装使用black的方法详解
Apr 02 Python
jupyter 导入csv文件方式
Apr 21 Python
python 在threading中如何处理主进程和子线程的关系
Apr 25 Python
PyTorch中Tensor的维度变换实现
Aug 18 #Python
PyTorch中Tensor的拼接与拆分的实现
Aug 18 #Python
详解PyTorch中Tensor的高阶操作
Aug 18 #Python
浅析PyTorch中nn.Linear的使用
Aug 18 #Python
Pytorch实现GoogLeNet的方法
Aug 18 #Python
PyTorch之图像和Tensor填充的实例
Aug 18 #Python
Pytorch Tensor的索引与切片例子
Aug 18 #Python
You might like
支持中文的php加密解密类代码
2011/11/27 PHP
深入理解PHP几个算法:PHP冒泡、PHP二分法、PHP求素数、PHP乘法表
2013/06/06 PHP
CI框架文件上传类及图像处理类用法分析
2016/05/18 PHP
360搜索引擎自动收录php改写方案
2018/04/28 PHP
PHP的JSON封装、转变及输出操作示例
2019/09/27 PHP
JavaScript基本编码模式小结
2012/05/23 Javascript
深入讲解AngularJS中的自定义指令的使用
2015/06/18 Javascript
jQuery的事件委托实例分析
2015/07/15 Javascript
jQuery实现鼠标悬停背景翻转的黑色导航菜单代码
2015/09/14 Javascript
JS实现浏览器状态栏显示时间的方法
2015/10/27 Javascript
基于jQuery实现网页打印功能
2015/12/01 Javascript
JavaScript组件开发完整示例
2015/12/15 Javascript
简单的JS轮播图代码
2016/07/18 Javascript
Vue2 模板template的四种写法总结
2018/02/23 Javascript
微信小程序使用wxParse解析html的实现示例
2018/08/30 Javascript
vue与django集成打包的实现方法
2019/11/11 Javascript
使用PreloadJS加载图片资源的基础方法详解
2020/02/03 Javascript
VUE页面中通过双击实现复制表格中内容的示例代码
2020/06/11 Javascript
[35:34]Liquid vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
python中pycurl库的用法实例
2014/09/30 Python
python获取当前计算机cpu数量的方法
2015/04/18 Python
Python工厂函数用法实例分析
2018/05/14 Python
对python3 一组数值的归一化处理方法详解
2018/07/11 Python
python隐藏类中属性的3种实现方法
2019/12/19 Python
使用Tensorflow实现可视化中间层和卷积层
2020/01/24 Python
tensorflow 报错unitialized value的解决方法
2020/02/06 Python
jupyter notebook 调用环境中的Keras或者pytorch教程
2020/04/14 Python
Pandas将列表(List)转换为数据框(Dataframe)
2020/04/24 Python
python简单实现插入排序实例代码
2020/12/16 Python
Html5游戏开发之乒乓Ping Pong游戏示例(一)
2013/01/21 HTML / CSS
使用Html5中的cavas画一面国旗
2019/09/25 HTML / CSS
美国中小型企业领先的办公家具供应商:Office Designs
2016/11/26 全球购物
人力资源管理毕业生自荐信
2013/11/21 职场文书
财务会计大学生自我评价
2014/04/09 职场文书
服务承诺书范文
2014/05/19 职场文书
中国汉字听写大会观后感
2015/06/02 职场文书