-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtrain.py
More file actions
executable file
·629 lines (569 loc) · 24.7 KB
/
train.py
File metadata and controls
executable file
·629 lines (569 loc) · 24.7 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
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
import transformers
from config import *
from model import CLModel, FGM
from data_process import *
from util_methods import *
from spcl_loss import SupProtoConLoss
def get_paramsgroup(model, warmup=False):
no_decay = ['bias', 'LayerNorm.weight']
pre_train_lr = CONFIG['ptmlr']
bert_params = list(map(id, model.f_context_encoder.parameters()))
params = []
warmup_params = []
for name, param in model.named_parameters():
lr = CONFIG['lr']
weight_decay = 0.01
if id(param) in bert_params:
lr = pre_train_lr
if any(nd in name for nd in no_decay):
weight_decay = 0
params.append({
'params': param,
'lr': lr,
'weight_decay': weight_decay
})
warmup_params.append({
'params':
param,
'lr':
CONFIG['ptmlr'] / 4 if id(param) in bert_params else lr,
'weight_decay':
weight_decay
})
if warmup:
return warmup_params
params = sorted(params, key=lambda x: x['lr'])
return params
def train_epoch(model,
optimizer,
lr_scheduler,
trainset,
centers,
centers_mask=None,
epoch=0,
max_step=-1,
train_obj='all'):
model.train()
inner_model = model.module if hasattr(model, 'module') else model
ce_loss_func = torch.nn.CrossEntropyLoss(ignore_index=-1)
spcl_loss = SupProtoConLoss(
num_classes=CONFIG['num_classes'],
temp=CONFIG['temperature'],
pool_size=CONFIG['pool_size'],
support_set_size=CONFIG['support_set_size'],
centers=centers)
accumulation_steps = CONFIG['accumulation_steps']
sampler = RandomSampler(
trainset) if CONFIG['local_rank'] == -1 else DistributedSampler(
trainset)
if CONFIG['local_rank'] != -1:
sampler.set_epoch(epoch)
dataloader = DataLoader(trainset,
batch_size=CONFIG['batch_size'],
sampler=sampler,
num_workers=0,
drop_last=True)
tq_train = tqdm(total=max_step if max_step > 0 else len(dataloader),
position=1,
disable=CONFIG['local_rank'] not in [-1, 0])
def calc_loss(sentences, emotion_idxs, labels):
ccl_reps = inner_model.gen_f_reps(sentences)
if train_obj == 'ce':
direct_loss = ce_loss_func(inner_model.predictor(ccl_reps),
emotion_idxs)
else:
direct_loss = ce_loss_func(inner_model.predictor(ccl_reps.detach()),
emotion_idxs)
ins_cl_loss = torch.zeros(1).to(CONFIG['device'])
if train_obj == 'spcl':
ins_cl_loss = spcl_loss(ccl_reps, labels)
if train_obj == 'spdcl':
ins_cl_loss = spcl_loss(ccl_reps, labels, decoupled=True)
loss = direct_loss + ins_cl_loss
tq_train.set_description(
'direct loss {:.2f}, instance_cl_loss {:.2f}'
.format(direct_loss.item(),
ins_cl_loss.item() if train_obj in ['spcl', 'spdcl'] else 0))
return loss
fgm = FGM(inner_model)
for batch_id, batch_data in enumerate(dataloader):
batch_data = [x.to(inner_model.device()) for x in batch_data]
sentences = batch_data[0]
emotion_idxs = batch_data[1].reshape(-1)
labels = batch_data[2].reshape(-1)
loss = calc_loss(sentences, emotion_idxs, labels)
loss = loss / accumulation_steps
loss.backward()
if CONFIG['fgm']:
fgm.attack()
loss = calc_loss(sentences, emotion_idxs, labels)
loss = loss / accumulation_steps
loss.backward()
fgm.restore()
nn.utils.clip_grad_norm_(inner_model.parameters(), max_norm=5, norm_type=2)
tq_train.update()
if batch_id % accumulation_steps == 0:
optimizer.step()
lr_scheduler.step()
optimizer.zero_grad()
if max_step > 0 and batch_id > max_step:
optimizer.zero_grad()
break
optimizer.zero_grad()
tq_train.close()
def train(model, train_dialogues, dev_dialogues, test_dialogues):
devset = build_dataset(dev_dialogues)
testset = build_dataset(test_dialogues)
tq_epoch = tqdm(total=CONFIG['epochs'],
position=0,
disable=CONFIG['local_rank'] not in [-1, 0])
centers = None
if CONFIG['local_rank'] != -1:
model = torch.nn.parallel.DistributedDataParallel(
model,
device_ids=[CONFIG['local_rank']],
output_device=CONFIG['local_rank'],
find_unused_parameters=True)
optimizer = torch.optim.AdamW(
get_paramsgroup(
model.module if hasattr(model, 'module') else model))
checkpoint_saver = FGM(model)
# train
best_f1 = -1
epochs_not_update = 0
train_obj = CONFIG['train_obj']
for epoch in range(CONFIG['epochs']+1):
tq_epoch.set_description('training on epoch {}'.format(epoch))
tq_epoch.update()
trainset = build_dataset(train_dialogues, train=True)
if CONFIG['local_rank'] in [-1, 0]:
all_reps, all_corr_labels = gen_all_reps(model, trainset)
logging.info('clustering...')
centers, centers_mask, cluster2dataid, cluster2classid, all_centers = cluster(
all_reps, all_corr_labels, init_centers=centers, epoch=epoch)
num_data = all_reps.shape[0]
if epoch > 0:
f1 = test(model,
devset,
centers,
centers_mask,
desc='dev @ epoch {}'.format(epoch - 1))
if f1 > best_f1:
epochs_not_update = 0
os.system('rm {}/models/*'.format(CONFIG['temp_path']))
os.system('rm {}/centers/*'.format(CONFIG['temp_path']))
best_f1 = f1
model_to_save = model.module if hasattr(
model, "module") else model
torch.save(
model_to_save, CONFIG['temp_path'] +
'/models/f1_{:.4f}_@epoch{}.pkl'.format(
best_f1, epoch - 1))
torch.save(
centers, CONFIG['temp_path'] +
'/centers/f1_{:.4f}_@epoch{}.pkl'.format(
best_f1, epoch - 1))
torch.save(
centers_mask, CONFIG['temp_path'] +
'/centers/f1_{:.4f}_@epoch{}.msk'.format(
best_f1, epoch - 1))
f1 = test(model,
testset,
centers,
centers_mask,
desc='new best test @ epoch {}'.format(
epoch - 1))
# checkpoint_saver.save_checkpoint()
else:
epochs_not_update += 1
# checkpoint_saver.load_checkpoint()
torch.cuda.empty_cache()
if epochs_not_update >= 5:
break
selection, cluster_idxs = gen_cl_data(all_reps,
all_centers,
cluster2dataid,
cluster2classid,
epoch=epoch)
# st = 1 - epoch / 10
# ed = epoch / 10
st = 1 - epoch / CONFIG['epochs']
ed = epoch / CONFIG['epochs']
num_data = len(selection)
selection = torch.LongTensor(selection)
prob_list = [
st + (ed - st) / (num_data - 1) * i for i in range(num_data)
]
prob_tensor = torch.FloatTensor(prob_list)
rand_prob_tensor = torch.bernoulli(torch.ones(num_data) * 0.5)
if CONFIG['cl']:
sample = torch.bernoulli(prob_tensor).long()
else:
sample = torch.bernoulli(rand_prob_tensor).long()
selection = selection * sample
sample_results = selection[torch.nonzero(selection)]
all_idxs = sample_results.numpy().tolist()
epoch_trainset = TensorDataset(trainset[all_idxs][0],
trainset[all_idxs][1],
cluster_idxs[all_idxs])
torch.save(epoch_trainset,
CONFIG['temp_path'] + '/temp/train_set.pkl')
torch.save(centers, CONFIG['temp_path'] + '/temp/centers.pkl')
torch.save(centers_mask,
CONFIG['temp_path'] + '/temp/centers_mask.pkl')
if CONFIG['local_rank'] != -1:
torch.distributed.barrier()
epoch_trainset = torch.load(CONFIG['temp_path'] +
'/temp/train_set.pkl',
map_location=CONFIG['device'])
centers = torch.load(CONFIG['temp_path'] + '/temp/centers.pkl',
map_location=CONFIG['device'])
centers_mask = torch.load(CONFIG['temp_path'] +
'/temp/centers_mask.pkl',
map_location=CONFIG['device'])
num_training_steps = len(epoch_trainset)//(CONFIG['batch_size'] * CONFIG['accumulation_steps'])
num_warmup_steps = min(CONFIG['warm_up'], num_training_steps) if epoch == 0 else 0
lr_scheduler = transformers.get_cosine_schedule_with_warmup(
optimizer,
num_warmup_steps=num_warmup_steps,
num_training_steps=num_training_steps
)
if epoch < CONFIG['epochs']:
train_epoch(model,
optimizer,
lr_scheduler,
epoch_trainset,
centers,
centers_mask,
epoch,
train_obj=train_obj)
tq_epoch.close()
if CONFIG['local_rank'] in [0, -1]:
model, centers, centers_mask = load_latest()
f1 = test(model, testset, centers, centers_mask)
print('best f1 test is {:.4f}'.format(f1), flush=True)
os.system('rm -r {}'.format(CONFIG['temp_path']))
def test(model, data, centers, centers_mask, desc=''):
model.eval()
inner_model = model.module if hasattr(model, 'module') else model
y_true_list = []
direct_list = [[], [], [], []]
cluster_list = [[], [], [], []]
sampler = SequentialSampler(data)
dataloader = DataLoader(
data,
batch_size=CONFIG['batch_size'],
sampler=sampler,
num_workers=0, # multiprocessing.cpu_count()
)
tq_test = tqdm(total=len(dataloader), desc="testing", position=2)
for batch_id, batch_data in enumerate(dataloader):
batch_data = [x.to(inner_model.device()) for x in batch_data]
sentences = batch_data[0]
emotion_idxs = batch_data[1].reshape(-1)
with torch.no_grad():
ccl_reps = inner_model.gen_f_reps(sentences)
cluster_outputs, direct_outputs = [], []
feature_list = [ccl_reps]
num_feature = len(feature_list)
for idx, feature in enumerate(feature_list):
#
outputs = inner_model(feature, centers, score_func)
outputs -= (1 - centers_mask) * 2
cluster_outputs.append(torch.argmax(outputs.max(-1)[0], -1))
direct_outputs.append(
torch.argmax(inner_model.predictor(feature), -1))
for batch_id in range(emotion_idxs.shape[0]):
if emotion_idxs[batch_id] > -1:
for idx in range(num_feature):
direct_list[idx].append(
direct_outputs[idx][batch_id].item())
cluster_list[idx].append(
cluster_outputs[idx][batch_id].item())
y_true_list.append(emotion_idxs[batch_id].item())
tq_test.update()
direct_f1_scores = [
f1_score(y_true=y_true_list,
y_pred=direct_list[idx],
average='weighted') for idx in range(num_feature)
]
cluster_f1_scores = [
f1_score(y_true=y_true_list,
y_pred=cluster_list[idx],
average='weighted') for idx in range(num_feature)
]
# f1 = max(max(direct_f1_scores), max(cluster_f1_scores))
f1 = max(cluster_f1_scores)
print('\n{} best w-f1 is {:.4f}'.format(desc, f1), flush=True)
print('direct f1 cls {:.4f}'.format(*direct_f1_scores))
print('cluster f1 cls {:.4f}'.format(*cluster_f1_scores),
flush=True)
return f1
def load_latest():
model_path = CONFIG['temp_path'] + '/models'
lst = os.listdir(model_path)
lst = list(filter(lambda item: item.endswith('.pkl'), lst))
lst.sort(key=lambda x: os.path.getmtime(os.path.join(model_path, x)))
model = torch.load(os.path.join(model_path, lst[-1]))
logging.info(
'model checkpoint {} is loaded'.format(
os.path.join(model_path, lst[-1])), )
center_path = CONFIG['temp_path'] + '/centers'
lst = os.listdir(center_path)
lst = list(filter(lambda item: item.endswith('.pkl'), lst))
lst.sort(key=lambda x: os.path.getmtime(os.path.join(center_path, x)))
centers = torch.load(os.path.join(center_path, lst[-1]))
logging.info(
'center checkpoint {} is loaded'.format(
os.path.join(center_path, lst[-1])), )
lst = os.listdir(center_path)
lst = list(filter(lambda item: item.endswith('.msk'), lst))
lst.sort(key=lambda x: os.path.getmtime(os.path.join(center_path, x)))
centers_mask = torch.load(os.path.join(center_path, lst[-1]))
logging.info(
'centers mask checkpoint {} is loaded'.format(
os.path.join(center_path, lst[-1])), )
return model, centers, centers_mask
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('-te',
'--test',
action='store_true',
help='run test',
default=False)
parser.add_argument('-tr',
'--train',
action='store_true',
help='run train',
default=False)
parser.add_argument('-ft',
'--finetune',
action='store_true',
help='fine tune the best model',
default=False)
parser.add_argument('-cl',
'--cl',
action='store_true',
help='use CL',
default=False)
parser.add_argument('-pr',
'--print_error',
action='store_true',
help='print error case',
default=False)
parser.add_argument('-mlp',
'--output_mlp',
action='store_true',
help='use an additional mlp layer on the model output',
default=False)
parser.add_argument('-fgm',
'--fgm',
action='store_true',
help='use fgm',
default=False)
parser.add_argument('-bsz',
'--batch_size',
help='Batch_size per gpu',
required=False,
default=CONFIG['batch_size'],
type=int)
parser.add_argument('-seed',
'--seed',
help='seed',
required=False,
default=42,
type=int)
parser.add_argument('-psz',
'--pool_size',
help='Batch_size per gpu',
required=False,
default=CONFIG['pool_size'],
type=int)
parser.add_argument('-ssz',
'--support_set_size',
help='support size per gpu',
required=False,
default=CONFIG['support_set_size'],
type=int)
parser.add_argument('-epochs',
'--epochs',
help='epochs',
required=False,
default=CONFIG['epochs'],
type=int)
parser.add_argument('-cluster_size',
'--avg_cluster_size',
help='avg_cluster_size',
required=False,
default=CONFIG['avg_cluster_size'],
type=int)
parser.add_argument('-lr',
'--lr',
help='learning rate',
required=False,
default=CONFIG['lr'],
type=float)
parser.add_argument('-ptmlr',
'--ptmlr',
help='ptm learning rate',
required=False,
default=CONFIG['ptmlr'],
type=float)
parser.add_argument('-tsk', '--task_name', default='meld', type=str)
parser.add_argument('-fp16',
'--fp_16',
action='store_true',
help='use fp 16',
default=False)
parser.add_argument('-wp',
'--warm_up',
default=CONFIG['warm_up'],
type=int,
required=False)
parser.add_argument('-dpt',
'--dropout',
default=CONFIG['dropout'],
type=float,
required=False)
parser.add_argument('-temp',
'--temperature',
default=CONFIG['temperature'],
type=float,
required=False)
parser.add_argument('-bert_path',
'--bert_path',
default=CONFIG['bert_path'],
type=str,
required=False)
parser.add_argument('-train_obj',
'--train_obj',
default=CONFIG['train_obj'],
type=str,
required=False)
parser.add_argument('-data_path',
'--data_path',
default=CONFIG['data_path'],
type=str,
required=False)
parser.add_argument('-temp_path',
'--temp_path',
default=CONFIG['temp_path'],
type=str,
required=False)
parser.add_argument('-acc_step',
'--accumulation_steps',
default=CONFIG['accumulation_steps'],
type=int,
required=False)
parser.add_argument("--local_rank",
type=int,
default=-1,
help="local_rank for distributed training on gpus")
parser.add_argument("--n_gpu", type=int, default=0, help="gpu per process")
parser.add_argument("--no_cuda",
action="store_true",
help="Whether not to use CUDA when available")
parser.add_argument('--device',
default='cuda:0',
help='Device used for inference')
args = parser.parse_args()
if args.local_rank == -1:
device = torch.device("cuda" if torch.cuda.is_available()
and not args.no_cuda else "cpu")
args.n_gpu = 0 if args.no_cuda else torch.cuda.device_count()
else:
torch.cuda.set_device(args.local_rank)
device = torch.device("cuda", args.local_rank)
os.environ["MASTER_ADDR"] = "127.0.0.1"
os.environ["MASTER_PORT"] = "29500"
torch.distributed.init_process_group(backend="nccl")
args.n_gpu = 1
args.device = device
torch.cuda.set_device(args.local_rank)
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN,
)
logger.warning(
"Process rank: %s, device: %s, n_gpu: %s, distributed training: %s",
args.local_rank,
device,
args.n_gpu,
bool(args.local_rank != -1),
)
set_seed(args.seed)
args_dict = vars(args)
for k, v in args_dict.items():
CONFIG[k] = v
if CONFIG['temp_path'] == '':
if args.local_rank in [-1]:
os.makedirs('/test/diyi/temp', exist_ok=True)
temp_path = tempfile.mkdtemp(dir='/test/diyi/temp')
else:
temp_path = '/test/diyi/temp'
CONFIG['temp_path'] = temp_path
CONFIG['emotion_vocab'] = CONFIG['temp_path'] + '/vocabs/emotion_vocab.pkl'
if args.local_rank in [-1, 0]:
os.makedirs(CONFIG['temp_path'], exist_ok=True)
os.makedirs(os.path.join(CONFIG['temp_path'], 'vocabs'), exist_ok=True)
os.makedirs(os.path.join(CONFIG['temp_path'], 'models'), exist_ok=True)
os.makedirs(os.path.join(CONFIG['temp_path'], 'temp'), exist_ok=True)
os.makedirs(os.path.join(CONFIG['temp_path'], 'centers'),
exist_ok=True)
os.makedirs(os.path.join(CONFIG['temp_path'], 'cluster_results'),
exist_ok=True)
if args.task_name == 'meld':
CONFIG['data_path'] = './MELD'
CONFIG['num_classes'] = 7
train_data_path = os.path.join(CONFIG['data_path'], 'train_sent_emo.csv')
test_data_path = os.path.join(CONFIG['data_path'], 'test_sent_emo.csv')
dev_data_path = os.path.join(CONFIG['data_path'], 'dev_sent_emo.csv')
get_meld_vocabs([train_data_path, dev_data_path, test_data_path])
dev_dialogues = load_meld_turn(dev_data_path)
test_dialogues = load_meld_turn(test_data_path)
train_dialogues = load_meld_turn(train_data_path)
if args.task_name == 'emorynlp':
CONFIG['data_path'] = './emorynlp'
CONFIG['num_classes'] = 7
train_data_path = os.path.join(CONFIG['data_path'],
'emotion-detection-trn.json')
test_data_path = os.path.join(CONFIG['data_path'],
'emotion-detection-tst.json')
dev_data_path = os.path.join(CONFIG['data_path'], 'emotion-detection-dev.json')
get_emorynlp_vocabs([train_data_path, dev_data_path, test_data_path])
dev_dialogues = load_emorynlp_turn(dev_data_path)
test_dialogues = load_emorynlp_turn(test_data_path)
train_dialogues = load_emorynlp_turn(train_data_path)
if args.task_name =='iemocap':
CONFIG['data_path'] = './IEMOCAP'
CONFIG['num_classes'] = 6
train_data_path = os.path.join(CONFIG['data_path'], 'train_data.json')
test_data_path = os.path.join(CONFIG['data_path'], 'test_data.json')
dev_data_path = os.path.join(CONFIG['data_path'], 'dev_data.json')
get_iemocap_vocabs([train_data_path, dev_data_path, test_data_path])
dev_dialogues = load_iemocap_turn(dev_data_path)
test_dialogues = load_iemocap_turn(test_data_path)
train_dialogues = load_iemocap_turn(train_data_path)
if CONFIG['local_rank'] != -1:
torch.distributed.barrier()
model = CLModel(CONFIG)
if CONFIG['local_rank'] != -1:
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
model.to(args.device)
if args.local_rank in [-1, 0]:
print('---config---')
for k, v in CONFIG.items():
print(k, '\t\t\t', v, flush=True)
if args.finetune:
model, centers, centers_mask = load_latest()
if args.train:
train(model, train_dialogues, dev_dialogues, test_dialogues)
if args.test:
if args.task_name == 'emorynlp':
test_dialogues = load_emorynlp_turn(test_data_path)
if args.task_name == 'meld':
test_dialogues = load_meld_turn(test_data_path)
testset = build_dataset(test_dialogues)
best_f1 = test(model, testset, centers, centers_mask)
print(best_f1)