【发布时间】:2021-03-30 09:39:05
【问题描述】:
让a 成为(n, d, l) 张量。让 indices 成为 (n, 1) 张量,包含索引。我想从indices 给出的索引中收集中间维度张量中的a。因此,生成的张量的形状将是 (n, l)。
n = 3
d = 2
l = 3
a = tensor([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]],
[[12, 13, 14],
[15, 16, 17]]])
indices = tensor([[0],
[1],
[0]])
# Shape of result is (n, l)
result = tensor([[ 0, 1, 2], # a[0, 0, :] since indices[0] == 0
[ 9, 10, 11], # a[1, 1, :] since indices[1] == 1
[12, 13, 14]]) # a[2, 0, :] since indices[2] == 0
这确实类似于a.gather(1, indices),但gather 不起作用,因为indices 与a 的形状不同。如何在此设置中使用gather?或者我应该使用什么?
【问题讨论】: