mindspore.mint.nonzero

View Source On Gitee
mindspore.mint.nonzero(input, *, as_tuple=False)[source]

Return the positions of all non-zero values.

Parameters

input (Tensor) – The input tensor.

Note

  • Ascend: Rank of Input tensor can be equal to 0 except jit level O2 mode.

Keyword Arguments

as_tuple (bool, optional) – Whether the output is tuple. Default False .

Returns

Tensor or tuple of tensors

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> x = mindspore.tensor([[[1,  0], [-5, 0]]], mindspore.int32)
>>> output = mindspore.mint.nonzero(x)
>>> print(output)
[[0 0 0]
 [0 1 0]]
>>> x = mindspore.tensor([1, 0, 2, 0, 3], mindspore.int32)
>>> output = mindspore.mint.nonzero(x, as_tuple=False)
>>> print(output)
[[0]
 [2]
 [4]]
>>> x = mindspore.tensor([[[1,  0], [-5, 0]]], mindspore.int32)
>>> output = mindspore.mint.nonzero(x, as_tuple=True)
>>> print(output)
(Tensor(shape=[2], dtype=Int64, value=[0, 0]),
 Tensor(shape=[2], dtype=Int64, value=[0, 1]),
 Tensor(shape=[2], dtype=Int64, value=[0, 0]))
>>> x = mindspore.tensor([1, 0, 2, 0, 3], mindspore.int32)
>>> output = mindspore.mint.nonzero(x, as_tuple=True)
>>> print(output)
(Tensor(shape=[3], dtype=Int64, value=[0, 2, 4]), )