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.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. default_half_range = 0.5
  39. if range_left >= range_right:
  40. range_left -= default_half_range
  41. range_right += default_half_range
  42. with np.errstate(invalid='ignore'):
  43. # if don't ignore state above, when np.nan exists,
  44. # it will occur RuntimeWarning: invalid value encountered in less_equal
  45. counts, edges = np.histogram(np_value, bins=bins, range=(range_left, range_right))
  46. histogram_bins = [None] * len(counts)
  47. for ind, count in enumerate(counts):
  48. histogram_bins[ind] = [float(edges[ind]), float(edges[ind + 1] - edges[ind]), float(count)]
  49. return histogram_bins
  50. def is_simple_numpy_number(dtype):
  51. """Verify if it is simple number."""
  52. if np.issubdtype(dtype, np.integer):
  53. return True
  54. if np.issubdtype(dtype, np.floating):
  55. return True
  56. return False
  57. def get_nested_message(info: dict, out_err_msg=""):
  58. """Get error message from the error dict generated by schema validation."""
  59. if not isinstance(info, dict):
  60. if isinstance(info, list):
  61. info = info[0]
  62. return f'Error in {out_err_msg}: {info}'
  63. for key in info:
  64. if isinstance(key, str) and key != '_schema':
  65. if out_err_msg:
  66. out_err_msg = f'{out_err_msg}.{key}'
  67. else:
  68. out_err_msg = key
  69. return get_nested_message(info[key], out_err_msg)