forked from CoNG-harvard/bio_batch_BO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbayesian_opt_code.py
More file actions
363 lines (325 loc) · 12.6 KB
/
bayesian_opt_code.py
File metadata and controls
363 lines (325 loc) · 12.6 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#Adapted and modified from https://github.com/mahaitongdae/dbo
import anndata as an
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.widgets import Slider
import scipy.stats as stats
import random
import joblib
from joblib import Parallel, delayed
import multiprocessing
import itertools
import time
import sklearn.gaussian_process as gp
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import Matern, RBF, ConstantKernel
from scipy.stats import norm
from scipy.optimize import minimize
import torch
import gpytorch
from sklearn.utils import check_random_state
from sklearn.model_selection import train_test_split
from gpytorch.constraints.constraints import Interval
import re
import os
import copy
from GP_models import ExactGPModel, TorchGPModel
#bayesian opt acquisition functions
def expected_improvement(a, s, gaussian_process, evaluated_loss, greater_is_better=True, n_params=2, s_dim = 1):
""" expected_improvement
Expected improvement acquisition function.
Arguments:
----------
s: current s
a: array-like, shape = [n_samples, n_hyperparams]
The point for which the expected improvement needs to be computed.
gaussian_process: GaussianProcessRegressor object.
Gaussian process trained on previously evaluated hyperparameters.
evaluated_loss: Numpy array.
Numpy array that contains the values off the loss function for the previously
evaluated hyperparameters.
greater_is_better: Boolean.
Boolean flag that indicates whether the loss function is to be maximised or minimised.
n_params: int.
Dimension of the hyperparameter space.
"""
a_to_predict = a.reshape(-1, n_params)
sa_to_predict = np.insert(a_to_predict,0,s)
# print("shape", sa_to_predict.shape)
mu, sigma = gaussian_process.predict(sa_to_predict.reshape(-1,n_params +s_dim), return_std=True)
# print("s:", s)
# print("sa:", sa_to_predict)
# print("mu", mu)
# print("sigma", sigma)
# print("hi here", mu)
if greater_is_better:
loss_optimum = np.max(evaluated_loss)
# print("loss_optimum", loss_optimum)
# print("evaluated loss", evaluated_loss)
else:
loss_optimum = np.min(evaluated_loss)
scaling_factor = (-1) ** (not greater_is_better)
# In case sigma equals zero
with np.errstate(divide='ignore'):
Z = scaling_factor * (mu - loss_optimum) / sigma
expected_improvement = scaling_factor * (mu - loss_optimum) * norm.cdf(Z) + sigma * norm.pdf(Z)
expected_improvement[sigma == 0.0] == 0.0
# print("expected improvement", expected_improvement.reshape(1,))
return -1 * expected_improvement.reshape(1,)
def TS(a, s, gaussian_process, n_params=2, s_dim=1):
"""Thompson sampling variant of acquisition function
For each (s,a) pair, sample f(s,a) from N(mu(s,a), sigma(s,a)^2)
where the mu(s,a) and sigma(s,a) are based on current GP
Arguments
--------------
s: curr state
a: any input
gaussian_process: GaussianProcessRegressor object.
Gaussian process trained on previously evaluated hyperparameters.
n_params: dimension of a
-------------
Output
-------------
-f(s,a) ; negative sign is because we later want to minimize -f(s,a).
-------------
"""
a_to_sample = a.reshape(-1, n_params)
sa_to_sample = np.insert(a_to_sample, 0, s)
# print("sa_to sample shaoe", sa_to_sample.shape)
# crucial to not fix random_state, otherwise diff agents will use same TS, i.e. not adding info
res = gaussian_process.sample_y(
sa_to_sample.reshape(-1, s_dim + n_params), random_state=None
).reshape(
1,
)
# print("sa", sa_to_sample)
# print("gp sample", -res)
if res <= 0: # gp might return negative value
return 0
else:
return -res
def ucb(a, s, gaussian_process, n_params=2, beta=0.15, s_dim=1):
"""UCB variant of acquisition function
Arguments
--------------
s: single state
a: any input
gaussian_process: GaussianProcessRegressor object.
Gaussian process trained on previously evaluated hyperparameters.
n_params: dimension of action a
-------------
Output
-------------
-(mu(s,a) + beta * sigma(s,a)) ; negative sign is because we want to minimize a cost
-------------
"""
a_to_sample = a.reshape(-1, n_params)
sa_to_sample = np.insert(a_to_sample, 0, s)
mu, sigma = gaussian_process.predict(
sa_to_sample.reshape(-1, n_params + s_dim), return_std=True
)
mu = np.squeeze(mu)
# print("mu", mu)
# print("beta * sigma", beta * sigma)
# print("this is mu shape", mu.shape)
# print("this is sigma shape", sigma.shape)
acq = mu + beta * sigma
return -1.0 * acq
def ES(
sa,
gp,
state_dim=1,
n_params=2,
beta=0.2,
alpha=0.0,
a_circ=0.15,
tr_iters=200,
domain=[0, 1],
init_points=5,
noise=1e-4,
):
"""entropy search variant of action proposal
Arguments
--------------
sa: (state,action) pairs, shape = [n_agents, state_dim + action_dim],
a[i] is estimated ucb action for state s[i]
gp: GaussianProcessRegressor object.
Gaussian process trained on previously evaluated hyperparameters.
n_params: dimension of action a
beta: ucb exploration bonus
alpha: (our belief of) observation noise variance, default 0 b/c gp should already include the noise
-------------
Output
-------------
proposed new a for states s, shape = [n_agents, action_dim]
-------------
"""
s = sa[:, :state_dim]
a = sa[:, state_dim:]
# print("these are the s",s)
# print("these are ucb/ei a:", a)
n_agents = sa.shape[0]
a_dim = n_params
best_loss = 1e5
best_a = None
for n_init in range(init_points):
a_new = np.random.normal(loc=a, scale=a_circ, size=(n_agents, a_dim))
a_new = np.minimum(1.,np.maximum(a_new,0))
# print("initial a", a_new)
a_new = torch.tensor(a_new, requires_grad=True, dtype=torch.float32)
# print("initial a_new", a_new)
s_torch = torch.tensor(s, requires_grad=False, dtype=torch.float32)
optimizer = torch.optim.Adam([a_new], lr=0.01)
training_iter = tr_iters
for i in range(training_iter):
optimizer.zero_grad()
xucb_torch = torch.tensor(sa, dtype=torch.float32)
xnew_torch = torch.hstack((s_torch, a_new))
# print("xnew torch require grad", xnew_torch.requires_grad)
# print("xucb_torch", xucb_torch)
# print("xnew_torch", xnew_torch)
joint_x = torch.vstack((xucb_torch, xnew_torch))
# print("joint x requires grad", joint_x.requires_grad)
# joint_x = joint_x.detach().numpy()
_, cov = gp.predict(joint_x, return_cov=True, return_tensor = True)
# cov = torch.tensor(cov, requires_grad=True).float()
cov_xucb_x = cov[:n_agents, n_agents:]
cov_xx = cov[n_agents:, n_agents:]
# print("cov_xucb_x", cov_xucb_x)
# print("cov_xx", cov_xx)
loss = -torch.trace(
torch.matmul(
torch.matmul(
cov_xucb_x,
torch.linalg.inv(cov_xx + noise * torch.eye(n_agents)),
),
cov_xucb_x.T,
)
)
# print("loss", loss)
# print("loss requires grad", loss.requires_grad)
loss.backward()
optimizer.step()
# print("a new", a_new)
# project a_new back to domain
a_new = torch.where(
a_new > torch.tensor(domain[1]), torch.tensor(domain[1]), a_new
)
a_new = torch.where(
a_new < torch.tensor(domain[0]), torch.tensor(domain[0]), a_new
)
a_new.detach_() # what's the point of this?
if loss.clone().detach().numpy() < best_loss:
# print("i am here, best loss", best_loss)
a_new = a_new.clone().detach().numpy()
best_a = np.copy(a_new)
best_loss = loss.clone().detach().numpy()
# rearrange a_new such that each a_new[i] gets assigned to the a[j] that is closest to it
print("best a", best_a)
a_new_reorg = np.copy(best_a)
for i in range(n_agents):
dist_i = np.linalg.norm(a[i, :] - best_a, axis=1)
idx_i = np.argmin(dist_i)
a_new_reorg[i] = best_a[idx_i, :]
best_a = np.delete(
best_a, (idx_i), axis=0
) # remove corresponding row from best_a
# print("these are proposed a after reorg", a_new_reorg)
return a_new_reorg
#BO sampling and optimization functions
def sample_next_hyperparameter(s,acquisition_func, gaussian_process, evaluated_loss, greater_is_better=True,
bounds=(0, 1), n_restarts=10, acq = "ES"):
""" sample_next_hyperparameter
Proposes the next hyperparameter(s) to sample the loss function for.
Arguments:
----------
s: current state(s), numpy array
acquisition_func: function.
Acquisition function to optimise.
gaussian_process: GaussianProcessRegressor object.
Gaussian process trained on previously evaluated hyperparameters.
evaluated_loss: array-like, shape = [n_obs,]
Numpy array that contains the values off the loss function for the previously
evaluated hyperparameters.
greater_is_better: Boolean.
Boolean flag that indicates whether the loss function is to be maximised or minimised.
bounds: Tuple.
Bounds for the L-BFGS optimiser.
n_restarts: integer.
Number of times to run the minimiser with different starting points.
"""
best_a = None
best_acquisition_value = 1
n_params = bounds.shape[0] -1 #-1 is because s is not part of the params for acquisition
for starting_point in np.random.uniform(bounds[1:, 0], bounds[1:, 1], size=(n_restarts, n_params)):
# print("s", s)
# print("hi", starting_point, acq)
# print("sampling for state:", s)
# print("initial a for sample next fn", starting_point)
# print("before optimizing")
if acq == "EI":
res = minimize(fun=acquisition_func,
x0=starting_point.reshape(n_params,),
bounds=((0,1),(0,1)),
method='L-BFGS-B',
args=(s,gaussian_process, evaluated_loss, greater_is_better, n_params))
elif acq == "TS" or acq == "ucb":
res = minimize(fun=acquisition_func,
x0 = starting_point.reshape(n_params,),
bounds = ((0,1), (0,1)),
method = 'L-BFGS-B',
args = (s,gaussian_process,n_params)
)
# print("after optimizing")
# print("res final func value", res.fun)
# print("number of iters", res.nit)
if res.fun < best_acquisition_value:
# print("best acq value", best_acquisition_value)
best_acquisition_value = res.fun
best_a = res.x
# print("a after optimizing", best_a)
return best_a
def main():
# Input xp and yp
model = TorchGPModel(torch.tensor(xp).float(), torch.tensor(yp).float())
model.train() # is this necessary
# Choose the parameters depending on usecase
bounds = np.asarray([[0,1],[0,1],[0,1]])
n_restarts = 10
n_agents = 3
s_dim = 1
a_dim = 2
acq = "ucb"
#perform Entropy Search to select next acquisition points
use_ES = True
next_sa = np.empty((n_agents, s_dim + a_dim))
ES_init_points = 10
yp = np.copy(yp)
for i in range(n_agents):
next_sa[i,s_dim:] = sample_next_hyperparameter(s[i], acq_fn, model, yp, greater_is_better=True, bounds=bounds, n_restarts = 10,acq = acq)
next_sa[i,:s_dim] = s[i]
if use_ES == True:
a_new = ES( next_sa,
model,
state_dim=1,
n_params=2,
beta=0.2,
alpha=0.0,
a_circ=0.2,
tr_iters=20,
domain=bounds[0, :],
init_points=ES_init_points,
)
next_sa[:, s_dim:] = a_new
print(
f"device joint proposal after using ES: ",
next_sa[:, s_dim:],
"state is ",
s,
" acq is ",
acq,
)
if __name__ == '__main__':
main()