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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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
  18. from mindinsight.optimizer.common.enums import HyperParamKey, HyperParamType
  19. from mindinsight.optimizer.common.log import logger
  20. def generate_param(param_info, n=1):
  21. """Generate param."""
  22. value = None
  23. if HyperParamKey.BOUND.value in param_info:
  24. bound = param_info[HyperParamKey.BOUND.value]
  25. value = np.random.uniform(bound[0], bound[1], n)
  26. if param_info[HyperParamKey.TYPE.value] == HyperParamType.INT.value:
  27. value = value.astype(HyperParamType.INT.value)
  28. if HyperParamKey.CHOICE.value in param_info:
  29. indexes = np.random.randint(0, len(param_info[HyperParamKey.CHOICE.value]), n)
  30. value = [param_info[HyperParamKey.CHOICE.value][index] for index in indexes]
  31. if HyperParamKey.DECIMAL.value in param_info:
  32. value = np.around(value, decimals=param_info[HyperParamKey.DECIMAL.value])
  33. return np.array(value)
  34. def generate_arrays(params_info: dict, n=1):
  35. """Generate arrays."""
  36. suggest_params = None
  37. for _, param_info in params_info.items():
  38. suggest_param = generate_param(param_info, n).reshape((-1, 1))
  39. if suggest_params is None:
  40. suggest_params = suggest_param
  41. else:
  42. suggest_params = np.hstack((suggest_params, suggest_param))
  43. if n == 1:
  44. return suggest_params[0]
  45. return suggest_params
  46. def match_value_type(array, params_info: dict):
  47. """Make array match params type."""
  48. array_new = []
  49. index = 0
  50. for _, param_info in params_info.items():
  51. param_type = param_info[HyperParamKey.TYPE.value]
  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_type == 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_name):
  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 = list(params_info.keys())
  74. lineage_df = lineage_table.dataframe_data
  75. try:
  76. lineage_df = lineage_df[param_keys + [target_name]]
  77. lineage_df = lineage_df.dropna(axis=0, how='any')
  78. return lineage_df[param_keys], lineage_df[target_name]
  79. except KeyError as exc:
  80. logger.warning("Some keys not exist in specified params or target. It will suggest params randomly."
  81. "Detail: %s.", str(exc))
  82. return empty_result, empty_result