If I have a tensor A [1,5,3], then I perform shuffle operation, like torch.randperm(len(A)) and get shuffled index [2,1,0]. Then I shuffle A according to shuffled index, the resulted tensor is [3, 5, 1]. but how can I get original tensor ([1,5,3])? Is there any reverse shuffle function of pytorch? I know I can assign elements according to shuffled index, but it’s very slow. Can anybody help me? Thank you very much!!!
If I understand your use case correctly, this should work:
a = torch.randn(7)
print(a)
> tensor([ 1.1022, -0.3638, -0.4358, 0.2048, 1.3897, -2.2341, -0.3528])
idx = torch.randperm(a.size(0))
print(idx)
> tensor([5, 6, 0, 2, 4, 3, 1])
b = a[idx]
print(b)
> tensor([-2.2341, -0.3528, 1.1022, -0.4358, 1.3897, 0.2048, -0.3638])
c = torch.zeros_like(a)
c[idx] = b
print(c)
> tensor([ 1.1022, -0.3638, -0.4358, 0.2048, 1.3897, -2.2341, -0.3528])
print((c == a).all())
> tensor(True)
Yes, thank you very much!!!