【发布时间】:2021-04-25 04:18:35
【问题描述】:
任务看起来很简单,但我不知道该怎么做。
所以我有两个张量:
- 形状为
(2, 5, 2)的索引张量indices,其中最后一个维度对应于x 和y 维度中的索引 - 形状为
(2, 5, 2, 16, 16)的“值张量”value,我希望使用 x 和 y 索引选择最后两个维度
更具体地说,索引在 0 到 15 之间,我想得到一个输出:
out = value[:, :, :, x_indices, y_indices]
因此输出的形状应该是(2, 5, 2)。有人可以在这里帮助我吗?非常感谢!
编辑:
我尝试了用聚集的建议,但不幸的是它似乎不起作用(我改变了尺寸,但没关系):
首先我生成一个坐标网格:
y_t = torch.linspace(-1., 1., 16, device='cpu').reshape(16, 1).repeat(1, 16).unsqueeze(-1)
x_t = torch.linspace(-1., 1., 16, device='cpu').reshape(1, 16).repeat(16, 1).unsqueeze(-1)
grid = torch.cat((y_t, x_t), dim=-1).permute(2, 0, 1).unsqueeze(0)
grid = grid.unsqueeze(1).repeat(1, 3, 1, 1, 1)
下一步,我将创建一些索引。在这种情况下,我总是取索引 1:
indices = torch.ones([1, 3, 2], dtype=torch.int64)
接下来,我用的是你的方法:
indices = indices.unsqueeze(-1).unsqueeze(-1)
new_coords = torch.gather(grid, -1, indices).squeeze(-1).squeeze(-1)
最后,我手动为 x 和 y 坐标选择索引 1:
new_coords_manual = grid[:, :, :, 1, 1]
这会输出以下新坐标:
new_coords
tensor([[[-1.0000, -0.8667],
[-1.0000, -0.8667],
[-1.0000, -0.8667]]])
new_coords_manual
tensor([[[-0.8667, -0.8667],
[-0.8667, -0.8667],
[-0.8667, -0.8667]]])
如您所见,它仅适用于一维。你知道如何解决这个问题吗?
【问题讨论】:
-
您能否展示一个最小的示例
indices和value以及所需的输出? -
产生
new_coords_manual时达到想要的输出