Skip to content

Commit 13cd0ce

Browse files
committed
Final touches to examples and advanced tour
This commit puts the final touches in the examples. It fixes a number of little bugs along the way while also making the package more consistent internally.
1 parent c6a754e commit 13cd0ce

File tree

9 files changed

+451
-282
lines changed

9 files changed

+451
-282
lines changed

bayes_opt/bayesian_optimization.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def __init__(self, f, pbounds, random_state=None, verbose=2):
7575
# Internal GP regressor
7676
self._gp = GaussianProcessRegressor(
7777
kernel=Matern(nu=2.5),
78-
alpha=1e-5,
78+
alpha=1e-6,
7979
normalize_y=True,
8080
n_restarts_optimizer=25,
8181
random_state=self._random_state,
@@ -118,7 +118,7 @@ def suggest(self, utility_function):
118118
# we don't really need to see them here.
119119
with warnings.catch_warnings():
120120
warnings.simplefilter("ignore")
121-
self._gp.fit(self._space.x, self._space.target)
121+
self._gp.fit(self._space.params, self._space.target)
122122

123123
# Finding argmax of the acquisition function.
124124
suggestion = acq_max(
@@ -157,6 +157,7 @@ def maximize(self,
157157
self._prime_subscriptions()
158158
self.dispatch(Events.OPTMIZATION_START)
159159
self._prime_queue(init_points)
160+
self.set_gp_params(**gp_params)
160161

161162
util = UtilityFunction(kind=acq, kappa=kappa, xi=xi)
162163
iteration = 0
@@ -181,3 +182,6 @@ def set_bounds(self, new_bounds):
181182
A dictionary with the parameter name and its new bounds
182183
"""
183184
self._space.set_bounds(new_bounds)
185+
186+
def set_gp_params(self, **params):
187+
self._gp.set_params(**params)

bayes_opt/event.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,9 @@ class Events:
33
OPTMIZATION_STEP = 'optmization:step'
44
OPTMIZATION_END = 'optmization:end'
55

6-
PROBE_FROM_SUGGESTION = "probe:suggestion"
7-
PROBE_FROM_QUEUE = "probe:queue"
8-
9-
ELEMENT_ADDED_TO_QUEUE = ""
10-
QUEUE_IS_EMPTY = ""
11-
126

137
DEFAULT_EVENTS = [
148
Events.OPTMIZATION_START,
159
Events.OPTMIZATION_STEP,
1610
Events.OPTMIZATION_END,
17-
Events.ELEMENT_ADDED_TO_QUEUE,
18-
Events.QUEUE_IS_EMPTY,
1911
]

bayes_opt/observer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
from .util import Colours
1010

1111

12+
class Observer:
13+
def update(self, event, instance):
14+
raise NotImplementedError
15+
16+
1217
class _Tracker:
1318
def __init__(self):
1419
self._iterations = 0

bayes_opt/target_space.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __init__(self, target_func, pbounds: dict, random_state=None):
5050
)
5151

5252
# preallocated memory for X and Y points
53-
self._x = np.empty(shape=(0, self.dim))
53+
self._params = np.empty(shape=(0, self.dim))
5454
self._target = np.empty(shape=(0))
5555

5656
# keep track of unique points we have seen so far
@@ -60,16 +60,16 @@ def __contains__(self, x):
6060
return _hashable(x) in self._cache
6161

6262
def __len__(self):
63-
assert len(self._x) == len(self._target)
63+
assert len(self._params) == len(self._target)
6464
return len(self._target)
6565

6666
@property
6767
def empty(self):
6868
return len(self) == 0
6969

7070
@property
71-
def x(self):
72-
return self._x
71+
def params(self):
72+
return self._params
7373

7474
@property
7575
def target(self):
@@ -117,7 +117,7 @@ def _as_array(self, x):
117117
assert x.size == self.dim, 'x must have the same dimensions'
118118
return x
119119

120-
def register(self, x, target):
120+
def register(self, params, target):
121121
"""
122122
Append a point and its target value to the known data.
123123
@@ -150,17 +150,17 @@ def register(self, x, target):
150150
>>> len(space)
151151
1
152152
"""
153-
x = self._as_array(x)
153+
x = self._as_array(params)
154154
if x in self:
155155
raise KeyError('Data point {} is not unique'.format(x))
156156

157157
# Insert data into unique dictionary
158158
self._cache[_hashable(x.ravel())] = target
159159

160-
self._x = np.concatenate([self._x, x.reshape(1, -1)])
160+
self._params = np.concatenate([self._params, x.reshape(1, -1)])
161161
self._target = np.concatenate([self._target, [target]])
162162

163-
def probe(self, x):
163+
def probe(self, params):
164164
"""
165165
Evaulates a single point x, to obtain the value y and then records them
166166
as observations.
@@ -179,7 +179,7 @@ def probe(self, x):
179179
y : float
180180
target function value.
181181
"""
182-
x = self._as_array(x)
182+
x = self._as_array(params)
183183

184184
try:
185185
target = self._cache[_hashable(x)]
@@ -217,15 +217,17 @@ def max(self):
217217
try:
218218
res = {
219219
'target': self.target.max(),
220-
'params': dict(zip(self.keys, self.x[self.target.argmax()]))
220+
'params': dict(
221+
zip(self.keys, self.params[self.target.argmax()])
222+
)
221223
}
222224
except ValueError:
223225
res = {}
224226
return res
225227

226228
def res(self):
227229
"""Get all target values found and corresponding parametes."""
228-
params = [dict(zip(self.keys, p)) for p in self.x]
230+
params = [dict(zip(self.keys, p)) for p in self.params]
229231

230232
return [
231233
{"target": target, "params": param}

bayes_opt/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def load_logs(optimizer, logs):
147147
iteration = json.loads(iteration)
148148
try:
149149
optimizer.register(
150-
x=iteration["params"],
150+
params=iteration["params"],
151151
target=iteration["target"],
152152
)
153153
except KeyError:

0 commit comments

Comments
 (0)