示例#1
0
def create_output_directory(
    scenario: Scenario,
    run_id: int,
    logger: Logger = None,
):
    """Create output directory for this run.

    Side effect: Adds the current output directory to the scenario object!

    Parameters:
    -----------

    scenario : ~smac.scenario.scenario.Scenario

    run_id : int
    """
    if scenario.output_dir:
        output_dir = os.path.join(
            scenario.output_dir,
            "run_%d" % (run_id),
        )
    else:
        return ""
    if os.path.exists(output_dir):
        move_to = output_dir + ".OLD"
        while (os.path.exists(move_to)):
            move_to += ".OLD"
        shutil.move(output_dir, move_to)
        if logger is not None:
            logger.warning(
                "Output directory \"%s\" already exists! "
                "Moving old folder to \"%s\".", output_dir, move_to)
    scenario.output_dir_for_this_run = output_dir
    return output_dir
示例#2
0
    def test_write(self):
        """ Test whether a reloaded scenario still holds all the necessary
        information. A subset of parameters might change, such as the paths to
        pcs- or instance-files, so they are checked manually. """

        def check_scen_eq(scen1, scen2):
            print('check_scen_eq')
            """ Customized check for scenario-equality, ignoring file-paths """
            for name in scen1._arguments:
                dest = scen1._arguments[name]['dest']
                name = dest if dest else name  # if 'dest' is None, use 'name'
                if name in ["pcs_fn", "train_inst_fn", "test_inst_fn",
                            "feature_fn", "output_dir"]:
                    continue  # Those values are allowed to change when writing to disk
                elif name == 'cs':
                    # Using repr because of cs-bug
                    # (https://github.com/automl/ConfigSpace/issues/25)
                    self.assertEqual(repr(scen1.cs), repr(scen2.cs))
                elif name == 'feature_dict':
                    self.assertEqual(len(scen1.feature_dict),
                                     len(scen2.feature_dict))
                    for key in scen1.feature_dict:
                        self.assertTrue((scen1.feature_dict[key] == scen2.feature_dict[key]).all())
                else:
                    print(name, getattr(scen1, name), getattr(scen2, name))
                    self.assertEqual(getattr(scen1, name),
                                     getattr(scen2, name))

        # First check with file-paths defined
        feature_filename = 'test/test_files/scenario_test/features_multiple.txt'
        feature_filename = os.path.abspath(feature_filename)
        self.test_scenario_dict['feature_file'] = feature_filename
        scenario = Scenario(self.test_scenario_dict)
        # This injection would usually happen by the facade object!
        scenario.output_dir_for_this_run = scenario.output_dir
        scenario.write()
        path = os.path.join(scenario.output_dir, 'scenario.txt')
        scenario_reloaded = Scenario(path)
        check_scen_eq(scenario, scenario_reloaded)
        # Test whether json is the default pcs_fn
        self.assertTrue(os.path.exists(os.path.join(scenario.output_dir, 'param.pcs')))
        self.assertTrue(os.path.exists(os.path.join(scenario.output_dir, 'param.json')))
        self.assertEqual(scenario_reloaded.pcs_fn, os.path.join(scenario.output_dir, 'param.json'))

        # Now create new scenario without filepaths
        self.test_scenario_dict.update({
            'paramfile': None, 'cs': scenario.cs,
            'feature_file': None, 'features': scenario.feature_dict,
            'feature_names': scenario.feature_names,
            'instance_file': None, 'instances': scenario.train_insts,
            'test_instance_file': None, 'test_instances': scenario.test_insts})
        logging.debug(scenario_reloaded)
        scenario_no_fn = Scenario(self.test_scenario_dict)
        scenario_reloaded = Scenario(path)
        check_scen_eq(scenario_no_fn, scenario_reloaded)
        # Test whether json is the default pcs_fn
        self.assertTrue(os.path.exists(os.path.join(scenario.output_dir, 'param.pcs')))
        self.assertTrue(os.path.exists(os.path.join(scenario.output_dir, 'param.json')))
        self.assertEqual(scenario_reloaded.pcs_fn, os.path.join(scenario.output_dir, 'param.json'))
 def test_write_except(self, patch_isdir, patch_mkdirs):
     patch_isdir.return_value = False
     patch_mkdirs.side_effect = OSError()
     scenario = Scenario(self.test_scenario_dict)
     # This injection would usually happen by the facade object!
     scenario.output_dir_for_this_run = scenario.output_dir
     with self.assertRaises(OSError) as cm:
         scenario.write()
