How Pytorch Tensor get the index of specific value

前端 未结 5 1439
谎友^
谎友^ 2020-12-14 15:34

In python list, we can use list.index(somevalue). How can pytorch do this?
For example:

    a=[1,2,3]
    print(a.index(2))
5条回答
  •  醉梦人生
    2020-12-14 15:49

    For floating point tensors, I use this to get the index of the element in the tensor.

    print((torch.abs((torch.max(your_tensor).item()-your_tensor))<0.0001).nonzero())
    

    Here I want to get the index of max_value in the float tensor, you can also put your value like this to get the index of any elements in tensor.

    print((torch.abs((YOUR_VALUE-your_tensor))<0.0001).nonzero())
    

提交回复
热议问题