コード例 #1
0
    def test_get_parent_and_children_of(self):
        cs = ConfigurationSpace()
        hp1 = CategoricalHyperparameter("parent", [0, 1])
        cs.add_hyperparameter(hp1)
        hp2 = UniformIntegerHyperparameter("child", 0, 10)
        cs.add_hyperparameter(hp2)
        cond1 = EqualsCondition(hp2, hp1, 0)
        cs.add_condition(cond1)

        self.assertEqual([hp1], cs.get_parents_of(hp2.name))
        self.assertEqual([hp1], cs.get_parents_of(hp2))
        self.assertEqual([hp2], cs.get_children_of(hp1.name))
        self.assertEqual([hp2], cs.get_children_of(hp1))

        self.assertRaisesRegex(
            KeyError, "Hyperparameter 'Foo' does not exist in this "
            "configuration space.", cs.get_parents_of, "Foo")
        self.assertRaisesRegex(
            KeyError, "Hyperparameter 'Foo' does not exist in this "
            "configuration space.", cs.get_children_of, "Foo")
コード例 #2
0
    def test_get_parent_and_children_of(self):
        cs = ConfigurationSpace()
        hp1 = CategoricalHyperparameter("parent", [0, 1])
        cs.add_hyperparameter(hp1)
        hp2 = UniformIntegerHyperparameter("child", 0, 10)
        cs.add_hyperparameter(hp2)
        cond1 = EqualsCondition(hp2, hp1, 0)
        cs.add_condition(cond1)

        self.assertEqual([hp1], cs.get_parents_of(hp2.name))
        self.assertEqual([hp1], cs.get_parents_of(hp2))
        self.assertEqual([hp2], cs.get_children_of(hp1.name))
        self.assertEqual([hp2], cs.get_children_of(hp1))

        self.assertRaisesRegex(KeyError,
                               "Hyperparameter 'Foo' does not exist in this "
                               "configuration space.", cs.get_parents_of,
                               "Foo")
        self.assertRaisesRegex(KeyError,
                               "Hyperparameter 'Foo' does not exist in this "
                               "configuration space.", cs.get_children_of,
                               "Foo")
コード例 #3
0
ファイル: utils.py プロジェクト: auto-flow/auto-flow
 def fit(self, config_space: ConfigurationSpace):
     mask = []
     n_choices_list = []
     n_constants = 0
     n_variables = 0
     n_top_levels = 0
     parents = []
     parent_values = []
     # todo: 划分parents与groups
     for hp in config_space.get_hyperparameters():
         if isinstance(
                 hp, Constant) or (isinstance(hp, CategoricalHyperparameter)
                                   and len(hp.choices) == 1):
             # ignore
             mask.append(False)
             n_constants += 1
         else:
             mask.append(True)
             n_variables += 1
             if isinstance(hp, CategoricalHyperparameter):
                 n_choices_list.append(len(hp.choices))
             else:
                 n_choices_list.append(0)
             cur_parents = config_space.get_parents_of(hp.name)
             if len(cur_parents) == 0:
                 n_top_levels += 1
                 parents.append(None)
                 parent_values.append(None)
             else:
                 parents.append(cur_parents[0])
                 parent_conditions = config_space.get_parent_conditions_of(
                     hp.name)
                 parent_condition = parent_conditions[0]
                 parent_values.append(parent_condition.value)
     groups_str = [
         f"{parent}-{parent_value}"
         for parent, parent_value in zip(parents, parent_values)
     ]
     group_encoder = LabelEncoder()
     groups = group_encoder.fit_transform(groups_str)
     self.config_space = config_space
     self.groups_str = groups_str
     self.group_encoder = group_encoder
     self.groups = groups
     self.n_groups = np.max(groups) + 1
     self.mask = np.array(mask, dtype="bool")
     self.n_choices_list = n_choices_list
     self.n_constants = n_constants
     self.n_variables = n_variables
     self.n_top_levels = n_top_levels
     return self
