コード例 #1
0
    def create_configuration(self, values: Union[None,  Dict[str, Union[str, float, int]]] = None,
                             vector: Union[None, np.ndarray] = None) -> Configuration:
        """
        Method that creates new BRISE Configuration using ConfigSpace notions of `vector` and `values`.
        If both, `vector` and `values` are provided - create from `vector`, ignore `values`.
        :param values: dict. Optional parameter. Should be set to get Configuration from pre-defined values
        :param vector: np.ndarray. Optional parameter. Should be set to get Configuration from set vector
        :return: sampled Configuration
        """
        try:
            if vector is not None:
                cs_vector = [None]*len(self.get_hyperparameter_names())
                for cs_idx, cs_param in enumerate(self._config_space.get_hyperparameter_names()):
                    for idx, param in enumerate(self.get_hyperparameter_names()):
                        if cs_param == param:
                            cs_vector[cs_idx] = vector[idx]
                cs_configuration = CSConfiguration(self._config_space, vector=cs_vector)
            elif values is not None:
                cs_configuration = CSConfiguration(self._config_space, values=values)
            else:
                raise TypeError("A new Configuration creation requires either parameter 'vector' or parameter 'values'.")
        except ForbiddenValueError as error:
            self.logger.error(f"Tried to sample forbidden configuration: {error}")
            raise error

        # todo: its a work-around similar we had in original hh
        if cs_configuration.get("low level heuristic", "") == "jMetalPy.EvolutionStrategy":
            if cs_configuration['py.ES|lambda_'] < cs_configuration['py.ES|mu']:
                self.logger.warning(f"Hyperparameter 'lambda_' was altered from {cs_configuration['py.ES|lambda_']} to"
                                        f" {cs_configuration['py.ES|mu']}!")
                cs_configuration['py.ES|lambda_'] = cs_configuration['py.ES|mu']
        parameters = []
        for parameter_name in self.__hyperparameter_names:
            parameters.append(cs_configuration.get(parameter_name))
        return Configuration(parameters, Configuration.Type.FROM_SELECTOR)
コード例 #2
0
 def alternative_configuration_recovery(config_list: typing.List[str], cs: ConfigurationSpace):
     """ Used to recover ints and bools as categoricals or constants from trajectory """
     config_dict = {}
     for param in config_list:
         k,v = param.split("=")
         v = v.strip("'")
         hp = cs.get_hyperparameter(k)
         if isinstance(hp, FloatHyperparameter):
             v = float(v)
         elif isinstance(hp, IntegerHyperparameter):
             v = int(v)
         ################# DIFFERENCE: ################
         elif isinstance(hp, CategoricalHyperparameter) or isinstance(hp, Constant):
             if isinstance(hp.default_value, bool):
                 v = True if v == 'True' else False
             elif isinstance(hp.default_value, int):
                 v = int(v)
             elif isinstance(hp.default_value, float):
                 v = float(v)
             else:
                 v = v
         ##############################################
         config_dict[k] = v
     config = Configuration(configuration_space=cs, values=config_dict)
     config.origin = "External Trajectory"
     return config
コード例 #3
0
ファイル: statistics.py プロジェクト: jtuyls/pc_smac
    def _convert_dict_to_config(self, config_dict, config_space):
        # Method come from SMAC3

        config = Configuration(configuration_space=config_space,
                               values=config_dict)
        config.origin = "External Trajectory"
        return config
コード例 #4
0
ファイル: autofolio.py プロジェクト: bmmalone/AutoFolio
    def fit_selector(self, scenario: ASlibScenario, config: Configuration):
        '''
            fits an algorithm selector for a given scenario wrt a given configuration

            Arguments
            ---------
            scenario: aslib_scenario.aslib_scenario.ASlibScenario
                aslib scenario at hand
            config: Configuration
                parameter configuration
        '''

        if config.get("selector") == "PairwiseClassifier":

            clf_class = None
            if config.get("classifier") == "RandomForest":
                clf_class = RandomForest
            if config.get("classifier") == "XGBoost":
                clf_class = XGBoost

            selector = PairwiseClassifier(classifier_class=clf_class)
            selector.fit(scenario=scenario, config=config)

        if config.get("selector") == "PairwiseRegressor":
            reg_class = None
            if config.get("regressor") == "RandomForestRegressor":
                reg_class = RandomForestRegressor
                
            selector = PairwiseRegression(regressor_class=reg_class)
            selector.fit(scenario=scenario, config=config)

        return selector
