Nonzero elements of tensors.
Arguments
- self
(Tensor) the input tensor.
- as_list
If
FALSE, the output tensor containing indices. IfTRUE, one 1-D tensor for each dimension, containing the indices of each nonzero element along that dimension.When
as_listisFALSE(default):Returns a tensor containing the indices of all non-zero elements of
input. Each row in the result contains the indices of a non-zero element ininput. The result is sorted lexicographically, with the last index changing the fastest (C-style).If
inputhas dimensions, then the resulting indices tensoroutis of size , where is the total number of non-zero elements in theinputtensor.When
as_listisTRUE:Returns a tuple of 1-D tensors, one for each dimension in
input, each containing the indices (in that dimension) of all non-zero elements ofinput.If
inputhas dimensions, then the resulting tuple contains tensors of size , where is the total number of non-zero elements in theinputtensor.As a special case, when
inputhas zero dimensions and a nonzero scalar value, it is treated as a one-dimensional tensor with one element.
Examples
if (torch_is_installed()) {
torch_nonzero(torch_tensor(c(1, 1, 1, 0, 1)))
}
#> torch_tensor
#> 1
#> 2
#> 3
#> 5
#> [ CPULongType{4,1} ]