Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions bayes_opt/bayesian_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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())

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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])

Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions bayes_opt/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down