torch.gather()

作用:收集输入的特定维度指定位置的数值
参数:
input(tensor):   待操作数。不妨设其维度为(x1, x2, …, xn)
dim(int):   待操作的维度。
index(LongTensor):   如何对input进行操作。其维度有限定,例如当dim=i时,index的维度为(x1, x2, …y, …,xn),既是将input的第i维的大小更改为y,且要满足y>=1(除了第i维之外的其他维度,大小要和input保持一致)。
out:   注意输出和index的维度是一致的

复制代码
input = [
    [2, 3, 4, 5, 0, 0],
    [1, 4, 3, 0, 0, 0],
    [4, 2, 2, 5, 7, 0],
    [1, 0, 0, 0, 0, 0]
]
input = torch.tensor(input)
print(input.shape)
length = torch.LongTensor([[4],[3],[5],[1]])
print(length.shape)
#index之所以减1,是因为序列维度是从0开始计算的
out = torch.gather(input, 1, length-1)
print(out)
复制代码

 

 

length = torch.LongTensor([[4],[3],[5]])
print(length.shape)
#index之所以减1,是因为序列维度是从0开始计算的
out = torch.gather(input, 1, length-1)
print(out)

 

a = torch.range(1,6).reshape(2,3)
index = torch.LongTensor([0,1,1]).expand(2,3)
print('index',index)
print('a',a)
out = torch.gather(a,1,index)
print('out',out)
print(out.shape)

 

posted on   cltt  阅读(213)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2019-03-13 hdu 2544
2019-03-13 L1-043 阅览室 (20 分)
2019-03-13 L1-049 天梯赛座位分配 (20 分)
2019-03-13 2018年长沙理工大学第十三届程序设计竞赛 G 逃离迷宫
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示