Pytorch argmax 跨多个维度
Pytorch argmax across multiple dimensions
我有一个 4D 张量,我想得到最后两个维度的 argmax。 torch.argmax
只接受整数作为“dim”参数,不接受元组。
我怎样才能做到这一点?
这是我的想法,但我不知道如何匹配我的两个“索引”张量的维度。 original_array
是形状 [1, 512, 37, 59].
max_vals, indices_r = torch.max(original_array, dim=2)
max_vals, indices_c = torch.max(max_vals, dim=2)
indices = torch.hstack((indices_r, indices_c))
你可以用 torch.flatten
and apply torch.argmax
压平最后两个维度:
>>> x = torch.rand(2,3,100,100)
>>> x.flatten(-2).argmax(-1)
tensor([[2660, 6328, 8166],
[5934, 5494, 9717]])
正如其他人提到的,最好将最后两个维度展平并应用 argmax
original_array = torch.rand(1, 512, 37, 59)
original_flatten = original_array.view(1, 512, -1)
_, max_ind = original_flatten.max(-1)
.. 您将获得最大值的线性索引。如果你想要最大值的二维索引,你可以使用列数
来“展开”索引
# 59 is the number of columns for the (37, 59) part
torch.stack([max_ind // 59, max_ind % 59], -1)
这将为您提供一个 (1, 512, 2)
,其中每个最后 2 维包含二维坐标。
我有一个 4D 张量,我想得到最后两个维度的 argmax。 torch.argmax
只接受整数作为“dim”参数,不接受元组。
我怎样才能做到这一点?
这是我的想法,但我不知道如何匹配我的两个“索引”张量的维度。 original_array
是形状 [1, 512, 37, 59].
max_vals, indices_r = torch.max(original_array, dim=2)
max_vals, indices_c = torch.max(max_vals, dim=2)
indices = torch.hstack((indices_r, indices_c))
你可以用 torch.flatten
and apply torch.argmax
压平最后两个维度:
>>> x = torch.rand(2,3,100,100)
>>> x.flatten(-2).argmax(-1)
tensor([[2660, 6328, 8166],
[5934, 5494, 9717]])
正如其他人提到的,最好将最后两个维度展平并应用 argmax
original_array = torch.rand(1, 512, 37, 59)
original_flatten = original_array.view(1, 512, -1)
_, max_ind = original_flatten.max(-1)
.. 您将获得最大值的线性索引。如果你想要最大值的二维索引,你可以使用列数
来“展开”索引# 59 is the number of columns for the (37, 59) part
torch.stack([max_ind // 59, max_ind % 59], -1)
这将为您提供一个 (1, 512, 2)
,其中每个最后 2 维包含二维坐标。