Issue
Say I have tensor
A
, and indexes Tensor
: A = [1, 2, 3, 4]
, indexes = [1, 0, 3, 2]
I want to create a new Tensor
from these two with the following result : [2, 1, 4, 3]
Each element of the result is element from A
and the order is defined by the indexes Tensor
.
Is there a way to do it with PyTorch tensor
ops without loops?
My goal is to do it for 2D Tensor
, but I don't think there is a way to do it without loops, so I thought to project it to 1D, do the work and project it back to the 2D.
Solution
You can use scatter:
A = torch.tensor([1, 2, 3, 4])
indices = torch.tensor([1, 0, 3, 2])
result = torch.tensor([0, 0, 0, 0])
print(result.scatter_(0, indices, A))
Answered By - teplandr
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.