Skip to content

Commit 2ffcd6c

Browse files
committed
Added hidden option to control aqc_max. Sped up tests.
1 parent 0dce16e commit 2ffcd6c

File tree

7 files changed

+33
-13
lines changed

7 files changed

+33
-13
lines changed

.coverage

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!coverage.py: This is a private format, don't read it directly!{"arcs":{"/home/joncrall/code/BayesianOptimization/bayes_opt/helpers.py":[[55,47],[6,9],[86,87],[64,111],[39,40],[164,165],[87,-85],[-85,86],[168,169],[172,-172],[146,147],[64,67],[95,96],[135,150],[-49,49],[49,50],[161,178],[166,168],[245,-159],[104,-64],[153,154],[100,101],[96,-93],[73,75],[75,77],[159,-1],[-69,73],[175,176],[-159,159],[86,88],[176,-161],[165,166],[182,204],[141,143],[83,-69],[41,42],[-135,141],[-111,120],[2,3],[69,85],[47,61],[154,155],[144,147],[51,52],[128,129],[40,41],[172,175],[55,56],[45,46],[163,164],[125,127],[85,93],[120,124],[141,142],[147,-135],[159,161],[156,-150],[46,47],[9,64],[3,4],[130,132],[-172,172],[150,151],[5,6],[172,172],[-93,95],[-64,64],[-150,150],[150,159],[52,55],[47,49],[1,2],[88,89],[-1,1],[127,128],[102,-98],[56,57],[4,5],[124,125],[93,98],[111,135],[143,146],[-98,100],[180,-178],[129,130],[178,182],[121,-111],[101,102],[98,104],[42,45],[142,147],[120,121],[61,-9],[50,51],[77,83],[151,152],[67,69],[204,245],[89,-85],[38,39],[-178,179],[143,144],[132,-111],[152,153],[57,47],[179,180],[-161,163],[-9,38],[49,-49],[169,172],[155,156]],"/home/joncrall/code/BayesianOptimization/bayes_opt/__init__.py":[[1,2],[-1,1],[2,4],[4,-1]],"/home/joncrall/code/BayesianOptimization/bayes_opt/bayesian_optimization.py":[[-12,12],[346,-12],[253,254],[292,293],[40,41],[274,275],[279,282],[287,288],[239,241],[80,81],[85,84],[6,7],[326,331],[101,103],[45,48],[120,121],[100,101],[-14,28],[287,291],[-99,100],[283,284],[48,49],[284,287],[341,346],[12,14],[84,85],[252,253],[30,34],[65,68],[41,42],[12,-1],[195,304],[294,295],[288,291],[112,113],[70,99],[298,265],[123,149],[117,118],[291,292],[58,61],[61,63],[111,120],[269,274],[97,-70],[68,-14],[178,191],[275,279],[103,-99],[7,8],[2,4],[265,268],[-105,111],[331,336],[265,301],[247,250],[5,6],[293,294],[81,84],[14,70],[8,9],[117,-105],[99,105],[336,341],[192,193],[55,58],[88,97],[191,192],[193,194],[-1,1],[-190,230],[50,51],[28,30],[236,237],[63,64],[4,5],[301,-190],[111,112],[118,117],[1,2],[230,233],[295,298],[42,45],[237,239],[254,257],[241,244],[304,321],[116,117],[194,195],[113,116],[321,326],[-70,80],[250,251],[34,37],[37,40],[64,65],[244,247],[105,123],[282,283],[84,88],[149,178],[49,50],[268,269],[9,12],[51,55],[121,-105],[251,252],[233,236],[257,265]],"/home/joncrall/code/BayesianOptimization/bayes_opt/target_space.py":[[-77,78],[54,55],[62,64],[61,62],[179,182],[141,-111],[279,-11],[286,287],[80,111],[214,215],[176,179],[68,-66],[219,-197],[96,97],[-111,130],[262,263],[294,296],[287,-279],[212,213],[211,212],[78,-77],[25,26],[-221,224],[135,141],[-197,201],[291,292],[8,-6],[253,252],[-96,96],[292,293],[252,253],[97,103],[44,47],[94,93],[296,-279],[2,3],[-279,284],[254,-226],[218,219],[208,211],[179,180],[284,285],[289,290],[211,214],[204,207],[263,-256],[-70,72],[58,61],[189,191],[197,221],[213,214],[-143,176],[-256,261],[93,94],[256,265],[290,291],[195,-143],[207,208],[177,-143],[201,204],[-26,41],[49,51],[138,139],[51,54],[261,262],[105,104],[75,-74],[104,105],[-6,8],[96,-96],[109,-80],[55,58],[293,294],[215,218],[224,-221],[185,188],[47,49],[1,2],[-1,1],[11,-1],[140,141],[70,74],[296,298],[77,80],[111,143],[6,11],[104,108],[221,226],[3,6],[-11,11],[-74,75],[91,93],[103,104],[72,-70],[-226,251],[66,70],[180,182],[188,189],[74,77],[96,96],[26,66],[226,256],[-80,91],[194,195],[191,194],[-66,68],[143,197],[139,140],[131,133],[176,177],[182,185],[133,138],[252,254],[64,-26],[285,286],[133,135],[93,96],[265,279],[108,109],[284,289],[298,-279],[130,131],[11,25],[251,252],[41,44]]}}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ scratch/
88
.DS_Store
99
bo_eg*.png
1010
gif/
11+
htmlcov
12+
.cache

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
[![Travis](https://img.shields.io/travis/fmfn/BayesianOptimization/master.svg?label=Travis%20CI)](https://travis-ci.org/fmfn/BayesianOptimization)
44
[![Codecov](https://codecov.io/github/fmfn/BayesianOptimization/badge.svg?branch=master&service=github)](https://codecov.io/github/fmfn/BayesianOptimization?branch=master)
5-
[![Appveyor](https://ci.appveyor.com/api/projects/status/github/fmfn/BayesianOptimization?svg=True)](https://ci.appveyor.com/project/fmfn/BayesianOptimization/branch/master)
65
[![Pypi](https://img.shields.io/pypi/v/bayesian-optimization.svg)](https://pypi.python.org/pypi/bayesian-optimization)
76

87
Pure Python implementation of bayesian global optimization with gaussian

bayes_opt/bayesian_optimization.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ def __init__(self, f, pbounds, random_state=None, verbose=1):
6464
'max_params': None}
6565
self.res['all'] = {'values': [], 'params': []}
6666

67+
# non-public config for maximizing the aquisition function
68+
# (used to speedup tests, but generally leave these as is)
69+
self._acqkw = {'n_warmup': 100000, 'n_iter': 250}
70+
6771
# Verbose
6872
self.verbose = verbose
6973

@@ -251,7 +255,8 @@ def maximize(self,
251255
gp=self.gp,
252256
y_max=y_max,
253257
bounds=self.space.bounds,
254-
random_state=self.random_state)
258+
random_state=self.random_state,
259+
**self._acqkw)
255260

256261
# Print new header
257262
if self.verbose:
@@ -292,7 +297,8 @@ def maximize(self,
292297
gp=self.gp,
293298
y_max=y_max,
294299
bounds=self.space.bounds,
295-
random_state=self.random_state)
300+
random_state=self.random_state,
301+
**self._acqkw)
296302

297303
# Keep track of total number of iterations
298304
self.i += 1

bayes_opt/helpers.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
from scipy.optimize import minimize
77

88

9-
def acq_max(ac, gp, y_max, bounds, random_state):
9+
def acq_max(ac, gp, y_max, bounds, random_state, n_warmup=100000, n_iter=250):
1010
"""
1111
A function to find the maximum of the acquisition function
1212
1313
It uses a combination of random sampling (cheap) and the 'L-BFGS-B'
14-
optimization method. First by sampling 1e5 points at random, and then
15-
running L-BFGS-B from 250 random starting points.
14+
optimization method. First by sampling `n_warmup` (1e5) points at random,
15+
and then running L-BFGS-B from `n_iter` (250) random starting points.
1616
1717
Parameters
1818
----------
@@ -28,6 +28,14 @@ def acq_max(ac, gp, y_max, bounds, random_state):
2828
:param bounds:
2929
The variables bounds to limit the search of the acq max.
3030
31+
:param random_state:
32+
instance of np.RandomState random number generator
33+
34+
:param n_warmup:
35+
number of times to randomly sample the aquisition function
36+
37+
:param n_iter:
38+
number of times to run scipy.minimize
3139
3240
Returns
3341
-------
@@ -36,14 +44,14 @@ def acq_max(ac, gp, y_max, bounds, random_state):
3644

3745
# Warm up with random points
3846
x_tries = random_state.uniform(bounds[:, 0], bounds[:, 1],
39-
size=(100000, bounds.shape[0]))
47+
size=(n_warmup, bounds.shape[0]))
4048
ys = ac(x_tries, gp=gp, y_max=y_max)
4149
x_max = x_tries[ys.argmax()]
4250
max_acq = ys.max()
4351

4452
# Explore the parameter space more throughly
4553
x_seeds = random_state.uniform(bounds[:, 0], bounds[:, 1],
46-
size=(250, bounds.shape[0]))
54+
size=(n_iter, bounds.shape[0]))
4755
for x_try in x_seeds:
4856
# Find the minimum of minus the acquisition function
4957
res = minimize(lambda x: -ac(x.reshape(1, -1), gp=gp, y_max=y_max),

tests/test_bayesian_optimization.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ def test_bayes_opt_demo():
1919
random_state=random_state,
2020
verbose=0)
2121
gp_params = {'alpha': 1e-5, 'n_restarts_optimizer': 2}
22-
bo.maximize(init_points=10, n_iter=10, acq='ucb', kappa=5, **gp_params)
22+
# Change aquisition params to speedup optimization for testing purposes
23+
bo._acqkw['n_iter'] = 5
24+
bo._acqkw['n_warmup'] = 1000
25+
bo.maximize(init_points=10, n_iter=5, acq='ucb', kappa=5, **gp_params)
2326
res = bo.space.max_point()
2427
max_params = res['max_params']
2528
max_val = res['max_val']
2629

27-
assert max_val > 1.2, 'function range is ~.2 - ~1.4, should be above 1.'
28-
assert max_val / f.max() > .9, 'should be better than 90% of max val'
30+
ratio = max_val / f.max()
31+
assert max_val > 1.1, 'got {}, but should be > 1'.format(max_val)
32+
assert ratio > .9, 'got {}, should be better than 90% of max val'.format(ratio)
2933

3034
assert max_params['x'] > 300, 'should be in a peak area (around 300)'
3135
assert max_params['x'] < 400, 'should be in a peak area (around 300)'

tests/test_helper_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_acq_max_function_with_ucb_algo(self):
6363
self.setUp(kind='ucb', kappa=1.0, xi=1.0)
6464
max_arg = acq_max(
6565
self.util.utility, GP, self.y_max, bounds=np.array([[0, 1], [0, 1]]),
66-
random_state=self.random_state
66+
random_state=self.random_state, n_iter=20
6767
)
6868
_, brute_max_arg = brute_force_maximum(MESH, GP)
6969

@@ -73,7 +73,7 @@ def test_ei_max_function_with_ucb_algo(self):
7373
self.setUp(kind='ei', kappa=1.0, xi=1e-6)
7474
max_arg = acq_max(
7575
self.util.utility, GP, self.y_max, bounds=np.array([[0, 1], [0, 1]]),
76-
random_state=self.random_state
76+
random_state=self.random_state, n_iter=20
7777
)
7878
_, brute_max_arg = brute_force_maximum(MESH, GP, kind='ei')
7979

0 commit comments

Comments
 (0)