Exemplo n.º 1
0
 def set_bounds(self):
     bounds = []
     for key in sorted(self.config.params.keys()):
         value = self.config.params[key]
         self._features.append(key)
         # one hot encoding for categorical type
         if value.is_categorical:
             values = to_numpy(value)
             num_feasible = len(values)
             for _ in range(num_feasible):
                 bounds.append((0, 1))
             self._categorical_features[key] = {
                 "values": values,
                 "number": num_feasible,
             }
             self._dim += num_feasible
         elif value.is_discrete:
             self._dim = self._dim + 1
             discrete_values = to_numpy(value)
             bounds.append((get_min(value), get_max(value)))
             self._discrete_features[key] = {"values": discrete_values}
         elif value.is_uniform:
             self._dim = self._dim + 1
             bounds.append((float(get_min(value)), float(get_max(value))))
     self._bounds = np.asarray(bounds)
Exemplo n.º 2
0
    def test_matrix_pchoice_option(self):
        config_dict = {"kind": "pchoice", "value": [(1, 0.1), (2, 0.3), (3, 6)]}
        with self.assertRaises(ValidationError):
            V1HpPChoice.from_dict(config_dict)

        config_dict["value"] = [(1, 0.1), (2, 0.3), (3, 0.8)]
        with self.assertRaises(ValidationError):
            V1HpPChoice.from_dict(config_dict)

        config_dict["value"] = [(1, 0.1), (2, 0.3), (3, -0.6)]
        with self.assertRaises(ValidationError):
            V1HpPChoice.from_dict(config_dict)

        config_dict["value"] = ["ok", "nook"]
        with self.assertRaises(ValidationError):
            V1HpPChoice.from_dict(config_dict)

        # Pass for correct config
        config_dict["value"] = [(1, 0.1), (2, 0.1), (3, 0.8)]
        config = V1HpPChoice.from_dict(config_dict)
        assert config.to_dict() == config_dict
        with self.assertRaises(ValidationError):
            to_numpy(config)
        assert sample(config) in [1, 2, 3]
        assert get_length(config) == 3
        assert config.is_categorical is False
        assert config.is_distribution is True
        assert config.is_range is False
        assert config.is_uniform is False
        assert config.is_discrete is True
        assert config.is_continuous is False
        assert get_min(config) is None
        assert get_max(config) is None
Exemplo n.º 3
0
    def test_matrix_values_option(self):
        config_dict = {"kind": "choice", "value": [1, 2, 3]}
        config = V1HpChoice.from_dict(config_dict)
        assert config.to_dict() == config_dict
        assert to_numpy(config) == config_dict["value"]
        assert sample(config) in [1, 2, 3]
        assert get_length(config) == 3
        assert config.is_categorical is False
        assert config.is_distribution is False
        assert config.is_range is False
        assert config.is_uniform is False
        assert config.is_discrete is True
        assert config.is_continuous is False
        assert get_min(config) == 1
        assert get_max(config) == 3

        config_dict["value"] = ["ok", "nook"]
        config = V1HpChoice.from_dict(config_dict)
        assert config.to_dict() == config_dict
        assert to_numpy(config) == config_dict["value"]
        assert sample(config) in ["ok", "nook"]
        assert get_length(config) == 2
        assert config.is_categorical is True
        assert config.is_distribution is False
        assert config.is_range is False
        assert config.is_uniform is False
        assert config.is_discrete is True
        assert config.is_continuous is False
        assert get_min(config) is None
        assert get_max(config) is None

        config_dict["value"] = [[1, 2], [2, 4]]
        config = V1HpChoice.from_dict(config_dict)
        assert config.to_dict() == config_dict
        assert to_numpy(config) == config_dict["value"]
        assert sample(config) in [[1, 2], [2, 4]]
        assert get_length(config) == 2
        assert config.is_categorical is True
        assert config.is_distribution is False
        assert config.is_range is False
        assert config.is_uniform is False
        assert config.is_discrete is True
        assert config.is_continuous is False
        assert get_min(config) is None
        assert get_max(config) is None
Exemplo n.º 4
0
 def assert_equal(config, v1, v2, q, v3=None):
     result = {"loc": v1, "scale": v2, "q": q}
     if v3:
         result["size"] = v3
     assert config.to_dict()["value"] == result
     with self.assertRaises(ValidationError):
         to_numpy(config)
     with self.assertRaises(ValidationError):
         get_length(config)
     assert isinstance(sample(config), float)
     assert config.is_categorical is False
     assert config.is_distribution is True
     assert config.is_range is False
     assert config.is_uniform is False
     assert config.is_discrete is False
     assert config.is_continuous is True
     assert get_min(config) is None
     assert get_max(config) is None
