Pytorch 中 gather 函数讲解
文章目录
- 官方解读分析
- 小例子
官方解读分析
该函数的功能为:沿着 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 函数讲解相关推荐
- Pytorch中gather函数的个人理解方法
之前一直理解不了Pytorch中gather的用法,看了官方的文档也是一头雾水.然后自己琢磨,找规律,用以下方法进行理解. 一.官方文档 torch.gather(input, dim, index, ...
- PyTorch中gather()函数的用法
torch.gather(input, dim, index, out=None) → Tensor 沿给定轴,按照索引张量将原张量的指定位置的元素重新聚合成一个新的张量 参数含义: input (T ...
- pytorch中gather函数的理解
官方解释,很清楚了 torch.gather(input,dim,index,out=None) → Tensortorch.gather(input, dim, index, out=None) → ...
- python中split()函数讲解
python中split()函数讲解 本文讲述的是string.split(s[, sep[, maxsplit]]),针对string类型的split()函数.它主要是切割字符串,结果返回由字符串元 ...
- pytorch中repeat()函数理解
pytorch中repeat()函数理解 最近在学习过程中遇到了repeat()函数的使用,这里记录一下自己对这个函数的理解. 情况1:repeat参数个数与tensor维数一致时 a = torch ...
- pytorch 中 contiguous() 函数理解
pytorch 中 contiguous() 函数理解 文章目录 pytorch 中 contiguous() 函数理解 引言 使用 contiguous() 后记 文章抄自 Pytorch中cont ...
- **Pytorch 中view函数和reshape函数的区别*
Pytorch 中view函数和reshape函数的区别(我是一名大一刚学计算机的学生 希望我的说法对你有帮助) 首先:要了解这个问题我们要先了解一个基本知识 张量的储存方式 跟据图片我们可以清楚的看 ...
- pytorch中gather用法
pytorch中gather的用法 2维度tensor进行映射: 3维度tensor进行映射: gather其实是对input进行一种映射,index必须是 LongTensor格式. 2维度tens ...
- python batchnorm2d_BatchNorm2d原理、作用及其pytorch中BatchNorm2d函数的参数讲解
BN原理.作用: 函数参数讲解: BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 1. ...
最新文章
- 让页面先发送ajax,Ajax, 了解一下
- c语言 int top,顺序栈(C语言,静态栈)
- IcbcDaemon.exe是什么东西啊?
- Nearest Interesting Number
- spark算子_Spark 性能优化(四)——程序开发调优
- 开放地址法开放地址法
- Verilog测试:TestBench结构
- 粒子群算法及C语言实现
- NC文件的查阅,读取和分析(1)
- VS配置arduino环境
- FastDFS的安装及上传下载(二)
- Detours Hook初探
- dht11传感器c语言程序,树莓派 DHT11 温湿度传感器读取 C 语言版
- Python3教程--和小名一起学Python
- NW和Electron的区别
- Maven实战(四)--坐标
- 模块一 day02 快速上手
- echarts 动态数据动画效果
- Extjs GridPanel 常用属性总结
- python英汉词典