当前位置: 首页 > article >正文

【pytorch】torch.gather()函数

dim=0时

index=[ [x1,x2,x2],
		[y1,y2,y2],
		[z1,z2,z3] ]

如果dim=0
填入方式为:
index=[ [(x1,0),(x2,1),(x3,2)]
		[(y1,0),(y2,1),(y3,2)]
		[(z1,0),(z2,1),(z3,2)] ]
input = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12]
] # shape(3,4)
input = torch.tensor(input)
length = torch.LongTensor([
    [2,2,2,2],
    [1,1,1,1],
    [0,0,0,0],
    [0,1,2,0]
])# shape(4,4)
out = torch.gather(input, dim=0, index=length)
print(out)
tensor([[9, 10, 11, 12],
        [5, 6, 7, 8],
        [1, 2, 3, 4],
        [1, 6, 11, 4]])
#### dim=0后,根据new_index对input进行索引
new_index=[ [(2,0),(2,1),(2,2),(2,3)],
			[(1,0),(1,1),(1,2),(1,3)],
			[(0,0),(0,1),(0,2),(0,3)],
			[(0,0),(1,1),(2,2),(0,3)] ]
			
可以观察到第四行,行索引变为0,所以当gather函数里的index超过input的唯独时,会从0重新计数。

dim=1时

input = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12]
] # shape(3,4)
input = torch.tensor(input)
length = torch.LongTensor([
    [2,2,2,2],
    [1,1,1,1],
    [0,1,2,0]
]) # shape(3,4)
out = torch.gather(input, dim=1, index=length)
print(out)
tensor([[3, 3, 3, 3],
        [6, 6, 6, 6],
        [9, 10, 11, 9]])

new_index = [
	[(0,2),(0,2),(0,2),(0,2)],
	[(1,1),(1,1),(1,1),(1,1)],
	[(2,0),(2,1),(2,2)(2,0)]
]

http://www.kler.cn/news/107208.html

相关文章:

  • CH8571 沁恒微 RISC-V EC 用芯片实验 (一)参考手册 第一章 接脚描述
  • 当线性规划与算法相遇:揭秘单纯形法(Simplex)的独特魅力
  • 最新Python深度学习技术进阶与应用
  • 【广州华锐互动】智能家居设计3D虚拟还原系统
  • FPGA时序分析与约束(7)——通过Tcl扩展SDC
  • WORD中的表格内容回车行距过大无法调整行距
  • 【STM32】STM32中断体系
  • 【期中复习】深度学习
  • Games 103 作业二
  • 10款轻量型的嵌入式GUI库分享
  • C++前缀和算法的应用:使数组相等的最小开销
  • 基于Python Django 的微博舆论、微博情感分析可视化系统(V2.0)
  • 如何快速安装MONAI(莫奈)医学标注软件
  • Xray的简单使用
  • Spring 更简单的读取和存储对象
  • docker - window Docker Desktop升级
  • redis集群的多key原子性操作如何实现?
  • Oracle RU 19.21及 datapatch -sanity_checks
  • js中HTMLCollection如何循环
  • 基于springboot实现校园志愿者管理系统项目【项目源码+论文说明】计算机毕业设计
  • 【ARMv8 SIMD和浮点指令编程】NEON 存储指令——如何将数据从寄存器存储到内存?
  • 系列二十二、如何在Spring中所有的bean都创建完成后做扩展
  • MySQL篇---第四篇
  • 论文阅读——InstructGPT
  • 数据库MySQL(四):表中字段约束和外键约束
  • 磁场设备主要有哪些
  • DBA笔记(1)
  • 云服务器搭建Zookeeper集群
  • nginx 动静分离 防盗链
  • 先讲结论、逻辑先行,6个必备的职场技能