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

【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.lryc.cn/news/211767.html

相关文章:

  • Mac 安装psycopg2,报错Error: pg_config executable not found.
  • 域名系统 DNS
  • Vue $nextTick 模板解析后在执行的函数
  • VBA技术资料MF76:将自定义颜色添加到调色板
  • zilong-20231030
  • 目标检测算法发展史
  • React 生成传递给无障碍属性的唯一 ID
  • 十种排序算法(1) - 准备测试函数和工具
  • IRF联动 BFD-MAD
  • 双向链表的初步练习
  • IDE的组成
  • 项目解读_v2
  • 杀毒软件哪个好,杀毒软件有哪些
  • Ubuntu上安装配置Nginx
  • C++之string
  • 多线程---单例模式
  • SpringBoot相比于Spring的优点(自动配置和依赖管理)
  • SAP SPAD新建打印纸张
  • C# 图解教程 第5版 —— 第11章 结构
  • 车载电子电器架构 —— 基于AP定义车载HPC
  • Redis原理-IO模型和持久化
  • PID控制示例
  • GoLand GC(垃圾回收机制)简介及调优
  • AI:40-基于深度学习的森林火灾识别
  • 37基于MATLAB平台的图像去噪,锐化,边缘检测,程序已调试通过,可直接运行。
  • 通过Metasploit+Ngrok穿透内网长期维持访问外网Android设备
  • STM32 CubeMX配置USB HID功能,及安装路径
  • 【错误解决方案】ModuleNotFoundError: No module named ‘transformers‘
  • Mac 配置环境变量
  • 如何在linux服务器上安装Anaconda与pytorch,以及pytorch卸载