import torch
a = torch.randn(3,4)
print(a)
print()
i, idx = a.sort(dim=1, descending=True)
print(i)
print(idx)
print()
j, rank = idx.sort(dim=1)
print(rank)
结果:
tensor([[ 2.3326, 0.0275, -0.0799, 0.4156],
[-2.2066, 1.7997, -2.2767, 0.4704],
[-0.6980, 0.2285, 1.0018, -0.8874]])
tensor([[ 2.3326, 0.4156, 0.0275, -0.0799],
[ 1.7997, 0.4704, -2.2066, -2.2767],
[ 1.0018, 0.2285, -0.6980, -0.8874]])
tensor([[0, 3, 1, 2],
[1, 3, 0, 2],
[2, 1, 0, 3]])
tensor([[0, 2, 3, 1],
[2, 0, 3, 1],
[2, 1, 0, 3]])