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
def config_space2string(config_space: ConfigurationSpace): pattern = r'[,|{}\'=<>&]' for hp in config_space.get_hyperparameters(): if re.search(pattern, hp.name): raise NameError('Invalid character in hyperparameter name!') if hasattr(hp, 'choices'): for value in hp.choices: if re.search(pattern, value): raise NameError('Invalid character in categorical hyperparameter value!') return str(config_space)
def __init__(self, config_space: ConfigurationSpace, size, lower_bounds=None, upper_bounds=None, random_state=None): """ Parameters ---------- config_space : ConfigurationSpace ConfigurationSpace to do sampling. size : int N Number of samples. lower_bounds : lower bounds in [0, 1] for continuous dimensions (optional) upper_bounds : upper bounds in [0, 1] for continuous dimensions (optional) """ self.config_space = config_space self.search_dims = [] for i, param in enumerate(config_space.get_hyperparameters()): if isinstance(param, UniformFloatHyperparameter): self.search_dims.append((0.0, 1.0)) elif isinstance(param, UniformIntegerHyperparameter): self.search_dims.append((0.0, 1.0)) else: raise NotImplementedError( 'Only Integer and Float are supported in %s.' % self.__class__.__name__) self.size = size default_lb, default_ub = zip(*self.search_dims) self.lower_bounds = np.array( default_lb) if lower_bounds is None else np.clip( lower_bounds, default_lb, default_ub) self.upper_bounds = np.array( default_ub) if upper_bounds is None else np.clip( upper_bounds, default_lb, default_ub) self.rng = check_random_state(random_state)