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.

utils.py 2.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. """Utils for optimizer."""
  16. import numpy as np
  17. _DEFAULT_HISTOGRAM_BINS = 5
  18. def calc_histogram(np_value: np.ndarray, bins=_DEFAULT_HISTOGRAM_BINS):
  19. """
  20. Calculates histogram.
  21. This is a simple wrapper around the error-prone np.histogram() to improve robustness.
  22. """
  23. ma_value = np.ma.masked_invalid(np_value)
  24. valid_cnt = ma_value.count()
  25. if not valid_cnt:
  26. max_val = 0
  27. min_val = 0
  28. else:
  29. # Note that max of a masked array with dtype np.float16 returns inf (numpy issue#15077).
  30. if np.issubdtype(np_value.dtype, np.floating):
  31. max_val = ma_value.max(fill_value=np.NINF)
  32. min_val = ma_value.min(fill_value=np.PINF)
  33. else:
  34. max_val = ma_value.max()
  35. min_val = ma_value.min()
  36. range_left = min_val
  37. range_right = max_val
  38. if range_left >= range_right:
  39. range_left -= 0.5
  40. range_right += 0.5
  41. with np.errstate(invalid='ignore'):
  42. # if don't ignore state above, when np.nan exists,
  43. # it will occur RuntimeWarning: invalid value encountered in less_equal
  44. counts, edges = np.histogram(np_value, bins=bins, range=(range_left, range_right))
  45. histogram_bins = [None] * len(counts)
  46. for ind, count in enumerate(counts):
  47. histogram_bins[ind] = [float(edges[ind]), float(edges[ind + 1] - edges[ind]), float(count)]
  48. return histogram_bins
  49. def is_simple_numpy_number(dtype):
  50. """Verify if it is simple number."""
  51. if np.issubdtype(dtype, np.integer):
  52. return True
  53. if np.issubdtype(dtype, np.floating):
  54. return True
  55. return False