示例#4
0
文件: gen_CFG.py 项目: 36000/HFSF
def main():
    cs = ConfigurationSpace()

    cell_size = CategoricalHyperparameter("cell_size", [128],
                                          default_value=128)  # kick up to 256
    n_cell = CategoricalHyperparameter("n_cell", [2], default_value=2)
    dropout = CategoricalHyperparameter("dropout", [0.5], default_value=0.5)

    activation = CategoricalHyperparameter("activation", ['sigmoid'],
                                           default_value='sigmoid')
    optimizer = CategoricalHyperparameter("optimizer", ['adam'],
                                          default_value='adam')
    optimizer_lr = CategoricalHyperparameter(
        "optimizer_lr", [.001, .003, .006, .01, 0.03, 0.1], default_value=.01)
    learning_decay_rate = UniformFloatHyperparameter("learning_decay_rate",
                                                     0,
                                                     0.9,
                                                     default_value=.6)

    nn_type = CategoricalHyperparameter("nn_type", ['RNN', 'LSTM', 'GRU'],
                                        default_value='LSTM')

    epochs = CategoricalHyperparameter("epochs", [10], default_value=10)

    cs.add_hyperparameters([
        cell_size, n_cell, dropout, nn_type, activation, optimizer,
        optimizer_lr, learning_decay_rate, epochs
    ])

    scenario = Scenario({
        "run_obj": "quality",
        "runcount-limit": 32,
        "cs": cs,
        "deterministic": "true"
    })
    scenario.output_dir_for_this_run = "C:\\NNwork\\HFSF\\SMAC3out"
    scenario.output_dir = "C:\\NNwork\\HFSF\\SMAC3out"
    smac = SMAC(scenario=scenario,
                rng=np.random.RandomState(23),
                tae_runner=rnn_from_cfg)

    best_model = smac.optimize()
    print_incumb(best_model)
    np.save("C:\\NNwork\\HFSF\\SMAC3out\\best.cfg", best_model)
示例#5
0
    def hpbandster2smac(self, folder2result, cs_options, output_dir: str):
        """Reading hpbandster-result-object and creating RunHistory and trajectory...  treats each budget as an
        individual 'smac'-run, creates an output-directory with subdirectories for each budget.

        Parameters
        ----------
        folder2result: Dict(str : hpbandster.core.result.Result)
            folder mapping to bohb's result-objects
        cs_options: list[ConfigurationSpace]
            the configuration spaces. in the best case it's a single element, but for pcs-format we need to guess
            through a list of possible configspaces
        output_dir: str
            the output-dir to save the smac-runs to
        
        Returns
        -------
        folder2budgets: dict(dict(str) - str)
            maps each folder (from parallel execution) to a dict, which in turn maps all budgets of
            the specific parallel execution to their paths
        """
        folder2budgets = OrderedDict()
        self.logger.debug("Loading with %d configspace alternative options...",
                          len(cs_options))
        self.logger.info(
            "Assuming BOHB treats target algorithms as deterministic (and does not re-evaluate)"
        )
        for folder, result in folder2result.items():
            folder2budgets[folder] = OrderedDict()
            self.logger.debug("Budgets for '%s': %s" %
                              (folder, str(result.HB_config['budgets'])))
            ##########################
            # 1. Create runhistory   #
            ##########################
            id2config_mapping = result.get_id2config_mapping()
            skipped = {'None': 0, 'NaN': 0}
            budget2rh = OrderedDict()
            for run in result.get_all_runs():
                # Choose runhistory to add run to
                if not run.budget in budget2rh:
                    budget2rh[run.budget] = RunHistory(average_cost)
                rh = budget2rh[run.budget]

                # Load config...
                config = None
                while config is None:
                    if len(cs_options) == 0:
                        self.logger.debug("None of the alternatives worked...")
                        raise ValueError(
                            "Your configspace seems to be corrupt. If you use floats (or mix up ints, bools and strings) as categoricals, "
                            "please consider using the .json-format, as the .pcs-format cannot recover the type "
                            "of categoricals. Otherwise please report this to "
                            "https://github.com/automl/CAVE/issues (and attach the debug.log)"
                        )
                    try:
                        config = self._get_config(run.config_id,
                                                  id2config_mapping,
                                                  cs_options[0])
                    except ValueError as err:
                        self.logger.debug(
                            "Loading configuration failed... trying %d alternatives"
                            % len(cs_options) - 1,
                            exc_info=1)
                        cs_options = cs_options[
                            1:]  # remove the failing cs-version

                # Filter corrupted loss-values (ignore them)
                if run.loss is None:
                    skipped['None'] += 1
                    continue
                if np.isnan(run.loss):
                    skipped['NaN'] += 1
                    continue

                rh.add(config=config,
                       cost=run.loss,
                       time=run.time_stamps['finished'] -
                       run.time_stamps['started'],
                       status=StatusType.SUCCESS,
                       seed=0,
                       additional_info={
                           'info': run.info,
                           'timestamps': run.time_stamps
                       })

            self.logger.debug(
                "Skipped %d None- and %d NaN-loss-values in BOHB-result",
                skipped['None'], skipped['NaN'])

            ##########################
            # 2. Create all else     #
            ##########################
            formatted_budgets = format_budgets(
                budget2rh.keys()
            )  # Make budget-names readable [0.021311, 0.031211] to [0.02, 0.03]
            for b, rh in budget2rh.items():
                output_path = os.path.join(output_dir, folder,
                                           formatted_budgets[b])
                folder2budgets[folder][b] = output_path

                scenario = Scenario({
                    'run_obj': 'quality',
                    'cs': cs_options[0],
                    'output_dir': output_dir,
                    'deterministic':
                    True,  # At the time of writing, BOHB is always treating ta's as deterministic
                })
                scenario.output_dir_for_this_run = output_path
                scenario.write()

                with open(os.path.join(output_path, 'configspace.json'),
                          'w') as fh:
                    fh.write(pcs_json.write(cs_options[0]))

                rh.save_json(fn=os.path.join(output_path, 'runhistory.json'))

                self.get_trajectory(folder2result[folder],
                                    output_path,
                                    scenario,
                                    rh,
                                    budget=b)

        return folder2budgets
