Skip to content

Commit

Permalink
UPDATE: example for bo_ei
Browse files Browse the repository at this point in the history
  • Loading branch information
jungtaekkim committed Jun 11, 2018
1 parent 179187c commit 7b167cd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bayeso/bo.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _get_initial(self, is_random=False, is_grid=False, fun_obj=None, int_seed=No
def _optimize_objective(self, fun_acq, X_train, Y_train, X_test, cov_X_X, inv_cov_X_X, hyps):
X_test = np.atleast_2d(X_test)
pred_mean, pred_std = gp.predict_test_(X_train, Y_train, X_test, cov_X_X, inv_cov_X_X, hyps, self.str_cov, self.prior_mu)
result_acq = fun_acq(pred_mean, pred_std, Y_train)
result_acq = fun_acq(np.squeeze(pred_mean), np.squeeze(pred_std), Y_train)
return result_acq

def _optimize(self, fun_obj, is_grid_optimized=True, verbose=False):
Expand Down
16 changes: 8 additions & 8 deletions bayeso/utils/utils_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ def plot_gp(X_train, Y_train, X_test, mu, sigma,

if Y_test_truth is not None:
ax.plot(X_test.flatten(), Y_test_truth.flatten(),
c=colors[2],
c=colors[1],
linewidth=4,
marker='None')
ax.plot(X_test.flatten(), mu.flatten(),
c=colors[1],
c=colors[2],
linewidth=4,
marker='None')
ax.fill_between(X_test.flatten(),
mu.flatten() - range_shade * sigma.flatten(),
mu.flatten() + range_shade * sigma.flatten(),
color=colors[1],
color=colors[2],
alpha=0.3)
ax.plot(X_train.flatten(), Y_train.flatten(),
'x',
Expand Down Expand Up @@ -177,7 +177,7 @@ def plot_bo_step(X_train, Y_train, X_test, Y_test, mean_test, std_test,
ax.fill_between(X_test.flatten(),
mean_test.flatten() - range_shade * std_test.flatten(),
mean_test.flatten() + range_shade * std_test.flatten(),
color='blue',
color='b',
alpha=0.3)
if num_init is not None:
if X_train.shape[0] > num_init:
Expand All @@ -190,7 +190,7 @@ def plot_bo_step(X_train, Y_train, X_test, Y_test, mean_test, std_test,
ax.plot(X_train[:X_train.shape[0]-1, :], Y_train[:X_train.shape[0]-1, :], 'rx', ms=14, markeredgewidth=6)
ax.plot(X_train[X_train.shape[0]-1, :], Y_train[X_train.shape[0]-1, :], c='orange', marker='+', ms=18, markeredgewidth=6)

ax.set_xlabel('$x$', fontsize=32)
ax.set_xlabel('$\mathbf{x}$', fontsize=32)
ax.set_ylabel('$y$', fontsize=32)
ax.set_xlim([np.min(X_test), np.max(X_test)])
ax.tick_params(labelsize=22)
Expand Down Expand Up @@ -222,7 +222,7 @@ def plot_bo_step_acq(X_train, Y_train, X_test, Y_test, mean_test, std_test, acq_
ax1.fill_between(X_test.flatten(),
mean_test.flatten() - range_shade * std_test.flatten(),
mean_test.flatten() + range_shade * std_test.flatten(),
color='blue',
color='b',
alpha=0.3)
if num_init is not None:
if X_train.shape[0] > num_init:
Expand All @@ -236,8 +236,8 @@ def plot_bo_step_acq(X_train, Y_train, X_test, Y_test, mean_test, std_test, acq_
ax1.tick_params(labelsize=22)

ax2.plot(X_test, acq_test, 'b', linewidth=4)
ax2.set_xlabel('$x$', fontsize=32)
ax2.set_ylabel('Acq.', fontsize=32)
ax2.set_xlabel('$\mathbf{x}$', fontsize=32)
ax2.set_ylabel('acq.', fontsize=32)
ax2.spines['top'].set_color('none')
ax2.spines['right'].set_color('none')
ax2.set_xlim([np.min(X_test), np.max(X_test)])
Expand Down
17 changes: 9 additions & 8 deletions examples/example_bo_ei.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import numpy as np
import sys
sys.path.append('../bayeso')

import gp
import bo
import acquisition
import utils
from bayeso import gp
from bayeso import bo
from bayeso import acquisition
from bayeso.utils import utils_plot


def fun_target(X):
return 4.0 * np.cos(X) + 0.1 * X + 2.0 * np.sin(X) + 0.4 * (X - 0.5)**2
Expand All @@ -17,6 +16,7 @@ def main():
[1],
[2],
])
num_init = X_train.shape[0]
model_bo = bo.BO(np.array([[-6., 6.]]))
X_test = np.linspace(-6, 6, 400)
X_test = np.reshape(X_test, (400, 1))
Expand All @@ -28,8 +28,9 @@ def main():
acq_test = np.reshape(acq_test, (acq_test.shape[0], 1))
X_train = np.vstack((X_train, next_x))
Y_train = fun_target(X_train)
utils.plot_bo_step(X_train, Y_train, X_test, fun_target(X_test), mu_test, sigma_test, '../results/gp/', 'test_cos_' + str(ind_), 4)
utils.plot_bo_step_acq(X_train, Y_train, X_test, fun_target(X_test), mu_test, sigma_test, acq_test, '../results/gp/', 'test_cos_' + str(ind_), 4)
utils_plot.plot_bo_step(X_train, Y_train, X_test, fun_target(X_test), mu_test, sigma_test, '../results/gp/', 'test_cos_' + str(ind_), num_init)
utils_plot.plot_bo_step_acq(X_train, Y_train, X_test, fun_target(X_test), mu_test, sigma_test, acq_test, '../results/gp/', 'test_cos_' + str(ind_), num_init)


if __name__ == '__main__':
main()
Expand Down

0 comments on commit 7b167cd

Please sign in to comment.