-
Notifications
You must be signed in to change notification settings - Fork 0
/
ICT.py
616 lines (530 loc) · 26.3 KB
/
ICT.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
from grad import *
from utils import *
import design_bench
import argparse
import higher
# Unpacked Co-teaching Loss function
def loss_coteaching(y_1, y_2, t, num_remember):
# ind, noise_or_not
loss_1 = F.mse_loss(y_1, t, reduction='none').view(128)
ind_1_sorted = np.argsort(loss_1.cpu().data).cuda()
loss_1_sorted = loss_1[ind_1_sorted]
loss_2 = F.mse_loss(y_2, t, reduction='none').view(128)
ind_2_sorted = np.argsort(loss_2.cpu().data).cuda()
loss_2_sorted = loss_2[ind_2_sorted]
ind_1_update = ind_1_sorted[:num_remember]
ind_2_update = ind_2_sorted[:num_remember]
# exchange
loss_1_update = F.mse_loss(y_1[ind_2_update], t[ind_2_update], reduction='none')
loss_2_update = F.mse_loss(y_2[ind_1_update], t[ind_1_update], reduction='none')
return loss_1_update, loss_2_update
def meta_weight(args):
task = design_bench.make(args.task)
load_y(args.task)
task_y0 = task.y
task_x, task_y, shape0 = process_data(task, args.task, task_y0)
task_x = torch.Tensor(task_x).to(device)
task_y = torch.Tensor(task_y).to(device)
indexs = torch.argsort(task_y.squeeze())
# Find the x with the maximum corresponding y, that is the top1
index = indexs[-1:]
x_init = copy.deepcopy(task_x[index])
# get top k candidates
if args.reweight_mode == "top128":
index_val = indexs[-args.topk:]
elif args.reweight_mode == "half":
index_val = indexs[-(len(indexs) // 2):]
else:
index_val = indexs
x_val = copy.deepcopy(task_x[index_val])
label_val = copy.deepcopy(task_y[index_val])
f1 = SimpleMLP(task_x.shape[1]).to(device)
f1.load_state_dict(
torch.load(args.proxy_path + args.task + "_proxy_" + str(args.seed1) + ".pt", map_location='cuda:0'))
f2 = SimpleMLP(task_x.shape[1]).to(device)
f2.load_state_dict(
torch.load(args.proxy_path + args.task + "_proxy_" + str(args.seed2) + ".pt", map_location='cuda:0'))
f3 = SimpleMLP(task_x.shape[1]).to(device)
f3.load_state_dict(
torch.load(args.proxy_path + args.task + "_proxy_" + str(args.seed3) + ".pt", map_location='cuda:0'))
candidate = x_init[0] # i.e., x_0
candidate.requires_grad = True
candidate_opt = optim.Adam([candidate], lr=args.ft_lr)
optimizer1 = torch.optim.Adam(f1.parameters(), lr=args.alpha, weight_decay=args.wd)
optimizer2 = torch.optim.Adam(f2.parameters(), lr=args.alpha, weight_decay=args.wd)
optimizer3 = torch.optim.Adam(f3.parameters(), lr=args.alpha, weight_decay=args.wd)
for i in range(1, args.Tmax + 1):
loss = -1.0 / 3.0 * (f1(candidate) + f2(candidate) + f3(candidate))
candidate_opt.zero_grad()
loss.backward()
candidate_opt.step()
x_train = []
y1_label = []
y2_label = []
y3_label = []
# sample K points around current candidate
for k in range(args.K):
temp_x = candidate.data + args.noise_coefficient * np.random.normal(args.mu,
args.std) # add gaussian noise
x_train.append(temp_x)
temp_y1 = f1(temp_x)
y1_label.append(temp_y1)
temp_y2 = f2(temp_x)
y2_label.append(temp_y2)
temp_y3 = f3(temp_x)
y3_label.append(temp_y3)
x_train = torch.stack(x_train)
y1_label = torch.Tensor(y1_label).to(device)
y1_label = torch.reshape(y1_label, (args.K, 1))
y2_label = torch.Tensor(y2_label).to(device)
y2_label = torch.reshape(y2_label, (args.K, 1))
y3_label = torch.Tensor(y3_label).to(device)
y3_label = torch.reshape(y3_label, (args.K, 1))
if args.if_reweight and args.if_coteach:
# Round 1, use f3 to update f1 and f2
weight_1 = torch.ones(args.num_coteaching).to(device)
weight_1.requires_grad = True
weight_2 = torch.ones(args.num_coteaching).to(device)
weight_2.requires_grad = True
with higher.innerloop_ctx(f1, optimizer1) as (model1, opt1):
with higher.innerloop_ctx(f2, optimizer2) as (model2, opt2):
l1, l2 = loss_coteaching(model1(x_train), model2(x_train), y3_label, args.num_coteaching)
l1_t = weight_1 * l1
l1_t = torch.sum(l1_t) / args.num_coteaching
opt1.step(l1_t)
logit1 = model1(x_val)
loss1_v = F.mse_loss(logit1, label_val)
g1 = torch.autograd.grad(loss1_v, weight_1)[0].data
g1 = F.normalize(g1, p=args.clamp_norm, dim=0)
g1 = torch.clamp(g1, min=args.clamp_min, max=args.clamp_max)
weight_1 = weight_1 - args.beta * g1
weight_1 = torch.clamp(weight_1, min=0, max=2)
l2_t = weight_2 * l2
l2_t = torch.sum(l2_t) / args.num_coteaching
opt2.step(l2_t)
logit2 = model2(x_val)
loss2_v = F.mse_loss(logit2, label_val)
g2 = torch.autograd.grad(loss2_v, weight_2)[0].data
g2 = F.normalize(g2, p=args.clamp_norm, dim=0)
g2 = torch.clamp(g2, min=args.clamp_min, max=args.clamp_max)
weight_2 = weight_2 - args.beta * g2
weight_2 = torch.clamp(weight_2, min=0, max=2)
loss1 = weight_1 * F.mse_loss(f1(x_train), y3_label, reduction='none')
loss1 = torch.sum(loss1) / args.num_coteaching
optimizer1.zero_grad()
loss1.backward()
optimizer1.step()
loss2 = weight_2 * F.mse_loss(f2(x_train), y3_label, reduction='none')
loss2 = torch.sum(loss2) / args.num_coteaching
optimizer2.zero_grad()
loss2.backward()
optimizer2.step()
# Round 2, use f2 to update f1 and f3
weight_1 = torch.ones(args.num_coteaching).to(device)
weight_1.requires_grad = True
weight_3 = torch.ones(args.num_coteaching).to(device)
weight_3.requires_grad = True
with higher.innerloop_ctx(f1, optimizer1) as (model1, opt1):
with higher.innerloop_ctx(f3, optimizer3) as (model3, opt3):
l1, l3 = loss_coteaching(model1(x_train), model3(x_train), y2_label, args.num_coteaching)
l1_t = weight_1 * l1
l1_t = torch.sum(l1_t) / args.num_coteaching
opt1.step(l1_t)
logit1 = model1(x_val)
loss1_v = F.mse_loss(logit1, label_val)
g1 = torch.autograd.grad(loss1_v, weight_1)[0].data
g1 = F.normalize(g1, p=args.clamp_norm, dim=0)
g1 = torch.clamp(g1, min=args.clamp_min, max=args.clamp_max)
weight_1 = weight_1 - args.beta * g1
weight_1 = torch.clamp(weight_1, min=0, max=2)
l3_t = weight_3 * l3
l3_t = torch.sum(l3_t) / args.num_coteaching
opt3.step(l3_t)
logit3 = model3(x_val)
loss3_v = F.mse_loss(logit3, label_val)
g3 = torch.autograd.grad(loss3_v, weight_3)[0].data
g3 = F.normalize(g3, p=args.clamp_norm, dim=0)
g3 = torch.clamp(g3, min=args.clamp_min, max=args.clamp_max)
weight_3 = weight_3 - args.beta * g3
weight_3 = torch.clamp(weight_3, min=0, max=2)
loss1 = weight_1 * F.mse_loss(f1(x_train), y2_label, reduction='none')
loss1 = torch.sum(loss1) / args.num_coteaching
optimizer1.zero_grad()
loss1.backward()
optimizer1.step()
loss3 = weight_3 * F.mse_loss(f3(x_train), y2_label, reduction='none')
loss3 = torch.sum(loss3) / args.num_coteaching
optimizer3.zero_grad()
loss3.backward()
optimizer3.step()
# Round 3, use f1 to update f2 and f3
weight_2 = torch.ones(args.num_coteaching).to(device)
weight_2.requires_grad = True
weight_3 = torch.ones(args.num_coteaching).to(device)
weight_3.requires_grad = True
with higher.innerloop_ctx(f2, optimizer2) as (model2, opt2):
with higher.innerloop_ctx(f3, optimizer3) as (model3, opt3):
l2, l3 = loss_coteaching(model2(x_train), model3(x_train), y1_label, args.num_coteaching)
l2_t = weight_2 * l2
l2_t = torch.sum(l2_t) / args.num_coteaching
opt2.step(l2_t)
logit2 = model2(x_val)
loss2_v = F.mse_loss(logit2, label_val)
g2 = torch.autograd.grad(loss2_v, weight_2)[0].data
g2 = F.normalize(g2, p=args.clamp_norm, dim=0)
g2 = torch.clamp(g2, min=args.clamp_min, max=args.clamp_max)
weight_2 = weight_2 - args.beta * g2
weight_2 = torch.clamp(weight_2, min=0, max=2)
l3_t = weight_3 * l3
l3_t = torch.sum(l3_t) / args.num_coteaching
opt3.step(l3_t)
logit3 = model3(x_val)
loss3_v = F.mse_loss(logit3, label_val)
g3 = torch.autograd.grad(loss3_v, weight_3)[0].data
g3 = F.normalize(g3, p=args.clamp_norm, dim=0)
g3 = torch.clamp(g3, min=args.clamp_min, max=args.clamp_max)
weight_3 = weight_3 - args.beta * g3
weight_3 = torch.clamp(weight_3, min=0, max=2)
loss2 = weight_2 * F.mse_loss(f2(x_train), y1_label, reduction='none')
loss2 = torch.sum(loss2) / args.num_coteaching
optimizer2.zero_grad()
loss2.backward()
optimizer2.step()
loss3 = weight_3 * F.mse_loss(f3(x_train), y1_label, reduction='none')
loss3 = torch.sum(loss3) / args.num_coteaching
optimizer3.zero_grad()
loss3.backward()
optimizer3.step()
elif args.if_reweight and not args.if_coteach:
# Round 1, use f3 to update f1 and f2
weight_1 = torch.ones(args.K).to(device)
weight_1.requires_grad = True
weight_2 = torch.ones(args.K).to(device)
weight_2.requires_grad = True
with higher.innerloop_ctx(f1, optimizer1) as (model1, opt1):
with higher.innerloop_ctx(f2, optimizer2) as (model2, opt2):
l1 = F.mse_loss(model1(x_train), y3_label, reduction='none')
l2 = F.mse_loss(model2(x_train), y3_label, reduction='none')
l1_t = weight_1 * l1
l1_t = torch.sum(l1_t) / args.K
opt1.step(l1_t)
logit1 = model1(x_val)
loss1_v = F.mse_loss(logit1, label_val)
g1 = torch.autograd.grad(loss1_v, weight_1)[0].data
g1 = F.normalize(g1, p=args.clamp_norm)
g1 = torch.clamp(g1, min=args.clamp_min, max=args.clamp_max)
weight_1 = weight_1 - args.beta * g1
weight_1 = torch.clamp(weight_1, min=0, max=2)
l2_t = weight_2 * l2
l2_t = torch.sum(l2_t) / args.K
opt2.step(l2_t)
logit2 = model2(x_val)
loss2_v = F.mse_loss(logit2, label_val)
g2 = torch.autograd.grad(loss2_v, weight_2)[0].data
g2 = F.normalize(g2, p=args.clamp_norm)
g2 = torch.clamp(g2, min=args.clamp_min, max=args.clamp_max)
weight_2 = weight_2 - args.beta * g2
weight_2 = torch.clamp(weight_2, min=0, max=2)
loss1 = weight_1 * F.mse_loss(f1(x_train), y3_label, reduction='none')
loss1 = torch.sum(loss1) / args.K
optimizer1.zero_grad()
loss1.backward()
optimizer1.step()
loss2 = weight_2 * F.mse_loss(f2(x_train), y3_label, reduction='none')
loss2 = torch.sum(loss2) / args.K
optimizer2.zero_grad()
loss2.backward()
optimizer2.step()
# Round 2, use f2 to update f1 and f3
weight_1 = torch.ones(args.K).to(device)
weight_1.requires_grad = True
weight_3 = torch.ones(args.K).to(device)
weight_3.requires_grad = True
with higher.innerloop_ctx(f1, optimizer1) as (model1, opt1):
with higher.innerloop_ctx(f3, optimizer3) as (model3, opt3):
l1 = F.mse_loss(model1(x_train), y2_label, reduction='none')
l3 = F.mse_loss(model3(x_train), y2_label, reduction='none')
l1_t = weight_1 * l1
l1_t = torch.sum(l1_t) / args.K
opt1.step(l1_t)
logit1 = model1(x_val)
loss1_v = F.mse_loss(logit1, label_val)
g1 = torch.autograd.grad(loss1_v, weight_1)[0].data
g1 = F.normalize(g1, p=args.clamp_norm)
g1 = torch.clamp(g1, min=args.clamp_min, max=args.clamp_max)
weight_1 = weight_1 - args.beta * g1
weight_1 = torch.clamp(weight_1, min=0, max=2)
l3_t = weight_3 * l3
l3_t = torch.sum(l3_t) / args.K
opt3.step(l3_t)
logit3 = model3(x_val)
loss3_v = F.mse_loss(logit3, label_val)
g3 = torch.autograd.grad(loss3_v, weight_3)[0].data
g3 = F.normalize(g3, p=args.clamp_norm)
g3 = torch.clamp(g3, min=args.clamp_min, max=args.clamp_max)
weight_3 = weight_3 - args.beta * g3
weight_3 = torch.clamp(weight_3, min=0, max=2)
loss1 = weight_1 * F.mse_loss(f1(x_train), y2_label, reduction='none')
loss1 = torch.sum(loss1) / args.K
optimizer1.zero_grad()
loss1.backward()
optimizer1.step()
loss3 = weight_3 * F.mse_loss(f3(x_train), y2_label, reduction='none')
loss3 = torch.sum(loss3) / args.K
optimizer3.zero_grad()
loss3.backward()
optimizer3.step()
# Round 3, use f1 to update f2 and f3
weight_2 = torch.ones(args.K).to(device)
weight_2.requires_grad = True
weight_3 = torch.ones(args.K).to(device)
weight_3.requires_grad = True
with higher.innerloop_ctx(f2, optimizer2) as (model2, opt2):
with higher.innerloop_ctx(f3, optimizer3) as (model3, opt3):
l2 = F.mse_loss(model2(x_train), y1_label, reduction='none')
l3 = F.mse_loss(model3(x_train), y1_label, reduction='none')
l2_t = weight_2 * l2
l2_t = torch.sum(l2_t) / args.K
opt2.step(l2_t)
logit2 = model2(x_val)
loss2_v = F.mse_loss(logit2, label_val)
g2 = torch.autograd.grad(loss2_v, weight_2)[0].data
g2 = F.normalize(g2, p=args.clamp_norm)
g2 = torch.clamp(g2, min=args.clamp_min, max=args.clamp_max)
weight_2 = weight_2 - args.beta * g2
weight_2 = torch.clamp(weight_2, min=0, max=2)
l3_t = weight_3 * l3
l3_t = torch.sum(l3_t) / args.K
opt3.step(l3_t)
logit3 = model3(x_val)
loss3_v = F.mse_loss(logit3, label_val)
g3 = torch.autograd.grad(loss3_v, weight_3)[0].data
g3 = F.normalize(g3, p=args.clamp_norm)
g3 = torch.clamp(g3, min=args.clamp_min, max=args.clamp_max)
weight_3 = weight_3 - args.beta * g3
weight_3 = torch.clamp(weight_3, min=0, max=2)
loss2 = weight_2 * F.mse_loss(f2(x_train), y1_label, reduction='none')
loss2 = torch.sum(loss2) / args.K
optimizer2.zero_grad()
loss2.backward()
optimizer2.step()
loss3 = weight_3 * F.mse_loss(f3(x_train), y1_label, reduction='none')
loss3 = torch.sum(loss3) / args.K
optimizer3.zero_grad()
loss3.backward()
optimizer3.step()
elif not args.if_reweight and args.if_coteach:
# f1 label, f2 and f3 coteaching
loss_2, loss_3 = loss_coteaching(f2(x_train), f3(x_train), y1_label, args)
optimizer2.zero_grad()
loss_2.backward()
optimizer2.step()
optimizer3.zero_grad()
loss_3.backward()
optimizer3.step()
# f2 label, f1 and f3 coteaching
loss_1, loss_33 = loss_coteaching(f1(x_train), f3(x_train), y2_label, args)
optimizer1.zero_grad()
loss_1.backward()
optimizer1.step()
optimizer3.zero_grad()
loss_33.backward()
optimizer3.step()
# f3 label, f1 and f2 coteaching
loss_11, loss_22 = loss_coteaching(f1(x_train), f2(x_train), y3_label, args)
optimizer1.zero_grad()
loss_11.backward()
optimizer1.step()
optimizer2.zero_grad()
loss_22.backward()
optimizer2.step()
# optimizer1.zero_grad()
# ((loss_1 + loss_11) / 2).backward()
# optimizer1.step()
# optimizer2.zero_grad()
# ((loss_2 + loss_22) / 2).backward()
# optimizer2.step()
# optimizer3.zero_grad()
# ((loss_3 + loss_33) / 2).backward()
# optimizer3.step()
elif not args.if_reweight and not args.if_coteach:
pass
torch.save(f1.state_dict(), args.reweighting_path + args.task + "_proxy_" + str(args.seed1) + "_"
+ str(args.num_coteaching) + ".pt")
torch.save(f2.state_dict(), args.reweighting_path + args.task + "_proxy_" + str(args.seed2) + "_"
+ str(args.num_coteaching) + ".pt")
torch.save(f3.state_dict(), args.reweighting_path + args.task + "_proxy_" + str(args.seed3) + "_"
+ str(args.num_coteaching) + ".pt")
def experiment(args):
task = [args.task]
seeds1 = [0, 1, 2, 3, 4, 5, 6, 7]
seeds2 = [7, 0, 1, 2, 3, 4, 5, 6]
seeds3 = [6, 7, 0, 1, 2, 3, 4, 5]
seed = [0, 1, 2, 3, 4, 5, 6, 7]
# Training Proxy
args.mode = 'train'
for sd, s1, s2, s3 in zip(seed, seeds1, seeds2, seeds3):
print("Current seed is " + str(sd), end="\t")
set_seed(sd)
args.seed1 = s1
args.seed2 = s2
args.seed3 = s3
for t in task:
if t == 'TFBind8-Exact-v0' or t == 'TFBind10-Exact-v0': # since this is a discrete task
args.ft_lr = 1e-1
else:
args.ft_lr = 1e-3
print("Current task is " + str(t))
args.task = t
print("this is my setting", args)
meta_weight(args)
def design_opt(args):
task = design_bench.make(args.task)
load_y(args.task)
task_y0 = task.y
task_x, task_y, shape0 = process_data(task, args.task, task_y0)
task_x = torch.Tensor(task_x).to(device)
task_y = torch.Tensor(task_y).to(device)
indexs = torch.argsort(task_y.squeeze())
index = indexs[-args.topk:]
x_init = copy.deepcopy(task_x[index])
scores = []
for x_i in range(x_init.shape[0]):
proxy1 = SimpleMLP(task_x.shape[1]).to(device)
proxy1.load_state_dict(
torch.load(args.reweighting_path + args.task + "_proxy_" + str(args.seed1) + "_"
+ str(args.num_coteaching) + ".pt", map_location='cuda:0'))
proxy2 = SimpleMLP(task_x.shape[1]).to(device)
proxy2.load_state_dict(
torch.load(args.reweighting_path + args.task + "_proxy_" + str(args.seed2) + "_"
+ str(args.num_coteaching) + ".pt", map_location='cuda:0'))
proxy3 = SimpleMLP(task_x.shape[1]).to(device)
proxy3.load_state_dict(
torch.load(args.reweighting_path + args.task + "_proxy_" + str(args.seed3) + "_"
+ str(args.num_coteaching) + ".pt", map_location='cuda:0'))
candidate = x_init[x_i:x_i + 1]
score_before, _ = evaluate_sample(task, candidate, args.task, shape0)
candidate.requires_grad = True
candidate_opt = optim.Adam([candidate], lr=args.ft_lr)
for i in range(1, args.Tmax + 1):
loss = -1.0 / 3.0 * (proxy1(candidate) + proxy2(candidate) + proxy3(candidate))
candidate_opt.zero_grad()
loss.backward()
candidate_opt.step()
if i % args.interval == 0:
score_after, _ = evaluate_sample(task, candidate.data, args.task, shape0)
print("candidate {} score before {} score now {}".format(x_i, score_before.squeeze(),
score_after.squeeze()))
scores.append(score_after.squeeze())
x_init[x_i] = candidate.data
from statistics import median
max_score = max(scores)
median_score = median(scores)
print("After max {} median {}\n".format(max_score, median_score))
return max_score, median_score
def evaluate(args, coteaching: bool):
task = [args.task]
# Find the optimal design
result = {}
for t in task:
result[t] = {'max': [], 'median': []}
args.mode = 'design'
seeds1 = [0, 1, 2, 3, 4, 5, 6, 7]
seeds2 = [7, 0, 1, 2, 3, 4, 5, 6]
seeds3 = [6, 7, 0, 1, 2, 3, 4, 5]
seed = [0, 1, 2, 3, 4, 5, 6, 7]
for sd, s1, s2, s3 in zip(seed, seeds1, seeds2, seeds3):
print("Current seed is " + str(sd), end="\t")
set_seed(sd)
args.seed1 = s1
args.seed2 = s2
args.seed3 = s3
for t in task:
if t == 'TFBind8-Exact-v0' or t == 'TFBind10-Exact-v0' or t == 'CIFARNAS-Exact-v0': # since this is a discrete task
args.ft_lr = 1e-1
else:
args.ft_lr = 1e-3
print("Current task is " + str(t))
args.task = t
print("this is my setting", args)
max_score, median_score = design_opt(args)
result[t]['max'].append(max_score)
result[t]['median'].append(median_score)
if coteaching:
np.save("./meta_weight/fine_tune_results_" + str(args.num_coteaching) + ".npy", result)
else:
np.save("./meta_weight/baseline_results.npy", result)
def read_result(path):
if "baseline" in path:
print("Results of baseline models")
else:
print("Results of fine tuned models")
result = np.load(path, allow_pickle=True).item()
for t in result:
print("Results for task " + t + ":")
print("\tAverage for max score: " + str(np.mean(result[t]['max'])),
"\tStd for max score: " + str(np.std(result[t]['max'])))
print("\tAverage for median score: " + str(np.mean(result[t]['median'])),
"\tStd for median score: " + str(np.std(result[t]['median'])))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="pairwise offline")
parser.add_argument('--device', default=0, type=int)
parser.add_argument('--task', choices=['Superconductor-RandomForest-v0', 'HopperController-Exact-v0',
'AntMorphology-Exact-v0', 'DKittyMorphology-Exact-v0', 'TFBind8-Exact-v0',
'TFBind10-Exact-v0', 'CIFARNAS-Exact-v0'],
type=str, default='TFBind10-Exact-v0')
parser.add_argument('--mode', choices=['design', 'train'], type=str, default='train')
# grad descent to train proxy
parser.add_argument('--epochs', default=200, type=int)
parser.add_argument('--wd', default=0.0, type=float)
# grad ascent to obtain design
parser.add_argument('--Tmax', default=100, type=int)
parser.add_argument('--ft_lr', default=1e-1, type=float)
parser.add_argument('--topk', default=128, type=int)
parser.add_argument('--interval', default=100, type=int)
parser.add_argument('--proxy_path', default="new_proxies/", type=str)
parser.add_argument('--reweighting_path', default="meta_weight/", type=str)
parser.add_argument('--K', default=128, type=int)
parser.add_argument('--mu', default=0, type=int)
parser.add_argument('--std', default=1, type=int)
parser.add_argument('--seed1', default=1, type=int)
parser.add_argument('--seed2', default=10, type=int)
parser.add_argument('--seed3', default=100, type=int)
parser.add_argument('--noise_coefficient', type=float, default=0.1)
parser.add_argument('--alpha', default=1e-3, type=float)
parser.add_argument('--beta', default=1e-1, type=float)
parser.add_argument('--num_coteaching', default=64, type=int)
parser.add_argument('--if_reweight', default=True, type=bool)
parser.add_argument('--if_coteach', default=True, type=bool)
parser.add_argument('--clamp_norm', default=1, type=int)
parser.add_argument('--clamp_min', default=-0.2, type=float)
parser.add_argument('--clamp_max', default=0.2, type=float)
parser.add_argument('--reweight_mode', choices=['top128', 'half', 'full'], type=str, default='half')
args = parser.parse_args()
device = torch.device('cuda:' + str(args.device))
print(device)
args.num_coteaching = 128
experiment(args)
evaluate(args, coteaching=True)
args.num_coteaching = 64
experiment(args)
evaluate(args, coteaching=True)
args.num_coteaching = 32
experiment(args)
evaluate(args, coteaching=True)
args.num_coteaching = 16
experiment(args)
evaluate(args, coteaching=True)
args.num_coteaching = 8
experiment(args)
evaluate(args, coteaching=True)
print("Num of Co-Teaching 128:")
read_result("./meta_weight/fine_tune_results_128.npy")
print("Num of Co-Teaching 64:")
read_result("./meta_weight/fine_tune_results_64.npy")
print("Num of Co-Teaching 32:")
read_result("./meta_weight/fine_tune_results_32.npy")
print("Num of Co-Teaching 16:")
read_result("./meta_weight/fine_tune_results_16.npy")
print("Num of Co-Teaching 8:")
read_result("./meta_weight/fine_tune_results_8.npy")