コード例 #5
0
    def _convert_dict_to_config(config_list: typing.List[str], cs: ConfigurationSpace):
        # CAN BE DONE IN CONFIGSPACE
        """Since we save a configurations in a dictionary str->str we have to
        try to figure out the type (int, float, str) of each parameter value

        Parameters
        ----------
        config_list: typing.List[str]
            Configuration as a list of "str='str'"
        cs: ConfigurationSpace
            Configuration Space to translate dict object into Confiuration object
        """
        config_dict = {}
        for param in config_list:
            k, v = param.split("=")
            v = v.strip("'")
            hp = cs.get_hyperparameter(k)
            if isinstance(hp, FloatHyperparameter):
                v = float(v)
            elif isinstance(hp, IntegerHyperparameter):
                v = int(v)
            config_dict[k] = v

        config = Configuration(configuration_space=cs, values=config_dict)
        config.origin = "External Trajectory"

        return config
コード例 #6
0
ファイル: hpbandster2smac.py プロジェクト: ywuywu/ml_monorepo
 def _get_config(self, config_id, id2config, cs):
     config = Configuration(cs, id2config[config_id]['config'])
     try:
         model_based_pick = id2config[config_id]['config_info']['model_based_pick']
         config.origin = 'Model based pick' if model_based_pick else 'Random'
     except KeyError:
         self.logger.debug("No origin for config!", exc_info=True)
     return config
コード例 #7
0
ファイル: traj_logging.py プロジェクト: schrodinger/SMAC3
    def _convert_dict_to_config(config_list: typing.List[str], cs: ConfigurationSpace) -> Configuration:
        """Since we save a configurations in a dictionary str->str we have to
        try to figure out the type (int, float, str) of each parameter value

        Parameters
        ----------
        config_list: typing.List[str]
            Configuration as a list of "str='str'"
        cs: ConfigurationSpace
            Configuration Space to translate dict object into Confiuration object
        """
        config_dict = {}
        v = ''  # type: typing.Union[str, float, int, bool]
        for param in config_list:
            k, v = param.split("=")
            v = v.strip("'")
            hp = cs.get_hyperparameter(k)
            if isinstance(hp, FloatHyperparameter):
                v = float(v)
            elif isinstance(hp, IntegerHyperparameter):
                v = int(v)
            elif isinstance(hp, (CategoricalHyperparameter, Constant)):
                # Checking for the correct type requires jumping some hoops
                # First, we gather possible interpretations of our string
                interpretations = [v]  # type: typing.List[typing.Union[str, bool, int, float]]
                if v in ["True", "False"]:
                    # Special Case for booleans (assuming we support them)
                    # This is important to avoid false positive warnings triggered by 1 == True or "False" == True
                    interpretations.append(True if v == 'True' else False)
                else:
                    for t in [int, float]:
                        try:
                            interpretations.append(t(v))
                        except ValueError:
                            continue

                # Second, check if it's in the choices / the correct type.
                legal = {interpretation for interpretation in interpretations if hp.is_legal(interpretation)}

                # Third, issue warnings if the interpretation is ambigious
                if len(legal) != 1:
                    logging.getLogger("smac.trajlogger").warning(
                        "Ambigous or no interpretation of value {} for hp {} found ({} possible interpretations). "
                        "Passing string, but this will likely result in an error".format(v, hp.name, len(legal)))
                else:
                    v = legal.pop()

            config_dict[k] = v

        config = Configuration(configuration_space=cs, values=config_dict)
        config.origin = "External Trajectory"

        return config
コード例 #8
0
    def _overwrite_configuration(self, config: Configuration,
                                 overwrite_args: list):
        '''
            overwrites a given configuration with some new settings

            Arguments
            ---------
            config: Configuration
                initial configuration to be adapted
            overwrite_args: list
                new parameter settings as a list of strings

            Returns
            -------
            Configuration
        '''
        def pairwise(iterable):
            a, b = tee(iterable)
            next(b, None)
            return zip(a, b)

        dict_conf = config.get_dictionary()
        for param, value in pairwise(overwrite_args):
            try:
                ok = self.cs.get_hyperparameter(param)
            except KeyError:
                ok = None
            if ok is not None:
                if type(self.cs.get_hyperparameter(
                        param)) is UniformIntegerHyperparameter:
                    dict_conf[param] = int(value)
                elif type(self.cs.get_hyperparameter(
                        param)) is UniformFloatHyperparameter:
                    dict_conf[param] = float(value)
                elif value == "True":
                    dict_conf[param] = True
                elif value == "False":
                    dict_conf[param] = False
                else:
                    dict_conf[param] = value
            else:
                self.logger.warn("Unknown given parameter: %s %s" %
                                 (param, value))
        config = Configuration(self.cs,
                               values=dict_conf,
                               allow_inactive_with_values=True)

        return config
