Output of torch.argsort is not right

Hi,

In brief, my code is like this:

a = torch.randn(3,4).view(-1)
print(a)
print(a.argsort)

my pytorch version is 1.5.1, and the output on my platform is:

tensor([-0.3868, -1.0274,  2.1992,  0.2730, -0.7430, -1.2569, -0.9533,  0.4771,
        -1.5247, -0.0411, -0.9043,  0.2035])
tensor([ 8,  5,  1,  6, 10,  4,  0,  9, 11,  3,  7,  2])

How could I make it work, or is there any problem with my understanding of this function ?

Hi,

Could you explain what the issue is? Based on your printed output, it is working fine.

torch.argsort docs
Bests

1 Like

Hi,

If I have not misunderstood the function definition, a.argsort() would give the index of the ordered array.

In this case, the tensor is [-0.3868, -1.0274, 2.1992, 0.2730, -0.7430, -1.2569, -0.9533, 0.4771, -1.5247, -0.0411, -0.9043, 0.2035], and the smallest element of it is -1.5247 with index 7. However, the first element of the output index array of a.argsort() is 8, the corresponding element of which in the array is -0.0411. I thinks this is not correct, since the smallest element is the 7th, and the output of a.argsort() should be [7, ...].

I think you are using Matlab logic, in python we count from zero, so 7th element is 0.4771 and 8th element is -1.5247.

yes, the 7th is 0.4771 which is the neither the largest nor the smallest value in the given array.since the largest is the one with index 2(count from 0) with value 2.1992 and the smallest is the one of index 8 with value -1.5247.

1 Like

Ah, it is my bad, I will check my logic carefully. Thanks for replying !!