How Pytorch Tensor get the index of specific value

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

    Can be done by converting to numpy as follows

    import torch
    x = torch.range(1,4)
    print(x)
    ===> tensor([ 1.,  2.,  3.,  4.]) 
    nx = x.numpy()
    np.where(nx == 3)[0][0]
    ===> 2
    

提交回复
热议问题