コード例 #9
0
ファイル: utils.py プロジェクト: automl/Auto-PyTorch
def read_return_initial_configurations(
        config_space: ConfigurationSpace,
        portfolio_selection: str) -> List[Configuration]:

    # read and validate initial configurations
    portfolio_path = portfolio_selection if portfolio_selection != "greedy" else \
        os.path.join(os.path.dirname(__file__), '../configs/greedy_portfolio.json')
    try:
        initial_configurations_dict: List[Dict[str, Any]] = json.load(
            open(portfolio_path))
    except FileNotFoundError:
        raise FileNotFoundError(
            "The path: {} provided for 'portfolio_selection' for "
            "the file containing the portfolio configurations "
            "does not exist. Please provide a valid path".format(
                portfolio_path))
    initial_configurations: List[Configuration] = list()
    for configuration_dict in initial_configurations_dict:
        try:
            configuration = Configuration(config_space, configuration_dict)
            initial_configurations.append(configuration)
        except Exception as e:
            warnings.warn(
                f"Failed to convert {configuration_dict} into"
                f" a Configuration with error {e}. "
                f"Therefore, it can't be used as an initial "
                f"configuration as it does not match the current config space. "
            )
    return initial_configurations
コード例 #10
0
ファイル: traj_logging.py プロジェクト: schrodinger/SMAC3
    def _add_in_alljson_format(self, train_perf: float, incumbent_id: int,
                               incumbent: Configuration, budget: float,
                               ta_time_used: float,
                               wallclock_time: float) -> None:
        """Adds entries to AClib2-like (but with configs as json) trajectory file

        Parameters
        ----------
        train_perf: float
            Estimated performance on training (sub)set
        incumbent_id: int
            Id of incumbent
        incumbent: Configuration()
            Current incumbent configuration
        budget: float
            budget (cutoff) used in intensifier to limit TA (default: 0)
        ta_time_used: float
            CPU time used by the target algorithm
        wallclock_time: float
            Wallclock time used so far
        """
        traj_entry = {"cpu_time": ta_time_used,
                      "wallclock_time": wallclock_time,
                      "evaluations": self.stats.ta_runs,
                      "cost": train_perf,
                      "incumbent": incumbent.get_dictionary(),
                      "budget": budget,
                      "origin": incumbent.origin,
                      }

        with open(self.alljson_traj_fn, "a") as fp:
            json.dump(traj_entry, fp)
            fp.write("\n")
コード例 #11
0
ファイル: traj_logging.py プロジェクト: schrodinger/SMAC3
    def read_traj_alljson_format(
        fn: str,
        cs: ConfigurationSpace,
    ) -> typing.List[typing.Dict[str, typing.Union[float, int, Configuration]]]:
        """Reads trajectory from file

        Parameters
        ----------
        fn: str
            Filename with saved runhistory in self._add_in_alljson_format format
        cs: ConfigurationSpace
            Configuration Space to translate dict object into Confiuration object

        Returns
        -------
        trajectory: list
            Each entry in the list is a dictionary of the form
            {
            "cpu_time": float,
            "wallclock_time": float,
            "evaluations": int
            "cost": float,
            "budget": budget,
            "incumbent": Configuration
            }
        """

        trajectory = []
        with open(fn) as fp:
            for line in fp:
                entry = json.loads(line)
                entry["incumbent"] = Configuration(cs, entry["incumbent"])
                trajectory.append(entry)

        return trajectory
