class BOOptimizer(object): def __init__(self, hptuning_config): self.hptuning_config = hptuning_config self.n_initial_trials = self.hptuning_config.bo.n_initial_trials self.space = SearchSpace(hptuning_config=hptuning_config) self.utility_function = UtilityFunction( config=hptuning_config.bo.utility_function, seed=hptuning_config.seed) self.n_warmup = hptuning_config.bo.utility_function.n_warmup or 5000 self.n_iter = hptuning_config.bo.utility_function.n_iter or 150 def _maximize(self): """ Find argmax of the acquisition function.""" if not self.space.is_observations_valid(): return None y_max = self.space.y.max() self.utility_function.gaussian_process.fit(self.space.x, self.space.y) return self.utility_function.max_compute(y_max=y_max, bounds=self.space.bounds, n_warmup=self.n_warmup, n_iter=self.n_iter) def add_observations(self, configs, metrics): # Turn configs and metrics into data points self.space.add_observations(configs=configs, metrics=metrics) def get_suggestion(self): x = self._maximize() return self.space.get_suggestion(x)
def test_space_get_suggestion(self): space1 = SearchSpace(hptuning_config=self.manager1.hptuning_config) suggestion = space1.get_suggestion(suggestion=[1, 1, 1]) assert suggestion == {'feature1': 1, 'feature2': 1, 'feature3': 1} suggestion = space1.get_suggestion(suggestion=[1, 1.2, 2]) assert suggestion == {'feature1': 1, 'feature2': 1.25, 'feature3': 2} suggestion = space1.get_suggestion(suggestion=[1, 1.5, 3]) assert suggestion == {'feature1': 1, 'feature2': 1.5, 'feature3': 3} space2 = SearchSpace(hptuning_config=self.manager2.hptuning_config) suggestion = space2.get_suggestion(suggestion=[1, 1, 1, 1, 1, 0, 0]) assert suggestion == {'feature1': 1, 'feature2': 1, 'feature3': 1, 'feature4': 1, 'feature5': 'a'} suggestion = space2.get_suggestion(suggestion=[1, 1.2, 2, 3, 0, 0, 1]) assert suggestion == {'feature1': 1, 'feature2': 1, 'feature3': 2, 'feature4': 3, 'feature5': 'c'} suggestion = space2.get_suggestion(suggestion=[1, 1.8, 3, 3, 0, 1, 0]) assert suggestion == {'feature1': 1, 'feature2': 2, 'feature3': 3, 'feature4': 3, 'feature5': 'b'}