千家信息网

Python torch.gather()怎么使用

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,这篇文章主要介绍"Python torch.gather()怎么使用",在日常操作中,相信很多人在Python torch.gather()怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的
千家信息网最后更新 2025年02月03日Python torch.gather()怎么使用

这篇文章主要介绍"Python torch.gather()怎么使用",在日常操作中,相信很多人在Python torch.gather()怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Python torch.gather()怎么使用"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!


使用说明:

input和index两个参数的形状要满足一定的条件,即,除了dim参数指定的轴上,其他所有的轴形状必须完全相同.计算得到的输出结果out的形状和index相同,out输出结果的值由input给出,具体在input中的位置由当前out输出结果的位置以及index所决定,dim轴的位置由index给出,其他轴上的位置由out的当前输出结果位置决定.

代码实验展示:

Microsoft Windows [版本 10.0.18363.1256](c) 2019 Microsoft Corporation。保留所有权利。C:\Users\chenxuqi>conda activate ssd4pytorch2_2_0(ssd4pytorch2_2_0) C:\Users\chenxuqi>pythonPython 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32Type "help", "copyright", "credits" or "license" for more information.>>> import torch>>>>>> torch.manual_seed(seed=20200910)>>>>>> t = torch.tensor([[1,2],[3,4]])>>> ttensor([[1, 2],[3, 4]])>>> torch.gather(t, 1, torch.tensor([[0,0],[1,0]]))tensor([[1, 1],[4, 3]])>>> index = torch.tensor([[0,0],[1,0]])>>> indextensor([[0, 0],[1, 0]])>>> torch.gather(t, 1, index )tensor([[1, 1],[4, 3]])>>> torch.gather(t, 0, index)tensor([[1, 2],[3, 2]])>>>>>>>>>

到此,关于"Python torch.gather()怎么使用"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0