def obtain_latin_hypercube(self): hps = self.hp_utils.config_space._hyperparameters hp_idx = {"numerical": [], "categorical": []} choices = [] for var_name, hp in hps.items(): idx = self.hp_utils.config_space._hyperparameter_idx[var_name] dist = utils.distribution_type(self.hp_utils.config_space, var_name) if dist is str or dist is bool: hp_idx["categorical"].append(idx) choices.append(hp.choices) else: hp_idx["numerical"].append(idx) n_num_dim = len(hp_idx["numerical"]) n_dim = n_num_dim + len(hp_idx["categorical"]) rnd_grid = np.array([ self.rng.permutation(list(range(1, self.n_points + 1))) for _ in range(n_num_dim) ]) hp_confs = [[ self.value_in_grid(rnd_grid, hp_idx, i, k, choices) for i in range(n_dim) ] for k in range(self.n_points)] hp_confs = self.hp_utils.revert_hp_confs(hp_confs) return hp_confs
def _initial_sampler(self): """ random sampling for an initialization Returns ------- hyperparameter configurations: list """ hps = self.hp_utils.config_space._hyperparameters sample = [None for _ in range(len(hps))] for var_name, hp in hps.items(): idx = self.hp_utils.config_space._hyperparameter_idx[var_name] dist = utils.distribution_type(self.hp_utils.config_space, var_name) if dist is str or dist is bool: # categorical choices = hp.choices rnd = self.rng.randint(len(choices)) sample[idx] = choices[rnd] else: # numerical rnd = self.rng.uniform() sample[idx] = self.hp_utils.revert_hp(rnd, var_name) return sample
def sample(self): hps_conf, _ = self.hp_utils.load_hps_conf(convert=True, do_sort=True, index_from_conf=False) hp_conf = [] for idx, hps in enumerate(hps_conf): n_lower = self.gamma_func(len(hps)) lower_hps, upper_hps = hps[:n_lower], hps[n_lower:] var_name = self.hp_utils.config_space._idx_to_hyperparameter[idx] var_type = utils.distribution_type(self.hp_utils.config_space, var_name) if var_type in [float, int]: pe_lower, pe_upper = self._construct_numerical_parzen_estimator( var_name, var_type, lower_hps, upper_hps) hp_value = self._compare_candidates(pe_lower, pe_upper) else: pe_lower, pe_upper, choices = self._construct_categorical_parzen_estimator( var_name, lower_hps, upper_hps) hp_value = self._compare_candidates(pe_lower, pe_upper, choices) hp_conf.append(hp_value) return self.hp_utils.revert_hp_conf(hp_conf)
def sample(self): hps_conf, _ = self.hp_utils.load_hps_conf(convert=True, do_sort=True, index_from_conf=False) hp_confs = [] # ndarray (D, n_ei_candidates) choices_list = [] n_evals = len(hps_conf[0]) n_lower = self.gamma_func(n_evals) basis_loglikelihoods_lower = np.zeros( (len(hps_conf), n_lower + 1, self.n_ei_candidates)) basis_loglikelihoods_upper = np.zeros( (len(hps_conf), n_evals - n_lower + 1, self.n_ei_candidates)) for idx, hps in enumerate(hps_conf): lower_hps, upper_hps = hps[:n_lower], hps[n_lower:] var_name = self.hp_utils.config_space._idx_to_hyperparameter[idx] var_type = utils.distribution_type(self.hp_utils.config_space, var_name) if var_type in [float, int]: pe_lower, pe_upper = self._construct_numerical_parzen_estimator( var_name, var_type, lower_hps, upper_hps) choices_list.append(None) else: pe_lower, pe_upper, choices = self._construct_categorical_parzen_estimator( var_name, lower_hps, upper_hps) choices_list.append(choices) samples_lower = pe_lower.sample_from_density_estimator( self.rng, self.n_ei_candidates) hp_confs.append(samples_lower) basis_loglikelihoods_lower[idx] += pe_lower.basis_loglikelihood( samples_lower) basis_loglikelihoods_upper[idx] += pe_upper.basis_loglikelihood( samples_lower) hp_conf = self._compare_configurations(basis_loglikelihoods_lower, basis_loglikelihoods_upper, hp_confs, choices_list) return self.hp_utils.revert_hp_conf(hp_conf)
def sample(self): hps_conf, _ = self.hp_utils.load_hps_conf(convert=True, do_sort=True, index_from_conf=False) hp_conf = [] for idx, hps in enumerate(hps_conf): n_lower = self.gamma_func(len(hps)) lower_vals, upper_vals = hps[:n_lower], hps[n_lower:] var_name = self.hp_utils.config_space._idx_to_hyperparameter[idx] var_type = utils.distribution_type(self.hp_utils.config_space, var_name) if var_type in [float, int]: hp_value = self._sample_numerical(var_name, var_type, lower_vals, upper_vals) else: hp_value = self._sample_categorical(var_name, lower_vals, upper_vals) hp_conf.append(hp_value) return self.hp_utils.revert_hp_conf(hp_conf)