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

torch.gather()函数

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

torch.gather()函数

作用:

收集输入的特定维度指定位置的数值

函数定义: torch.gather(input, dim, index, *, sparse_grad=False, out=None) → Tensor

参数:

    input (Tensor) – the source tensor

    dim (int) – the axis along which to index

    index (LongTensor) – the indices of elements to gather
理解例子:

一个二维矩阵:

input_tensor=[
    [2, 3, 4, 5, 0, 0],
    [1, 4, 3, 0, 0, 0],
    [4, 2, 2, 5, 7, 0],
    [1, 0, 0, 0, 0, 0]
]

现在有需求,如何取每行的最后一个非零元素呢? (如果说用循环的同学,可以离开了)

函数使用:
torch.gather(input= input_tensor , dim = 1,index=[[4],[3],[5],[1]])

  1. dim=1是因为每行的最后一个非零元素是一个列!
  2. index之所以维度是4*1,是为了满足index维度和output维度之间相等的关系。index的元素可以发现,是每行所要取的数的下标索引(从1开始索引而不是0)
  3. 结果就是[[5,3,7,1]]
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/348647.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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