From 49a653081bea2dd3a8196f1184720928e258e20a Mon Sep 17 00:00:00 2001 From: Xiaoxu Guo Date: Sat, 7 Oct 2017 15:42:15 +0800 Subject: [PATCH] added random state --- bayes_opt/bayesian_optimization.py | 20 +++++++++++++++----- bayes_opt/helpers.py | 6 +++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/bayes_opt/bayesian_optimization.py b/bayes_opt/bayesian_optimization.py index f497431f3..c28399d54 100644 --- a/bayes_opt/bayesian_optimization.py +++ b/bayes_opt/bayesian_optimization.py @@ -9,7 +9,7 @@ class BayesianOptimization(object): - def __init__(self, f, pbounds, verbose=1): + def __init__(self, f, pbounds, random_state=None, verbose=1): """ :param f: Function to be maximized. @@ -25,6 +25,13 @@ def __init__(self, f, pbounds, verbose=1): # Store the original dictionary self.pbounds = pbounds + if random_state is None: + self.random_state = np.random.RandomState() + elif isinstance(random_state, int): + self.random_state = np.random.RandomState(random_state) + else: + self.random_state = random_state + # Get the name of the parameters self.keys = list(pbounds.keys()) @@ -59,6 +66,7 @@ def __init__(self, f, pbounds, verbose=1): self.gp = GaussianProcessRegressor( kernel=Matern(nu=2.5), n_restarts_optimizer=25, + random_state=self.random_state ) # Utility Function placeholder @@ -87,7 +95,7 @@ def init(self, init_points): """ # Generate random points - l = [np.random.uniform(x[0], x[1], size=init_points) + l = [self.random_state.uniform(x[0], x[1], size=init_points) for x in self.bounds] # Concatenate new random points to possible existing @@ -276,7 +284,8 @@ def maximize(self, x_max = acq_max(ac=self.util.utility, gp=self.gp, y_max=y_max, - bounds=self.bounds) + bounds=self.bounds, + random_state=self.random_state) # Print new header if self.verbose: @@ -293,7 +302,7 @@ def maximize(self, pwarning = False if np.any((self.X - x_max).sum(axis=1) == 0): - x_max = np.random.uniform(self.bounds[:, 0], + x_max = self.random_state.uniform(self.bounds[:, 0], self.bounds[:, 1], size=self.bounds.shape[0]) @@ -315,7 +324,8 @@ def maximize(self, x_max = acq_max(ac=self.util.utility, gp=self.gp, y_max=y_max, - bounds=self.bounds) + bounds=self.bounds, + random_state=self.random_state) # Print stuff if self.verbose: diff --git a/bayes_opt/helpers.py b/bayes_opt/helpers.py index aa8d0fb13..eb3159c27 100644 --- a/bayes_opt/helpers.py +++ b/bayes_opt/helpers.py @@ -6,7 +6,7 @@ from scipy.optimize import minimize -def acq_max(ac, gp, y_max, bounds): +def acq_max(ac, gp, y_max, bounds, random_state): """ A function to find the maximum of the acquisition function @@ -35,14 +35,14 @@ def acq_max(ac, gp, y_max, bounds): """ # Warm up with random points - x_tries = np.random.uniform(bounds[:, 0], bounds[:, 1], + x_tries = random_state.uniform(bounds[:, 0], bounds[:, 1], size=(100000, bounds.shape[0])) ys = ac(x_tries, gp=gp, y_max=y_max) x_max = x_tries[ys.argmax()] max_acq = ys.max() # Explore the parameter space more throughly - x_seeds = np.random.uniform(bounds[:, 0], bounds[:, 1], + x_seeds = random_state.uniform(bounds[:, 0], bounds[:, 1], size=(250, bounds.shape[0])) for x_try in x_seeds: # Find the minimum of minus the acquisition function