文章目录

  • 官方解读分析
  • 小例子

官方解读分析

该函数的功能为:沿着 dim 指定的轴收集值

torch.gather(input, dim, index, out=None) → TensorGathers 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=0out[i][j][k] = input[i][index[i][j][k]][k]  # dim=1out[i][j][k] = input[i][j][index[i][j][k]]  # dim=2Parameters:  input (Tensor) – The source tensordim (int) – The axis along which to indexindex (LongTensor) – The indices of elements to gatherout (Tensor, optional) – Destination tensorExample:>>> t = torch.Tensor([[1,2],[3,4]])>>> torch.gather(t, 1, torch.LongTensor([[0,0],[1,0]]))1  14  3[torch.FloatTensor of size 2x2]

我们输出的是一个 tensor 数组,因为函数的作用是收集值,且收集的值是遍历index 得到的。因此输出数组的大小与 index 大小相同

总结来说,就是out[][][] = input[][][]out 的参数就是位置的索引,input 的参数,第dim 维由 index 中的值确定,其余参数与 out 相同

  • dim = 0时,out[i][j][k] = input[ [index[i][j][k]] [j][k]]。这代表什么意思呢?

    当我们遍历 index 来收集输出结果的时候,当前位置的输出 out[当前位置],是 input 中的一个数。这个数的第零维是 index 中当前位置对应的数,其余维度的参数是当前位置的索引

  • dim = 其他值时同理

以上面的例子为说明:

dim = 1,代表,第 1 维的参数由 index 中的数控制,第 0 维参数,out 与 input 相同

  • out[0][0] = t[0][index[0][0]] = t[0][0] = 1
  • out[0][1] = t[0][index[0][1]] = t[0][0] = 1
  • out[1][0] = t[1][index[1][0]] = t[1][1] = 4
  • out[1][1] = t[1][index[1][1]] = t[1][0] = 3

小例子

import torch
a = torch.tensor([[1, 2, 3], [4, 5, 6]])
index = torch.tensor([[1, 0, 1],[0, 0, 0]])
print(a)
print(torch.gather(a, 1, index))
print(torch.gather(a, 0, index))
tensor([[1, 2, 3],[4, 5, 6]])tensor([[2, 1, 2],[4, 4, 4]])tensor([[4, 2, 6],[1, 2, 3]])

我们先分析 torch.gather(a, 1, index)。因为dim = 1,因此第 1 维参数由 index 中的数确定。这与官方例子很类似,不用过多讲解

那么 dim = 0 的呢?这时,第 0 维由 index 中的数确定

  • out[0][0] = a[index[0][0]][0] = a[1][0] = 4
  • out[0][1] = a[index[0][1]][1] = a[0][1] = 2
  • out[0][2] = a[index[0][2]][2] = a[1][2] = 6

剩下的不再推导,由上例容易得出。

收集的数就是:遍历 index ,遍历到的数作为一个参数,剩下的参数就是当前数的位置。遍历到的数作为哪个参数,由 dim 决定

Pytorch 中 gather 函数讲解相关推荐

  1. Pytorch中gather函数的个人理解方法

    之前一直理解不了Pytorch中gather的用法,看了官方的文档也是一头雾水.然后自己琢磨,找规律,用以下方法进行理解. 一.官方文档 torch.gather(input, dim, index, ...

  2. PyTorch中gather()函数的用法

    torch.gather(input, dim, index, out=None) → Tensor 沿给定轴,按照索引张量将原张量的指定位置的元素重新聚合成一个新的张量 参数含义: input (T ...

  3. pytorch中gather函数的理解

    官方解释,很清楚了 torch.gather(input,dim,index,out=None) → Tensortorch.gather(input, dim, index, out=None) → ...

  4. python中split()函数讲解

    python中split()函数讲解 本文讲述的是string.split(s[, sep[, maxsplit]]),针对string类型的split()函数.它主要是切割字符串,结果返回由字符串元 ...

  5. pytorch中repeat()函数理解

    pytorch中repeat()函数理解 最近在学习过程中遇到了repeat()函数的使用,这里记录一下自己对这个函数的理解. 情况1:repeat参数个数与tensor维数一致时 a = torch ...

  6. pytorch 中 contiguous() 函数理解

    pytorch 中 contiguous() 函数理解 文章目录 pytorch 中 contiguous() 函数理解 引言 使用 contiguous() 后记 文章抄自 Pytorch中cont ...

  7. **Pytorch 中view函数和reshape函数的区别*

    Pytorch 中view函数和reshape函数的区别(我是一名大一刚学计算机的学生 希望我的说法对你有帮助) 首先:要了解这个问题我们要先了解一个基本知识 张量的储存方式 跟据图片我们可以清楚的看 ...

  8. pytorch中gather用法

    pytorch中gather的用法 2维度tensor进行映射: 3维度tensor进行映射: gather其实是对input进行一种映射,index必须是 LongTensor格式. 2维度tens ...

  9. python batchnorm2d_BatchNorm2d原理、作用及其pytorch中BatchNorm2d函数的参数讲解

    BN原理.作用: 函数参数讲解: BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 1. ...

最新文章

  1. 让页面先发送ajax,Ajax, 了解一下
  2. c语言 int top,顺序栈(C语言,静态栈)
  3. IcbcDaemon.exe是什么东西啊?
  4. Nearest Interesting Number
  5. spark算子_Spark 性能优化(四)——程序开发调优
  6. 开放地址法开放地址法
  7. Verilog测试:TestBench结构
  8. 粒子群算法及C语言实现
  9. NC文件的查阅,读取和分析(1)
  10. VS配置arduino环境
  11. FastDFS的安装及上传下载(二)
  12. Detours Hook初探
  13. dht11传感器c语言程序,树莓派 DHT11 温湿度传感器读取 C 语言版
  14. Python3教程--和小名一起学Python
  15. NW和Electron的区别
  16. Maven实战(四)--坐标
  17. 模块一 day02 快速上手
  18. echarts 动态数据动画效果
  19. Extjs GridPanel 常用属性总结
  20. python英汉词典

热门文章

  1. 2022-4-2考试
  2. 收藏关于wifi技术论坛
  3. [hiho 21]线段树-离散化
  4. 丰迈实验工坊学习笔记——5G系统组成与架构演进分析
  5. 艾恩ASP无组件上传最新更新说明
  6. 英文版资料[庖丁解牛—纵向切入Asp.net 3.5控件和组件开发技术]
  7. 从零开发短视频电商 利用CDN加速OSS图片传输
  8. t-SNE实践——sklearn教程
  9. 讨厌的360日历怎么彻底删除?360日历功能彻底卸载方法
  10. 【汇正财经】沪深创健康回调