Pytorch:torch.nonzero()函数 - 代码先锋网
www.codeleading.com › article › 283244083321 torch.nonzero () 找出tensor中 非零的元素的索引。 返回一个包含输入 input 中非零元素索引的张量.输出张量中的每行包含 input 中非零元素的索引。 torch.nonzero (input, *, out= None, as_tuple= False) 栗子: import torch label = torch.tensor ( [ [ 1, 0, 0 ], [ 1, 0, 1 ]]) print (label.nonzero ()) 输出: tensor ( [ [ 0, 0 ], [ 1, 0 ], [ 1, 2 ]])
torch.is_nonzero — PyTorch 1.10.1 documentation
pytorch.org › docs › stabletorch.is_nonzero(input) Returns True if the input is a single element tensor which is not equal to zero after type conversions. i.e. not equal to torch.tensor ( [0.]) or torch.tensor ( [0]) or torch.tensor ( [False]) . Throws a RuntimeError if torch.numel () != 1 (even in case of sparse tensors). Parameters input ( Tensor) – the input tensor.
torch.nonzero — PyTorch 1.10.1 documentation
pytorch.org › docs › stabletorch.nonzero (..., as_tuple=True) returns a tuple of 1-D index tensors, allowing for advanced indexing, so x [x.nonzero (as_tuple=True)] gives all nonzero values of tensor x. Of the returned tuple, each index tensor contains nonzero indices for a certain dimension. See below for more details on the two behaviors.