Пример #1
0
    def test_check_neighbouring_config_diamond_str(self):
        diamond = ConfigurationSpace()
        head = CategoricalHyperparameter('head', ['red', 'green'])
        left = CategoricalHyperparameter('left', ['red', 'green'])
        right = CategoricalHyperparameter('right', ['red', 'green', 'blue', 'yellow'])
        bottom = CategoricalHyperparameter('bottom', ['red', 'green'])
        diamond.add_hyperparameters([head, left, right, bottom])
        diamond.add_condition(EqualsCondition(left, head, 'red'))
        diamond.add_condition(EqualsCondition(right, head, 'red'))
        diamond.add_condition(AndConjunction(EqualsCondition(bottom, left, 'green'),
                                             EqualsCondition(bottom, right, 'green')))

        config = Configuration(diamond, {'bottom': 'red', 'head': 'red', 'left': 'green', 'right': 'green'})
        hp_name = "head"
        index = diamond.get_idx_by_hyperparameter_name(hp_name)
        neighbor_value = 1

        new_array = ConfigSpace.c_util.change_hp_value(
            diamond,
            config.get_array(),
            hp_name,
            neighbor_value,
            index
        )
        expected_array = np.array([1, np.nan, np.nan, np.nan])

        np.testing.assert_almost_equal(new_array, expected_array)
Пример #2
0
    def test_check_neighbouring_config_diamond_str(self):
        diamond = ConfigurationSpace()
        head = CategoricalHyperparameter('head', ['red', 'green'])
        left = CategoricalHyperparameter('left', ['red', 'green'])
        right = CategoricalHyperparameter('right',
                                          ['red', 'green', 'blue', 'yellow'])
        bottom = CategoricalHyperparameter('bottom', ['red', 'green'])
        diamond.add_hyperparameters([head, left, right, bottom])
        diamond.add_condition(EqualsCondition(left, head, 'red'))
        diamond.add_condition(EqualsCondition(right, head, 'red'))
        diamond.add_condition(
            AndConjunction(EqualsCondition(bottom, left, 'green'),
                           EqualsCondition(bottom, right, 'green')))

        config = Configuration(diamond, {
            'bottom': 'red',
            'head': 'red',
            'left': 'green',
            'right': 'green'
        })
        hp_name = "head"
        index = diamond.get_idx_by_hyperparameter_name(hp_name)
        neighbor_value = 1

        new_array = ConfigSpace.c_util.change_hp_value(diamond,
                                                       config.get_array(),
                                                       hp_name, neighbor_value,
                                                       index)
        expected_array = np.array([1, np.nan, np.nan, np.nan])

        np.testing.assert_almost_equal(new_array, expected_array)
Пример #3
0
    def test_check_neighbouring_config_diamond(self):
        diamond = ConfigurationSpace()
        head = CategoricalHyperparameter('head', [0, 1])
        left = CategoricalHyperparameter('left', [0, 1])
        right = CategoricalHyperparameter('right', [0, 1, 2, 3])
        bottom = CategoricalHyperparameter('bottom', [0, 1])
        diamond.add_hyperparameters([head, left, right, bottom])
        diamond.add_condition(EqualsCondition(left, head, 0))
        diamond.add_condition(EqualsCondition(right, head, 0))
        diamond.add_condition(
            AndConjunction(EqualsCondition(bottom, left, 1),
                           EqualsCondition(bottom, right, 1)))

        config = Configuration(diamond, {
            'bottom': 0,
            'head': 0,
            'left': 1,
            'right': 1
        })
        hp_name = "head"
        index = diamond.get_idx_by_hyperparameter_name(hp_name)
        neighbor_value = 1

        new_array = change_hp_value(diamond, config.get_array(), hp_name,
                                    neighbor_value, index)
        expected_array = np.array([1, np.nan, np.nan, np.nan])

        np.testing.assert_almost_equal(new_array, expected_array)
Пример #4
0
def impute_default_values(configuration_space: ConfigurationSpace,
                          configs_array: np.ndarray) -> np.ndarray:
    """Impute inactive hyperparameters in configuration array with their default.

    Necessary to apply an EPM to the data.

    Parameters
    ----------
    configuration_space : ConfigurationSpace

    configs_array : np.ndarray
        Array of configurations.

    Returns
    -------
    np.ndarray
        Array with configuration hyperparameters. Inactive values are imputed
        with their default value.
    """
    for hp in configuration_space.get_hyperparameters():
        default = hp.normalized_default_value
        idx = configuration_space.get_idx_by_hyperparameter_name(hp.name)
        nonfinite_mask = ~np.isfinite(configs_array[:, idx])
        configs_array[nonfinite_mask, idx] = default

    return configs_array
Пример #5
0
def change_hp_value(configuration_space: ConfigurationSpace,
                    configuration_array: np.ndarray, hp_name: str,
                    hp_value: float, index: int) -> np.ndarray:
    """Change hyperparameter value in configuration array to given value.
    
    Does not check if the new value is legal. Activates and deactivates other 
    hyperparameters if necessary. Does not check if new hyperparameter value 
    results in the violation of any forbidden clauses.
    
    Parameters
    ----------
    configuration_space : ConfigurationSpace
    
    configuration_array : np.ndarray
    
    hp_name : str
    
    hp_value : float
    
    index : int
    
    Returns
    -------
    np.ndarray
    """

    configuration_array[index] = hp_value

    # Hyperparameters which are going to be set to inactive
    disabled = []

    # Activate hyperparameters if their parent node got activated
    children = configuration_space._children_of[hp_name]
    if len(children) > 0:
        to_visit = deque()  # type: deque
        to_visit.extendleft(children)
        visited = set()  # type: Set[str]
        activated_values = dict()  # type: Dict[str, Union[int, float, str]]

        while len(to_visit) > 0:
            current = to_visit.pop()
            if current.name in visited:
                continue
            visited.add(current.name)
            if current.name in disabled:
                continue

            current_idx = configuration_space.get_idx_by_hyperparameter_name(
                current.name)
            current_value = configuration_array[current_idx]

            conditions = configuration_space._parent_conditions_of[
                current.name]

            active = True
            for condition in conditions:
                if not condition.evaluate_vector(configuration_array):
                    active = False
                    break

            if active and (current_value is None
                           or not np.isfinite(current_value)):
                default = current._inverse_transform(current.default)
                configuration_array[current_idx] = default
                children_ = configuration_space._children_of[current.name]
                if len(children_) > 0:
                    to_visit.extendleft(children_)

            # If the hyperparameter was made inactive,
            # all its children need to be deactivade as well
            if not active and (current_value is not None
                               or np.isfinite(current_value)):
                configuration_array[current_idx] = np.NaN

                children = configuration_space._children_of[current.name]

                if len(children) > 0:
                    to_disable = set()
                    for ch in children:
                        to_disable.add(ch.name)
                    while len(to_disable) > 0:
                        child = to_disable.pop()
                        child_idx = configuration_space. \
                            get_idx_by_hyperparameter_name(child)
                        disabled.append(child_idx)
                        children = configuration_space._children_of[child]

                        for ch in children:
                            to_disable.add(ch.name)

    for idx in disabled:
        configuration_array[idx] = np.NaN

    return configuration_array