Skip to content

Commit 5acad4e

Browse files
authored
Merge pull request #77 from Erotemic/shields
Fixes for Travis
2 parents 643ba78 + fe34cbe commit 5acad4e

File tree

9 files changed

+71
-18
lines changed

9 files changed

+71
-18
lines changed

.coveragerc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[run]
2+
branch = True
3+
source = bayes_opt
4+
5+
[report]
6+
exclude_lines =
7+
pragma: no cover
8+
.* # pragma: no cover
9+
.* # nocover
10+
def __repr__
11+
raise AssertionError
12+
raise NotImplementedError
13+
if 0:
14+
verbose = .*
15+
raise
16+
pass
17+
if __name__ == .__main__.:
18+
print(.*)
19+
20+
omit =
21+
*/setup.py
22+
examples/*
23+
tests/*

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,14 @@ scratch/
88
.DS_Store
99
bo_eg*.png
1010
gif/
11+
12+
# Unit test / coverage reports
13+
htmlcov/
14+
.tox/
15+
.coverage
16+
.coverage.*
17+
.cache
18+
nosetests.xml
19+
coverage.xml
20+
*,cover
21+
.hypothesis/

.travis.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@ before_install:
1717
- pip install pytest-cov -U
1818
- pip install codecov -U
1919
#- pip install xdoctest -U
20-
# - pip install delorean
2120
install:
2221
#- travis_retry python setup.py build develop
2322
- travis_retry pip install -e .
2423
script:
25-
- travis_wait pytest --cov-config .coveragerc --cov-report html --cov bayes_opt bayes_opt
26-
#-p no:doctest --xdoctest --cov=ubelt ubelt
24+
- travis_wait pytest --cov-config .coveragerc --cov-report html --cov=bayes_opt
25+
#-p no:doctest --xdoctest
2726
after_success:
2827
#- coveralls || echo "Coveralls upload failed"
2928
- codecov
30-
#after_failure:
31-
# - cat failed_doctests.txt
3229
cache:
3330
apt: true
3431
directories:

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Bayesian Optimization
22

3+
[![Travis](https://img.shields.io/travis/fmfn/BayesianOptimization/master.svg?label=Travis%20CI)](https://travis-ci.org/fmfn/BayesianOptimization)
4+
[![Codecov](https://codecov.io/github/fmfn/BayesianOptimization/badge.svg?branch=master&service=github)](https://codecov.io/github/fmfn/BayesianOptimization?branch=master)
5+
[![Pypi](https://img.shields.io/pypi/v/bayesian-optimization.svg)](https://pypi.python.org/pypi/bayesian-optimization)
6+
37
Pure Python implementation of bayesian global optimization with gaussian
48
processes.
59

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),

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
author='Fernando Nogueira',
99
author_email="[email protected]",
1010
description='Bayesian Optimization package',
11-
download_url = 'https://github.com/fmfn/BayesianOptimization/tarball/0.4',
11+
download_url='https://github.com/fmfn/BayesianOptimization/tarball/0.4',
1212
install_requires=[
1313
"numpy >= 1.9.0",
1414
"scipy >= 0.14.0",

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)