示例#6
0
    def main_cli(self, commandline_arguments: typing.List[str] = None):
        """Main function of SMAC for CLI interface"""
        self.logger.info("SMAC call: %s" % (" ".join(sys.argv)))

        cmd_reader = CMDReader()
        kwargs = {}
        if commandline_arguments:
            kwargs['commandline_arguments'] = commandline_arguments
        main_args_, smac_args_, scen_args_ = cmd_reader.read_cmd(**kwargs)

        root_logger = logging.getLogger()
        root_logger.setLevel(main_args_.verbose_level)
        logger_handler = logging.StreamHandler(stream=sys.stdout)
        if root_logger.level >= logging.INFO:
            formatter = logging.Formatter("%(levelname)s:\t%(message)s")
        else:
            formatter = logging.Formatter(
                "%(asctime)s:%(levelname)s:%(name)s:%(message)s",
                "%Y-%m-%d %H:%M:%S")
        logger_handler.setFormatter(formatter)
        root_logger.addHandler(logger_handler)
        # remove default handler
        if len(root_logger.handlers) > 1:
            root_logger.removeHandler(root_logger.handlers[0])

        # Create defaults
        rh = None
        initial_configs = None
        stats = None
        incumbent = None

        # Create scenario-object
        scenario = {}
        scenario.update(vars(smac_args_))
        scenario.update(vars(scen_args_))
        scen = Scenario(scenario=scenario)

        # Restore state
        if main_args_.restore_state:
            root_logger.debug("Restoring state from %s...",
                              main_args_.restore_state)
            rh, stats, traj_list_aclib, traj_list_old = self.restore_state(
                scen, main_args_)

            scen.output_dir_for_this_run = create_output_directory(
                scen,
                main_args_.seed,
                root_logger,
            )
            scen.write()
            incumbent = self.restore_state_after_output_dir(
                scen, stats, traj_list_aclib, traj_list_old)

        if main_args_.warmstart_runhistory:
            aggregate_func = average_cost
            rh = RunHistory(aggregate_func=aggregate_func)

            scen, rh = merge_foreign_data_from_file(
                scenario=scen,
                runhistory=rh,
                in_scenario_fn_list=main_args_.warmstart_scenario,
                in_runhistory_fn_list=main_args_.warmstart_runhistory,
                cs=scen.cs,
                aggregate_func=aggregate_func)

        if main_args_.warmstart_incumbent:
            initial_configs = [scen.cs.get_default_configuration()]
            for traj_fn in main_args_.warmstart_incumbent:
                trajectory = TrajLogger.read_traj_aclib_format(fn=traj_fn,
                                                               cs=scen.cs)
                initial_configs.append(trajectory[-1]["incumbent"])

        if main_args_.mode == "SMAC":
            optimizer = SMAC(scenario=scen,
                             rng=np.random.RandomState(main_args_.seed),
                             runhistory=rh,
                             initial_configurations=initial_configs,
                             stats=stats,
                             restore_incumbent=incumbent,
                             run_id=main_args_.seed)
        elif main_args_.mode == "BORF":
            optimizer = BORF(scenario=scen,
                             rng=np.random.RandomState(main_args_.seed),
                             runhistory=rh,
                             initial_configurations=initial_configs,
                             stats=stats,
                             restore_incumbent=incumbent,
                             run_id=main_args_.seed)
        elif main_args_.mode == "BOGP":
            optimizer = BOGP(scenario=scen,
                             rng=np.random.RandomState(main_args_.seed),
                             runhistory=rh,
                             initial_configurations=initial_configs,
                             stats=stats,
                             restore_incumbent=incumbent,
                             run_id=main_args_.seed)
        elif main_args_.mode == "ROAR":
            optimizer = ROAR(scenario=scen,
                             rng=np.random.RandomState(main_args_.seed),
                             runhistory=rh,
                             initial_configurations=initial_configs,
                             run_id=main_args_.seed)
        elif main_args_.mode == "EPILS":
            optimizer = EPILS(scenario=scen,
                              rng=np.random.RandomState(main_args_.seed),
                              runhistory=rh,
                              initial_configurations=initial_configs,
                              run_id=main_args_.seed)
        elif main_args_.mode == "Hydra":
            optimizer = Hydra(
                scenario=scen,
                rng=np.random.RandomState(main_args_.seed),
                runhistory=rh,
                initial_configurations=initial_configs,
                stats=stats,
                restore_incumbent=incumbent,
                run_id=main_args_.seed,
                random_configuration_chooser=main_args_.
                random_configuration_chooser,
                n_iterations=main_args_.hydra_iterations,
                val_set=main_args_.hydra_validation,
                incs_per_round=main_args_.hydra_incumbents_per_round,
                n_optimizers=main_args_.hydra_n_optimizers)
        elif main_args_.mode == "PSMAC":
            optimizer = PSMAC(
                scenario=scen,
                rng=np.random.RandomState(main_args_.seed),
                run_id=main_args_.seed,
                shared_model=smac_args_.shared_model,
                validate=main_args_.psmac_validate,
                n_optimizers=main_args_.hydra_n_optimizers,
                n_incs=main_args_.hydra_incumbents_per_round,
            )
        try:
            optimizer.optimize()
        except (TAEAbortException, FirstRunCrashedException) as err:
            self.logger.error(err)
