mindspore.ops.NonZero

View Source On Gitee
class mindspore.ops.NonZero[source]

Return a Tensor of the positions of all non-zero values.

Inputs:
  • 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.

Outputs:

Tensor, a 2-D Tensor whose data type is int64, containing the positions of all non-zero values of the input. If the dimension of input is D and the number of non-zero in input is N , then the shape of output is \((N, D)\) .

Raises
  • TypeError – If input is not Tensor.

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

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> input = Tensor(np.array([1, 0, 2, 0, 3]), mindspore.int32)
>>> output = ops.NonZero()(input)
>>> print(output)
[[0]
 [2]
 [4]]