pytorch中gather函数的理解
官方解释,很清楚了
torch.gather(input,dim,index,out=None) → Tensortorch.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 tensor
可惜没有看懂
其实就是 沿给定轴 dim ,将输入索引张量 index 指定位置的值进行聚合。
使用说明举例:
二维情况:
dim=0
>>> import torch as t
>>> a = t.arange(0,16).view(4,4)
>>> a
tensor([[ 0, 1, 2, 3],[ 4, 5, 6, 7],[ 8, 9, 10, 11],[12, 13, 14, 15]])#选取对角线的元素
>>> index = t.LongTensor([[0,1,2,3]])
>>> a.gather(0,index)
tensor([[ 0, 5, 10, 15]])
其实很简单,就是a.gather(0,index)中第一个0已经表明输出结果是行形式(0维),如果第一个是1说明输出结果是列形式(1维),然后按照index = tensor([[0, 1, 2, 3]])顺序作用在行上索引依次为0,1,2,3:
a[0][0] = 0
a[1][1] = 5
a[2][2] = 10
a[3][3] = 15
dim=1
>>> index3 = t.LongTensor([[0,1,2,3]]).t()
>>> a.gather(1,index3)
tensor([[ 0],[ 5],[10],[15]])
a[0][0] = 0
a[1][1] = 5
a[2][2] = 10
a[3][3] = 15
三维情况:
dim=0
import torch
a = torch.randint(0, 30, (2, 3, 5))
print(a)
'''
tensor([[[ 18., 5., 7., 1., 1.],[ 3., 26., 9., 7., 9.],[ 10., 28., 22., 27., 0.]],[[ 26., 10., 20., 29., 18.],[ 5., 24., 26., 21., 3.],[ 10., 29., 10., 0., 22.]]])
'''
index2 = torch.LongTensor([[[0,1,1,0,1],[0,1,1,1,1],[1,1,1,1,1]],[[1,0,0,0,0],[0,0,0,0,0],[1,1,0,0,0]]])
d = torch.gather(a, 0,index2)
print(d)
'''
tensor([[[ 18., 10., 20., 1., 18.],[ 3., 24., 26., 21., 3.],[ 10., 29., 10., 0., 22.]],[[ 26., 5., 7., 1., 1.],[ 3., 26., 9., 7., 9.],[ 10., 29., 22., 27., 0.]]])
这个有点特殊,dim = 0的时候(三维情况下),是从不同的页收集元素的。
这里举的例子只有两页。所有index在0,1两个之间选择。
输出的矩阵元素也是按照index的指定。分别在第一页和第二页之间跳着选的。
index [0,1,1,0,1]的意思就是。
在第一页选这个位置的元素,在第二页选这个位置的元素,在第二页选,第一页选,第二页选。'''
dim=1
import torch
a = torch.randint(0, 30, (2, 3, 5))
print(a)
'''
tensor([[[ 18., 5., 7., 1., 1.],[ 3., 26., 9., 7., 9.],[ 10., 28., 22., 27., 0.]],[[ 26., 10., 20., 29., 18.],[ 5., 24., 26., 21., 3.],[ 10., 29., 10., 0., 22.]]])
'''
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(a.size()==index.size())
b = torch.gather(a, 1,index)
print(b)
'''
True
tensor([[[ 18., 26., 22., 1., 0.],[ 18., 5., 7., 1., 1.],[ 3., 26., 9., 7., 9.]],[[ 5., 29., 10., 0., 22.],[ 26., 10., 20., 29., 18.],[ 10., 29., 10., 0., 22.]]])
可以看到沿着dim=1,也就是按 行 输出tensor第一页内容,
第一行分别是 按照index指定的,
input tensor的第一页
第一列的下标为0的元素 第二列的下标为1元素 第三列的下标为2的元素,第四列下标为0元素,第五列下标为2元素
index-->0,1,2,0,2 output--> 18., 26., 22., 1., 0.
'''
a[0][0][0]=18
a[0][1][1]=26
a[0][2][2]=22
a[0][0][3]=1
a[0][2][4]=0
dim=2
c = torch.gather(a, 2,index)
print(c)
'''
tensor([[[ 18., 5., 7., 18., 7.],[ 3., 3., 3., 3., 3.],[ 28., 28., 28., 28., 28.]],[[ 10., 20., 20., 20., 20.],[ 5., 5., 5., 5., 5.],[ 10., 10., 10., 10., 10.]]])
dim = 2的时候就按 列 聚合了。参照上面的举一反三。
'''
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 函数讲解
文章目录 官方解读分析 小例子 官方解读分析 该函数的功能为:沿着 dim 指定的轴收集值 torch.gather(input, dim, index, out=None) → TensorGath ...
- pytorch中repeat()函数理解
pytorch中repeat()函数理解 最近在学习过程中遇到了repeat()函数的使用,这里记录一下自己对这个函数的理解. 情况1:repeat参数个数与tensor维数一致时 a = torch ...
- pytorch 中 contiguous() 函数理解
pytorch 中 contiguous() 函数理解 文章目录 pytorch 中 contiguous() 函数理解 引言 使用 contiguous() 后记 文章抄自 Pytorch中cont ...
- pytorch中张量的阶数理解
pytorch中张量的阶数理解 推荐打开2个页面,对比原四阶张量理解各阶的对应关系. 创建一个四阶张量: import torch x = torch.linspace(0,71,72).view(2 ...
- matlab怎么画碎石图,成分分析中biplot函数不理解_主成分分析
成分分析中biplot函数不理解_主成分分析 对主成分分析中的biplot函数不理解,谁能帮忙解释?谢谢了 解答: 运行下面的例子,理解我加黑的那句话即可: Examples Perform a pr ...
- **Pytorch 中view函数和reshape函数的区别*
Pytorch 中view函数和reshape函数的区别(我是一名大一刚学计算机的学生 希望我的说法对你有帮助) 首先:要了解这个问题我们要先了解一个基本知识 张量的储存方式 跟据图片我们可以清楚的看 ...
- MATLAB中的函数句柄 理解
MATLAB中的函数句柄 理解 https://zhuanlan.zhihu.com/p/266263265 https://blog.csdn.net/kevinhg/article/details ...
最新文章
- 警惕技术人员的极端性
- opencv 学习笔记6:通道的拆分与合并
- 通过pxe远程安装linux,通过PXE远程安装多台Linux系统
- SQLServer中批量插入数据方式的性能对比 (转)
- java观察者_Java中的观察者模式
- 贪吃蛇程序不要白不要,一个赞就够了
- d.php xfso_centos平台基于snort、barnyard2以及base的IDS(入侵检测系统)的搭建与测试及所遇问题汇总...
- 国内主流设计作品分享社区,用作品动世界
- 安编译器错误_centos 安装pcre报c++编译器错误
- python中str与bytes
- 《深度学习Python实践》第18章——持久化加载模型
- 论如何优雅的处理回文串 - 回文自动机详解.
- CMSIS-RTOS相关的一些内容
- matlab simulink教程pdf,Simulink基础入门教程“完整版”.pdf
- 【开源微信】微信登入公众号、小程序
- Cause: java.lang.ArrayIndexOutOfBoundsException: 8
- 执教《送给盲婆婆的蝈蝈》有感
- STM32 解析 JSON 之 cJSON
- 国产光纤熔接机技术是否成熟?带你评测TFN-S7光纤熔接机性能
- EditPlus设置保存时不生成bak文件