栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

一看就懂的torch.gather()用法

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一看就懂的torch.gather()用法

官方文档中对此函数解释如下:
https://pytorch.org/docs/stable/generated/torch.gather.html?highlight=gather#torch.gather
其中Gathers values along an axis specified by dim.指出了gather函数本质是对一个张量进行取出其中元素的效果,类比于二维数组中通过index下标取出二维数组的元素,例如arr[1][2]=5

官方文档给出了计算式子,这里是实际计算的一个例子。
input:
[[1, 2],
[3, 4]]

index:
[[0, 0],
[1, 0]]

out的计算式如下:
out[i][j][k] = input[index[i][j][k]][j][k] # if dim == 0
out[i][j][k] = input[i][index[i][j][k]][k] # if dim == 1
out[i][j][k] = input[i][j][index[i][j][k]] # if dim == 2

torch.gather(input, 1, index)

out[i][j]=input[i][index[i][j]]
out[0][0]=input[0][index[0][0]]=input[0][0]=1
out[0][1]=input[0][index[0][1]]=input[0][0]=1
out[1][0]=input[1][index[1][0]]=input[1][1]=4
out[1][1]=input[1][index[1][1]]=input[1][0]=3

综上,该函数作用为Gathers values along an axis specified by dim,翻译过来也许你会看着迷糊。本质就是从完整数据中按索引取值,和二维数组一样

参考:https://zhuanlan.zhihu.com/p/352877584

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/580876.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号