You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

math_ops.py 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """math Operations."""
  16. from mindspore.ops.composite.multitype_ops import _constexpr_utils as const_utils
  17. from mindspore.common import dtype as mstype
  18. from mindspore._checkparam import Validator as validator
  19. from mindspore.ops.primitive import constexpr
  20. from mindspore.ops import functional as F
  21. from .. import operations as P
  22. @constexpr
  23. def _check_validate_axis(axis, name):
  24. if isinstance(axis, (tuple, list)):
  25. for idx, item in enumerate(axis):
  26. validator.check_value_type("axis[%d]" % idx, item, [int], name)
  27. axis = validator.check_value_type('axis', axis, [int, tuple, list], name)
  28. return axis
  29. @constexpr
  30. def _check_validate_keepdims(keep_dims, name):
  31. keep_dims = validator.check_value_type('keep_dims', keep_dims, [bool], name)
  32. return keep_dims
  33. def count_nonzero(x, axis=(), keep_dims=False, dtype=mstype.int32):
  34. """
  35. Count number of nonzero elements across axis of input tensor
  36. Args:
  37. - **x** (Tensor[Number]) - Input data is used to count non-zero numbers.
  38. - **axis** (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Only constant value is allowed.
  39. Default: (), reduce all dimensions.
  40. - **keep_dims** (bool) - If true, keep these reduced dimensions and the length is 1.
  41. If false, don't keep these dimensions. Default: False.
  42. - **dtype** (Union[Number, mstype.bool_]) - The data type of the output tensor. Only constant value is allowed.
  43. Default: mstype.int32
  44. Returns:
  45. Tensor, number of nonzero element. The data type is dtype.
  46. Examples:
  47. >>> input_tensor = Tensor(np.array([[0, 1, 0], [1, 1, 0]]).astype(np.float32))
  48. >>> nonzero_num = count_nonzero(x=input_x, axis=[0, 1], keep_dims=True, dtype=mstype.int32)
  49. nonzero_num: [[3]]
  50. """
  51. const_utils.check_valid_type(F.dtype(x), mstype.number_type, 'input x')
  52. axis = _check_validate_axis(axis, "count_nonzero")
  53. keep_dims = _check_validate_keepdims(keep_dims, "count_nonzero")
  54. const_utils.check_valid_type(dtype, mstype.number_type + (mstype.bool_,), 'dtype')
  55. not_equal = P.NotEqual()
  56. cast = P.Cast()
  57. reduce_sum = P.ReduceSum(keep_dims)
  58. nonzero_bool = not_equal(x, 0)
  59. # ReduceSum only support float16 or float32 tensor.
  60. nonzero_val = cast(nonzero_bool, mstype.float16)
  61. nonzero_num = cast(reduce_sum(nonzero_val, axis), dtype)
  62. return nonzero_num