Skip to content

Commit

Permalink
Bayesian optimization fixed for multidimensional functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-cortex committed Jun 27, 2019
1 parent 48a2254 commit 39336f2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
26 changes: 26 additions & 0 deletions examples/bayesian_optimization_multidim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import numpy as np
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import Matern
from modAL.models import BayesianOptimizer
from modAL.acquisition import max_EI


# generating the data
x1, x2 = np.linspace(0, 10, 11).reshape(-1, 1), np.linspace(0, 10, 11).reshape(-1, 1)
x1, x2 = np.meshgrid(x1, x2)
X = np.concatenate((x1.reshape(-1, 1), x2.reshape(-1, 1)), axis=1)

y = np.sin(np.linalg.norm(X, axis=1))/2 - ((10 - np.linalg.norm(X, axis=1))**2)/50 + 2

# assembling initial training set
X_initial, y_initial = X[:10], y[:10]

# defining the kernel for the Gaussian process
kernel = Matern(length_scale=1.0)

optimizer = BayesianOptimizer(estimator=GaussianProcessRegressor(kernel=kernel),
X_training=X_initial, y_training=y_initial,
query_strategy=max_EI)

query_idx, query_inst = optimizer.query(X)
optimizer.teach(X[query_idx].reshape(1, -1), y[query_idx])
6 changes: 3 additions & 3 deletions modAL/acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def optimizer_PI(optimizer: BaseLearner, X: modALinput, tradeoff: float = 0) ->
"""
try:
mean, std = optimizer.predict(X, return_std=True)
std = std.reshape(-1, 1)
mean, std = mean.reshape(-1, ), std.reshape(-1, )
except NotFittedError:
mean, std = np.zeros(shape=(X.shape[0], 1)), np.ones(shape=(X.shape[0], 1))

Expand All @@ -68,7 +68,7 @@ def optimizer_EI(optimizer: BaseLearner, X: modALinput, tradeoff: float = 0) ->
"""
try:
mean, std = optimizer.predict(X, return_std=True)
std = std.reshape(-1, 1)
mean, std = mean.reshape(-1, ), std.reshape(-1, )
except NotFittedError:
mean, std = np.zeros(shape=(X.shape[0], 1)), np.ones(shape=(X.shape[0], 1))

Expand All @@ -89,7 +89,7 @@ def optimizer_UCB(optimizer: BaseLearner, X: modALinput, beta: float = 1) -> np.
"""
try:
mean, std = optimizer.predict(X, return_std=True)
std = std.reshape(-1, 1)
mean, std = mean.reshape(-1, ), std.reshape(-1, )
except NotFittedError:
mean, std = np.zeros(shape=(X.shape[0], 1)), np.ones(shape=(X.shape[0], 1))

Expand Down
12 changes: 6 additions & 6 deletions tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ def test_acquisition_functions(self):

def test_optimizer_PI(self):
for n_samples in range(1, 100):
mean = np.random.rand(n_samples, 1)
std = np.random.rand(n_samples, 1)
mean = np.random.rand(n_samples, )
std = np.random.rand(n_samples, )
tradeoff = np.random.rand()
max_val = np.random.rand()

Expand Down Expand Up @@ -239,8 +239,8 @@ def test_optimizer_PI(self):

def test_optimizer_EI(self):
for n_samples in range(1, 100):
mean = np.random.rand(n_samples, 1)
std = np.random.rand(n_samples, 1)
mean = np.random.rand(n_samples, )
std = np.random.rand(n_samples, )
tradeoff = np.random.rand()
max_val = np.random.rand()

Expand Down Expand Up @@ -272,8 +272,8 @@ def test_optimizer_EI(self):

def test_optimizer_UCB(self):
for n_samples in range(1, 100):
mean = np.random.rand(n_samples, 1)
std = np.random.rand(n_samples, 1)
mean = np.random.rand(n_samples, )
std = np.random.rand(n_samples, )
beta = np.random.rand()

# 1. fitted estimator
Expand Down

0 comments on commit 39336f2

Please sign in to comment.