def test_domain(self): ps = ParamSpace() ps.add_value("value", True) domain = ps.domain("value") self.assertIn("domain", domain) self.assertIn("dtype", domain) self.assertEqual(DTypes.CATEGORICAL.value, domain["dtype"]) ps.add_list("bool", [True, False, True]) domain = ps.domain("bool") self.assertIn("domain", domain) self.assertIn("dtype", domain) self.assertEqual(DTypes.CATEGORICAL.value, domain["dtype"]) self.assertListEqual([True, False], domain["domain"]) ps.add_range("bounds", 0, 10, dtype=float) domain = ps.domain("bounds") self.assertIn("domain", domain) self.assertIn("dtype", domain) self.assertIn("prior", domain) self.assertEqual("float", domain["dtype"]) self.assertEqual("uniform", domain["prior"]) ps.add_random("random", 0, 10, prior="log-uniform", dtype=float) domain = ps.domain("bounds") self.assertIn("domain", domain) self.assertIn("dtype", domain) self.assertIn("prior", domain) self.assertEqual("float", domain["dtype"]) self.assertEqual("uniform", domain["prior"])
def params_to_skopt(param_space: ParamSpace): """ Converts a parameter space to a list of Dimention objects that can be used with a skopt Optimizer. A skopt Optimizer only receives 3 types of Dimensions: Categorical, Real, or Integer we convert parameters from our parameter space into one of those 3 types. Note that we only convert parameters that have either bounds or with a categorical domain with more than 1 value. If we have constant values in our parameter space, these don't need to be optimized anyway. Another function is provided to convert skopt output values back into a dictionary with a full configuration according to the parameter space (@see values_to_params). Args: param_space: a ParameterSpace where we can get the domain of each parameter Returns: a list of Dimension that can be passed to a skopt Optimizer """ dimensions = [] for param_name in param_space.param_names(): domain_param = param_space.domain(param_name) domain = domain_param["domain"] dtype = DTypes.from_type(domain_param["dtype"]) if len(domain) > 1: if dtype == DTypes.INT: low = min(domain) high = max(domain) dimensions.append(Integer(low, high, name=param_name)) elif dtype == DTypes.FLOAT: low = min(domain) high = max(domain) prior = domain_param.get("prior", None) dimensions.append(Real(low, high, prior=prior, name=param_name)) elif dtype == DTypes.CATEGORICAL: prior = domain_param.get("prior", None) dimensions.append( Categorical(domain, prior, transform="onehot", name=param_name)) return dimensions