Exemplo n.º 5
0
    def get_suggestions(self, params: List[Dict] = None) -> List[Dict]:
        suggestions = []
        keys = list(self.config.params.keys())
        values = [to_numpy(v) for v in self.config.params.values()]
        for v in itertools.product(*values):
            suggestions.append(dict(zip(keys, v)))

        if self.config.num_runs:
            return suggestions[: self.config.num_runs]
        return suggestions
Exemplo n.º 6
0
 def assert_equal(config, v1, v2, v3=None):
     result = {"low": v1, "high": v2}
     if v3:
         result["size"] = v3
     assert config.to_dict()["value"] == result
     with self.assertRaises(ValidationError):
         to_numpy(config)
     with self.assertRaises(ValidationError):
         to_numpy(config)
     with self.assertRaises(ValidationError):
         get_length(config)
     assert v1 <= sample(config) <= v2
     assert config.is_categorical is False
     assert config.is_distribution is True
     assert config.is_range is False
     assert config.is_uniform is True
     assert config.is_discrete is False
     assert config.is_continuous is True
     assert get_min(config) == v1
     assert get_max(config) == v2
Exemplo n.º 7
0
    def _set_search_space(self):
        for k, v in self.config.params.items():
            self._search_space[k] = to_hyperopt(k, v)

            if v.IDENTIFIER in {
                    V1HpChoice.IDENTIFIER,
                    V1HpRange.IDENTIFIER,
                    V1HpLinSpace.IDENTIFIER,
                    V1HpLogSpace.IDENTIFIER,
                    V1HpGeomSpace.IDENTIFIER,
            }:
                # Get the categorical/discrete values mapping
                self._param_to_value[k] = to_numpy(v)
Exemplo n.º 8
0
 def assert_equal(config, v1, v2, v3):
     result = {"start": v1, "stop": v2, "num": v3}
     assert config.to_dict()["value"] == result
     np.testing.assert_array_equal(to_numpy(config), np.geomspace(**result))
     assert get_length(config) == len(np.geomspace(**result))
     assert sample(config) in np.geomspace(**result)
     assert config.is_categorical is False
     assert config.is_distribution is False
     assert config.is_range is True
     assert config.is_uniform is False
     assert config.is_discrete is True
     assert config.is_continuous is False
     assert get_min(config) == v1
     assert get_max(config) == v2
Exemplo n.º 9
0
def to_hyperopt(name, matrix):
    if matrix.IDENTIFIER in {
            V1HpChoice.IDENTIFIER,
            V1HpRange.IDENTIFIER,
            V1HpLinSpace.IDENTIFIER,
            V1HpLogSpace.IDENTIFIER,
            V1HpGeomSpace.IDENTIFIER,
    }:
        return hyperopt.hp.choice(name, to_numpy(matrix))

    if matrix.IDENTIFIER == V1HpPChoice.IDENTIFIER:
        raise ValidationError("{} is not supported by Hyperopt.".format(
            matrix.IDENTIFIER))

    if matrix.IDENTIFIER == V1HpUniform.IDENTIFIER:
        return hyperopt.hp.uniform(name, matrix.value.get("low"),
                                   matrix.value.get("high"))

    if matrix.IDENTIFIER == V1HpQUniform.IDENTIFIER:
        return hyperopt.hp.quniform(
            name,
            matrix.value.get("low"),
            matrix.value.get("high"),
            matrix.value.get("q"),
        )

    if matrix.IDENTIFIER == V1HpLogUniform.IDENTIFIER:
        return hyperopt.hp.loguniform(name, matrix.value.get("low"),
                                      matrix.value.get("high"))

    if matrix.IDENTIFIER == V1HpQLogUniform.IDENTIFIER:
        return hyperopt.hp.qloguniform(
            name,
            matrix.value.get("low"),
            matrix.value.get("high"),
            matrix.value.get("q"),
        )

    if matrix.IDENTIFIER == V1HpNormal.IDENTIFIER:
        return hyperopt.hp.normal(name, matrix.value.get("loc"),
                                  matrix.value.get("scale"))

    if matrix.IDENTIFIER == V1HpQNormal.IDENTIFIER:
        return hyperopt.hp.qnormal(
            name,
            matrix.value.get("loc"),
            matrix.value.get("scale"),
            matrix.value.get("q"),
        )

    if matrix.IDENTIFIER == V1HpLogNormal.IDENTIFIER:
        return hyperopt.hp.lognormal(name, matrix.value.get("loc"),
                                     matrix.value.get("scale"))

    if matrix.IDENTIFIER == V1HpQLogNormal.IDENTIFIER:
        return hyperopt.hp.qlognormal(
            name,
            matrix.value.get("loc"),
            matrix.value.get("scale"),
            matrix.value.get("q"),
        )