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.

validation.py 3.5 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright 2019 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. """Define a validation class which contain all check methods of datavisual module."""
  16. from numbers import Number
  17. from mindinsight.utils.exceptions import ParamValueError
  18. from mindinsight.utils.exceptions import ParamMissError
  19. from mindinsight.datavisual.common.exceptions import PluginNotAvailableError
  20. from mindinsight.datavisual.common.enums import PluginNameEnum
  21. from mindinsight.datavisual.utils.tools import to_int
  22. class Validation:
  23. """Validation class, define all check methods."""
  24. @classmethod
  25. def check_offset(cls, offset, default_value=0):
  26. """
  27. Check offset parameter, it must be greater or equal 0.
  28. Args:
  29. offset (Union[str, int]): Value can be string number or int.
  30. default_value (int): Default value for checked offset. Default: 0.
  31. Returns:
  32. int, offset.
  33. """
  34. if offset is None:
  35. return default_value
  36. offset = to_int(offset, 'offset')
  37. if offset < 0:
  38. raise ParamValueError("'offset' should be greater than or equal to 0.")
  39. return offset
  40. @classmethod
  41. def check_limit(cls, limit, min_value=1, max_value=1000, default_value=100):
  42. """
  43. Check limit parameter, it should between min_value and max_value.
  44. Args:
  45. limit (Union[str, int]): Value can be string number or int.
  46. min_value (int): Limit should greater or equal this value. Default: 1.
  47. max_value (int): Limit should less or equal this value. Default: 1000.
  48. default_value (int): Default value for limit. Default: 100.
  49. Returns:
  50. int, limit.
  51. """
  52. if limit is None:
  53. return default_value
  54. limit = to_int(limit, 'limit')
  55. if limit < min_value or limit > max_value:
  56. raise ParamValueError("'limit' should in [{}, {}].".format(min_value, max_value))
  57. return limit
  58. @classmethod
  59. def check_param_empty(cls, **kwargs):
  60. """
  61. Check param.
  62. Args:
  63. kwargs (Any): Check if arg is truthy.
  64. Raises:
  65. ParamMissError: When param missing.
  66. """
  67. for key, value in kwargs.items():
  68. # When value is 0, 0.0 or False, it is not empty.
  69. if isinstance(value, Number):
  70. continue
  71. if not value:
  72. raise ParamMissError(key)
  73. @classmethod
  74. def check_plugin_name(cls, plugin_name):
  75. """
  76. Check plugin name.
  77. Args:
  78. plugin_name (str): The plugin name.
  79. Raises:
  80. PluginNotAvailableError: When plugin name is not valid.
  81. """
  82. plugin_name_list = PluginNameEnum.list_members()
  83. if plugin_name not in plugin_name_list:
  84. raise PluginNotAvailableError(f"'plugin_name' only can be one of {plugin_name_list}")

MindInsight为MindSpore提供了简单易用的调优调试能力。在训练过程中,可以将标量、张量、图像、计算图、模型超参、训练耗时等数据记录到文件中,通过MindInsight可视化页面进行查看及分析。