Skip to content

Commit 8f95aef

Browse files
committed
Adds a couple of tests to make sure optimizer acq_max is not completely off. And finishes a couple of things in the migration to the new GP objects.
1 parent bd6efbc commit 8f95aef

File tree

3 files changed

+75
-29
lines changed

3 files changed

+75
-29
lines changed

bayes_opt/helpers.py

+7-15
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,19 @@ def utility(self, x, gp, y_max):
8686

8787
@staticmethod
8888
def _ucb(x, gp, kappa):
89-
mean, var = gp.predict(x, return_cov=True)
90-
return mean + kappa * np.sqrt(var)
89+
mean, std = gp.predict(x, return_std=True)
90+
return mean + kappa * std
9191

9292
@staticmethod
9393
def _ei(x, gp, y_max, xi):
94-
mean, var = gp.predict(x, return_cov=True)
95-
96-
# Avoid points with zero variance
97-
var = np.maximum(var, 1e-9 + 0 * var)
98-
99-
z = (mean - y_max - xi)/np.sqrt(var)
100-
return (mean - y_max - xi) * norm.cdf(z) + np.sqrt(var) * norm.pdf(z)
94+
mean, std = gp.predict(x, return_std=True)
95+
z = (mean - y_max - xi)/std
96+
return (mean - y_max - xi) * norm.cdf(z) + std * norm.pdf(z)
10197

10298
@staticmethod
10399
def _poi(x, gp, y_max, xi):
104-
mean, var = gp.predict(x, return_cov=True)
105-
106-
# Avoid points with zero variance
107-
var = np.maximum(var, 1e-9 + 0 * var)
108-
109-
z = (mean - y_max - xi)/np.sqrt(var)
100+
mean, std = gp.predict(x, return_std=True)
101+
z = (mean - y_max - xi)/std
110102
return norm.cdf(z)
111103

112104

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
setup(
44
name='bayesian-optimization',
5-
version='0.1.0',
5+
version='0.3.0',
66
url='https://github.com/fmfn/BayesianOptimization',
77
packages=find_packages(),
88
description='Bayesian Optimization package',
99
install_requires=[
1010
"numpy >= 1.9.0",
1111
"scipy >= 0.14.0",
12-
"scikit-learn >= 0.16.1",
12+
"scikit-learn >= 0.18.0",
1313
],
1414
)

tests/tests_helper_functions.py

+66-12
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,82 @@
11
from __future__ import print_function
22
from __future__ import division
33
import unittest
4+
import numpy as np
45
from sklearn.gaussian_process import GaussianProcessRegressor
56
from sklearn.gaussian_process.kernels import Matern
7+
import sys
8+
sys.path.append("./")
9+
from bayes_opt.helpers import UtilityFunction, acq_max
10+
11+
12+
def get_globals():
13+
X = np.array([
14+
[0.00, 0.00],
15+
[0.99, 0.99],
16+
[0.00, 0.99],
17+
[0.99, 0.00],
18+
[0.50, 0.50],
19+
[0.25, 0.50],
20+
[0.50, 0.25],
21+
[0.75, 0.50],
22+
[0.50, 0.75],
23+
])
24+
25+
def get_y(X):
26+
return -(X[:, 0] - 0.3) ** 2 - 0.5 * (X[:, 1] - 0.6)**2 + 2
27+
y = get_y(X)
28+
29+
mesh = np.dstack(
30+
np.meshgrid(np.arange(0, 1, 0.01), np.arange(0, 1, 0.01))
31+
).reshape(-1, 2)
32+
33+
GP = GaussianProcessRegressor(
34+
kernel=Matern(),
35+
n_restarts_optimizer=25,
36+
)
37+
GP.fit(X, y)
38+
39+
return {'x': X, 'y': y, 'gp': GP, 'mesh': mesh}
40+
41+
42+
def brute_force_maximum(MESH, GP, kind='ucb', kappa=1.0, xi=1e-6):
43+
uf = UtilityFunction(kind=kind, kappa=kappa, xi=xi)
44+
45+
mesh_vals = uf.utility(MESH, GP, 2)
46+
max_val = mesh_vals.max()
47+
max_arg_val = MESH[np.argmax(mesh_vals)]
48+
49+
return max_val, max_arg_val
50+
51+
52+
GLOB = get_globals()
53+
X, Y, GP, MESH = GLOB['x'], GLOB['y'], GLOB['gp'], GLOB['mesh']
654

755

856
class TestMaximizationOfAcquisitionFunction(unittest.TestCase):
957

10-
def setUp(self, msg=""):
11-
self.gp = GaussianProcessRegressor(
12-
kernel=Matern(),
13-
n_restarts_optimizer=25,
58+
def setUp(self, kind='ucb', kappa=1.0, xi=1e-6):
59+
self.util = UtilityFunction(kind=kind, kappa=kappa, xi=xi)
60+
self.episilon = 1e-2
61+
self.y_max = 2.0
62+
63+
def test_acq_max_function_with_ucb_algo(self):
64+
self.setUp(kind='ucb', kappa=1.0, xi=1.0)
65+
max_arg = acq_max(
66+
self.util.utility, GP, self.y_max, bounds=np.array([[0, 1], [0, 1]])
1467
)
68+
_, brute_max_arg = brute_force_maximum(MESH, GP)
1569

16-
X = np.array([
17-
[1, 2],
18-
[2, 3],
19-
[0, -1],
20-
])
70+
self.assertTrue( all(abs(brute_max_arg - max_arg) < self.episilon))
2171

22-
y = X[:, 0]**2 - 2 * X[:, 1]
72+
def test_ei_max_function_with_ucb_algo(self):
73+
self.setUp(kind='ei', kappa=1.0, xi=1e-6)
74+
max_arg = acq_max(
75+
self.util.utility, GP, self.y_max, bounds=np.array([[0, 1], [0, 1]])
76+
)
77+
_, brute_max_arg = brute_force_maximum(MESH, GP, kind='ei')
2378

24-
def test_something(self):
25-
pass
79+
self.assertTrue( all(abs(brute_max_arg - max_arg) < self.episilon))
2680

2781

2882
if __name__ == '__main__':

0 commit comments

Comments
 (0)