Strange behavior of torch.argsort

 import torch

 a = torch.rand(3, 4)

 print(a)

 b = torch.argsort(a)

 print(b)


However, I get

tensor([[0.4143, 0.4293, 0.9506, 0.2267],
        [0.8637, 0.8718, 0.2595, 0.2888],
        [0.6663, 0.6066, 0.8233, 0.6793]])
tensor([[3, 0, 1, 2],
        [2, 3, 0, 1],
        [1, 0, 3, 2]])

For the first line, why the smallest value 0.2267 accords with 2 in b, which seems strange.
My pytorch version is 1.5.

This seems the right answer…
The first row is 3,0,1,2 since 0.2267 is smallest (which is at index 3), then 0.4143 (which is at index 0), then 0.4293, then 0.9506.
Similarly for second and third row.
Note argsort is done by default along the last dimension.

1 Like

Quite a silly question… Thank you for your explanation.