How Pytorch Tensor Get The Index Of Elements?
I have 2 Tensors named x and list and their definitions are below: x = torch.tensor(3) list = torch.tensor([1,2,3,4,5]) Now I want to get the index of element x from list. The ex
Solution 1:
import torch
x = torch.tensor(3)
list = torch.tensor([1,2,3,4,5])
idx = (list == x).nonzero().flatten()
print (idx.tolist()) # [2]
list = torch.tensor([1,2,3,3,5])
idx = (list == x).nonzero().flatten()
print (idx.tolist()) # [2, 3]
Post a Comment for "How Pytorch Tensor Get The Index Of Elements?"