Hi Damien (and Alex)!



 dallonsi:

What would be a simple and efficient way to get the inverse of this permutation … ?


In my mind, the cleanest approach is to take the argsort of the
permutation:
import torch
torch.__version__

t = torch.LongTensor ([1, 5, 3, 4])
perm = torch.sort (t, descending = True)[1]
perm
inverse = torch.sort (perm)[1]   # invert permutation
inverse
perm[inverse]    # check inverse
inverse[perm]    # check inverse again

Here is the output:
>>> import torch
>>> torch.__version__
'0.3.0b0+591e73e'
>>>
>>> t = torch.LongTensor ([1, 5, 3, 4])
>>> perm = torch.sort (t, descending = True)[1]
>>> perm

 1
 3
 2
 0
[torch.LongTensor of size 4]

>>> inverse = torch.sort (perm)[1]   # invert permutation
>>> inverse

 3
 0
 2
 1
[torch.LongTensor of size 4]

>>> perm[inverse]    # check inverse

 0
 1
 2
 3
[torch.LongTensor of size 4]

>>> inverse[perm]    # check inverse again

 0
 1
 2
 3
[torch.LongTensor of size 4]

This is, however, an n log (n) algorithm.  Nonetheless, by using
pytorch’s built-in sort function, it ought to be quite fast.
Best.
K. Frank