Skip to content

Commit

Permalink
Fix initial point sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
dwoiwode committed Aug 10, 2022
1 parent 4937a2d commit 6ba01b5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyPDP/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
author = "Yannik Mahlau and Dominik Woiwode"
author_email = "[email protected]"
description = "A python implementation of 'Explaining Hyperparameter Optimization via Partial Dependence Plots' by Moosbauer et al."
version = "0.1.7"
version = "0.1.8"
license = "MIT"
url = "https://github.com/dwoiwode/py-pdp-partitioner"
9 changes: 5 additions & 4 deletions pyPDP/sampler/bayesian_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ def _sample_initial_points(self, max_sampled_points=None):
else:
sampled_points = min(self.initial_points, max_sampled_points)

self.config_list = self.config_space.sample_configuration(sampled_points)
if self.initial_points == 1: # for a single value, the sampling does not return a list
self.config_list = [self.config_list]
configs = self.config_space.sample_configuration(sampled_points)
if sampled_points == 1: # for a single value, the sampling does not return a list
configs = [configs]

self.y_list = [self.obj_func(**config) for config in self.config_list]
self.y_list += [self.obj_func(**config) for config in configs]
self.config_list += configs
self.fit_surrogate(force=True)

def fit_surrogate(self, force: bool = False):
Expand Down
32 changes: 32 additions & 0 deletions tests/sampler/test_optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,35 @@ def test_cache_different_acq_class(self):

self.assertGreater(t_dif_1, 0.1) # If values are loaded from cache, it is pretty fast
self.assertGreater(t_dif_2, 0.1) # If values are loaded from cache, it is pretty fast

def test_sample_1_init(self):
f = Square.for_n_dimensions(5, seed=0)
cs = f.config_space
# First try with seed
bo = BayesianOptimizationSampler(
obj_func=f,
config_space=cs,
initial_points=10,
)
bo.clear_cache()

bo.sample(1)
bo.sample(1)
bo.sample(1)

self.assertEqual(3, len(bo))

def test_sample_1_after_init(self):
f = Square.for_n_dimensions(5, seed=0)
cs = f.config_space
# First try with seed
bo = BayesianOptimizationSampler(
obj_func=f,
config_space=cs,
initial_points=5,
)

bo.sample(5)
bo.sample(1)
bo.sample(1)
self.assertEqual(7, len(bo))

0 comments on commit 6ba01b5

Please sign in to comment.