mindspore.ops.nonzero

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

Return the positions of all non-zero values.

Parameters

input (Tensor) –

The input Tensor.

  • Ascend: its rank can be equal to 0 except O2 mode.

  • CPU/GPU: its rank should be greater than or eaqual to 1.

Keyword Arguments

as_tuple (bool, optional) – Whether the output is tuple. If False , return Tensor. Default: False . If True , return Tuple of Tensor, only support Ascend .

Returns

  • If as_tuple is False, return the Tensor, a 2-D Tensor whose data type is int64, containing the positions of all non-zero values of the input.

  • If as_tuple is True, return the Tuple of Tensor and data type is int64. The Tuple length is the dimension of the input tensor, and each element is the 1D tensor of the subscript of all non-zero elements of the input tensor in that dimension.

Raises
  • TypeError – If input is not Tensor.

  • TypeError – If as_tuple is not bool.

  • RuntimeError – On GPU or CPU or Ascend O2 mode, if dim of input equals to 0.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> x = Tensor(np.array([[[1,  0], [-5, 0]]]), mindspore.int32)
>>> output = ops.nonzero(x)
>>> print(output)
[[0 0 0]
 [0 1 0]]
>>> x = Tensor(np.array([1, 0, 2, 0, 3]), mindspore.int32)
>>> output = ops.nonzero(x, as_tuple=False)
>>> print(output)
[[0]
 [2]
 [4]]
>>> x = Tensor(np.array([[[1,  0], [-5, 0]]]), mindspore.int32)
>>> output = ops.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 = Tensor(np.array([1, 0, 2, 0, 3]), mindspore.int32)
>>> output = ops.nonzero(x, as_tuple=True)
>>> print(output)
(Tensor(shape=[3], dtype=Int64, value=[0, 2, 4]), )