This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 261
/
plots_bo.py
177 lines (154 loc) · 6.18 KB
/
plots_bo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Copyright (c) 2016, the GPyOpt Authors
# Licensed under the BSD 3-clause license (see LICENSE.txt)
import numpy as np
from pylab import grid
import matplotlib.pyplot as plt
from pylab import savefig
import pylab
def plot_acquisition(bounds, input_dim, model, Xdata, Ydata, acquisition_function, suggested_sample,
filename=None, label_x=None, label_y=None, color_by_step=True):
'''
Plots of the model and the acquisition function in 1D and 2D examples.
'''
# Plots in dimension 1
if input_dim ==1:
# X = np.arange(bounds[0][0], bounds[0][1], 0.001)
# X = X.reshape(len(X),1)
# acqu = acquisition_function(X)
# acqu_normalized = (-acqu - min(-acqu))/(max(-acqu - min(-acqu))) # normalize acquisition
# m, v = model.predict(X.reshape(len(X),1))
# plt.ioff()
# plt.figure(figsize=(10,5))
# plt.subplot(2, 1, 1)
# plt.plot(X, m, 'b-', label=u'Posterior mean',lw=2)
# plt.fill(np.concatenate([X, X[::-1]]), \
# np.concatenate([m - 1.9600 * np.sqrt(v),
# (m + 1.9600 * np.sqrt(v))[::-1]]), \
# alpha=.5, fc='b', ec='None', label='95% C. I.')
# plt.plot(X, m-1.96*np.sqrt(v), 'b-', alpha = 0.5)
# plt.plot(X, m+1.96*np.sqrt(v), 'b-', alpha=0.5)
# plt.plot(Xdata, Ydata, 'r.', markersize=10, label=u'Observations')
# plt.axvline(x=suggested_sample[len(suggested_sample)-1],color='r')
# plt.title('Model and observations')
# plt.ylabel('Y')
# plt.xlabel('X')
# plt.legend(loc='upper left')
# plt.xlim(*bounds)
# grid(True)
# plt.subplot(2, 1, 2)
# plt.axvline(x=suggested_sample[len(suggested_sample)-1],color='r')
# plt.plot(X,acqu_normalized, 'r-',lw=2)
# plt.xlabel('X')
# plt.ylabel('Acquisition value')
# plt.title('Acquisition function')
# grid(True)
# plt.xlim(*bounds)
if not label_x:
label_x = 'x'
if not label_y:
label_y = 'f(x)'
x_grid = np.arange(bounds[0][0], bounds[0][1], 0.001)
x_grid = x_grid.reshape(len(x_grid),1)
acqu = acquisition_function(x_grid)
acqu_normalized = (-acqu - min(-acqu))/(max(-acqu - min(-acqu)))
m, v = model.predict(x_grid)
model.plot_density(bounds[0], alpha=.5)
plt.plot(x_grid, m, 'k-',lw=1,alpha = 0.6)
plt.plot(x_grid, m-1.96*np.sqrt(v), 'k-', alpha = 0.2)
plt.plot(x_grid, m+1.96*np.sqrt(v), 'k-', alpha=0.2)
plt.plot(Xdata, Ydata, 'r.', markersize=10)
plt.axvline(x=suggested_sample[len(suggested_sample)-1],color='r')
factor = max(m+1.96*np.sqrt(v))-min(m-1.96*np.sqrt(v))
plt.plot(x_grid,0.2*factor*acqu_normalized-abs(min(m-1.96*np.sqrt(v)))-0.25*factor, 'r-',lw=2,label ='Acquisition (arbitrary units)')
plt.xlabel(label_x)
plt.ylabel(label_y)
plt.ylim(min(m-1.96*np.sqrt(v))-0.25*factor, max(m+1.96*np.sqrt(v))+0.05*factor)
plt.axvline(x=suggested_sample[len(suggested_sample)-1],color='r')
plt.legend(loc='upper left')
if filename!=None:
savefig(filename)
else:
plt.show()
if input_dim == 2:
if not label_x:
label_x = 'X1'
if not label_y:
label_y = 'X2'
n = Xdata.shape[0]
colors = np.linspace(0, 1, n)
cmap = plt.cm.Reds
norm = plt.Normalize(vmin=0, vmax=1)
points_var_color = lambda X: plt.scatter(
X[:,0], X[:,1], c=colors, label=u'Observations', cmap=cmap, norm=norm)
points_one_color = lambda X: plt.plot(
X[:,0], X[:,1], 'r.', markersize=10, label=u'Observations')
X1 = np.linspace(bounds[0][0], bounds[0][1], 200)
X2 = np.linspace(bounds[1][0], bounds[1][1], 200)
x1, x2 = np.meshgrid(X1, X2)
X = np.hstack((x1.reshape(200*200,1),x2.reshape(200*200,1)))
acqu = acquisition_function(X)
acqu_normalized = (-acqu - min(-acqu))/(max(-acqu - min(-acqu)))
acqu_normalized = acqu_normalized.reshape((200,200))
m, v = model.predict(X)
plt.figure(figsize=(15,5))
plt.subplot(1, 3, 1)
plt.contourf(X1, X2, m.reshape(200,200),100)
plt.colorbar()
if color_by_step:
points_var_color(Xdata)
else:
points_one_color(Xdata)
plt.ylabel(label_y)
plt.title('Posterior mean')
plt.axis((bounds[0][0],bounds[0][1],bounds[1][0],bounds[1][1]))
##
plt.subplot(1, 3, 2)
plt.contourf(X1, X2, np.sqrt(v.reshape(200,200)),100)
plt.colorbar()
if color_by_step:
points_var_color(Xdata)
else:
points_one_color(Xdata)
plt.xlabel(label_x)
plt.ylabel(label_y)
plt.title('Posterior sd.')
plt.axis((bounds[0][0],bounds[0][1],bounds[1][0],bounds[1][1]))
##
plt.subplot(1, 3, 3)
plt.contourf(X1, X2, acqu_normalized,100)
plt.colorbar()
plt.plot(suggested_sample[:,0],suggested_sample[:,1],'m.', markersize=10)
plt.xlabel(label_x)
plt.ylabel(label_y)
plt.title('Acquisition function')
plt.axis((bounds[0][0],bounds[0][1],bounds[1][0],bounds[1][1]))
if filename!=None:
savefig(filename)
else:
plt.show()
def plot_convergence(Xdata, best_Y, filename=None):
'''
Plots to evaluate the convergence of standard Bayesian optimization algorithms
'''
n = Xdata.shape[0]
aux = (Xdata[1:n,:]-Xdata[0:n-1,:])**2
distances = np.sqrt(aux.sum(axis=1))
## Distances between consecutive x's
plt.figure(figsize=(10,5))
plt.subplot(1, 2, 1)
plt.plot(list(range(n-1)), distances, '-ro')
plt.xlabel('Iteration')
plt.ylabel('d(x[n], x[n-1])')
plt.title('Distance between consecutive x\'s')
grid(True)
# Estimated m(x) at the proposed sampling points
plt.subplot(1, 2, 2)
plt.plot(list(range(n)),best_Y,'-o')
plt.title('Value of the best selected sample')
plt.xlabel('Iteration')
plt.ylabel('Best y')
grid(True)
if filename!=None:
savefig(filename)
else:
plt.show()