예제 #1
0
    def optimize(self, max_expr_times=1):
        """Method for auto tuning."""
        target_info = self._config_info.get('target')
        params_info = self._config_info.get('parameters')
        command = self._config_info.get('command')
        tuner = self._config_info.get('tuner')
        for _ in range(max_expr_times):
            self._update_from_lineage()
            suggestion, user_defined_info = self._suggest(
                self._lineage_table, params_info, target_info, tuner)

            hyper_config = {
                'params':
                suggestion,
                'summary_dir':
                os.path.join(self._summary_base_dir,
                             f'{self._dir_prefix}_{str(uuid.uuid1())}'),
                'custom_lineage_data':
                user_defined_info
            }
            logger.info("Suggest values are: %s.", suggestion)
            os.environ[HYPER_CONFIG_ENV_NAME] = json.dumps(hyper_config)
            s = subprocess.Popen(shlex.split(command))
            s.wait()
            if s.returncode != _OK:
                logger.error(
                    "An error occurred during execution, the auto tuning will be terminated."
                )
                raise OptimizerTerminateError(
                    "An error occurred during execution, the auto tuning was terminated."
                )
예제 #2
0
    def suggest(self, params, target, params_info: dict):
        """Get suggest values."""
        bounds = []
        for param_info in params_info.values():
            bound = param_info[HyperParamKey.BOUND.value] if HyperParamKey.BOUND.value in param_info \
                else param_info['choice']
            bounds.append([min(bound), max(bound)])
        bounds = np.array(bounds)

        min_lineage_rows = 2
        if not np.array(params).any() or params.shape[0] < min_lineage_rows:
            logger.info(
                "Without valid histories or the rows of lineages < %s, "
                "parameters will be recommended randomly.", min_lineage_rows)
            suggestion = generate_arrays(params_info)
        else:
            self._gp.fit(params, target)
            suggestion = self._acq_max(gp=self._gp,
                                       y_max=target.max(),
                                       bounds=bounds,
                                       params_info=params_info)

        suggestion, user_defined_info = Transformer.transform_list_to_dict(
            params_info, suggestion)
        return suggestion, user_defined_info
예제 #3
0
    def suggest(self, params, target, params_info: dict):
        """Get suggest values."""
        bounds = []
        for param_info in params_info.values():
            bound = param_info[HyperParamKey.BOUND.value] if HyperParamKey.BOUND.value in param_info \
                else param_info['choice']
            bounds.append([min(bound), max(bound)])
        bounds = np.array(bounds)

        min_lineage_rows = 2
        if not np.array(params).any() or params.shape[0] < min_lineage_rows:
            logger.info(
                "Without valid histories or the rows of lineages < %s, "
                "parameters will be recommended randomly.", min_lineage_rows)
            suggestion = generate_arrays(params_info)
        # in scikit-learn<0.24.2, if np.std(target) include 0, errors would occur.
        elif (np.std(target, axis=0) == 0).any():
            logger.warning(
                "The std of target: %s include zero. To avoid errors, "
                "parameters will be recommended randomly.", target)
            suggestion = generate_arrays(params_info)
        else:
            self._gp.fit(params, target)
            suggestion = self._acq_max(gp=self._gp,
                                       y_max=target.max(),
                                       bounds=bounds,
                                       params_info=params_info)

        suggestion, user_defined_info = Transformer.transform_list_to_dict(
            params_info, suggestion)
        return suggestion, user_defined_info
예제 #4
0
    def _update_from_lineage(self):
        """Update lineage from lineagemgr."""
        try:
            lineage_table = get_lineage_table(
                summary_base_dir=self._summary_base_dir)
        except MindInsightException as err:
            logger.info("Can not query lineage. Detail: %s", str(err))
            lineage_table = None

        self._lineage_table = lineage_table
예제 #5
0
 def _make_summary_base_dir(self, summary_base_dir):
     """Check and make summary_base_dir."""
     if not os.path.exists(summary_base_dir):
         permissions = os.R_OK | os.W_OK | os.X_OK
         os.umask(permissions << 3 | permissions)
         mode = permissions << 6
         try:
             logger.info(
                 "The summary_base_dir is generated automatically, path is %s.",
                 summary_base_dir)
             os.makedirs(summary_base_dir, mode=mode, exist_ok=True)
         except OSError as exc:
             raise UnknownError(
                 "Can not make the summary base directory. Detail: %s." %
                 str(exc))