コード例 #12
0
ファイル: traj_logging.py プロジェクト: maxc01/SMAC3
    def _add_in_aclib_format(self, train_perf: float, incumbent_id: int,
                             incumbent: Configuration, ta_time_used: float,
                             wallclock_time: float) -> None:
        """Adds entries to AClib2-like trajectory file

        Parameters
        ----------
        train_perf: float
            Estimated performance on training (sub)set
        incumbent_id: int
            Id of incumbent
        incumbent: Configuration()
            Current incumbent configuration
        ta_time_used: float
            CPU time used by the target algorithm
        wallclock_time: float
            Wallclock time used so far
        """

        conf = []
        for p in incumbent:
            if not incumbent.get(p) is None:
                conf.append("%s='%s'" % (p, repr(incumbent[p])))

        traj_entry = {"cpu_time": ta_time_used,
                      "wallclock_time": wallclock_time,
                      "evaluations": self.stats.ta_runs,
                      "cost": train_perf,
                      "incumbent": conf,
                      "origin": incumbent.origin,
                      }

        with open(self.aclib_traj_fn, "a") as fp:
            json.dump(traj_entry, fp)
            fp.write("\n")
コード例 #13
0
 def _preprocess(self, runhistory):
     """
     Method to marginalize over instances such that fANOVA can determine the parameter importance without
     having to deal with instance features.
     :param runhistory: RunHistory that knows all configurations that were run. For all these configurations
                        we have to marginalize away the instance features with which fANOVA will make it's
                        predictions
     """
     self.logger.info('PREPROCESSING PREPROCESSING PREPROCESSING PREPROCESSING PREPROCESSING PREPROCESSING')
     self.logger.info('Marginalizing away all instances!')
     configs = runhistory.get_all_configs()
     if self.cs_contained_constant:
         configs = [Configuration(self.cs, vector=c.get_array()) for c in configs]
     X_non_hyper, X_prime = [], []
     for config in configs:
         config = impute_inactive_values(config).get_array()
         X_prime.append(config)
         X_non_hyper.append(config)
         for idx, param in enumerate(self.cs.get_hyperparameters()):
             if not (isinstance(param, CategoricalHyperparameter) or
                     isinstance(param, Constant)):
                 X_non_hyper[-1][idx] = param._transform(X_non_hyper[-1][idx])
     X_non_hyper = np.array(X_non_hyper)
     X_prime = np.array(X_prime)
     y_prime = np.array(self.model.predict_marginalized_over_instances(X_prime)[0])
     self._X = X_non_hyper
     self._y = y_prime
     self.logger.info('Size of training X after preprocessing: %s' % str(self.X.shape))
     self.logger.info('Size of training y after preprocessing: %s' % str(self.y.shape))
     self.logger.info('Finished Preprocessing')
コード例 #14
0
    def _add_in_aclib_format(self, train_perf: float, incumbent_id: int,
                             incumbent: Configuration, ta_time_used: float,
                             wallclock_time: float):
        """Adds entries to AClib2-like trajectory file

        Parameters
        ----------
        train_perf: float
            Estimated performance on training (sub)set
        incumbent_id: int
            Id of incumbent
        incumbent: Configuration()
            Current incumbent configuration
        ta_time_used: float
            CPU time used by the target algorithm
        wallclock_time: float
            Wallclock time used so far
        """

        conf = []
        for p in incumbent:
            if not incumbent.get(p) is None:
                conf.append("%s='%s'" % (p, repr(incumbent[p])))

        traj_entry = {
            "cpu_time": ta_time_used,
            "total_cpu_time": None,  # TODO: fix this
            "wallclock_time": wallclock_time,
            "evaluations": self.stats.ta_runs,
            "cost": train_perf,
            "incumbent": conf
        }
コード例 #15
0
    def _add_in_old_format(self, train_perf: Union[float, np.ndarray],
                           incumbent_id: int, incumbent: Configuration,
                           ta_time_used: float, wallclock_time: float) -> None:
        """Adds entries to old SMAC2-like trajectory file

        Parameters
        ----------
        train_perf: float or list of floats
            Estimated performance on training (sub)set
        incumbent_id: int
            Id of incumbent
        incumbent: Configuration()
            Current incumbent configuration
        ta_time_used: float
            CPU time used by the target algorithm
        wallclock_time: float
            Wallclock time used so far
        """

        conf = []
        for p in incumbent:
            if not incumbent.get(p) is None:
                conf.append("%s='%s'" % (p, repr(incumbent[p])))
        if isinstance(train_perf, float):
            # Make it compatible with old format
            with open(self.old_traj_fn, "a") as fp:
                fp.write(
                    f"{ta_time_used:f}, {train_perf:f}, {wallclock_time:f}, {incumbent_id:d}, "
                    f"{wallclock_time - ta_time_used:f}, {','.join(conf):s}\n")
        else:
            # We recommend to use pandas to read this csv file
            with open(self.old_traj_fn, "a") as fp:
                fp.write(
                    f"{ta_time_used:f}, {train_perf}, {wallclock_time:f}, {incumbent_id:d}, "
                    f"{wallclock_time - ta_time_used:f}, {','.join(conf):s}\n")