コード例 #4
0
 def fit(self, config_space: ConfigurationSpace):
     mask = []
     n_choices_list = []
     is_ordinal_list = []
     sequence_mapper = {}
     n_constants = 0
     n_variables = 0
     n_variables_embedded = 0
     n_top_levels = 0
     parents = []
     parent_values = []
     is_child = []
     # todo: 划分parents与groups
     for hp in config_space.get_hyperparameters():
         if isinstance(hp, Constant) or \
                 (isinstance(hp, CategoricalHyperparameter) and len(hp.choices) == 1) or \
                 (isinstance(hp, OrdinalHyperparameter) and len(hp.sequence) == 1):
             # ignore
             mask.append(False)
             n_constants += 1
         else:
             mask.append(True)
             n_variables += 1
             if isinstance(hp, CategoricalHyperparameter):
                 n_choices = len(hp.choices)
                 n_choices_list.append(n_choices)
                 n_variables_embedded += get_embed_dims(n_choices)
             else:
                 n_choices_list.append(0)
                 n_variables_embedded += 1
             if isinstance(hp, OrdinalHyperparameter):
                 is_ordinal_list.append(True)
                 sequence_mapper[len(is_ordinal_list) - 1] = hp.sequence
             else:
                 is_ordinal_list.append(False)
             cur_parents = config_space.get_parents_of(hp.name)
             if len(cur_parents) == 0:
                 n_top_levels += 1
                 parents.append(None)
                 parent_values.append(None)
                 is_child.append(False)
             else:
                 is_child.append(True)
                 parents.append(cur_parents[0])
                 parent_conditions = config_space.get_parent_conditions_of(
                     hp.name)
                 parent_condition = parent_conditions[0]
                 parent_values.append(parent_condition.value)
     groups_str = [
         f"{parent}-{parent_value}"
         for parent, parent_value in zip(parents, parent_values)
     ]
     group_encoder = LabelEncoder()
     groups = group_encoder.fit_transform(groups_str)
     self.is_child = is_child
     self.sequence_mapper = sequence_mapper
     self.is_ordinal_list = is_ordinal_list
     self.config_space = config_space
     self.groups_str = groups_str
     self.group_encoder = group_encoder
     self.groups = groups
     self.n_groups = np.max(groups) + 1
     self.mask = np.array(mask, dtype="bool")
     self.n_choices_list = n_choices_list
     self.n_constants = n_constants
     self.n_variables = n_variables
     self.n_variables_embedded = n_variables_embedded
     self.n_top_levels = n_top_levels
     self.hp_names = pd.Series(
         [hp.name for hp in config_space.get_hyperparameters()])[self.mask]
     high_r_mask = np.array(self.n_choices_list) > 2
     self.high_r_cols = self.hp_names[high_r_mask].to_list()
     self.high_r_cats = []
     for ix in np.arange(n_variables)[high_r_mask]:
         n_choices = n_choices_list[ix]
         cat = list(range(n_choices))
         if is_child[ix]:
             cat.insert(0, -1)
         self.high_r_cats.append(cat)
     if self.encoder is not None:
         self.encoder.cols = copy(self.high_r_cols)
         self.encoder.categories = copy(self.high_r_cats)
     return self
コード例 #5
0
ファイル: util_funcs.py プロジェクト: TheVinhLuong102/SMAC3
def get_types(
    config_space: ConfigurationSpace,
    instance_features: typing.Optional[np.ndarray] = None,
) -> typing.Tuple[typing.List[int], typing.List[typing.Tuple[float, float]]]:
    """TODO"""
    # Extract types vector for rf from config space and the bounds
    types = [0] * len(config_space.get_hyperparameters())
    bounds = [(np.nan, np.nan)] * len(types)

    for i, param in enumerate(config_space.get_hyperparameters()):
        parents = config_space.get_parents_of(param.name)
        if len(parents) == 0:
            can_be_inactive = False
        else:
            can_be_inactive = True

        if isinstance(param, (CategoricalHyperparameter)):
            n_cats = len(param.choices)
            if can_be_inactive:
                n_cats = len(param.choices) + 1
            types[i] = n_cats
            bounds[i] = (int(n_cats), np.nan)

        elif isinstance(param, (OrdinalHyperparameter)):
            n_cats = len(param.sequence)
            types[i] = 0
            if can_be_inactive:
                bounds[i] = (0, int(n_cats))
            else:
                bounds[i] = (0, int(n_cats) - 1)

        elif isinstance(param, Constant):
            # for constants we simply set types to 0 which makes it a numerical
            # parameter
            if can_be_inactive:
                bounds[i] = (2, np.nan)
                types[i] = 2
            else:
                bounds[i] = (0, np.nan)
                types[i] = 0
            # and we leave the bounds to be 0 for now
        elif isinstance(param, UniformFloatHyperparameter):
            # Are sampled on the unit hypercube thus the bounds
            # are always 0.0, 1.0
            if can_be_inactive:
                bounds[i] = (-1.0, 1.0)
            else:
                bounds[i] = (0, 1.0)
        elif isinstance(param, UniformIntegerHyperparameter):
            if can_be_inactive:
                bounds[i] = (-1.0, 1.0)
            else:
                bounds[i] = (0, 1.0)
        elif not isinstance(
                param,
            (UniformFloatHyperparameter, UniformIntegerHyperparameter,
             OrdinalHyperparameter, CategoricalHyperparameter)):
            raise TypeError("Unknown hyperparameter type %s" % type(param))

    if instance_features is not None:
        types = types + [0] * instance_features.shape[1]

    return types, bounds