示例#7
0
    def hpbandster2smac(self, result, cs: ConfigurationSpace, output_dir: str):
        """Reading hpbandster-result-object and creating RunHistory and
        trajectory...
        treats each budget as an individual 'smac'-run, creates an
        output-directory with subdirectories for each budget.

        Parameters
        ----------
        result: hpbandster.core.result.Result
            bohb's result-object
        cs: ConfigurationSpace
            the configuration space
        output_dir: str
            the output-dir to save the smac-runs to
        """
        # Create runhistories (one per budget)
        id2config_mapping = result.get_id2config_mapping()
        budget2rh = {}
        for run in result.get_all_runs():
            if not run.budget in budget2rh:
                budget2rh[run.budget] = RunHistory(average_cost)
            rh = budget2rh[run.budget]
            rh.add(config=Configuration(
                cs, id2config_mapping[run.config_id]['config']),
                   cost=run.loss,
                   time=run.time_stamps['finished'] -
                   run.time_stamps['started'],
                   status=StatusType.SUCCESS,
                   seed=0,
                   additional_info={'info': run.info})

        # Write to disk
        budget2path = {}  # paths to individual budgets
        for b, rh in budget2rh.items():
            output_path = os.path.join(output_dir, 'budget_' + str(b))
            budget2path[b] = output_path

            scenario = Scenario({'run_obj': 'quality', 'cs': cs})
            scenario.output_dir_for_this_run = output_path
            scenario.write()
            rh.save_json(fn=os.path.join(output_path, 'runhistory.json'))

            # trajectory
            traj_dict = result.get_incumbent_trajectory()
            traj_logger = TrajLogger(output_path, Stats(scenario))
            for config_id, time, budget, loss in zip(
                    traj_dict['config_ids'], traj_dict['times_finished'],
                    traj_dict['budgets'], traj_dict['losses']):
                incumbent = Configuration(
                    cs, id2config_mapping[config_id]['config'])
                try:
                    incumbent_id = rh.config_ids[incumbent]
                except KeyError as e:
                    # This config was not evaluated on this budget, just skip it
                    continue
                except:
                    raise
                ta_runs = -1
                ta_time_used = -1
                wallclock_time = time
                train_perf = loss
                # add
                traj_logger._add_in_old_format(train_perf, incumbent_id,
                                               incumbent, ta_time_used,
                                               wallclock_time)
                traj_logger._add_in_aclib_format(train_perf, incumbent_id,
                                                 incumbent, ta_time_used,
                                                 wallclock_time)

        return budget2path
