-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.py
More file actions
369 lines (327 loc) · 16.4 KB
/
algorithms.py
File metadata and controls
369 lines (327 loc) · 16.4 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
363
364
365
366
367
368
import numpy as np
import torch.optim as optim
import copy
from nets import NN, GNN, normalize_init
from typing import Optional
from config import *
class UCBalg:
def __init__(self, net: str, feat_dim: int, num_nodes: int, num_mlp_layers: int = 1, alg_lambda: float = 1,
exploration_coef: float = 1, neuron_per_layer: int = 100,
complete_cov_mat: bool = False, lr: float = 1e-3,
random_state = None, nn_aggr_feat: bool = True,
train_from_scratch=False, verbose = True,
path: Optional[str] = None, **kwargs):
if net == 'NN':
self.func = NN(input_dim=feat_dim * num_nodes, depth=num_mlp_layers, width=neuron_per_layer, aggr_feats=nn_aggr_feat).to(device)
elif net == 'GNN':
self.func = GNN(input_dim=feat_dim, depth=num_mlp_layers, width=neuron_per_layer, aggr_feats=nn_aggr_feat).to(device)
else:
raise NotImplementedError
self._rds = np.random if random_state is None else random_state
self.alg_lambda = alg_lambda # lambda regularization for the algorithm
self.num_net_params = sum(p.numel() for p in self.func.parameters() if p.requires_grad)
self.U = alg_lambda * torch.ones((self.num_net_params,)).to(device)
self.U_inv_small = None
self.exploration_coef = exploration_coef
self.neuron_per_layer = neuron_per_layer
self.train_from_scratch = train_from_scratch
self.complete_cov_mat = complete_cov_mat # if true, the complete covariance matrix is considered. Otherwise, just the diagonal.
self.lr = lr
self.verbose = verbose
if path is None:
self.path = 'trained_models/{}_{}dim_{}L_{}m_{:.3e}beta_{:.1e}sigma'.format(net, feat_dim, num_mlp_layers,
neuron_per_layer,
self.exploration_coef,
self.alg_lambda)
else:
self.path = path
self.G = None
def get_infogain(self):
kernel_matrix = self.U - self.alg_lambda * torch.ones((self.num_net_params,)).to(device)
return 0.5 * np.log(torch.prod(1 + kernel_matrix / self.alg_lambda).cpu().numpy())
def save_model(self):
torch.save(self.func, self.path)
def load_model(self):
try:
self.func = torch.load(self.path)
self.func.eval()
except:
print('Pretrained model not found.')
class GnnUCB(UCBalg): # Our main method
# This class currently uses Woodbury's Identity. For scalability experiment, we need to use the regular gradient.
def __init__(self, net: str,num_nodes: int, feat_dim: int, num_actions: int, action_domain: list,
alg_lambda: float = 1, exploration_coef: float = 1, t_intersect: int = np.inf,
num_mlp_layers: int = 2, neuron_per_layer: int = 128, lr: float = 1e-3,
nn_aggr_feat = True, train_from_scratch = False, verbose = True,
nn_init_lazy: bool = True, complete_cov_mat: bool = False, random_state = None, path: Optional[str] = None, **kwargs):
super().__init__(net=net, feat_dim=feat_dim, num_mlp_layers=num_mlp_layers, alg_lambda=alg_lambda, verbose = verbose,
lr = lr, complete_cov_mat = complete_cov_mat, nn_aggr_feat = nn_aggr_feat, train_from_scratch = train_from_scratch, num_nodes=num_nodes,
exploration_coef=exploration_coef, neuron_per_layer=neuron_per_layer, random_state=random_state, path=path, **kwargs)
# Create the network for computing gradients and subsequently variance.
self.f0 = copy.deepcopy(self.func)
self.f0 = normalize_init(self.f0)
if nn_init_lazy:
self.func = normalize_init(self.func)
self.f0 = copy.deepcopy(self.func)
if net == 'NN':
self.name = 'NN-UCB'
else:
self.name = 'GNN-UCB'
self.data = {
'graph_indices': [],
'rewards': []
}
self.num_actions = num_actions
self.action_domain = action_domain
self.init_grad_list = []
self.get_init_grads()
def save_model(self):
super().save_model()
torch.save(self.f0, self.path + "/f0_model")
def get_init_grads(self):
post_mean0 = []
for graph in self.action_domain:
self.f0.zero_grad()
post_mean0.append(self.f0(graph))
post_mean0[-1].backward(retain_graph=True)
# Get the Variance.
g = torch.cat([p.grad.flatten().detach() for p in self.f0.parameters()])
self.init_grad_list.append(g)
# def get_small_cov(self, g: np.ndarray):
# # Need to check square root. In any case, it is an issue of scaling - sweeping over beta properly would work.
# k_xx = g.dot(g)
# k_xy = torch.matmul(g.reshape(1, -1), self.G.T)
# k_xy = torch.matmul(k_xy, self.U_inv_small)
# k_xy = torch.matmul(k_xy, torch.matmul(self.G, g.reshape(-1, 1)))
# final_val = k_xx - k_xy
# return final_val
def add_data(self, indices, rewards):
# add the new observations, only if it didn't exist already
for idx, reward in zip(indices, rewards):
#if idx not in self.data['graph_indices']: #TODO: uncomment?
self.data['graph_indices'].append(idx)
self.data['rewards'].append(reward)
#g_to_add = self.init_grad_list[idx]
if self.complete_cov_mat:
raise NotImplementedError
# if self.G is None:
# self.G = g_to_add.reshape(1, -1) / np.sqrt(self.neuron_per_layer)
# else:
# self.G = torch.cat((self.G, g_to_add.reshape(1, -1) / np.sqrt(self.neuron_per_layer)), dim=0)
#
# kernel_matrix = torch.matmul(self.G, self.G.t())
# self.U_inv_small = torch.inverse(
# torch.diag(torch.ones(self.G.shape[0]).to(device) * self.alg_lambda) + kernel_matrix)
else:
self.U += self.init_grad_list[idx] * self.init_grad_list[idx] / self.neuron_per_layer # U is diagonal
def select(self):
ucbs = []
for ix in range(self.num_actions):
post_mean = self.func(self.action_domain[ix])
g = self.init_grad_list[ix]
if self.complete_cov_mat:
raise NotImplementedError
# if self.G is None:
# post_var = torch.sqrt(torch.sum(self.exploration_coef * g * g / self.U / self.neuron_per_layer))
# else:
# post_var = np.sqrt(self.exploration_coef) * torch.sqrt(
# self.get_small_cov(g / np.sqrt(self.neuron_per_layer)))
else:
# Use Approximate Covariance.
post_var = torch.sqrt(torch.sum( g * g / self.U) / self.neuron_per_layer)
ucbs.append(post_mean.item() + np.sqrt(self.exploration_coef) * post_var.item())
ix = np.argmax(ucbs)
return ix
def explore(self):
ix = self._rds.choice(range(self.num_actions))
return ix
def exploit(self):
if len(self.data['rewards'])>0:
list_ix = np.argmax(self.data['rewards'])
ix = self.data['graph_indices'][list_ix]
return ix
else:
return self.explore()
def best_predicted(self):
means = []
for ix in range(self.num_actions):
post_mean = self.func(self.action_domain[ix])
means.append(post_mean.item())
ix = np.argmax(means)
return ix
def get_post_var(self, idx):
g = self.init_grad_list[idx]
if self.complete_cov_mat:
raise NotImplementedError
else:
return torch.sqrt(torch.sum(g * g / self.U ) / self.neuron_per_layer).item()
def get_post_mean(self, idx):
return self.func(self.action_domain[idx]).item()
def pretrain(self, pre_train_data):
optimizer = optim.Adam(self.func.parameters(), lr=self.lr, weight_decay=self.alg_lambda/100)
self.data['graph_indices'].extend(pre_train_data['graph_indices'])
self.data['rewards'].extend(pre_train_data['rewards'])
index = list(np.arange(len(self.data['graph_indices'])))
length = len(index)
np.random.shuffle(index)
cnt = 0
tot_loss = 0
while True:
epoch_loss = 0
for ix in index:
label = self.data['rewards'][ix]
optimizer.zero_grad()
delta = self.func(self.action_domain[self.data['graph_indices'][ix]]).to(device)- torch.tensor(label).to(device)
loss = delta * delta
loss.backward()
optimizer.step()
epoch_loss += loss.item()
tot_loss += loss.item()
cnt += 1
if cnt >= 100: # train each epoch for J \leq 1000
return tot_loss / 1000
if epoch_loss / length <= 1e-6: # stop training if the average loss is less than 0.001
return epoch_loss / length
def train(self):
if self.train_from_scratch:
self.func.load_state_dict(self.f0.state_dict())
optimizer = optim.Adam(self.func.parameters(), lr=self.lr)#, weight_decay=self.alg_lambda)
#optimizer = optim.SGD(self.func.parameters(), lr=self.lr, weight_decay=self.alg_lambda)
# index = list(np.arange(len(self.data['graph_indices'])))
# # cnt = 0
# # tot_loss = 0
# for __ in range(10):
# epoch_loss = 0
# self._rds.shuffle(index)
# for ix in index:
# label = self.data['rewards'][ix]
# optimizer.zero_grad()
# delta = self.func(self.action_domain[self.data['graph_indices'][ix]]).to(device)- torch.tensor(label).to(device)
# loss = delta * delta
# loss.backward()
# optimizer.step()
# epoch_loss += loss.item()
# # tot_loss += loss.item()
# # cnt += 1
# # return tot_loss / len(index) / 10
# #self.save_model()
index = list(np.arange(len(self.data['graph_indices'])))
length = len(index)
np.random.shuffle(index)
cnt = 0
tot_loss = 0
epoch_losses = []
while True:
epoch_loss = 0
for ix in index:
label = self.data['rewards'][ix]
optimizer.zero_grad()
delta = self.func(self.action_domain[self.data['graph_indices'][ix]]).to(device)- torch.tensor(label).to(device)
loss = delta * delta
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_losses.append(epoch_loss)
tot_loss += loss.item()
cnt += 1
if cnt >= 1000: # train each epoch for J \leq 1000
if self.verbose:
print('Too many steps, stopping GD.')
print('The loss is', tot_loss / cnt)
return tot_loss / cnt
delta2 = epoch_losses[-2]-epoch_losses[-1]
delta1 = epoch_losses[-3]-epoch_losses[-2]
relative_improvement = (delta1-delta2)/delta1
if relative_improvement < 0.001:
if self.verbose:
print('Loss curve is getting flat, and the count is', cnt)
print('The loss is', epoch_loss / length)
return epoch_loss / length
if epoch_loss / length <= 1e-4: # stop training if the average loss is less than 0.0001
if self.verbose:
print('Loss is getting small and the count is', cnt)
print('The loss is', epoch_loss/length)
return epoch_loss / length
class PhasedGnnUCB(GnnUCB):
def __init__(self, net: str,num_nodes: int, feat_dim: int, num_actions: int, action_domain: list,
alg_lambda: float = 1, exploration_coef: float = 1, t_intersect: int= np.inf,
num_mlp_layers: int = 2, neuron_per_layer: int = 128, lr: float = 1e-3,
nn_aggr_feat = True, train_from_scratch = False, verbose = True,
nn_init_lazy: bool = True, complete_cov_mat: bool = False, random_state = None, path: Optional[str] = None, **kwargs):
super().__init__(net = net, num_nodes = num_nodes, feat_dim = feat_dim, num_actions = num_actions, action_domain = action_domain,
alg_lambda = alg_lambda, exploration_coef = exploration_coef,
num_mlp_layers = num_mlp_layers, neuron_per_layer = neuron_per_layer, lr = lr,
nn_aggr_feat = nn_aggr_feat, train_from_scratch = train_from_scratch, verbose = verbose,
nn_init_lazy = nn_init_lazy, complete_cov_mat = complete_cov_mat, random_state = random_state, path = path)
self.maximizers = [i for i in range(self.num_actions)]
self.t_intersect = t_intersect
if net == 'NN':
self.name = 'PhasedNN-UCB'
else:
self.name = 'PhasedGNN-UCB'
def select(self):
ucbs = []
lcbs = []
vars = []
for ix in range(self.num_actions):
post_mean = self.func(self.action_domain[ix])
g = self.init_grad_list[ix]
post_var = torch.sqrt(torch.sum( g * g / self.U) / self.neuron_per_layer)
vars.append(post_var.item())
ucbs.append(post_mean.item() + np.sqrt(self.exploration_coef ) * post_var.item())
lcbs.append(post_mean.item() - np.sqrt(self.exploration_coef ) * post_var.item())
# ucbs.append(post_mean.item() + np.sqrt(self.exploration_coef * ) * post_var.item())
# lcbs.append(post_mean.item() - np.sqrt(self.exploration_coef * ) * post_var.item())
#max_lcb = np.max(lcbs)
t = len(self.data['graph_indices'])
if t > self.t_intersect:
max_lcb = np.max([lcbs[i] for i in self.maximizers])
self.maximizers = [i for i in self.maximizers if max_lcb <= ucbs[i]]
print('intersecting...')
else:
max_lcb = np.max(lcbs)
self.maximizers = [i for i in range(len(ucbs)) if max_lcb <= ucbs[i]]
maximizer_vars = [vars[i] for i in self.maximizers]
ix = self.maximizers[np.argmax(maximizer_vars)]
return ix
def train(self):
if self.train_from_scratch:
self.func.load_state_dict(self.f0.state_dict())
optimizer = optim.Adam(self.func.parameters(), lr=self.lr)#, weight_decay=self.alg_lambda)
index = list(np.arange(len(self.data['graph_indices'])))
length = len(index)
np.random.shuffle(index)
cnt = 0
tot_loss = 0
epoch_losses = []
while True:
epoch_loss = 0
for ix in index:
label = self.data['rewards'][ix]
optimizer.zero_grad()
delta = self.func(self.action_domain[self.data['graph_indices'][ix]]).to(device)- torch.tensor(label).to(device)
loss = delta * delta
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_losses.append(epoch_loss)
tot_loss += loss.item()
cnt += 1
if cnt >= 1000: # train each epoch for J \leq 1000
if self.verbose:
print('Too many steps, stopping GD.')
print('The loss is', tot_loss / cnt)
return tot_loss / cnt
delta2 = epoch_losses[-2]-epoch_losses[-1]
delta1 = epoch_losses[-3]-epoch_losses[-2]
relative_improvement = (delta1-delta2)/delta1
if relative_improvement < 0.001:
if self.verbose:
print('Loss curve is getting flat, and the count is', cnt)
print('The loss is', epoch_loss / length)
return epoch_loss / length
if epoch_loss / length <= 1e-4: # stop training if the average loss is less than 0.0001
if self.verbose:
print('Loss is getting small and the count is', cnt)
print('The loss is', epoch_loss/length)
return epoch_loss / length