コード例 #16
0
def retrieve_matadata(validation_directory,
                      metric,
                      configuration_space,
                      cutoff=0,
                      only_best=True):
    if not only_best:
        raise NotImplementedError()
    if cutoff > 0:
        raise NotImplementedError()

    # Mapping from task id to a list of (config, score) tuples
    outputs = defaultdict(list)
    configurations = dict()
    configurations_to_ids = dict()

    possible_experiment_directories = os.listdir(validation_directory)

    for ped in possible_experiment_directories:
        task_name = None

        ped = os.path.join(validation_directory, ped)
        if not os.path.exists(ped) or not os.path.isdir(ped):
            continue
        print("Going through directory %s" % ped)

        validation_trajectory_file = os.path.join(
            ped, 'smac3-output_1_run1', 'validation_trajectory.json')
        with open(validation_trajectory_file) as fh:
            validation_trajectory = json.load(fh)

        best_value = np.inf
        best_configuration = None
        for entry in validation_trajectory:
            config = entry[2]
            task_name = entry[-2]
            score = entry[-1].get(str(metric), np.inf)

            if score < best_value:
                try:
                    best_configuration = Configuration(
                        configuration_space=configuration_space, values=config)
                    best_value = score
                except:
                    pass

        if task_name is None:
            continue

        if best_configuration is None:
            continue
        elif best_configuration in configurations_to_ids:
            config_id = configurations_to_ids[best_configuration]
        else:
            config_id = len(configurations_to_ids)
            configurations_to_ids[config_id] = best_configuration
            configurations[config_id] = best_configuration

        outputs[task_name].append((config_id, best_value))

    return outputs, configurations
コード例 #17
0
    def _combine_configurations_batch_vector(self, start_config,
                                             complemented_configs_values):
        constant_vector_values = self._get_vector_values(
            start_config, self.constant_pipeline_steps)
        batch = []

        for complemented_config_values in complemented_configs_values:
            vector = np.ndarray(len(self.config_space._hyperparameters),
                                dtype=np.float)

            vector[:] = np.NaN

            for key in constant_vector_values:
                vector[key] = constant_vector_values[key]

            for key in complemented_config_values:
                vector[key] = complemented_config_values[key]

            try:
                self.config_space._check_forbidden(vector)
                config_object = Configuration(
                    configuration_space=self.config_space, vector=vector)
                batch.append(config_object)
            except ValueError as v:
                pass

        return batch
コード例 #18
0
    def _add_in_old_format(self, train_perf: float, incumbent_id: int,
                           incumbent: Configuration, ta_time_used: float,
                           wallclock_time: float):
        """Adds entries to old SMAC2-like trajectory file

        Parameters
        ----------
        train_perf: float
            Estimated performance on training (sub)set
        incumbent_id: int
            Id of incumbent
        incumbent: Configuration()
            Current incumbent configuration
        ta_time_used: float
            CPU time used by the target algorithm
        wallclock_time: float
            Wallclock time used so far
        """

        conf = []
        for p in incumbent:
            if not incumbent.get(p) is None:
                conf.append("%s='%s'" % (p, repr(incumbent[p])))

        txt = ("%f, %f, %f, %d, %f, %s\n" % (
            ta_time_used,
            train_perf,
            wallclock_time,
            incumbent_id,
            wallclock_time - ta_time_used,
            ", ".join(conf)
        ))
        self.file_system.write_txt(self.old_traj_fn, txt, append=True)
