-
Notifications
You must be signed in to change notification settings - Fork 0
/
mt_comp_full_old.py
executable file
·643 lines (592 loc) · 27.4 KB
/
mt_comp_full_old.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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# -*- coding: utf-8 -*-
import os
import numpy as np
from copy import deepcopy
import pickle as pk
import random
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader,Dataset
from optim_prob.optim_utils.optim_parser import optim_parser
from optim_prob.div_utils.neural_nets import MLP, CNN, test_img_strain, GCNN,\
feature_extract, class_classifier, GRL, test_img_gr
from utils.mt_utils import rescale_alphas, test_img_ttest, alpha_avg
from optim_prob.mnist_m import MNISTM
cwd = os.getcwd()
oargs = optim_parser()
np.random.seed(oargs.seed)
random.seed(oargs.seed)
if oargs.label_split == 0:
oargs.labels_type = 'iid'
# %%
import io
class CPU_Unpickler(pk.Unpickler):
def find_class(self, module, name):
if module == 'torch.storage' and name == '_load_from_bytes':
return lambda b: torch.load(io.BytesIO(b), map_location='cpu')
else: return super().find_class(module, name)
# %% load in optimization and divergence results
psi_vals,alpha_vals = None, None
tval_dict = {'psi_val':psi_vals,'alpha_val':alpha_vals}
if oargs.nrg_mt == 0:
if oargs.dset_split == 0:
for ie,entry in enumerate(tval_dict.keys()):
with open(cwd+'/optim_prob/optim_results/'+entry+'/devices'+str(oargs.t_devices)+\
'_seed'+str(oargs.seed)+'_'+oargs.div_nn\
+'_'+oargs.dset_type\
+'_'+oargs.labels_type,'rb') as f:
tval_dict[entry] = pk.load(f)
## load in the model parameters of all devices with labeled data
with open(cwd+'/optim_prob/source_errors/devices'+str(oargs.t_devices)+\
'_seed'+str(oargs.seed)+'_'+oargs.div_nn\
+'_'+oargs.dset_type\
+'_'+oargs.labels_type+'_modelparams_'+oargs.div_nn,'rb') as f:
lmp = pk.load(f) #labeled model parameters
else:
if oargs.dset_split == 1:
pre = ''
elif oargs.dset_split == 2:
pre = 'total_'
for ie,entry in enumerate(tval_dict.keys()):
with open(cwd+'/optim_prob/optim_results/'+entry+'/'+pre+'devices'+str(oargs.t_devices)+\
'_seed'+str(oargs.seed)+'_'+oargs.div_nn\
+'_'+oargs.split_type\
+'_'+oargs.labels_type,'rb') as f:
tval_dict[entry] = pk.load(f)
with open(cwd+'/optim_prob/source_errors/'+pre+'devices'+str(oargs.t_devices)+\
'_seed'+str(oargs.seed)+'_'+oargs.div_nn\
+'_'+oargs.split_type\
+'_'+oargs.labels_type+'_modelparams_'+oargs.div_nn,'rb') as f:
lmp = pk.load(f) #labeled model parameters
else: #load in the phi_e results
if oargs.grad_rev == True:
end2 = 'gr'
else:
end2 = ''
if oargs.fl == True:
prefl = 'fl'
else:
prefl = ''
if oargs.dset_split == 0:
if oargs.dset_type == 'MM':
end = '_base_6'
else:
end = ''
for ie,entry in enumerate(tval_dict.keys()):
with open(cwd+'/optim_prob/optim_results/'+entry+'/NRG_'+str(oargs.phi_e)+'_'+\
'devices'+str(oargs.t_devices)+\
'_seed'+str(oargs.seed)+'_'+oargs.div_nn\
+'_'+oargs.dset_type\
+'_'+oargs.labels_type+prefl+end+end2,'rb') as f:
tval_dict[entry] = pk.load(f)
## load in the model parameters of all devices with labeled data
with open(cwd+'/optim_prob/source_errors/devices'+str(oargs.t_devices)+\
'_seed'+str(oargs.seed)+'_'+oargs.div_nn\
+'_'+oargs.dset_type+'_'+oargs.labels_type\
+'_'+prefl+'_modelparams_'+oargs.div_nn+end+end2,'rb') as f:
lmp = pk.load(f) #labeled model parameters
else:
if oargs.dset_split == 1:
pre = ''
elif oargs.dset_split == 2:
pre = 'total_'
if 'MM' in oargs.split_type:
end = '_base_6'
else:
end = ''
for ie,entry in enumerate(tval_dict.keys()):
with open(cwd+'/optim_prob/optim_results/'+entry+'/NRG_'+str(oargs.phi_e)+'_'\
+pre+'devices'+str(oargs.t_devices)+\
'_seed'+str(oargs.seed)+'_'+oargs.div_nn\
+'_'+oargs.split_type\
+'_'+oargs.labels_type+prefl+end+end2,'rb') as f:
tval_dict[entry] = pk.load(f)
with open(cwd+'/optim_prob/source_errors/'+pre+'devices'+str(oargs.t_devices)+\
'_seed'+str(oargs.seed)+'_'+oargs.div_nn\
+'_'+oargs.split_type+'_'+oargs.labels_type\
+'_'+prefl+'_modelparams_'+oargs.div_nn+end+end2,'rb') as f:
# lmp = pk.load(f) #labeled model parameters
lmp = CPU_Unpickler(f).load()
psi_vals = tval_dict['psi_val']
alpha_vals = tval_dict['alpha_val']
psi_vals = [int(np.round(j,0)) for j in psi_vals[len(psi_vals.keys())-1]]
s_alpha,t_alpha,ovr_alpha,s_pv,t_pv= rescale_alphas(psi_vals,alpha_vals)
## load in the device data characteristics
with open(cwd+'/optim_prob/data_div/devices'+str(oargs.t_devices)+\
'_seed'+str(oargs.seed)+'_data_qty','rb') as f:
data_qty = pk.load(f)
if oargs.dset_split == 0:
with open(cwd+'/optim_prob/data_div/devices'+str(oargs.t_devices)\
+'_seed'+str(oargs.seed)\
+'_'+oargs.div_nn\
+'_'+oargs.dset_type+'_'+oargs.labels_type+'_lpd','rb') as f:
lpd = pk.load(f)
with open(cwd+'/optim_prob/data_div/devices'+str(oargs.t_devices)\
+'_seed'+str(oargs.seed)\
+'_'+oargs.div_nn\
+'_'+oargs.dset_type+'_'+oargs.labels_type+'_dindexsets','rb') as f:
d_dsets = pk.load(f)
else:
with open(cwd+'/optim_prob/data_div/'+pre+'devices'+str(oargs.t_devices)\
+'_seed'+str(oargs.seed)\
+'_'+oargs.div_nn\
+'_'+oargs.split_type+'_'+oargs.labels_type+'_lpd','rb') as f:
lpd = pk.load(f)
with open(cwd+'/optim_prob/data_div/'+pre+'devices'+str(oargs.t_devices)\
+'_seed'+str(oargs.seed)\
+'_'+oargs.div_nn\
+'_'+oargs.split_type+'_'+oargs.labels_type+'_dindexsets','rb') as f:
d_dsets = pk.load(f)
# %% load in datasets
if oargs.dset_split == 0:
if oargs.dset_type == 'M':
print('Using MNIST \n')
d_train = torchvision.datasets.MNIST(cwd+'/data/',train=True,download=True,\
transform=transforms.ToTensor())
elif oargs.dset_type == 'S': #needs scipy
print('Using SVHN \n')
tx_dat = torchvision.transforms.Compose([transforms.ToTensor(),\
transforms.Grayscale(),transforms.CenterCrop(28)])
d_train = torchvision.datasets.SVHN(cwd+'/data/svhn',split='train',download=True,\
transform=tx_dat)
d_train.targets = d_train.labels
#http://ufldl.stanford.edu/housenumbers/
elif oargs.dset_type == 'U':
print('Using USPS \n')
tx_dat = torchvision.transforms.Compose([transforms.ToTensor(),transforms.Pad(14-8)])
try:
d_train = torchvision.datasets.USPS(cwd+'/data/',train=True,download=True,\
transform=tx_dat)
except:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
d_train = torchvision.datasets.USPS(cwd+'/data/',train=True,download=True,\
transform=tx_dat)
elif oargs.dset_type == 'MM':
print('Using MNIST-M \n')
tx_dat = torchvision.transforms.Compose([transforms.ToTensor()])
d_train = MNISTM(cwd+'/data/',train=True,download=True,\
transform=tx_dat)
else:
raise TypeError('Dataset exceeds sims')
else: #if oargs.dset_split == 1:
tx_m = torchvision.transforms.Compose([transforms.ToTensor()])
tx_mm = torchvision.transforms.Compose([transforms.ToTensor(),\
transforms.Grayscale()])
tx_u = torchvision.transforms.Compose([transforms.ToTensor(),transforms.Pad(14-8)])
d_m = torchvision.datasets.MNIST(cwd+'/data/',train=True,download=True,\
transform=tx_m)
d_mm = MNISTM(cwd+'/data/',train=True,download=True,\
transform=tx_mm)
try:
d_u = torchvision.datasets.USPS(cwd+'/data/',train=True,download=True,\
transform=tx_u)
except:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
d_u = torchvision.datasets.USPS(cwd+'/data/',train=True,download=True,\
transform=tx_u)
d_u.targets = torch.tensor(d_u.targets)
if oargs.dset_split == 1:
if oargs.split_type == 'M+MM':
print('Using MNIST + MNIST-M')
d_train = d_m+d_mm
d_train.targets = torch.concat([d_m.targets,d_mm.targets])
elif oargs.split_type == 'M+U':
print('Using MNIST + USPS')
d_train = d_m+d_u
d_train.targets = torch.concat([d_m.targets,d_u.targets])
elif oargs.split_type == 'MM+U':
print('Using MNIST-M + USPS')
d_train = d_mm+d_u
d_train.targets = torch.concat([d_mm.targets,d_u.targets])
elif oargs.split_type == 'A':
print('Using MNIST + MNIST-M + USPS')
d_train = d_m+d_mm+d_u
d_train.targets = torch.concat([d_m.targets,d_mm.targets,d_u.targets])
else:
raise TypeError('Datasets exceed sims')
elif oargs.dset_split == 2:
with open(cwd+'/optim_prob/data_div/d2dset_devices'+str(oargs.t_devices)+\
'_seed'+str(oargs.seed),'rb') as f:
d2dset = pk.load(f)
d_train_dict = {}
if oargs.split_type == 'M+MM':
d0,d1= d_m,d_mm
elif oargs.split_type == 'M+U':
d0,d1 = d_m,d_u
elif oargs.split_type == 'MM+U':
d0,d1 = d_mm,d_u
else:
raise TypeError('datasets exceed sims')
for ind,dc in enumerate(np.where(d2dset==0)[0]):
d_train_dict[dc] = d0
for ind,dc in enumerate(np.where(d2dset==1)[0]):
d_train_dict[dc] = d1
d_train_dict = dict(sorted(d_train_dict.items()))
if oargs.div_comp == 'gpu':
device = torch.device('cuda:'+str(oargs.div_gpu_num))
else:
device = torch.device('cpu')
if oargs.grad_rev == True:
feature_net_base = feature_extract().to(device)
features_2_class_base = class_classifier().to(device)
GRL_base = GRL().to(device)
try:
with open(cwd+'/optim_prob/optim_utils/fnet_base_w','rb') as f:
fnet_base_w = pk.load(f)
with open(cwd+'/optim_prob/optim_utils/f2c_base_w','rb') as f:
f2c_base_w = pk.load(f)
with open(cwd+'/optim_prob/optim_utils/GRL_base_w','rb') as f:
GRL_base_w = pk.load(f)
feature_net_base.load_state_dict(fnet_base_w)
features_2_class_base.load_state_dict(f2c_base_w)
GRL_base.load_state_dict(GRL_base_w)
except:
fnet_base_w = feature_net_base.state_dict()
f2c_base_w = features_2_class_base.state_dict()
GRL_base_w = GRL_base.state_dict()
with open(cwd+'/optim_prob/optim_utils/fnet_base_w','wb') as f:
pk.dump(fnet_base_w,f)
with open(cwd+'/optim_prob/optim_utils/f2c_base_w','wb') as f:
pk.dump(f2c_base_w,f)
with open(cwd+'/optim_prob/optim_utils/GRL_base_w','wb') as f:
pk.dump(GRL_base_w,f)
else:
if oargs.div_nn == 'MLP':
if oargs.dset_split < 2:
d_in = np.prod(d_train[0][0].shape)
elif oargs.dset_split == 2:
d_in = np.prod(d0[0][0].shape)
d_h = 64
d_out = 10
start_net = MLP(d_in,d_h,d_out).to(device)
os_append = 'MLP_start_w'
# try:
# with open(cwd+'/optim_prob/optim_utils/MLP_start_w','rb') as f:
# start_w = pk.load(f)
# start_net.load_state_dict(start_w)
# except:
# start_w = start_net.state_dict()
# with open(cwd+'/optim_prob/optim_utils/MLP_start_w','wb') as f:
# pk.dump(start_w,f)
elif oargs.div_nn == 'CNN':
if oargs.dset_type in ['M','U'] or oargs.dset_split > 0:
nchannels = 1 #grayscaled
nclasses = 10
start_net = CNN(nchannels,nclasses).to(device)
os_append = 'CNN_start_w_1c'
elif oargs.dset_type in ['MM'] and oargs.dset_split == 0:
nchannels = 3
nclasses = 10
start_net = CNN(nchannels,nclasses).to(device)
os_append = 'CNN_start_w_3c'
else:
raise TypeError('check here')
else:
nchannels = 1
nclasses = 10
start_net = GCNN(nchannels,nclasses).to(device)
try:
with open(cwd+'/optim_prob/optim_utils/{}'.format(os_append),'rb') as f:
start_w = pk.load(f)
start_net.load_state_dict(start_w)
except:
start_w = start_net.state_dict()
with open(cwd+'/optim_prob/optim_utils/{}'.format(os_append),'wb') as f:
pk.dump(start_w,f)
# %% build models at devices with labelled data
## special case here, where the optimization returns all devices with labelled data
## as sources [special case]
our_psi_vals = deepcopy(psi_vals)
our_smodels = {}
our_saccs = {}
wap_dict = {}
our_tmodels = {}
our_taccs = {}
if oargs.grad_rev == True:
lmp1, lmp2 = {}, {}
for tk in lmp.keys():
lmp1[tk] = lmp[tk][0]
lmp2[tk] = lmp[tk][1]
for i,j in enumerate(our_psi_vals):
if j == 0: # our algorithm's source
our_smodels[i] = [deepcopy(feature_net_base),deepcopy(features_2_class_base)]
our_smodels[i][0].load_state_dict(lmp1[i])
our_smodels[i][1].load_state_dict(lmp2[i])
if oargs.dset_split < 2:
our_saccs[i],_ = test_img_gr(our_smodels[i][0],our_smodels[i][1],\
oargs.div_bs,d_train,indx=d_dsets[i],device=device)
elif oargs.dset_split == 2:
our_saccs[i],_ = test_img_gr(our_smodels[i][0],our_smodels[i][1],\
oargs.div_bs,d_train_dict[i],indx=d_dsets[i],device=device)
else:
wap1 = alpha_avg(lmp1,ovr_alpha[:,i])
wap2 = alpha_avg(lmp2,ovr_alpha[:,i])
our_tmodels[i] = [deepcopy(feature_net_base),deepcopy(features_2_class_base)]
our_tmodels[i][0].load_state_dict(wap1)
our_tmodels[i][1].load_state_dict(wap2)
if oargs.dset_split < 2:
our_taccs[i],_ = test_img_gr(our_tmodels[i][0],our_tmodels[i][1],\
oargs.div_bs,d_train,indx=d_dsets[i],device=device)
elif oargs.dset_split == 2:
our_taccs[i],_ = test_img_gr(our_tmodels[i][0],our_tmodels[i][1],\
oargs.div_bs,d_train_dict[i],indx=d_dsets[i],device=device)
else:
for i,j in enumerate(our_psi_vals):
if j == 0: # our algorithm's source
our_smodels[i] = deepcopy(start_net)
our_smodels[i].load_state_dict(lmp[i])
if oargs.dset_split < 2:
our_saccs[i],_ = test_img_ttest(our_smodels[i],oargs.div_bs,\
d_train,d_dsets[i],device=device)
elif oargs.dset_split == 2:
our_saccs[i],_ = test_img_ttest(our_smodels[i],oargs.div_bs,\
d_train_dict[i],d_dsets[i],device=device)
else:
wap = alpha_avg(lmp,ovr_alpha[:,i])
wap_dict[i] = wap
# test the resulting models
our_tmodels[i] = deepcopy(start_net)
our_tmodels[i].load_state_dict(wap)
if oargs.dset_split < 2:
our_taccs[i],_ = test_img_ttest(our_tmodels[i],oargs.div_bs,\
d_train,d_dsets[i],device=device)
elif oargs.dset_split == 2:
our_taccs[i],_ = test_img_ttest(our_tmodels[i],oargs.div_bs,\
d_train_dict[i],d_dsets[i],device=device)
# %% energy compute fxn + load in vars
with open(cwd+'/optim_prob/nrg_constants/devices'+str(oargs.t_devices)\
+'_d2dtxrates','rb') as f:
d2d_tx_rates = pk.load(f)
with open(cwd+'/optim_prob/nrg_constants/devices'+str(oargs.t_devices)\
+'_txpowers','rb') as f:
tx_powers = pk.load(f)
def mt_nrg_calc(tc_alpha,c2d_rates,tx_pow=tx_powers,M=oargs.p2bits):
param_2_bits = M
# calculate energy used for model transferring
ctx_nrg = 0
for ind_ca,ca in enumerate(tc_alpha):
if ca > 1e-3:
ctx_nrg += param_2_bits/c2d_rates[ind_ca] * tx_powers[ind_ca] #* ca
return ctx_nrg #current tx energy
# %% determine the heurstic sources
## alg 1: g_avg_acc, all devices with source accuracies > avg become sources
## others become targets
h1_models = {}
h1_accs = {}
h1_nrg = 0
avg_saccs = np.average(list(our_saccs.values()))
h1_psi = deepcopy(psi_vals)
h1_th = list(np.where(list(our_saccs.values()) < avg_saccs)[0]) # targets
h1_s = list(np.where(list(our_saccs.values()) >= avg_saccs)[0])
h1_lmp = {}
for i in h1_th: #make target instead of source
h1_psi[i] = 1
if oargs.grad_rev == True:
h1_lmp1, h1_lmp2 = {}, {}
for i,j in enumerate(h1_s): # grab the parameters in a dict
h1_lmp1[i] = deepcopy(lmp1[j])
h1_lmp2[i] = deepcopy(lmp2[j])
for i,j in enumerate(h1_psi):
if j == 1:
# build models
h1_alphas = np.round(np.random.dirichlet(np.ones(len(h1_s))),5)
h1wp1 = alpha_avg(h1_lmp1,h1_alphas)
h1wp2 = alpha_avg(h1_lmp2,h1_alphas)
h1_models[i] = [deepcopy(feature_net_base),deepcopy(features_2_class_base)]
h1_models[i][0].load_state_dict(h1wp1)
h1_models[i][1].load_state_dict(h1wp2)
if oargs.dset_split < 2:
h1_accs[i],_ = test_img_gr(h1_models[i][0],h1_models[i][1],\
oargs.div_bs,d_train,indx=d_dsets[i],device=device)
elif oargs.dset_split == 2:
h1_accs[i],_ = test_img_gr(h1_models[i][0],h1_models[i][1],\
oargs.div_bs,d_train_dict[i],indx=d_dsets[i],device=device)
tmp_c2d_rates = d2d_tx_rates[:,i][h1_s]
h1_nrg += mt_nrg_calc(h1_alphas,tmp_c2d_rates)
elif j == 0 and oargs.grad_rev == True:
h1_accs[i] = our_saccs[i]
else:
for i,j in enumerate(h1_s): # grab the parameters in a dict
h1_lmp[i] = deepcopy(lmp[j])
for i,j in enumerate(h1_psi):
if j == 1:
# build models
h1_alphas = np.round(np.random.dirichlet(np.ones(len(h1_s))),5)
h1wp = alpha_avg(h1_lmp,h1_alphas)
h1_models[i] = deepcopy(start_net)
h1_models[i].load_state_dict(h1wp)
if oargs.dset_split < 2:
h1_accs[i],_ = test_img_ttest(h1_models[i],oargs.div_bs,\
d_train,d_dsets[i],device=device)
elif oargs.dset_split == 2:
h1_accs[i],_ = test_img_ttest(h1_models[i],oargs.div_bs,\
d_train_dict[i],d_dsets[i],device=device)
tmp_c2d_rates = d2d_tx_rates[:,i][h1_s]
h1_nrg += mt_nrg_calc(h1_alphas,tmp_c2d_rates)
#### alg 2: greatest accuracy
## alg 2 new: FL
h2_models = {}
h2_accs = {}
h2_nrg = 0
h2_psi = deepcopy(psi_vals)
h2_s = np.argmax(list(our_saccs.values()))
h2_s = [h2_s]
h2_th = list(np.where(np.round(list(our_saccs.values()),2) < \
np.round(our_saccs[h2_s[0]],2))[0])
h2_lmp = {}
for i in h2_th:
h2_psi[i] = 1
if oargs.grad_rev == True:
h2_lmp1, h2_lmp2 = {}, {}
for i,j in enumerate(h2_s): # grab the parameters in a dict
h2_lmp1[i] = deepcopy(lmp1[j])
h2_lmp2[i] = deepcopy(lmp2[j])
for i,j in enumerate(h2_psi):
if j == 1:
# build models
h2_alphas = np.round(np.random.dirichlet(np.ones(len(h2_s))),5)
h2wp1 = alpha_avg(h2_lmp1,h2_alphas)
h2wp2 = alpha_avg(h2_lmp2,h2_alphas)
h2_models[i] = [deepcopy(feature_net_base),deepcopy(features_2_class_base)]
h2_models[i][0].load_state_dict(h2wp1)
h2_models[i][1].load_state_dict(h2wp2)
if oargs.dset_split < 2:
h2_accs[i],_ = test_img_gr(h2_models[i][0],h2_models[i][1],\
oargs.div_bs,d_train,indx=d_dsets[i],device=device)
elif oargs.dset_split == 2:
h2_accs[i],_ = test_img_gr(h2_models[i][0],h2_models[i][1],\
oargs.div_bs,d_train_dict[i],indx=d_dsets[i],device=device)
tmp_c2d_rates = d2d_tx_rates[:,i][h2_s]
h2_nrg += mt_nrg_calc(h2_alphas,tmp_c2d_rates)
elif j == 0 and oargs.grad_rev == True:
h2_accs[i] = our_saccs[i]
else:
for i,j in enumerate(h2_s):
h2_lmp[i] = deepcopy(lmp[j])
for i,j in enumerate(h2_psi):
if j == 1:
h2_alphas = np.round(np.random.dirichlet(np.ones(len(h2_s))),5)
h2wp = alpha_avg(h2_lmp,h2_alphas)
h2_models[i] = deepcopy(start_net)
h2_models[i].load_state_dict(h2wp)
if oargs.dset_split < 2:
h2_accs[i],_ = test_img_ttest(h2_models[i],oargs.div_bs,\
d_train,d_dsets[i],device=device)
elif oargs.dset_split == 2:
h2_accs[i],_ = test_img_ttest(h2_models[i],oargs.div_bs,\
d_train_dict[i],d_dsets[i],device=device)
tmp_c2d_rates = d2d_tx_rates[:,i][h2_s]
h2_nrg += mt_nrg_calc(h2_alphas,tmp_c2d_rates)
## alg 3: random source determination from devices with labelled datasets
r_models = {}
r_accs = {}
r_s_accs = {}
r_nrg = 0
r_psi = np.array(deepcopy(psi_vals))
r_s_init = list(np.where(r_psi == 0)[0])
tt = np.random.rand(len(r_s_init))
# r_s_change = list(np.where(tt >= 0.75)[0]) #then a target and not a source
r_s_change = list(np.where(tt >= 0.5)[0])
for i in r_s_change:
r_s_init.pop(r_s_init.index(i))
r_psi[i] = 1
r_s = r_s_init
r_lmp = {}
if oargs.grad_rev == True:
r_lmp1, r_lmp2 = {}, {}
for i,j in enumerate(r_s): # grab the parameters in a dict
r_lmp1[i] = deepcopy(lmp1[j])
r_lmp2[i] = deepcopy(lmp2[j])
for i,j in enumerate(r_psi):
if j == 1:
# build models (random weights)
r_alphas = np.round(np.random.dirichlet(np.ones(len(r_s))),5)
rwp1 = alpha_avg(r_lmp1,r_alphas)
rwp2 = alpha_avg(r_lmp2,r_alphas)
r_models[i] = [deepcopy(feature_net_base),deepcopy(features_2_class_base)]
r_models[i][0].load_state_dict(rwp1)
r_models[i][1].load_state_dict(rwp2)
if oargs.dset_split < 2:
r_accs[i],_ = test_img_gr(r_models[i][0],r_models[i][1],\
oargs.div_bs,d_train,indx=d_dsets[i],device=device)
elif oargs.dset_split == 2:
r_accs[i],_ = test_img_gr(r_models[i][0],r_models[i][1],\
oargs.div_bs,d_train_dict[i],indx=d_dsets[i],device=device)
tmp_c2d_rates = d2d_tx_rates[:,i][r_s]
r_nrg += mt_nrg_calc(r_alphas,tmp_c2d_rates)
elif j == 0 and oargs.grad_rev == True:
r_accs[i] = our_saccs[i]
else:
for i,j in enumerate(r_s):
r_lmp[i] = deepcopy(lmp[j])
for i,j in enumerate(r_psi):
if j == 1:
# build models (random weights)
r_alphas = np.round(np.random.dirichlet(np.ones(len(r_s))),5)
r_wp = alpha_avg(r_lmp,r_alphas)
r_models[i] = deepcopy(start_net)
r_models[i].load_state_dict(r_wp)
if oargs.dset_split < 2:
r_accs[i],_ = test_img_ttest(r_models[i],oargs.div_bs,\
d_train,d_dsets[i],device=device)
elif oargs.dset_split == 2:
r_accs[i],_ = test_img_ttest(r_models[i],oargs.div_bs,\
d_train_dict[i],d_dsets[i],device=device)
tmp_c2d_rates = d2d_tx_rates[:,i][r_s]
r_nrg += mt_nrg_calc(r_alphas,tmp_c2d_rates)
# %% save the results
import pandas as pd
acc_df = pd.DataFrame()
accs_vec = [list(r_accs.values()),list(h1_accs.values()),list(h2_accs.values())]
accs_vec_lens = [len(i) for i in accs_vec]
df_max_len = np.max(accs_vec_lens)
df_max_len_ind = np.argmax(accs_vec_lens)
for i,j in enumerate(accs_vec):
if len(j) < df_max_len:
tarr = np.empty(df_max_len - len(j))
tarr[:] = np.nan
j += tarr.tolist()
accs_vec[i] = j
acc_df['rng'] = accs_vec[0]
acc_df['geq_avg_acc'] = accs_vec[1]
acc_df['max_acc'] = accs_vec[2]
nrg_df = pd.DataFrame()
nrg_df['rng'] = [r_nrg]
nrg_df['geq_avg_acc'] = [h1_nrg]
nrg_df['max_acc'] = [h2_nrg]
print(acc_df)
print(nrg_df)
# if oargs.nrg_mt == 0:
# if oargs.dset_split == 0: # only one dataset
# acc_df.to_csv(cwd+'/mt_results/'+oargs.dset_type+'/seed_'+str(oargs.seed)\
# +'_st_det_'+oargs.labels_type \
# +'_'+oargs.div_nn+'_acc.csv')
# nrg_df.to_csv(cwd+'/mt_results/'+oargs.dset_type+'/seed_'+str(oargs.seed)\
# +'_st_det_'+oargs.labels_type \
# +'_'+oargs.div_nn+'_nrg.csv')
# else:
# acc_df.to_csv(cwd+'/mt_results/'+oargs.split_type+'/seed_'+str(oargs.seed)\
# +'_st_det_'+oargs.labels_type \
# +'_'+oargs.div_nn+'_acc.csv')
# nrg_df.to_csv(cwd+'/mt_results/'+oargs.split_type+'/seed_'+str(oargs.seed)\
# +'_st_det_'+oargs.labels_type \
# +'_'+oargs.div_nn+'_nrg.csv')
# else: ## adjust file name with nrg
# if oargs.dset_split == 0: # only one dataset
# acc_df.to_csv(cwd+'/mt_results/'+oargs.dset_type+'/NRG'+str(oargs.phi_e)\
# +'_seed_'+str(oargs.seed)+'_st_det_'+oargs.labels_type \
# +'_'+oargs.div_nn+prefl+end+end2+'_acc.csv')
# nrg_df.to_csv(cwd+'/mt_results/'+oargs.dset_type+'/NRG'+str(oargs.phi_e)\
# +'_seed_'+str(oargs.seed)+'_st_det_'+oargs.labels_type \
# +'_'+oargs.div_nn+prefl+end+end2+'_nrg.csv')
# else:
# acc_df.to_csv(cwd+'/mt_results/'+oargs.split_type+'/NRG'+str(oargs.phi_e)+'_'\
# +pre+'seed_'+str(oargs.seed)+'_st_det_'+oargs.labels_type \
# +'_'+oargs.div_nn+prefl+end+end2+'_acc.csv')
# nrg_df.to_csv(cwd+'/mt_results/'+oargs.split_type+'/NRG'+str(oargs.phi_e)+'_'\
# +pre+'seed_'+str(oargs.seed)+'_st_det_'+oargs.labels_type \
# +'_'+oargs.div_nn+prefl+end+end2+'_nrg.csv')