mindspore.mint.nonzero
- mindspore.mint.nonzero(input, as_tuple=False)[source]
Return the positions of all non-zero values.
- Parameters
- 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.
ValueError – If dim of input equals to 0.
- Supported Platforms:
Ascend
Examples
>>> import mindspore >>> import numpy as np >>> from mindspore import Tensor, mint >>> x = Tensor(np.array([[[1, 0], [-5, 0]]]), mindspore.int32) >>> output = mint.nonzero(x) >>> print(output) [[0 0 0] [0 1 0]] >>> x = Tensor(np.array([1, 0, 2, 0, 3]), mindspore.int32) >>> output = mint.nonzero(x, False) >>> print(output) [[0] [2] [4]] >>> x = Tensor(np.array([[[1, 0], [-5, 0]]]), mindspore.int32) >>> output = mint.nonzero(x, 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 = mint.nonzero(x, True) >>> print(output) (Tensor(shape=[3], dtype=Int64, value=[0, 2, 4]), )