コード例 #19
0
ファイル: meta_base.py プロジェクト: wyslatitude/auto-sklearn
    def __init__(self, configuration_space, aslib_directory):
        """Container for dataset metadata and experiment results.

        Constructor arguments:
        - The configuration space
        - aslib_directory: directory with a problem instance in the aslib format
        """

        self.logger = logging.getLogger(__name__)

        self.configuration_space = configuration_space
        self.aslib_directory = aslib_directory

        aslib_reader = aslib_simple.AlgorithmSelectionProblem(
            self.aslib_directory)
        self.metafeatures = aslib_reader.metafeatures
        self.algorithm_runs = aslib_reader.algorithm_runs
        self.configurations = aslib_reader.configurations

        configurations = dict()
        for algorithm_id in self.configurations:
            configuration = self.configurations[algorithm_id]
            try:
                configurations[algorithm_id] = \
                    (Configuration(configuration_space, values=configuration))
            except (ValueError, KeyError) as e:
                self.logger.debug("Error reading configurations: %s", e)

        self.configurations = configurations
コード例 #20
0
    def hyperparameter_values(self, value: Configuration):
        """Encode hyperparameters from object to base64"""
        if value is None:
            d = {}
        else:
            d = value.get_dictionary()

        self.hyperparameter_values_64 = object_to_base_64(d)
コード例 #21
0
    def test_predict_proba_batched_sparse(self):
        cs = SimpleClassificationPipeline.get_hyperparameter_search_space(
            dataset_properties={'sparse': True})

        config = Configuration(
            cs,
            values={
                "balancing:strategy": "none",
                "classifier:__choice__": "random_forest",
                "imputation:strategy": "mean",
                "one_hot_encoding:minimum_fraction": 0.01,
                "one_hot_encoding:use_minimum_fraction": 'True',
                "preprocessor:__choice__": "no_preprocessing",
                'classifier:random_forest:bootstrap': 'True',
                'classifier:random_forest:criterion': 'gini',
                'classifier:random_forest:max_depth': 'None',
                'classifier:random_forest:min_samples_split': 2,
                'classifier:random_forest:min_samples_leaf': 2,
                'classifier:random_forest:min_weight_fraction_leaf': 0.0,
                'classifier:random_forest:max_features': 0.5,
                'classifier:random_forest:max_leaf_nodes': 'None',
                'classifier:random_forest:n_estimators': 100,
                "rescaling:__choice__": "min/max"
            })

        # Multiclass
        cls = SimpleClassificationPipeline(config)
        X_train, Y_train, X_test, Y_test = get_dataset(dataset='digits',
                                                       make_sparse=True)
        cls.fit(X_train, Y_train)
        X_test_ = X_test.copy()
        prediction_ = cls.predict_proba(X_test_)
        # The object behind the last step in the pipeline
        cls_predict = mock.Mock(wraps=cls.pipeline_.steps[-1][1])
        cls.pipeline_.steps[-1] = ("estimator", cls_predict)
        prediction = cls.predict_proba(X_test, batch_size=20)
        self.assertEqual((1647, 10), prediction.shape)
        self.assertEqual(84, cls_predict.predict_proba.call_count)
        assert_array_almost_equal(prediction_, prediction)

        # Multilabel
        cls = SimpleClassificationPipeline(config)
        X_train, Y_train, X_test, Y_test = get_dataset(dataset='digits',
                                                       make_sparse=True)
        Y_train = np.array(
            list([(list([1 if i != y else 0 for i in range(10)]))
                  for y in Y_train]))
        cls.fit(X_train, Y_train)
        X_test_ = X_test.copy()
        prediction_ = cls.predict_proba(X_test_)
        cls_predict = mock.Mock(wraps=cls.pipeline_.steps[-1][1])
        cls.pipeline_.steps[-1] = ("estimator", cls_predict)
        prediction = cls.predict_proba(X_test, batch_size=20)
        self.assertEqual(prediction.shape, ((1647, 10)))
        self.assertIsInstance(prediction, np.ndarray)
        self.assertEqual(84, cls_predict.predict_proba.call_count)
        assert_array_almost_equal(prediction_, prediction)
コード例 #22
0
ファイル: main.py プロジェクト: cczy1910/Hydrogen
def readData(cs, file):
    data = pd.read_csv(file)
    configs = []
    values = {}
    for i in data.index:
        line = data.T[i][:-1]
        config = Configuration(cs, values=dict(line))
        configs.append(config)
        values[config] = data.T[i][-1]
    return values, configs
