Skip to content
fn

nonzero

→Tensor or tuple of Tensor
nonzero(input: Tensor, as_tuple: _bool = ...)

Implementing kernel

C++ engine symbols that back this Python API.

Return the indices of all non-zero (or True) elements.

The output is a 2-D int64 tensor of shape (N, input.ndim) where N is the number of non-zero entries — each row is one multi-index. With as_tuple=True it is one 1-D int64 tensor per dim instead (a 0-d input counts as 1-D).

Parameters

inputTensor
Source tensor.
as_tuplebool= False
Return the columns of the index matrix as a tuple.

Returns

Tensor or tuple of Tensor

2-D int64 tensor of multi-indices, or its columns.

Notes

Because the output size is data-dependent this op forces a CPU round-trip on accelerator backends. For mask-style use cases prefer where or masked_select.

Examples

>>> import lucid
>>> x = lucid.tensor([[0, 1], [2, 0]])
>>> lucid.nonzero(x)
Tensor([[0, 1],
        [1, 0]])