Pytorch中的gather使用方法
作者:SY_curry 时间:2021-11-22 06:11:49
官方说明
gather可以对一个Tensor进行聚合,声明为:torch.gather(input, dim, index, out=None) → Tensor
一般来说有三个参数:输入的变量input、指定在某一维上聚合的dim、聚合的使用的索引index,输出为Tensor类型的结果(index必须为LongTensor类型)。
#参数介绍:
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
#当输入为三维时的计算过程:
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
#样例:
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]
实验
用下面的代码在二维上做测试,以便更好地理解
t = torch.Tensor([[1,2,3],[4,5,6]])
index_a = torch.LongTensor([[0,0],[0,1]])
index_b = torch.LongTensor([[0,1,1],[1,0,0]])
print(t)
print(torch.gather(t,dim=1,index=index_a))
print(torch.gather(t,dim=0,index=index_b))
输出为:
>>tensor([[1., 2., 3.],
[4., 5., 6.]])
>>tensor([[1., 1.],
[4., 5.]])
>>tensor([[1., 5., 6.],
[4., 2., 3.]])
由于官网给的计算过程不太直观,下面给出较为直观的解释:
对于index_a,dim为1表示在第二个维度上进行聚合,索引为列号,[[0,0],[0,1]]表示结果的第一行取原数组第一行列号为[0,0]的数,也就是[1,1],结果的第二行取原数组第二行列号为[0,1]的数,也就是[4,5],这样就得到了输出的结果[[1,1],[4,5]]。
对于index_b,dim为0表示在第一个维度上进行聚合,索引为行号,[[0,1,1],[1,0,0]]表示结果的第一行第d(d=0,1,2)列取原数组第d列行号为[0,1,1]的数,也就是[1,5,6],类似的,结果的第二行第d列取原数组第d列行号为[1,0,0]的数,也就是[4,2,3],这样就得到了输出的结果[[1,5,6],[4,2,3]]
接下来以index_a为例直接用官网的式子计算一遍加深理解:
output[0,0] = input[0,index[0,0]] #1 = input[0,0]
output[0,1] = input[0,index[0,1]] #1 = input[0,0]
output[1,0] = input[1,index[1,0]] #4 = input[1,0]
output[1,1] = input[1,index[1,1]] #5 = input[1,1]
注
以下两种写法得到的结果是一样的:
r1 = torch.gather(t,dim=1,index=index_a)
r2 = t.gather(1,index_a)
补充:Pytorch中的torch.gather函数的个人理解
最近在学习pytorch时遇到gather函数,开始没怎么理解,后来查阅网上相关资料后大概明白了原理。
gather()函数
在pytorch中,gather()函数的作用是将数据从input中按index提出,我们看gather函数的的官方文档说明如下:
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函数中我们用到的主要有三个参数:
1)input:输入
2)dim:维度,常用的为0和1
3)index:索引位置
贴一段代码举例说明:
a=t.arange(0,16).view(4,4)
print(a)
index_1=t.LongTensor([[3,2,1,0]])
b=a.gather(0,index_1)
print(b)
index_2=t.LongTensor([[0,1,2,3]]).t()#tensor转置操作:(a)T=a.t()
c=a.gather(1,index_2)
print(c)
输出如下:
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
tensor([[12, 9, 6, 3]])tensor([[ 0],
[ 5],
[10],
[15]])
在gather中,我们是通过index对input进行索引把对应的数据提取出来的,而dim决定了索引的方式。
在上面的例子中,a是一个4×4矩阵:
1)当维度dim=0,索引index_1为[3,2,1,0]时,此时可将a看成1×4的矩阵,通过index_1对a每列进行行索引:第一列第四行元素为12,第二列第三行元素为9,第三列第二行元素为6,第四列第一行元素为3,即b=[12,9,6,3];
2)当维度dim=1,索引index_2为[0,1,2,3]T时,此时可将a看成4×1的矩阵,通过index_1对a每行进行列索引:第一行第一列元素为0,第二行第二列元素为5,第三行第三列元素为10,第四行第四列元素为15,即c=[0,5,10,15]T;
来源:https://blog.csdn.net/qq_34392457/article/details/90206220
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python利用cv2动态绘制圆和矩形的示例详解
基于Python采集爬取微信公众号历史数据
![](https://img.aspxhome.com/file/2023/2/82762_0s.png)
网页屏蔽鼠标左右键和键盘按键功能
Python编码类型转换方法详解
禁止背景图在网页中平铺
![](https://img.aspxhome.com/file/UploadPic/20072/2007231139890s.gif)
在ASP.NET 2.0中操作数据之三十七:DataList批量更新
![](https://img.aspxhome.com/file/2023/3/76343_0s.png)
Python 制作查询商品历史价格的小工具
![](https://img.aspxhome.com/file/2023/3/84883_0s.png)
ASP中使用FileSystemObject时提高性能的一个小技巧
python输入、数据类型转换及运算符方式
![](https://img.aspxhome.com/file/2023/3/65863_0s.png)
Python切片操作实例分析
Python趣味挑战之用pygame实现飞机塔防游戏
![](https://img.aspxhome.com/file/2023/7/95867_0s.gif)
Python图像分割之均匀性度量法分析
![](https://img.aspxhome.com/file/2023/2/72872_0s.png)
使用OpenCV实现人脸图像卡通化的示例代码
![](https://img.aspxhome.com/file/2023/7/82527_0s.jpg)
Python使用Django实现博客系统完整版
![](https://img.aspxhome.com/file/2023/3/95393_0s.png)
Python中优雅使用assert断言的方法实例
python打开windows应用程序的实例
javascript 45种缓动效果(二)
![](https://img.aspxhome.com/file/UploadPic/up/2009091919271644.gif)
SNS用户体验和互动性浅析
![](https://img.aspxhome.com/file/UploadPic/20111/17/01-46s.jpg)
数据分页显示按钮,自动获取url链接
python+Selenium自动化测试——输入,点击操作
![](https://img.aspxhome.com/file/2023/1/59831_0s.jpg)