コード例 #23
0
ファイル: autofolio.py プロジェクト: wannabeGuru/AutoFolio
    def fit_selector(self, scenario: ASlibScenario, config: Configuration):
        '''
            fits an algorithm selector for a given scenario wrt a given configuration

            Arguments
            ---------
            scenario: autofolio.data.aslib_scenario.ASlibScenario
                aslib scenario at hand
            config: Configuration
                parameter configuration
        '''

        if config.get("selector") == "PairwiseClassifier":

            clf_class = None
            if config.get("classifier") == "RandomForest":
                clf_class = RandomForest

            selector = PairwiseClassifier(classifier_class=clf_class)
            selector.fit(scenario=scenario, config=config)

        return selector
コード例 #24
0
    def __init__(self, scen, ID, repetition=0, updates=""):
        """ This function creates a configuration with default parameters, which will
        (at the end of the function) be overwritten with the values in the updates-
        dictionary. """
        if isinstance(updates, str):
            updates = self.dict_from_file(scen, ID)
        elif not isinstance(updates, dict):
            raise ValueError("updates to Config must be of type str (for"
                             "filepath) or dict.")

        with open("dlas/dlas.pcs", 'r') as f:
            configspace = pcs.read(f.readlines())
        self.default_config = configspace.get_default_configuration()
        config_dict = self.default_config.get_dictionary()
        config_dict.update(updates)
        self.config = Configuration(configspace, config_dict)
        self.scen = scen
        self.ID = ID
        self.rep = repetition
        self.use_validation = True
        self.result_path = "results/{}/{}/{}/".format(self.scen, self.ID,
                                                      self.rep)
コード例 #25
0
class Config(object):
    """ This class is an extension of the ConfigurationSpace-Configuration,
    introducing module-members scenario, id, repetition etc. """
    def __init__(self, scen, ID, repetition=0, updates=""):
        """ This function creates a configuration with default parameters, which will
        (at the end of the function) be overwritten with the values in the updates-
        dictionary. """
        if isinstance(updates, str):
            updates = self.dict_from_file(scen, ID)
        elif not isinstance(updates, dict):
            raise ValueError("updates to Config must be of type str (for"
                             "filepath) or dict.")

        with open("dlas/dlas.pcs", 'r') as f:
            configspace = pcs.read(f.readlines())
        self.default_config = configspace.get_default_configuration()
        config_dict = self.default_config.get_dictionary()
        config_dict.update(updates)
        self.config = Configuration(configspace, config_dict)
        self.scen = scen
        self.ID = ID
        self.rep = repetition
        self.use_validation = True
        self.result_path = "results/{}/{}/{}/".format(self.scen, self.ID,
                                                      self.rep)

    def __getitem__(self, attr):
        return self.config[attr]

    def get_dictionary(self):
        return self.config.get_dictionary()

    def dict_from_file(self, s, ID):
        with open("experiments/{}.txt".format(ID), 'r') as f:
            content = f.readlines()
            content = [
                tuple(line.strip("\n").split("=")) for line in content
                if line != "\n"
            ]
            content = [(name.strip(), value.strip())
                       for name, value in content]
            content = dict(content)
            for c in content:
                try:
                    content[c] = float(content[c])
                    if content[c].is_integer():
                        content[c] = int(content[c])
                except ValueError:
                    pass
            return content
コード例 #26
0
ファイル: utils.py プロジェクト: tqichun/auto-pipeline
def get_id_of_config(config: Configuration):
    # todo:, instance="", seed=0
    X: np.ndarray = config.get_array()
    m = hashlib.md5()
    if X.flags['C_CONTIGUOUS']:
        m.update(X.data)
        m.update(str(X.shape).encode('utf8'))
    else:
        X_tmp = np.ascontiguousarray(X.T)
        m.update(X_tmp.data)
        m.update(str(X_tmp.shape).encode('utf8'))
    # m.update(instance.encode())
    # m.update(str(seed).encode())
    hash_value = m.hexdigest()
    return hash_value
コード例 #27
0
ファイル: smac_php2dhp.py プロジェクト: tqichun/auto-pipeline
 def convert(self, php: Configuration):
     dict_ = php.get_dictionary()
     ret = {}
     for k, v in dict_.items():
         if isinstance(v, str):
             v = _decode(v)
         key_path = k.split(":")
         if key_path[-1] == "__choice__":
             key_path = key_path[:-1]
             if v is not None:
                 key_path += [v]
                 v = {}
         if "None" in key_path:
             continue
         self.set_kv(ret, key_path, v)  # self.split_key(k)
     return ret
