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.

param_handler.py 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 params."""
  16. import numpy as np
  17. from mindinsight.lineagemgr.model import LineageTable, USER_DEFINED_PREFIX, METRIC_PREFIX
  18. from mindinsight.optimizer.common.enums import HyperParamKey, HyperParamType, HyperParamSource, TargetKey, \
  19. TargetGoal, TunableSystemDefinedParams, TargetGroup, SystemDefinedTargets
  20. from mindinsight.optimizer.common.log import logger
  21. def generate_param(param_info, n=1):
  22. """Generate param."""
  23. value = None
  24. if HyperParamKey.BOUND.value in param_info:
  25. bound = param_info[HyperParamKey.BOUND.value]
  26. value = np.random.uniform(bound[0], bound[1], n)
  27. if param_info[HyperParamKey.TYPE.value] == HyperParamType.INT.value:
  28. value = value.astype(HyperParamType.INT.value)
  29. if HyperParamKey.CHOICE.value in param_info:
  30. indexes = np.random.randint(0, len(param_info[HyperParamKey.CHOICE.value]), n)
  31. value = [param_info[HyperParamKey.CHOICE.value][index] for index in indexes]
  32. if HyperParamKey.DECIMAL.value in param_info:
  33. value = np.around(value, decimals=param_info[HyperParamKey.DECIMAL.value])
  34. return np.array(value)
  35. def generate_arrays(params_info: dict, n=1):
  36. """Generate arrays."""
  37. suggest_params = None
  38. for _, param_info in params_info.items():
  39. suggest_param = generate_param(param_info, n).reshape((-1, 1))
  40. if suggest_params is None:
  41. suggest_params = suggest_param
  42. else:
  43. suggest_params = np.hstack((suggest_params, suggest_param))
  44. if n == 1:
  45. return suggest_params[0]
  46. return suggest_params
  47. def match_value_type(array, params_info: dict):
  48. """Make array match params type."""
  49. array_new = []
  50. index = 0
  51. for _, param_info in params_info.items():
  52. value = array[index]
  53. if HyperParamKey.BOUND.value in param_info:
  54. bound = param_info[HyperParamKey.BOUND.value]
  55. value = max(bound[0], array[index])
  56. value = min(bound[1], value)
  57. if HyperParamKey.CHOICE.value in param_info:
  58. choices = param_info[HyperParamKey.CHOICE.value]
  59. nearest_index = int(np.argmin(np.fabs(np.array(choices) - value)))
  60. value = choices[nearest_index]
  61. if param_info.get(HyperParamKey.TYPE.value) == HyperParamType.INT.value:
  62. value = int(value)
  63. if HyperParamKey.DECIMAL.value in param_info:
  64. value = np.around(value, decimals=param_info[HyperParamKey.DECIMAL.value])
  65. array_new.append(value)
  66. index += 1
  67. return array_new
  68. def organize_params_target(lineage_table: LineageTable, params_info: dict, target_info):
  69. """Organize params and target."""
  70. empty_result = np.array([])
  71. if lineage_table is None:
  72. return empty_result, empty_result
  73. param_keys = []
  74. for param_key, param_info in params_info.items():
  75. # It will be a user_defined param:
  76. # 1. if 'source' is specified as 'user_defined'
  77. # 2. if 'source' is not specified and the param is not a system_defined key
  78. source = param_info.get(HyperParamKey.SOURCE.value)
  79. prefix = _get_prefix(param_key, source, HyperParamSource.USER_DEFINED.value,
  80. USER_DEFINED_PREFIX, TunableSystemDefinedParams.list_members())
  81. param_key = f'{prefix}{param_key}'
  82. if prefix == USER_DEFINED_PREFIX:
  83. param_info[HyperParamKey.SOURCE.value] = HyperParamSource.USER_DEFINED.value
  84. else:
  85. param_info[HyperParamKey.SOURCE.value] = HyperParamSource.SYSTEM_DEFINED.value
  86. param_keys.append(param_key)
  87. target_name = target_info[TargetKey.NAME.value]
  88. group = target_info.get(TargetKey.GROUP.value)
  89. prefix = _get_prefix(target_name, group, TargetGroup.METRIC.value,
  90. METRIC_PREFIX, SystemDefinedTargets.list_members())
  91. target_name = prefix + target_name
  92. lineage_df = lineage_table.dataframe_data
  93. try:
  94. lineage_df = lineage_df[param_keys + [target_name]]
  95. lineage_df = lineage_df.dropna(axis=0, how='any')
  96. target_column = np.array(lineage_df[target_name])
  97. if TargetKey.GOAL.value in target_info and \
  98. target_info.get(TargetKey.GOAL.value) == TargetGoal.MAXIMUM.value:
  99. target_column = -target_column
  100. return np.array(lineage_df[param_keys]), target_column
  101. except KeyError as exc:
  102. logger.warning("Some keys not exist in specified params or target. It will suggest params randomly."
  103. "Detail: %s.", str(exc))
  104. return empty_result, empty_result
  105. def _get_prefix(name, field, other_defined_field, other_defined_prefix, system_defined_fields):
  106. if (field == other_defined_field) or (field is None and name not in system_defined_fields):
  107. return other_defined_prefix
  108. return ''