Solved by tom in post #4 
              
              
                @KFrank 's method is a great ad hoc way to inverse a permutation. 
If the argument is rather large (say >=10000 elements) and you know it is a permutation (0…9999) then you could also use indexing: 
def inverse_permutation(perm):
    inv = torch.empty_like(perm)
    inv[perm] = torch.arange(perm.siz…