示例#8
0
    def hpbandster2smac(self, folder, result, cs_options, output_dir: str):
        """Reading hpbandster-result-object and creating RunHistory and trajectory...

        Parameters
        ----------
        folder: str (path)
            original folder
        result: hpbandster.core.result.Result
            bohb's result-object
        cs_options: list[ConfigurationSpace]
            the configuration spaces. in the best case it's a single element, but for pcs-format we need to guess
            through a list of possible configspaces
        output_dir_base: str
            the output-dir to save the smac-runs to
        
        Returns
        -------
        converted: dict{
                'new_path' : path_to_converted_input,
                'hp_bandster_result' : result_in_hpbandster_format,
                'config_space' : config_space,
                'runhistory' : runhistory,
                'validated_runhistory' : validated_runhistory,
                'scenario' : scenario,
                'trajectory' : trajectory,
                }

        """
        self.logger.debug("Budgets for '%s': %s" %
                          (folder, str(result.HB_config['budgets'])))
        ##########################
        # 1. Create runhistory   #
        ##########################
        id2config_mapping = result.get_id2config_mapping()
        skipped = {'None': 0, 'NaN': 0}
        rh = RunHistory()
        for run in result.get_all_runs():
            # Load config...
            config = None
            while config is None:
                if len(cs_options) == 0:
                    self.logger.debug("None of the alternatives worked...")
                    raise ValueError(
                        "Your configspace seems to be corrupt. If you use floats (or mix up ints, bools "
                        "and strings) as categoricals, please consider using the .json-format, as the "
                        ".pcs-format cannot recover the type of categoricals. Otherwise please report "
                        "this to https://github.com/automl/CAVE/issues (and attach the debug.log)"
                    )
                try:
                    config = self._get_config(run.config_id, id2config_mapping,
                                              cs_options[0])
                except ValueError as err:
                    self.logger.debug(
                        "Loading config failed. Trying %d alternatives" %
                        len(cs_options) - 1,
                        exc_info=1)
                    cs_options = cs_options[
                        1:]  # remove the failing cs-version

            # Filter corrupted loss-values (ignore them)
            if run.loss is None:
                skipped['None'] += 1
                continue
            if np.isnan(run.loss):
                skipped['NaN'] += 1
                continue

            rh.add(config=config,
                   cost=run.loss,
                   time=run.time_stamps['finished'] -
                   run.time_stamps['started'],
                   status=StatusType.SUCCESS,
                   budget=run.budget,
                   seed=0,
                   additional_info={
                       'info': run.info,
                       'timestamps': run.time_stamps
                   })

        self.logger.debug(
            "Skipped %d None- and %d NaN-loss-values in BOHB-result",
            skipped['None'], skipped['NaN'])

        ##########################
        # 2. Create all else     #
        ##########################
        scenario = Scenario({
            'run_obj': 'quality',
            'cs': cs_options[0],
            'output_dir': output_dir,
            'deterministic':
            True,  # At the time of writing, BOHB is always treating ta's as deterministic
        })
        scenario.output_dir_for_this_run = output_dir
        scenario.write()

        with open(os.path.join(output_dir, 'configspace.json'), 'w') as fh:
            fh.write(pcs_json.write(cs_options[0]))

        rh.save_json(fn=os.path.join(output_dir, 'runhistory.json'))

        trajectory = self.get_trajectory(result, output_dir, scenario, rh)

        return {
            'new_path': output_dir,
            'hpbandster_result': result,
            'config_space': cs_options[0],
            'runhistory': rh,
            'validated_runhistory': None,
            'scenario': scenario,
            'trajectory': trajectory,
        }
