How Pytorch Tensor get the index of specific value

前端 未结 5 1520
谎友^
谎友^ 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:42

    I think there is no direct translation from list.index() to a pytorch function. However, you can achieve similar results using tensor==number and then the nonzero() function. For example:

    t = torch.Tensor([1, 2, 3])
    print ((t == 2).nonzero())
    

    This piece of code returns

    1

    [torch.LongTensor of size 1x1]

提交回复
热议问题