コード例 #28
0
    def combine_configurations(self, configs):
        default = configs.pop()
        constant_values = self._get_values(default.get_dictionary(),
                                           self.constant_pipeline_steps)
        new_configs = [default]

        for config in configs:
            new_config_values = {}
            new_config_values.update(constant_values)
            variable_values = self._get_values(config.get_dictionary(),
                                               self.variable_pipeline_steps)
            new_config_values.update(variable_values)
            new_configs.append(
                Configuration(configuration_space=self.config_space,
                              values=new_config_values))

        return new_configs
コード例 #29
0
def combine_configurations_batch_version1(config_space, start_config, complemented_configs_values, constant_pipeline_steps):
    constant_values = get_values(start_config.get_dictionary(), constant_pipeline_steps)
    batch = []
    for complemented_config_values in complemented_configs_values:
        new_config_values = {}
        new_config_values.update(constant_values)

        new_config_values.update(complemented_config_values)

        try:
            # start_time = time.time()
            config_object = Configuration(configuration_space=config_space,
                                          values=new_config_values)
            # print("Constructing configuration: {}".format(time.time() - start_time))
            batch.append(config_object)
        except ValueError as v:
            pass
    return batch
コード例 #30
0
ファイル: base.py プロジェクト: mayurmorin/AutoML-Challenge
    def __init__(self, configuration_space, meta_dir, logger):
        self.configuration_space = configuration_space
        self.meta_dir = meta_dir
        self.logger = logger

        meta_reader = AlgorithmSelectionProblem(self.meta_dir, logger)
        self.metafeatures = meta_reader.metafeatures
        self.algorithm_runs = meta_reader.algorithm_runs
        self.configurations = meta_reader.configurations

        _configs = dict()
        for alg_id, config in self.configurations.items():
            try:
                _configs[alg_id] = \
                    (Configuration(configuration_space, values=config))
            except (ValueError, KeyError) as e:
                self.logger.error("Error reading configurations: %s", e)

        self.configurations = _configs
コード例 #31
0
    def set_hyperparameters(
            self,
            configuration: Configuration,
            init_params: Optional[Dict[str,
                                       Any]] = None) -> 'autoPyTorchChoice':
        """
        Applies a configuration to the given component.
        This method translate a hierarchical configuration key,
        to an actual parameter of the autoPyTorch component.

        Args:
            configuration (Configuration): which configuration to apply to
                the chosen component
            init_params (Optional[Dict[str, any]]): Optional arguments to
                initialize the chosen component

        Returns:
            self: returns an instance of self
        """
        new_params = {}

        params = configuration.get_dictionary()
        choice = params['__choice__']
        del params['__choice__']

        for param, value in params.items():
            param = param.replace(choice + ':', '')
            new_params[param] = value

        if init_params is not None:
            for param, value in init_params.items():
                param = param.replace(choice + ':', '')
                new_params[param] = value

        new_params['random_state'] = self.random_state

        self.new_params = new_params
        self.choice = self.get_components()[choice](**new_params)

        return self
コード例 #32
0
ファイル: autofolio.py プロジェクト: wannabeGuru/AutoFolio
    def _overwrite_configuration(self, config: Configuration, overwrite_args: list):
        '''
            overwrites a given configuration with some new settings

            Arguments
            ---------
            config: Configuration
                initial configuration to be adapted
            overwrite_args: list
                new parameter settings as a list of strings

            Returns
            -------
            Configuration
        '''

        def pairwise(iterable):
            a, b = tee(iterable)
            next(b, None)
            return zip(a, b)

        dict_conf = config.get_dictionary()
        for param, value in pairwise(overwrite_args):
            if dict_conf.get(param):
                if type(self.cs.get_hyperparameter(param)) is UniformIntegerHyperparameter:
                    dict_conf[param] = int(value)
                elif type(self.cs.get_hyperparameter(param)) is UniformFloatHyperparameter:
                    dict_conf[param] = float(value)
                elif value == "True":
                    dict_conf[param] = True
                elif value == "False":
                    dict_conf[param] = False
                else:
                    dict_conf[param] = value
            else:
                self.logger.warn(
                    "Unknown given parameter: %s %s" % (param, value))
        config = Configuration(self.cs, values=dict_conf)

        return config