gather torch_gather函数
Python
Python开发
Python语言
gather函数
gather(input, dim, index):根据 index,在 dim 维度上选取数据,输出的 size 与 index 一致
# input (Tensor) – 源张量
# dim (int) – 索引的轴
# index (LongTensor) – 聚合元素的下标(index需要是torch.longTensor类型)
# out (Tensor, optional) – 目标张量
for 3D tensor:
out[i][j][k] = tensor[index[i][j][k]][j][k] # dim=0
out[i][j][k] = tensor[i][index[i][j][k]][k] # dim=1
out[i][j][k] = tensor[i][j][index[i][j][k]] # dim=2
for 2D tensor:
out[i][j] = input[index[i][j]][j] # dim = 0
out[i][j] = input[i][index[i][j]] # dim = 1
import torch as t # 导入torch模块
c = t.arange(0, 60).view(3, 4, 5) # 定义tensor
print(c)
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]]])
b = t.gather(c, 0, index)
print(b)
输出:
tensor([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],
[[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]],
[[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]]])
报错:
Traceback (most recent call last):
File "E:/Release02/my_torch.py", line 14, in
b = t.gather(c, 0, index)
RuntimeError: Size does not match at dimension 1 get 4 vs 3
(第1维尺寸不匹配)
将index调整为:
index = t.LongTensor([[[0, 1, 2, 0, 2], [0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]],
[[1, 2, 2, 2, 2], [0, 0, 0, 0, 0], [2, 2, 2, 2, 2], [1, 1, 1, 1, 1]],
[[1, 2, 2, 2, 2], [0, 0, 0, 0, 0], [2, 2, 2, 2, 2], [1, 1, 1, 1, 1]]])
则上文输出为:
tensor([[[ 0, 21, 42, 3, 44],
[ 5, 6, 7, 8, 9],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]],
[[20, 41, 42, 43, 44],
[ 5, 6, 7, 8, 9],
[50, 51, 52, 53, 54],
[35, 36, 37, 38, 39]],
[[20, 41, 42, 43, 44],
[ 5, 6, 7, 8, 9],
[50, 51, 52, 53, 54],
[35, 36, 37, 38, 39]]])
对于2D tensor 则无“index与tensor 的size一致”之要求,
这个要求在官方文档和其他博文、日志中均无提到
(可能是个坑吧丨可能是个坑吧丨可能是个坑吧)
eg:
代码(此部分来自https://www.yzlfxy.com/jiaocheng/python/337618.html):
b = torch.Tensor([[1,2,3],[4,5,6]])
print b
index_1 = torch.LongTensor([[0,1],[2,0]])
index_2 = torch.LongTensor([[0,1,1],[0,0,0]])
print torch.gather(b, dim=1, index=index_1)
print torch.gather(b, dim=0, index=index_2)
输出:
1 2 3
4 5 6
[torch.FloatTensor of size 2x3]
1 2
6 4
[torch.FloatTensor of size 2x2]
1 5 6
1 2 3
[torch.FloatTensor of size 2x3]
官方文档:
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 torch_gather函数相关推荐
- gather() 的函数功能
参考文章:Pytorch中的torch.gather函数的含义 demo b = torch.Tensor([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) print(b) i ...
- 006_similarsites
title 006<SimilarSites> 一键查找姊妹网站 SimilarSites 当你浏览一个很棒的站点的时候, 或许你会想到, 和它"差不多"的站点有哪些, ...
- Linux2.6内核驱动与2.4的区别 .
随着Linux2.6的发布,由于2.6内核做了新的改动,各个设备的驱动程序在不同程度上要进行改写.为了方便各位Linux爱好者我把自己整理的这分文档share出来.该文当列举了2.6内核同以前版本的绝 ...
- Linux中wait接口用于延时,linux2.6驱动编写参考
1. 使用新的入口 必须包含 module_init(your_init_func); module_exit(your_exit_func); 老版本:int init_module(void); ...
- 合并相同数据的行_R语言笔记(六):数据框重塑(reshape2)
数据处理主要内容包括: 1. 特殊值处理 1.1 缺失值 1.2 离群值 1.3 日期 2. 数据转换(base vs. dplyr) 2.1 筛选(subset vs. filter/select/ ...
- GraphX:基于Spark的弹性分布式图计算系统
1 引言 在了解GraphX之前,需要先了解关于通用的分布式图计算框架的两个常见问题:图存储模式和图计算模式. 1.1 图存储模式 巨型图的存储总体上有边分割和点分割两种存储方式.2013年,G ...
- Linux 2.6下Driver开发的34个变化[转贴]
Linux2.6内核驱动移植嵌入式系统|linux|ARM|单片机'h7@!A-L5}7e[ I;H 随着Linux2.6的发布,由于2.6内核做了教的改动,各个设备的驱动程序在不同程度上要进行改写. ...
- 快刀初试:Spark GraphX在淘宝的实践
(本文由团队中梧苇和我一起撰写,并由团队中的林岳,岩岫,世仪等多人Review,发表于程序员的8月刊,由于篇幅原因,略作删减,本文为完整版) 对于网络科学而言,世间万物都可以抽象成点,而事物之间的关系 ...
- 明风:分布式图计算的平台Spark GraphX 在淘宝的实践
快刀初试:Spark GraphX在淘宝的实践 作者:明风 (本文由团队中梧苇和我一起撰写,并由团队中的林岳,岩岫,世仪等多人Review,发表于程序员的8月刊,由于篇幅原因,略作删减,本文为完整版) ...
最新文章
- Unity2D游戏开发和C#编程大师班
- C语言鸡尾酒排序cocktail sort算法(附完整源码)
- linux中的ftp是什么意思,什么是linux的ftp
- SSR -- 服务端渲染基础
- 设计模式之观察者模式(Java)
- Python Text I/O
- Linux下安装informix11.5数据库
- WinForm XML
- ftok函数(file to key)
- go c 语言,c语言与go语言的区别有哪些
- java编程过程——流程图
- 代理猎手(Proxy Hunter)教程(详细图文)
- ts无损剪辑合并_视频如何合并?视频合并太难?其实很简单
- 0xc0000225无法进系统_0xc0000225无法进系统 - 卡饭网
- php输出跳转下一页,tp5页面输出时,搜索后跳转下一页的处理
- 动手学深度学习(文本预处理+语言模型+循环神经网络基础)
- 注销公司是否需要办理注销呼叫中心许可证?
- 杀不死你的,终将使你更强大
- 大恒相机Python版Qt界面(二)
- 【感恩】为做运维的重病老同事李静波寻求帮助
热门文章
- Bind9:配置 DNS 授权服务器和 DNS 缓存服务器
- MyBatis 的简单应用(转载)
- 环洋市场调研-2021年全球反垄断咨询行业调研及趋势分析报告
- 腾讯安全副总裁黎巍谈WAF:通过云原生能力构建安全基座
- 5月10日 ksjsb抓包教程,小黄鸟无需ROOT抓包
- 打印系统开发(33)——打印机的使用(理光Ricoh Pro 907EX PCL 6)
- vue3+echarts实现世界地图以及轨线(label使用fomatter+rich动态添加图片及背景色,以及label如何添加动态边框色)
- 蓝桥杯第十一届单片机国赛
- xilinx SDK MSS文件
- MSS与MTU的关系