-
Notifications
You must be signed in to change notification settings - Fork 3
/
PO_model_matching.py
363 lines (311 loc) · 13.8 KB
/
PO_model_matching.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
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
from bipartite import get_cora, bmatching_diverse, get_qpt_matrices
import os
import torch
from torch import nn, optim
from torch.autograd import Variable
import torch.nn.functional as F
from torchmetrics.functional import auc
from torch.utils.data import DataLoader
import pytorch_lightning as pl
import numpy as np
def make_cora_net(n_features=2866, n_hidden=200, n_layers=2, n_targets=1):
if n_layers ==1:
return nn.Sequential(nn.Linear(n_features, n_targets), nn.Sigmoid())
else:
layers = []
# input layer
layers.append(nn.Sequential(
nn.Linear(n_features, n_hidden),
nn.ReLU()
))
# hidden layers
for _ in range(n_layers -2) :
layers.append(nn.Sequential(
nn.Linear(n_hidden, n_hidden),
nn.ReLU()
))
# output layer
layers.append(nn.Sequential(
nn.Linear(n_hidden, n_targets),
nn.Sigmoid()
))
return nn.Sequential(*layers)
solver = bmatching_diverse
objective_fun=lambda x,v,**param: x @ v
def batch_solve(param,y,m,relaxation =False):
sol = []
for i in range(len(y)):
sol.append( solver(y[i], m[i], relaxation=relaxation,**param) )
return np.array(sol)
def regret_fn(y_hat,y,sol_true,m,param,minimize=False):
mm = 1 if minimize else -1
sol_hat = torch.from_numpy(batch_solve(param, y_hat.detach().numpy(),m.numpy()))
return ((mm*(sol_hat - sol_true)*y).sum(1)/(sol_true*y).sum(1)).mean()
def regret_aslist(y_hat,y,sol_true,m,param,minimize=False):
mm = 1 if minimize else -1
sol_hat = torch.from_numpy(batch_solve(param, y_hat.detach().numpy(),m.numpy()))
return ((mm*(sol_hat - sol_true)*y).sum(1)/(sol_true*y).sum(1))
class twostage_regression(pl.LightningModule):
def __init__(self,param, lr=1e-1, seed=2, max_epochs=50):
super().__init__()
pl.seed_everything(seed)
self.param = param
self.net = make_cora_net(n_layers=2)
self.lr = lr
self.max_epochs= max_epochs
self.save_hyperparameters("lr")
def forward(self,x):
return self.net(x)
def training_step(self, batch, batch_idx):
x,y,sol,m = batch
y_hat = self(x).squeeze()
criterion = nn.BCEWithLogitsLoss()
loss = criterion(y_hat,y)
self.log("train_loss",loss, prog_bar=True, on_step=True, on_epoch=True, )
return loss
def validation_step(self, batch, batch_idx):
criterion = nn.MSELoss(reduction='mean')
x,y,sol,m = batch
y_hat = self(x).squeeze()
val_loss= regret_fn(y_hat,y, sol,m,self.param)
mseloss = criterion(y_hat, y)
# aucloss = auc(y,y_hat,reorder=True)
aucloss = torch.tensor([auc(y[i],y_hat[i],reorder=True) for i in range(len(y))]).mean()
self.log("val_regret", val_loss, prog_bar=True, on_step=True, on_epoch=True, )
self.log("val_mse", mseloss, prog_bar=True, on_step=True, on_epoch=True, )
self.log("val_auc", aucloss, prog_bar=True, on_step=True, on_epoch=True, )
return {"val_regret": val_loss, "val_mse": mseloss,"val_auc":aucloss}
def validation_epoch_end(self, outputs):
avg_regret = torch.stack([x["val_regret"] for x in outputs]).mean()
avg_mse = torch.stack([x["val_mse"] for x in outputs]).mean()
self.log("ptl/val_regret", avg_regret)
self.log("ptl/val_mse", avg_mse)
# self.log("ptl/val_accuracy", avg_acc)
def test_step(self, batch, batch_idx):
# Here we just reuse the validation_step for testing
return self.validation_step(batch, batch_idx)
def configure_optimizers(self):
optimizer = torch.optim.Adam(self.parameters(), lr=self.lr)
num_batches = len(self.train_dataloader()) // self.trainer.accumulate_grad_batches
scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer, max_lr = self.lr, epochs=self.max_epochs,
steps_per_epoch = num_batches)
return [optimizer], [scheduler]
def spograd(y,sol,m, param,minimize= False):
mm = 1 if minimize else -1
class spograd_cls(torch.autograd.Function):
@staticmethod
def forward(ctx, y_hat):
ctx.save_for_backward(y_hat)
return mm*((y_hat-y)*sol).sum()
@staticmethod
def backward(ctx, grad_output):
y_hat, = ctx.saved_tensors
y_spo = 2*y_hat - y
sol_spo = torch.from_numpy(batch_solve(param, y_spo.detach().numpy(),m.numpy()))
return (sol- sol_spo)*mm
return spograd_cls.apply
def bbgrad(y,sol,m, param,mu,minimize=True):
mm = 1 if minimize else -1
class bbgrad_cls(torch.autograd.Function):
@staticmethod
def forward(ctx, y_hat):
y_perturbed = (y_hat + mu* y)
ctx.save_for_backward(y_hat, y_perturbed)
return mm*((y_hat-y)*sol).sum()
@staticmethod
def backward(ctx, grad_output):
y_hat,y_perturbed = ctx.saved_tensors
sol_hat = torch.from_numpy(batch_solve(param, y_hat.detach().numpy(),m.numpy()))
sol_perturbed = torch.from_numpy(batch_solve(param, y_perturbed.detach().numpy(),m.numpy()))
return -mm*(sol_hat - sol_perturbed)/mu
return bbgrad_cls.apply
class SPO(twostage_regression):
def __init__(self, param, lr=1e-1, seed=2, max_epochs=50):
super().__init__(param, lr, seed, max_epochs)
def training_step(self, batch, batch_idx):
x,y,sol,m = batch
y_hat = self(x).squeeze()
loss = spograd(y,sol,m,self.param)(y_hat)
self.log("train_loss",loss, prog_bar=True, on_step=True, on_epoch=True, )
return loss
class Blackbox(twostage_regression):
def __init__(self,param, lr=1e-1, mu=0.1, seed=2, max_epochs=50):
super().__init__(param, lr, seed, max_epochs)
self.mu = mu
# self.automatic_optimization = False
def training_step(self, batch, batch_idx):
x,y,sol,m = batch
y_hat = self(x).squeeze()
loss = bbgrad(y,sol,m, self.param,self.mu)(y_hat)
self.log("train_loss",loss, prog_bar=True, on_step=True, on_epoch=True, )
return loss
def MAP(sol,y,cache,minimize=False):
'''
sol, y and y_hat are torch array [batch_size,48]
cache is torch array [currentpoolsize,48]
'''
mm = 1 if minimize else -1
loss = 0
# print("shape check", sol.shape, y.shape,y_hat.shape, cache.shape)
for ii in range(len(y)):
loss += torch.max(((sol[ii] - cache )*(mm*y[ii] )).sum(dim=1))
return loss
def MAP_c(y_hat,y_true,sol,cache,*wd,**kwd):
y = y_hat
return MAP(sol,y,cache)
def MAP_hatc_c(y_hat,y_true,sol,cache,*wd,**kwd):
y = y_hat - y_true
return MAP(sol,y,cache)
def NCE(sol,y,cache,minimize=False):
'''
sol, y and y_hat are torch array [batch_size,48]
cache is torch array [currentpoolsize,48]
'''
mm = 1 if minimize else -1
loss = 0
# print("shape check", sol.shape, y.shape,y_hat.shape, cache.shape)
for ii in range(len(y)):
loss += torch.mean(((sol[ii] - cache )*(mm*y[ii] )).sum(dim=1))
return loss
def NCE_c(y_hat,y_true,sol,cache,*wd,**kwd):
y = y_hat
return NCE(sol,y,cache)
def NCE_hatc_c(y_hat,y_true,sol,cache,*wd,**kwd):
y = y_hat - y_true
return NCE(sol,y,cache)
def pointwise_loss(y_hat,y_true,sol,cache,*wd,**kwd):
'''
sol, y and y_hat are torch array [batch_size,48]
cache is torch array [currentpoolsize,48]
f(y_hat,s) is regresson on f(y,s)
'''
loss = (torch.matmul(y_hat,cache.transpose(0,1))- torch.matmul(y_true,cache.transpose(0,1))).square().mean()
return loss
def pairwise_loss(y_hat,y_true,sol,cache,tau=0, minimize=False,mode= 'B'):
'''
sol, y and y_hat are torch array [batch_size,48]
cache is torch array [currentpoolsize,48]
'''
mm = 1 if minimize else -1
loss = 0
relu = nn.ReLU()
for ii in range(len(y_true)):
_,indices= np.unique((mm*y_true[ii]*cache).sum(dim=1).detach().numpy(),return_index=True)
## return indices after sorting the array in ascending order
if mode == 'B':
big_ind = [indices[0] for p in range(len(indices)-1)] #good one
small_ind = [indices[p+1] for p in range(len(indices)-1)] #bad one
if mode == 'W':
big_ind = [indices[p] for p in range(len(indices)-1)] #good one
small_ind = [indices[-1] for p in range(len(indices)-1)] #bad one
if mode == 'S':
big_ind = [indices[p] for p in range(len(indices)-1)] #good one
small_ind = [indices[p+1] for p in range(len(indices)-1)] #bad one
loss += relu(tau+ mm*( torch.matmul(cache[big_ind], y_hat[ii]) - torch.matmul(cache[small_ind], y_hat[ii])) ).mean()
return loss
def pairwise_diffloss(y_hat,y_true,sol ,cache, minimize=False,mode= 'B',*wd,**kwd):
'''
sol, y and y_hat are torch array [batch_size,48]
cache is torch array [currentpoolsize,48]
'''
mm = 1 if minimize else -1
loss = 0
for ii in range(len(y_true)):
_,indices= np.unique((mm*y_true[ii]*cache).sum(dim=1).detach().numpy(),return_index=True)
## return indices after sorting the array in ascending order
if mode == 'B':
big_ind = [indices[0] for p in range(len(indices)-1)] #good one
small_ind = [indices[p+1] for p in range(len(indices)-1)] #bad one
if mode == 'W':
big_ind = [indices[p] for p in range(len(indices)-1)] #good one
small_ind = [indices[-1] for p in range(len(indices)-1)] #bad one
if mode == 'S':
big_ind = [indices[p] for p in range(len(indices)-1)] #good one
small_ind = [indices[p+1] for p in range(len(indices)-1)] #bad one
loss += (mm*( torch.matmul(cache[big_ind], y_hat[ii]) - torch.matmul(cache[small_ind], y_hat[ii])
- (torch.matmul(cache[big_ind], y_true[ii]) - torch.matmul(cache[small_ind], y_true[ii])) )).square().mean()
return loss
def Listnet_loss(y_hat,y_true,sol,cache, tau=1.,minimize=False,*wd,**kwd):
mm = 1 if minimize else -1
loss = 0
for ii in range(len(y_true)):
loss += -(F.log_softmax((-mm*y_hat[ii]*cache/tau).sum(dim=1),
dim=0)*F.softmax((-mm*y_true[ii]*cache/tau).sum(dim=1),dim=0)).mean()
return loss
### function to grow the cache
def growcache(cache, y_hat, m, param):
'''
cache is torch array [currentpoolsize,48]
y_hat is torch array [batch_size,48]
'''
sol = batch_solve(param,y_hat.detach().numpy(),m.numpy(),relaxation =False)
cache_np = cache.detach().numpy()
cache_np = np.unique(np.append(cache_np,sol,axis=0),axis=0)
# torch has no unique function, so we have to do this
return torch.from_numpy(cache_np).float()
class CachingPO(twostage_regression):
def __init__(self, param, loss_fn, init_cache,growth=0.1, tau=1., lr=1e-1, seed=2, max_epochs=50):
super().__init__(param, lr, seed, max_epochs)
# self.save_hyperparameters()
self.loss_fn = loss_fn
### The cache
init_cache_np = init_cache.detach().numpy()
init_cache_np = np.unique(init_cache_np,axis=0)
# torch has no unique function, so we have to do this
self.cache = torch.from_numpy(init_cache_np).float()
self.growth = growth
self.tau = tau
self.save_hyperparameters("lr","growth","tau")
def training_step(self, batch, batch_idx):
x,y,sol,m = batch
y_hat = self(x).squeeze()
if (np.random.random(1)[0]< self.growth) or len(self.cache)==0:
self.cache = growcache(self.cache, y_hat,m, self.param)
loss = self.loss_fn(y_hat,y,sol,self.cache,tau = self.tau)
self.log("train_loss",loss, prog_bar=True, on_step=True, on_epoch=True, )
return loss
class CombinedPO(twostage_regression):
def __init__(self,loss_fn, init_cache, growpool_fn, growth =0.0, lr=1e-1,tau=0.,alpha=0.5):
super().__init__(lr)
# self.save_hyperparameters()
self.loss_fn = loss_fn
### The cache
init_cache_np = init_cache.detach().numpy()
init_cache_np = np.unique(init_cache_np,axis=0)
# torch has no unique function, so we have to do this
self.cache = torch.from_numpy(init_cache_np).float()
self.growpool_fn = growpool_fn
self.growth = growth
self.tau = tau
self.alpha = alpha
self.save_hyperparameters("lr","growth","tau","alpha")
@staticmethod
def add_model_specific_args(parent_parser):
parser = parent_parser.add_argument_group("SemanticPO")
parser.add_argument("--lr", type=float, default=0.1)
return parent_parser
def training_step(self, batch, batch_idx):
x,y,sol,m = batch
y_hat = self(x).squeeze()
if (np.random.random(1)[0]< self.growth) or len(self.cache)==0:
self.cache = self.growpool_fn(self.cache, y_hat,m)
criterion = nn.MSELoss(reduction='mean')
loss = self.alpha*self.loss_fn(y_hat,y,sol,self.cache,tau =self.tau) + (1- self.alpha)*criterion(y_hat,y)
self.log("train_loss",loss, prog_bar=True, on_step=True, on_epoch=True, )
return loss
class CoraDataset():
def __init__(self, x,y, M, param={'p':0.25, 'q':0.25}, relaxation=False, sols=None, verbose=False):
if sols is not None:
self.sols = sols
else:
y_iter = range(len(y))
it = tqdm(y_iter) if verbose else y_iter
self.sols = batch_solve(param,y,M,relaxation =False)
self.sols = torch.from_numpy(self.sols).float()
self.x = torch.from_numpy(x).float()
self.y = torch.from_numpy(y).float()
self.m = torch.from_numpy(M).float()
def __len__(self):
return len(self.y)
def __getitem__(self, index):
return self.x[index], self.y[index], self.sols[index], self.m[index]