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 1.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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."""
  16. import math
  17. def calc_histogram_bins(count):
  18. """
  19. Calculates experience-based optimal bins number for histogram.
  20. To suppress re-sample bias, there should be enough number in each bin. So we calc bin numbers according to
  21. count. For very small count(1 - 10), we assign carefully chosen number. For large count, we tried to make
  22. sure there are 9-10 numbers in each bucket on average. Too many bins will also distract users, so we set max
  23. number of bins to 30.
  24. Args:
  25. count (int): Valid number count for the tensor.
  26. Returns:
  27. int, number of histogram bins.
  28. """
  29. number_per_bucket = 10
  30. max_bins = 30
  31. if not count:
  32. return 1
  33. if count <= 5:
  34. return 2
  35. if count <= 10:
  36. return 3
  37. if count <= 280:
  38. # note that math.ceil(281/10) + 1 equals 30
  39. return math.ceil(count / number_per_bucket) + 1
  40. return max_bins