Transform a tensor using a bijection?

Hello,

I’d like to do the following using element wise operation :

I have a tensor a = [1,2,3,4]

And I have a bijection, that is represented by a dictionary for example

b = {1:4,2:1,3:2,4:3}

The transform would be :

f(a,b) = [4,1,2,3]

1 become a 4, 2 became a 1 and so forth…

Is this operation possible using only element wise operations?

You could try to create an index tensor from b and index a directly:

a = torch.tensor([1, 2, 3, 4])
b = torch.tensor([3, 0, 1, 2])
a[b]

Note that the index starts at 0 in python/PyTorch.