mindspore.Tensor.count_nonzero

View Source On Gitee
Tensor.count_nonzero(dim=None) Tensor

Counts the number of non-zero values in the tensor input along the given dim. If no dim is specified then all non-zeros in the tensor are counted.

Warning

This is an experimental API that is subject to change or deletion.

Parameters

dim (Union[None, int, tuple(int), list(int)], optional) – The dimension to reduce. Default value: None, which indicates that the number of non-zero elements is calculated. If dim is None, all elements in the tensor are summed up.

Returns

Tensor, number of nonzero element across dim specified by dim.

Raises
  • TypeError – If dim is not int, tuple(int), list(int) or None.

  • ValueError – If any value in dim is not in range \([-self.ndim, self.ndim)\).

Supported Platforms:

Ascend

Examples

>>> from mindspore import Tensor
>>> import numpy as np
>>> import mindspore
>>> # case 1: each value specified.
>>> x = Tensor(np.array([[0, 1, 0], [1, 1, 0]]).astype(np.float32))
>>> nonzero_num = x.count_nonzero(dim=[0, 1])
>>> print(nonzero_num)
[[3]]
>>> # case 2: all value is default.
>>> nonzero_num = x.count_nonzero()
>>> print(nonzero_num)
3
>>> # case 3: dim value was specified 0.
>>> nonzero_num = x.count_nonzero(dim=[0,])
>>> print(nonzero_num)
[1 2 0]
>>> # case 4: dim value was specified 1.
>>> nonzero_num = x.count_nonzero(dim=[1,])
>>> print(nonzero_num)
[1 2]
Tensor.count_nonzero(axis=(), keep_dims=False, dtype=None) Tensor

Count number of nonzero elements across axis of input tensor.

Parameters
  • axis (Union[int, tuple(int), list(int)], optional) – The dimensions to reduce. Default: () , reduce all dimensions.

  • keep_dims (bool, optional) – Whether to maintain dimensions specified by axis. If true, keep these reduced dimensions and the length is 1. If false, don't keep these dimensions. Default: False .

  • dtype (Union[Number, mindspore.bool_], optional) – The data type of the output tensor. Default: None .

Returns

Tensor, number of nonzero element across axis specified by axis. The data type is specified by dtype.

Raises
  • TypeError – If axis is not int, tuple or list.

  • ValueError – If any value in axis is not in range \([-self.ndim, self.ndim)\).

Supported Platforms:

Ascend GPU CPU

Examples

>>> from mindspore import Tensor
>>> import numpy as np
>>> import mindspore
>>> # case 1: each value specified.
>>> x = Tensor(np.array([[0, 1, 0], [1, 1, 0]]).astype(np.float32))
>>> nonzero_num = x.count_nonzero(x=x, axis=[0, 1], keep_dims=True, dtype=mindspore.int32)
>>> print(nonzero_num)
[[3]]
>>> # case 2: all value is default.
>>> nonzero_num = x.count_nonzero()
>>> print(nonzero_num)
3
>>> # case 3: axis value was specified 0.
>>> nonzero_num = x.count_nonzero(axis=[0,])
>>> print(nonzero_num)
[1 2 0]
>>> # case 4: axis value was specified 1.
>>> nonzero_num = x.count_nonzero(axis=[1,])
>>> print(nonzero_num)
[1 2]
>>> # case 5: keep_dims value was specified.
>>> nonzero_num = x.count_nonzero(keep_dims=True)
>>> print(nonzero_num)
[[3]]
>>> # case 6: keep_dims and axis value was specified.
>>> nonzero_num = x.count_nonzero(axis=[0,], keep_dims=True)
>>> print(nonzero_num)
[[1 2 0]]