def test_get_parent_and_chil_conditions_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([cond1], cs.get_parent_conditions_of(hp2.name)) self.assertEqual([cond1], cs.get_parent_conditions_of(hp2)) self.assertEqual([cond1], cs.get_child_conditions_of(hp1.name)) self.assertEqual([cond1], cs.get_child_conditions_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")
def test_get_parent_and_chil_conditions_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([cond1], cs.get_parent_conditions_of(hp2.name)) self.assertEqual([cond1], cs.get_parent_conditions_of(hp2)) self.assertEqual([cond1], cs.get_child_conditions_of(hp1.name)) self.assertEqual([cond1], cs.get_child_conditions_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")
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
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