示例#9
0
    def hpbandster2smac(self, folder2result, cs: ConfigurationSpace, backup_cs,
                        output_dir: str):
        """Reading hpbandster-result-object and creating RunHistory and trajectory...
        treats each budget as an individual 'smac'-run, creates an
        output-directory with subdirectories for each budget.

        Parameters
        ----------
        folder2result: Dict(str : hpbandster.core.result.Result)
            folder mapping to bohb's result-objects
        cs: ConfigurationSpace
            the configuration space
        backup_cs: List[ConfigurationSpace]
            if loading a configuration fails, try configspaces from this list until succeed
        output_dir: str
            the output-dir to save the smac-runs to
        """
        # Create runhistories (one per budget)
        budget2rh = OrderedDict()
        for folder, result in folder2result.items():
            self.logger.debug("Budgets for '%s': %s" %
                              (folder, str(result.HB_config['budgets'])))
            id2config_mapping = result.get_id2config_mapping()
            skipped = {'None': 0, 'NaN': 0}
            for run in result.get_all_runs():
                if not run.budget in budget2rh:
                    budget2rh[run.budget] = RunHistory(average_cost)
                rh = budget2rh[run.budget]

                # Load config...
                try:
                    config = self._get_config(run.config_id, id2config_mapping,
                                              cs)
                except ValueError as err:
                    self.logger.debug(
                        "Loading configuration failed... trying alternatives",
                        exc_info=1)
                    for bcs in backup_cs:
                        try:
                            config = self._get_config(run.config_id,
                                                      id2config_mapping, bcs)
                            cs = bcs
                            break
                        except ValueError:
                            self.logger.debug("", exc_info=1)
                            pass
                    else:
                        self.logger.debug("None of the alternatives worked...")
                        raise ValueError(
                            "Your configspace seems to be corrupt. If you use floats (or mix up ints, bools and strings) as categoricals, "
                            "please consider using the .json-format, as the .pcs-format cannot recover the type "
                            "of categoricals. Otherwise please report this to "
                            "https://github.com/automl/CAVE/issues (and attach the debug.log)"
                        )

                if run.loss is None:
                    skipped['None'] += 1
                    continue
                if np.isnan(run.loss):
                    skipped['NaN'] += 1
                    continue

                rh.add(config=config,
                       cost=run.loss,
                       time=run.time_stamps['finished'] -
                       run.time_stamps['started'],
                       status=StatusType.SUCCESS,
                       seed=0,
                       additional_info={
                           'info': run.info,
                           'timestamps': run.time_stamps
                       })

            self.logger.debug(
                "Skipped %d None- and %d NaN-loss-values in BOHB-result",
                skipped['None'], skipped['NaN'])

        # Write to disk
        budget2path = OrderedDict()  # paths to individual budgets
        self.logger.info(
            "Assuming BOHB treats target algorithms as deterministic (and does not re-evaluate)"
        )
        formatted_budgets = format_budgets(budget2rh.keys())
        for b, rh in budget2rh.items():
            output_path = os.path.join(output_dir, formatted_budgets[b])
            budget2path[b] = output_path

            scenario = Scenario({
                'run_obj': 'quality',
                'cs': cs,
                'output_dir': output_dir,
                'deterministic':
                True,  # At the time of writing, BOHB is always treating ta's as deterministic
            })
            scenario.output_dir_for_this_run = output_path
            scenario.write()

            with open(os.path.join(output_path, 'configspace.json'),
                      'w') as fh:
                fh.write(pcs_json.write(cs))

            rh.save_json(fn=os.path.join(output_path, 'runhistory.json'))
            self.get_trajectory(folder2result,
                                output_path,
                                scenario,
                                rh,
                                budget=b)

        return budget2path
示例#10
0
                                            default_value=2)

optimizer = CategoricalHyperparameter("optimizer",
                                      ['adam', 'sgd', 'nadam', 'RMSprop'],
                                      default_value='RMSprop')
