问题:
../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 中 deprecated 的 nonzero 函数,并提供使用 torch.where 函数作为替代的示例。展示了两种方法返回张量的不同形状。
2457

被折叠的 条评论
为什么被折叠?



