UserWarning: This overload of nonzero is deprecated: nonzero(Tensor input, Tensor out) 解决

问题:

../torch/csrc/utils/python_arg_parser.cpp:738: UserWarning: This overload of nonzero is deprecated:
        nonzero(Tensor input, Tensor out)
Consider using one of the following signatures instead:
        nonzero(Tensor input, bool as_tuple)

 解决:

你会发现你加上as_tuple也不行,torch.nonzero()也不行,官方推荐我们用torch.where函数代替,如何代替见例子.

>>> import torch
>>> array=torch.Tensor([0,1,1,0,0])
>>> torch.nonzero(array)
tensor([[1],
        [2]])
>>> torch.where(array)
(tensor([1, 2]),)
>>> 

PS:注意nonzero和where返回tensor的shape区别。

你可能感兴趣的:(Pytorch,pytorch,python,nonzero,深度学习)