optimizer_lr = CategoricalHyperparameter("optimizer_lr",
                                         [.0001, .0003, .001, .003, .01],
                                         default_value=.0003)
learning_decay_rate = UniformFloatHyperparameter("learning_decay_rate",
                                                 0,
                                                 0.9,
                                                 default_value=.6)

cs.add_hyperparameters([
    first_kernel_size, conv_filters, n_conv, dropout, activation, dense_width,
    dense_length, optimizer, optimizer_lr, learning_decay_rate
])

scenario = Scenario({
    "run_obj": "quality",
    "runcount-limit": 128,
    "cs": cs,
    "deterministic": "true"
})
scenario.output_dir_for_this_run = "C:\\NNwork\\SMAC3out"
scenario.output_dir = "C:\\NNwork\\SMAC3out"
smac = SMAC(scenario=scenario,
            rng=np.random.RandomState(23),
            tae_runner=cnn_from_cfg)

print_incumb(smac.optimize())
示例#11
0
    def main_cli(self):
        """Main function of SMAC for CLI interface"""
        self.logger.info("SMAC call: %s" % (" ".join(sys.argv)))

        cmd_reader = CMDReader()
        args_, misc_args = cmd_reader.read_cmd()

        root_logger = logging.getLogger()
        root_logger.setLevel(args_.verbose_level)
        logger_handler = logging.StreamHandler(stream=sys.stdout)
        if root_logger.level >= logging.INFO:
            formatter = logging.Formatter("%(levelname)s:\t%(message)s")
        else:
            formatter = logging.Formatter(
                "%(asctime)s:%(levelname)s:%(name)s:%(message)s",
                "%Y-%m-%d %H:%M:%S")
        logger_handler.setFormatter(formatter)
        root_logger.addHandler(logger_handler)
        # remove default handler
        root_logger.removeHandler(root_logger.handlers[0])

        # Create defaults
        rh = None
        initial_configs = None
        stats = None
        incumbent = None

        # Restore state (needs to be before scenario-creation!)
        if args_.restore_state:
            root_logger.debug("Restoring state from %s...",
                              args_.restore_state)
            rh, stats, traj_list_aclib, traj_list_old = self.restore_state_before_scen(
                args_)

        # Create scenario-object
        scen = Scenario(args_.scenario_file, misc_args)

        # Restore state (continued, needs to be after scenario-creation!)
        if args_.restore_state:
            scen.output_dir_for_this_run = create_output_directory(
                scen,
                args_.seed,
                root_logger,
            )
            scen.write()
            stats, incumbent = self.restore_state_after_scen(
                scen, stats, traj_list_aclib, traj_list_old)

        if args_.warmstart_runhistory:
            aggregate_func = average_cost
            rh = RunHistory(aggregate_func=aggregate_func)

            scen, rh = merge_foreign_data_from_file(
                scenario=scen,
                runhistory=rh,
                in_scenario_fn_list=args_.warmstart_scenario,
                in_runhistory_fn_list=args_.warmstart_runhistory,
                cs=scen.cs,
                aggregate_func=aggregate_func)

        if args_.warmstart_incumbent:
            initial_configs = [scen.cs.get_default_configuration()]
            for traj_fn in args_.warmstart_incumbent:
                trajectory = TrajLogger.read_traj_aclib_format(fn=traj_fn,
                                                               cs=scen.cs)
                initial_configs.append(trajectory[-1]["incumbent"])

        if args_.mode == "SMAC":
            optimizer = SMAC(scenario=scen,
                             rng=np.random.RandomState(args_.seed),
                             runhistory=rh,
                             initial_configurations=initial_configs,
                             stats=stats,
                             restore_incumbent=incumbent,
                             run_id=args_.seed)
        elif args_.mode == "ROAR":
            optimizer = ROAR(scenario=scen,
                             rng=np.random.RandomState(args_.seed),
                             runhistory=rh,
                             initial_configurations=initial_configs,
                             run_id=args_.seed)
        elif args_.mode == "EPILS":
            optimizer = EPILS(scenario=scen,
                              rng=np.random.RandomState(args_.seed),
                              runhistory=rh,
                              initial_configurations=initial_configs,
                              run_id=args_.seed)
        try:
            optimizer.optimize()
        except (TAEAbortException, FirstRunCrashedException) as err:
            self.logger.error(err)