【发布时间】:2021-10-01 05:25:54
【问题描述】:
给定一个输入张量x 和一个索引张量idxs,我想检索x 的所有元素,其索引在idxs 中不存在。也就是取torch.gather函数输出的反面。
以torch.gather 为例:
>>> x = torch.arange(30).reshape(3,10)
>>> idxs = torch.tensor([[1,2,3], [4,5,6], [7,8,9]], dtype=torch.long)
>>> torch.gather(x, 1, idxs)
tensor([[ 1, 2, 3],
[14, 15, 16],
[27, 28, 29]])
我真正想要实现的是
tensor([[ 0, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26]])
什么是有效且高效的实现,可能使用 torch 实用程序?我不想使用任何 for 循环。
我假设idxs 在其最深处只有独特 元素。例如idxs 将是调用torch.topk 的结果。
【问题讨论】:
-
您想要的输出不一致。如果
idxs在同一行有两个相同的元素会发生什么,例如[[1,1,3], [4,5,6], [7,8,9]]。在这种情况下会产生什么结果? -
@Ivan 我假设 idxs 在其最深处只有独特的元素。例如,假设 idxs 是
torch.topk的输出。
标签: python arrays pytorch tensor torch