-
Notifications
You must be signed in to change notification settings - Fork 4
/
run_mtcl.py
399 lines (290 loc) · 13.6 KB
/
run_mtcl.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
import sys,os,argparse,time
import numpy as np
import torch
import utils
from sklearn.decomposition import PCA
from sklearn.preprocessing import MinMaxScaler
import scipy
import statistics
import math
import scipy.stats as ss
tstart=time.time()
from config import set_args
########################################################################################################################
args = set_args()
args.output='./res/'+args.experiment+'_'+args.approach+'_'+str(args.note)+'.txt'
print('='*100)
print('Arguments =')
for arg in vars(args):
print('\t'+arg+':',getattr(args,arg))
print('='*100)
########################################################################################################################
# Seed
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if torch.cuda.is_available(): torch.cuda.manual_seed(args.seed)
else: print('[CUDA unavailable]'); sys.exit()
# Args -- Experiment
if args.experiment=='mixemnist':
from dataloaders import mixemnist as dataloader
elif args.experiment=='mixceleba':
from dataloaders import mixceleba as dataloader
# Args -- Approach
if 'mtcl_ncl' in args.approach:
from approaches import mtcl_ncl as approach
if 'mlp_mtcl' in args.approach:
from networks import mlp_mtcl as network
elif 'alexnet_mtcl' in args.approach:
from networks import alexnet_mtcl as network
########################################################################################################################
# Load
data,taskcla,inputsize=dataloader.get(seed=args.seed,args=args)
print('Input size =',inputsize,'\nTask info =',taskcla)
print('taskcla: ',len(taskcla))
# Loop tasks
acc_ac=np.zeros((len(taskcla),len(taskcla)),dtype=np.float32)
lss_ac=np.zeros((len(taskcla),len(taskcla)),dtype=np.float32)
acc_mcl=np.zeros((len(taskcla),len(taskcla)),dtype=np.float32)
lss_mcl=np.zeros((len(taskcla),len(taskcla)),dtype=np.float32)
acc_an=np.zeros((len(taskcla),len(taskcla)),dtype=np.float32)
lss_an=np.zeros((len(taskcla),len(taskcla)),dtype=np.float32)
unit_overlap_sum_transfer=np.zeros((len(taskcla),len(taskcla)),dtype=np.float32)
norm_transfer_raw=np.zeros((len(taskcla),len(taskcla)),dtype=np.float32)
norm_transfer_one=np.zeros((len(taskcla),len(taskcla)),dtype=np.float32)
norm_transfer=np.zeros((len(taskcla),len(taskcla)),dtype=np.float32)
acc_transfer=np.zeros((len(taskcla),len(taskcla)),dtype=np.float32)
acc_reference=np.zeros((len(taskcla),len(taskcla)),dtype=np.float32)
lss_transfer=np.zeros((len(taskcla),len(taskcla)),dtype=np.float32)
similarity_transfer=np.zeros((len(taskcla),len(taskcla)),dtype=np.float32)
def auto_similarity(task,appr):
if t > 0:
for pre_task in range(t+1):
print('pre_task: ',pre_task)
print('t: ',t)
pre_task_torch = torch.autograd.Variable(torch.LongTensor([pre_task]).cuda(),volatile=False)
# gfc1,gfc2 = appr.model.mask(pre_task_torch,s=appr.smax)
gfc1,gfc2 = appr.model.mask(pre_task_torch)
gfc1=gfc1.detach()
gfc2=gfc2.detach()
pre_mask=[gfc1,gfc2]
if pre_task == t: # the last one
print('>>> Now Training Phase: {:6s} <<<'.format('reference'))
appr.train(t,xtrain,ytrain,xvalid,yvalid,phase='reference',args=args,
pre_mask=pre_mask,pre_task=pre_task) # it is actually random mask
elif pre_task != t:
print('>>> Now Training Phase: {:6s} <<<'.format('transfer'))
appr.train(t,xtrain,ytrain,xvalid,yvalid,phase='transfer',args=args,
pre_mask=pre_mask,pre_task=pre_task)
if pre_task == t: # the last one
test_loss,test_acc=appr.eval(t,xvalid,yvalid,phase='reference',
pre_mask=pre_mask,pre_task=pre_task)
elif pre_task != t:
test_loss,test_acc=appr.eval(t,xvalid,yvalid,phase='transfer',
pre_mask=pre_mask,pre_task=pre_task)
print('>>> Test on task {:2d} - {:15s}: loss={:.3f}, acc={:5.1f}% <<<'.format(t,data[t]['name'],test_loss,100*test_acc))
acc_transfer[t,pre_task]=test_acc
lss_transfer[t,pre_task]=test_loss
print('test_acc: ',acc_transfer[t][:t+1])
print('test_loss: ',lss_transfer[t][:t+1])
print('Save at transfer_acc')
np.savetxt(args.output + '_acc_transfer',acc_transfer,'%.4f',delimiter='\t')
print('Save at transfer_loss')
np.savetxt(args.output + '_loss_transfer',lss_transfer,'%.4f',delimiter='\t')
similarity = [0]
if t > 0:
acc_list = acc_transfer[t][:t] #t from 0
print('acc_list: ',acc_list)
# print(type(acc_list))
if 'auto' in args.similarity_detection:
similarity = [0 if (acc_list[acc_id] <= acc_transfer[t][t]) else 1 for acc_id in range(len(acc_list))] # remove all acc < 0.5
else:
raise NotImplementedError
for source_task in range(len(similarity)):
similarity_transfer[t,source_task]=similarity[source_task]
print('Save at similarity_transfer')
np.savetxt(args.output + '_similarity_transfer',similarity_transfer,'%.4f',delimiter='\t')
acc_to_rank = []
for s in range(len(similarity)):
acc_to_rank.append(acc_transfer[t][s])
print('similarity: ',similarity)
return similarity
def read_pre_computed_similarity(f_name):
with open(f_name,'r') as f:
similarity = [int(float(_))for _ in f.readlines()[t].split('\t')[:t]]
return similarity
def true_similarity(task,data):
similarity = [0]
if t > 0:
for pre_task in range(t):
print('pre_task: ',pre_task)
print('t: ',t)
if 'fe-mnist' in data[pre_task]['name'] and 'fe-mnist' in data[t]['name']: #both femnist
norm_transfer_raw[t,pre_task] = 1
elif 'celeba' in data[pre_task]['name'] and 'celeba' in data[t]['name']: #both femnist
norm_transfer_raw[t,pre_task] = 1
else: # anything else
norm_transfer_raw[t,pre_task] = 0
np.savetxt(args.output + '_norm_transfer_raw',norm_transfer_raw,'%.4f',delimiter='\t')
similarity = norm_transfer_raw[t][:t]
print('similarity: ',similarity)
return similarity
def all_one_similarity(task,data):
print('all one')
similarity = [0]
if t > 0:
for pre_task in range(t):
print('pre_task: ',pre_task)
print('t: ',t)
norm_transfer_raw[t,pre_task] = 1
np.savetxt(args.output + '_norm_transfer_raw',norm_transfer_raw,'%.4f',delimiter='\t')
similarity = norm_transfer_raw[t][:t]
print('similarity: ',similarity)
return similarity
def all_zero_similarity(task,norm_transfer_raw,data):
print('all zero')
similarity = [0]
if t > 0:
for pre_task in range(t):
print('pre_task: ',pre_task)
print('t: ',t)
norm_transfer_raw[t,pre_task] = 0
np.savetxt(args.output + '_norm_transfer_raw',norm_transfer_raw,'%.4f',delimiter='\t')
similarity = norm_transfer_raw[t][:t]
print('similarity: ',similarity)
return similarity
########################################################################################################################
# Inits
print('Inits...')
net=network.Net(inputsize,taskcla,nhid=args.nhid,args=args).cuda()
utils.print_model_report(net)
appr=approach.Appr(net,args=args)
print(appr.criterion)
utils.print_optimizer_config(appr.optimizer)
print('-'*100)
check_federated = approach.CheckFederated()
similarity = [0]
history_mask_back = []
history_mask_pre = []
similarities = []
for t,ncla in taskcla:
print('*'*100)
print('Task {:2d} ({:s})'.format(t,data[t]['name']))
print('*'*100)
if 'mtl' in args.approach:
# Get data. We do not put it to GPU
if t==0:
xtrain=data[t]['train']['x']
ytrain=data[t]['train']['y']
xvalid=data[t]['valid']['x']
yvalid=data[t]['valid']['y']
task_t=t*torch.ones(xtrain.size(0)).int()
task_v=t*torch.ones(xvalid.size(0)).int()
task=[task_t,task_v]
else:
xtrain=torch.cat((xtrain,data[t]['train']['x']))
ytrain=torch.cat((ytrain,data[t]['train']['y']))
xvalid=torch.cat((xvalid,data[t]['valid']['x']))
yvalid=torch.cat((yvalid,data[t]['valid']['y']))
task_t=torch.cat((task_t,t*torch.ones(data[t]['train']['y'].size(0)).int()))
task_v=torch.cat((task_v,t*torch.ones(data[t]['valid']['y'].size(0)).int()))
task=[task_t,task_v]
else:
# Get data
xtrain=data[t]['train']['x'].cuda()
ytrain=data[t]['train']['y'].cuda()
xvalid=data[t]['valid']['x'].cuda()
yvalid=data[t]['valid']['y'].cuda()
task=t
if t==0:
candidate_phases = ['mcl']
elif t>0:
if 'pipeline' in args.loss_type:
candidate_phases = ['kt','mcl']
else:
candidate_phases = ['mcl']
else:
raise NotImplementedError
for candidate_phase in candidate_phases:
if 'pipeline' in args.loss_type:
if candidate_phase == 'kt' and 'auto' in args.similarity_detection:
similarity = auto_similarity(task,appr)
elif candidate_phase == 'kt' and 'by-name' in args.similarity_detection:
similarity = true_similarity(task,data)
elif candidate_phase == 'kt' and 'all-one' in args.similarity_detection:
similarity = all_one_similarity(task,data)
elif candidate_phase == 'kt' and 'all-zero' in args.similarity_detection:
similarity = all_zero_similarity(task,data)
else:
if candidate_phase == 'mcl' and 'auto' in args.similarity_detection:
similarity = auto_similarity(task,appr)
elif candidate_phase == 'mcl' and 'by-name' in args.similarity_detection:
similarity = true_similarity(task,data)
elif candidate_phase == 'mcl' and 'all-one' in args.similarity_detection:
similarity = all_one_similarity(task,data)
elif candidate_phase == 'mcl' and 'all-zero' in args.similarity_detection:
similarity = all_zero_similarity(task,data)
# else:
# raise NotImplementedError
similarities.append(similarity)
check_federated.set_similarities(similarities)
print('>>> Now Training Phase: {:6s} <<<'.format(candidate_phase))
appr.train(task,xtrain,ytrain,xvalid,yvalid,candidate_phase,args,
similarity=similarity,history_mask_back=history_mask_back,
history_mask_pre=history_mask_pre,check_federated=check_federated)
print('-'*100)
if candidate_phase == 'mcl':
history_mask_back.append(dict((k, v.data.clone()) for k, v in appr.mask_back.items()) )
history_mask_pre.append([m.data.clone() for m in appr.mask_pre])
for u in range(t+1):
xtest=data[u]['test']['x'].cuda()
ytest=data[u]['test']['y'].cuda()
xvalid=data[u]['valid']['x'].cuda()
yvalid=data[u]['valid']['y'].cuda()
test_loss,test_acc=appr.test(u,xtest,ytest,candidate_phase,args,
similarity=similarity,history_mask_pre=history_mask_pre,
check_federated=check_federated,
xvalid=xvalid,yvalid=yvalid)
print('>>> Test on task {:2d} - {:15s}: loss={:.3f}, acc={:5.1f}% <<<'.format(u,data[u]['name'],test_loss,100*test_acc))
acc_mcl[t,u]=test_acc
lss_mcl[t,u]=test_loss
# Save
print('Save at '+args.output + '_' + candidate_phase)
np.savetxt(args.output + '_' + candidate_phase,acc_mcl,'%.4f',delimiter='\t')
# Done
print('*'*100)
print('Accuracies =')
for i in range(acc_mcl.shape[0]):
print('\t',end='')
for j in range(acc_mcl.shape[1]):
print('{:5.1f}% '.format(100*acc_mcl[i,j]),end='')
print()
print('*'*100)
print('Done!')
print('[Elapsed time = {:.1f} h]'.format((time.time()-tstart)/(60*60)))
performance_output_mcl_backward=args.output+'_mcl_backward_performance'
performance_output_mcl_forward=args.output+'_mcl_forward_performance'
with open(performance_output_mcl_backward,'w') as file_backward, open(performance_output_mcl_forward,'w') as file_forward:
for j in range(acc_mcl.shape[1]):
file_backward.writelines(str(acc_mcl[-1][j]) + '\n')
for j in range(acc_mcl.shape[1]):
file_forward.writelines(str(acc_mcl[j][j]) + '\n')
if hasattr(appr, 'logs'):
if appr.logs is not None:
#save task names
from copy import deepcopy
appr.logs['task_name'] = {}
appr.logs['test_acc'] = {}
appr.logs['test_loss'] = {}
for t,ncla in taskcla:
appr.logs['task_name'][t] = deepcopy(data[t]['name'])
appr.logs['test_acc'][t] = deepcopy(acc_mcl[t,:])
appr.logs['test_loss'][t] = deepcopy(lss_mcl[t,:])
#pickle
import gzip
import pickle
with gzip.open(os.path.join(appr.logpath), 'wb') as output:
pickle.dump(appr.logs, output, pickle.HIGHEST_PROTOCOL)
# scipy eigs =======
# eigenvalues,eigenvectors = scipy.sparse.linalg.eigs(tournament, k=1, which='LM')
# p_eigenvectors = eigenvectors[:,torch.argmax(eigenvalues,dim=0)[0]]
# similarity = [x/sum(p_eigenvectors) for x in p_eigenvectors]