Skip to content

Commit

Permalink
UPDATE: all
Browse files Browse the repository at this point in the history
  • Loading branch information
jungtaekkim committed Jun 3, 2019
1 parent c28a7ec commit 2a3efa4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
25 changes: 11 additions & 14 deletions bayeso/gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time
import numpy as np
import scipy
import scipy.linalg
import scipy.optimize

from bayeso import covariance
Expand Down Expand Up @@ -35,7 +36,7 @@ def _check_str_cov(str_fun, str_cov, shape_X1, shape_X2=None):
def get_prior_mu(prior_mu, X):
assert isinstance(X, np.ndarray)
assert callable(prior_mu) or prior_mu is None
assert len(X.shape) == 2
assert len(X.shape) == 2 or len(X.shape) == 3

if prior_mu is None:
prior_mu_X = np.zeros((X.shape[0], 1))
Expand Down Expand Up @@ -70,7 +71,7 @@ def get_kernel_cholesky(X_train, hyps, str_cov,

cov_X_X = covariance.cov_main(str_cov, X_train, X_train, hyps) + hyps['noise']**2 * np.eye(X_train.shape[0])
cov_X_X = (cov_X_X + cov_X_X.T) / 2.0
lower = np.linalg.cholesky(cov_X_X)
lower = scipy.linalg.cholesky(cov_X_X, lower=True)
return cov_X_X, lower

def log_ml(X_train, Y_train, hyps, str_cov, prior_mu_train,
Expand All @@ -95,14 +96,10 @@ def log_ml(X_train, Y_train, hyps, str_cov, prior_mu_train,
new_Y_train = Y_train - prior_mu_train
if is_cholesky:
cov_X_X, lower = get_kernel_cholesky(X_train, hyps, str_cov, debug=debug)
try:
lower_new_Y_train, _, _, _ = np.linalg.lstsq(lower, new_Y_train, rcond=None)
except: # pragma: no cover
lower_new_Y_train, _, _, _ = np.linalg.lstsq(lower, new_Y_train, rcond=-1)
try:
alpha, _, _, _ = np.linalg.lstsq(lower.T, lower_new_Y_train, rcond=None)
except: # pragma: no cover
alpha, _, _, _ = np.linalg.lstsq(lower.T, lower_new_Y_train, rcond=-1)

lower_new_Y_train = scipy.linalg.cho_solve((lower, True), new_Y_train)
alpha = scipy.linalg.cho_solve((lower.T, True), lower_new_Y_train)

first_term = -0.5 * np.dot(new_Y_train.T, alpha)
second_term = -1.0 * np.sum(np.log(np.diagonal(lower) + constants.JITTER_LOG))
else:
Expand Down Expand Up @@ -192,8 +189,8 @@ def get_optimized_kernel(X_train, Y_train, prior_mu, str_cov,
neg_log_ml = lambda hyps: -1.0 * log_ml(X_train, Y_train, hyps, str_cov, prior_mu_train, is_fixed_noise=is_fixed_noise, debug=debug)
elif str_modelselection_method == 'loocv':
neg_log_ml = lambda hyps: -1.0 * log_pseudo_l_loocv(X_train, Y_train, hyps, str_cov, prior_mu_train, is_fixed_noise=is_fixed_noise, debug=debug)
else:
raise ValueError('get_optimized_kernel: missing condition for str_modelselection_method.')
else: # pragma: no cover
raise ValueError('get_optimized_kernel: missing conditions for str_modelselection_method.')

hyps_converted = utils_covariance.convert_hyps(
str_cov,
Expand All @@ -210,9 +207,9 @@ def get_optimized_kernel(X_train, Y_train, prior_mu, str_cov,
result_optimized = result_optimized.x
# TODO: Fill this conditions
elif str_optimizer_method == 'DIRECT':
pass
raise NotImplementedError('get_optimized_kernel: allowed str_optimizer_method, but it is not implemented.')
elif str_optimizer_method == 'CMA-ES':
pass
raise NotImplementedError('get_optimized_kernel: allowed str_optimizer_method, but it is not implemented.')
else:
raise ValueError('get_optimized_kernel: missing condition for str_optimizer_method')

Expand Down
14 changes: 7 additions & 7 deletions bayeso/utils/utils_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def plot_gp(X_train, Y_train, X_test, mu, sigma,
format='pdf',
transparent=True,
bbox_inches='tight',
frameon=False
# frameon=False
)
plt.ion()
if is_pause:
Expand Down Expand Up @@ -197,12 +197,12 @@ def plot_minimum(arr_minima, list_str_label, int_init, is_std,
str_figure = 'minimum_mean_std_' + str_postfix
else:
str_figure = 'minimum_mean_only_' + str_postfix
plt.savefig(os.path.join(path_save, str_figure + '.pdf'), format='pdf', transparent=True, bbox_inches='tight', frameon=False)
plt.savefig(os.path.join(path_save, str_figure + '.pdf'), format='pdf', transparent=True, bbox_inches='tight')#, frameon=False)

if not is_legend:
fig_legend = pylab.figure(figsize=(3, 2))
fig_legend.legend(lines, list_str_label, 'center', fancybox=False, edgecolor='black', fontsize=32)
fig_legend.savefig(os.path.join(path_save, 'legend_{}.pdf'.format(str_postfix)), format='pdf', transparent=True, bbox_inches='tight', frameon=False)
fig_legend.savefig(os.path.join(path_save, 'legend_{}.pdf'.format(str_postfix)), format='pdf', transparent=True, bbox_inches='tight')#, frameon=False)
plt.ion()
plt.pause(time_pause)
plt.close('all')
Expand Down Expand Up @@ -295,12 +295,12 @@ def plot_minimum_time(arr_times, arr_minima, list_str_label, int_init, is_std,
str_figure = 'minimum_time_mean_std_' + str_postfix
else:
str_figure = 'minimum_time_mean_only_' + str_postfix
plt.savefig(os.path.join(path_save, str_figure + '.pdf'), format='pdf', transparent=True, bbox_inches='tight', frameon=False)
plt.savefig(os.path.join(path_save, str_figure + '.pdf'), format='pdf', transparent=True, bbox_inches='tight')#, frameon=False)

if not is_legend:
fig_legend = pylab.figure(figsize=(3, 2))
fig_legend.legend(lines, list_str_label, 'center', fancybox=False, edgecolor='black', fontsize=32)
fig_legend.savefig(os.path.join(path_save, 'legend_{}.pdf'.format(str_postfix)), format='pdf', transparent=True, bbox_inches='tight', frameon=False)
fig_legend.savefig(os.path.join(path_save, 'legend_{}.pdf'.format(str_postfix)), format='pdf', transparent=True, bbox_inches='tight')#, frameon=False)
plt.ion()
plt.pause(time_pause)
plt.close('all')
Expand Down Expand Up @@ -380,7 +380,7 @@ def plot_bo_step(X_train, Y_train, X_test, Y_test, mean_test, std_test,
ax.set_xlim([np.min(X_test), np.max(X_test)])
ax.tick_params(labelsize=22)
if path_save is not None and str_postfix is not None:
plt.savefig(os.path.join(path_save, 'bo_step_{}.pdf'.format(str_postfix)), format='pdf', transparent=True, bbox_inches='tight', frameon=False)
plt.savefig(os.path.join(path_save, 'bo_step_{}.pdf'.format(str_postfix)), format='pdf', transparent=True, bbox_inches='tight')#, frameon=False)
plt.ion()
plt.pause(time_pause)
plt.close('all')
Expand Down Expand Up @@ -473,7 +473,7 @@ def plot_bo_step_acq(X_train, Y_train, X_test, Y_test, mean_test, std_test, acq_
if is_acq_axis_small:
ax2.tick_params('y', labelsize=14)
if path_save is not None and str_postfix is not None:
plt.savefig(os.path.join(path_save, 'bo_step_acq_{}.pdf'.format(str_postfix)), format='pdf', transparent=True, bbox_inches='tight', frameon=False)
plt.savefig(os.path.join(path_save, 'bo_step_acq_{}.pdf'.format(str_postfix)), format='pdf', transparent=True, bbox_inches='tight')#, frameon=False)
plt.ion()
plt.pause(time_pause)
plt.close('all')
11 changes: 10 additions & 1 deletion tests/test_gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def test_log_ml():

log_ml = gp.log_ml(X, Y, arr_hyps, str_cov, prior_mu_X)
print(log_ml)
truth_log_ml = -65.74995266591506
truth_log_ml = -65.74302420122783
assert np.abs(log_ml - truth_log_ml) < TEST_EPSILON

log_ml = gp.log_ml(X, Y, arr_hyps, str_cov, prior_mu_X, is_cholesky=False)
Expand Down Expand Up @@ -233,6 +233,8 @@ def test_get_optimized_kernel():
gp.get_optimized_kernel(X, Y, prior_mu, 'abc')
with pytest.raises(AssertionError) as error:
gp.get_optimized_kernel(X, Y, prior_mu, 'se', str_optimizer_method=1)
with pytest.raises(AssertionError) as error:
gp.get_optimized_kernel(X, Y, prior_mu, 'se', str_modelselection_method=1)
with pytest.raises(AssertionError) as error:
gp.get_optimized_kernel(X, Y, prior_mu, 'se', is_fixed_noise=1)
with pytest.raises(AssertionError) as error:
Expand All @@ -246,6 +248,13 @@ def test_get_optimized_kernel():
with pytest.raises(AssertionError) as error:
gp.get_optimized_kernel(X_set, Y, prior_mu, 'set_se', debug=1)

gp.get_optimized_kernel(X, Y, prior_mu, 'se')
gp.get_optimized_kernel(X, Y, prior_mu, 'se', str_optimizer_method='L-BFGS-B')
gp.get_optimized_kernel(X, Y, prior_mu, 'se', str_modelselection_method='loocv')
gp.get_optimized_kernel(X_set, Y, prior_mu, 'set_se')
gp.get_optimized_kernel(X_set, Y, prior_mu, 'set_se', str_optimizer_method='L-BFGS-B')
gp.get_optimized_kernel(X_set, Y, prior_mu, 'set_se', str_modelselection_method='loocv')

def test_predict_test_():
np.random.seed(42)
dim_X = 2
Expand Down

0 comments on commit 2a3efa4

Please sign in to comment.