-
Notifications
You must be signed in to change notification settings - Fork 10
/
train.py
490 lines (405 loc) · 21.7 KB
/
train.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
import sys
import os
import autoRun
autoRun.choose_gpu(retry=True, min_gpu_memory=10000, sleep_time=30)
import argparse
import numpy as np
import torch
import torch.optim as optim
# from apex import amp
import apex
import torch.cuda
import ujson as json
from torch.utils.data import DataLoader
from transformers import AutoConfig, AutoModel, AutoTokenizer
from transformers.optimization import AdamW, get_linear_schedule_with_warmup
from model import DocREModel
from utils import set_seed, collate_fn
from prepro import read_docred
from evaluation import * # to_official, official_evaluate, gen_official, to_score
import pickle
import copy
from tqdm import tqdm
from IPython import embed
def train(args, model, train_features, dev_features, test_features, tokenizer=None):
def finetune(features, optimizer, num_epoch, tokenizer=None):
best_score = -1
train_dataloader = DataLoader(features, batch_size=args.train_batch_size, shuffle=True, collate_fn=collate_fn, drop_last=True)
train_iterator = range(int(num_epoch))
total_steps = int(len(train_dataloader) * num_epoch // args.gradient_accumulation_steps)
warmup_steps = int(total_steps * args.warmup_ratio)
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=warmup_steps, num_training_steps=total_steps)
print("Total steps: {}".format(total_steps))
print("Warmup steps: {}".format(warmup_steps))
for epoch in train_iterator:
model.zero_grad()
for step, batch in enumerate( tqdm(train_dataloader) ):
model.train()
inputs = {'input_ids': batch[0].to(args.device),
'attention_mask': batch[1].to(args.device),
'labels': batch[2],
'entity_pos': batch[3],
'hts': batch[4],
}
if args.ablation in ['eider', 'eider_rule']:
inputs['sen_labels'] = batch[5]
inputs['sen_pos'] = batch[6]
outputs = model(**inputs)
loss = outputs[0] / args.gradient_accumulation_steps
with apex.amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
if step % args.gradient_accumulation_steps == 0:
if args.max_grad_norm > 0:
torch.nn.utils.clip_grad_norm_(apex.amp.master_params(optimizer), args.max_grad_norm)
optimizer.step()
scheduler.step()
model.zero_grad()
if (step == 0 and epoch==0) or (step + 1) == len(train_dataloader) - 1:
print('epoch', epoch, "loss:", loss.item())
dev_score, dev_output, dev_pred, thresh = evaluate(args, model, dev_features, tokenizer=tokenizer, tag="dev")
print(dev_output)
if dev_score > best_score:
best_score = dev_score
with open("dev_result_"+ args.ablation + "_" + args.name + args.ensemble_mode + '_' + args.model_name_or_path + ".json", "w") as fh:
json.dump(dev_pred, fh)
if test_features is not None:
pred = report(args, model, test_features)
with open("test_result_"+ args.ablation + "_" + args.name + args.ensemble_mode + '_' + args.model_name_or_path + ".json", "w") as fh:
json.dump(pred, fh)
if args.save_path != "":
torch.save(model.state_dict(), args.save_path)
print('best f1', best_score)
new_layer = ["extractor", "bilinear"]
if args.ablation in ['eider', 'eider_rule']:
new_layer.extend(['sr_bilinear'])
optimizer_grouped_parameters = [
{"params": [p for n, p in model.named_parameters() if not any(nd in n for nd in new_layer)], },
{"params": [p for n, p in model.named_parameters() if any(nd in n for nd in new_layer)], "lr": args.grouped_learning_rate},
]
optimizer = AdamW(optimizer_grouped_parameters, lr=args.learning_rate, eps=args.adam_epsilon)
model, optimizer = apex.amp.initialize(model, optimizer, opt_level="O1", verbosity=0)
set_seed(args)
model.zero_grad()
print('model initialized')
finetune(train_features, optimizer, args.num_train_epochs, tokenizer=tokenizer)
def evaluate(args, model, features, tokenizer=None, tag="dev", features2=None):
sen_preds = []
preds, scores, topks = [], [], []
preds2, scores2, topks2 = [], [], []
thresh = None
if args.load_path != '' and args.ensemble_mode != 'none':
load_model = args.load_path.split('/')[1].split('.')[0]
title2scores_file = os.path.join(args.feature_path, 'title2score_' + args.ablation + '_' + load_model + '.pkl')
title2scores2_file = os.path.join(args.feature_path, 'title2score_' + args.ensemble_ablation + '_' + load_model + '.pkl')
if os.path.exists(title2scores_file) and os.path.exists(title2scores2_file) and args.ensemble_mode == '2':
title2scores = pickle.load(open(title2scores_file, 'rb'))
title2scores2 = pickle.load(open(title2scores2_file, 'rb'))
title2gt = extract_gt(args.feature_path, features)
ans, thresh = ensemble_scores(title2scores, title2scores2, title2gt, thresh=thresh)
best_f1, best_evi_f1, best_f1_ign, _ = official_evaluate(ans, args.data_dir)
output = {
tag + "_F1": best_f1 * 100,
tag + "_F1_ign": best_f1_ign * 100,
}
return best_f1, output, ans, thresh, None
dataloader = DataLoader(features, batch_size=args.test_batch_size, shuffle=False, collate_fn=collate_fn, drop_last=False)
print('Evaluating original', len(dataloader), 'samples...')
for batch in dataloader:
model.eval()
inputs = {'input_ids': batch[0].to(args.device),
'attention_mask': batch[1].to(args.device),
'entity_pos': batch[3],
'hts': batch[4],
}
if args.ablation in ['eider', 'eider_rule'] and args.evi_eval_mode != 'none':
inputs['sen_pos'] = batch[6]
with torch.no_grad():
if args.ablation in ['eider', 'eider_rule'] and args.evi_eval_mode != 'none':
inputs['return_senatt'] = True
if args.ensemble_mode != 'none':
inputs['return_score'] = True
pred, score, topk, sen_pred = model(**inputs)
score = score.cpu().numpy() # (bs, )
topk = topk.cpu().numpy()
scores.append(score)
topks.append(topk)
else:
pred, sen_pred = model(**inputs)
sen_pred = sen_pred.cpu().numpy()
sen_pred[np.isnan(sen_pred)] = 0
sen_preds.append(sen_pred)
else:
if args.ensemble_mode != 'none':
inputs['return_score'] = True
pred, score, topk = model(**inputs)
score = score.cpu().numpy() # (bs, )
topk = topk.cpu().numpy()
scores.append(score)
topks.append(topk)
else:
pred, *_ = model(**inputs)
pred = pred.cpu().numpy()
pred[np.isnan(pred)] = 0
preds.append(pred)
if args.ensemble_mode != 'none':
dataloader2 = DataLoader(features2, batch_size=args.test_batch_size, shuffle=False, collate_fn=collate_fn, drop_last=False)
print('Evaluating additional', len(features2), 'samples...')
for batch in tqdm(dataloader2):
model.eval()
inputs = {'input_ids': batch[0].to(args.device),
'attention_mask': batch[1].to(args.device),
'entity_pos': batch[3],
'hts': batch[4],
}
with torch.no_grad():
inputs['return_score'] = True
pred2, score2, topk2 = model(**inputs)
score2 = score2.cpu().numpy() # (bs, )
topk2 = topk2.cpu().numpy()
scores2.append(score2)
topks2.append(topk2)
pred2 = pred2.cpu().numpy()
pred2[np.isnan(pred2)] = 0
preds2.append(pred2)
preds = np.concatenate(preds, axis=0).astype(np.float32)
if len(sen_preds) > 0:
sen_preds = np.concatenate(sen_preds, axis=0).astype(np.float32)
if args.ensemble_mode != 'none':
print('Ensembling two rounds of predictions...')
preds2 = np.concatenate(preds2, axis=0).astype(np.float32)
scores2 = np.concatenate(scores2, axis=0).astype(np.float32)
topks2 = np.concatenate(topks2, axis=0).astype(int)
title2scores2 = to_score(scores2, topks2, features2)
scores = np.concatenate(scores, axis=0).astype(np.float32)
topks = np.concatenate(topks, axis=0).astype(int)
title2scores = to_score(scores, topks, features)
title2gt = extract_gt(args.feature_path, features)
ans, thresh = ensemble_scores(title2scores, title2scores2, title2gt, thresh=thresh)
if args.load_path != '':
# save to file
pickle.dump(title2scores, open(title2scores_file, 'wb'))
pickle.dump(title2scores2, open(title2scores2_file, 'wb'))
else:
if args.ablation in ['eider', 'eider_rule'] and args.evi_eval_mode != 'none':
ans, evi_by_title = to_official(preds, features, sen_preds)
# save predicted evidece to file
output_evi_pred_file = "dev_evi_result_"+ args.ablation + "_" + args.name + '_' + args.model_name_or_path + ".pkl"
if not os.path.exists(output_evi_pred_file):
with open(output_evi_pred_file, "wb") as fh:
pickle.dump(evi_by_title, fh)
else:
ans = to_official(preds, features)
if len(ans) > 0:
best_f1, best_evi_f1, best_f1_ign, _ = official_evaluate(ans, args.data_dir, mode=tag)
else:
best_f1 = best_f1_ign = -1
output = {
tag + "_F1": best_f1 * 100,
tag + "_F1_ign": best_f1_ign * 100,
}
if args.ablation in ['eider', 'eider_rule'] and args.evi_eval_mode != 'none':
output[tag+'_evi_F1'] = best_evi_f1 * 100
return best_f1, output, ans, thresh
def report(args, model, features, features2=None, thresh=None):
sen_preds = []
preds, scores, topks = [], [], []
preds2, scores2, topks2 = [], [], []
dataloader = DataLoader(features, batch_size=args.test_batch_size, shuffle=False, collate_fn=collate_fn, drop_last=False)
print('Test: Evaluating original', len(dataloader), 'samples...')
for batch in tqdm(dataloader):
model.eval()
inputs = {'input_ids': batch[0].to(args.device),
'attention_mask': batch[1].to(args.device),
'entity_pos': batch[3],
'hts': batch[4],
}
if args.ablation in ['eider', 'eider_rule'] and args.evi_eval_mode != 'none':
inputs['sen_pos'] = batch[6]
with torch.no_grad():
if args.ablation in ['eider', 'eider_rule'] and args.evi_eval_mode != 'none':
inputs['return_senatt'] = True
if args.ensemble_mode != 'none':
inputs['return_score'] = True
pred, score, topk, sen_pred = model(**inputs)
score = score.cpu().numpy() # (bs, )
topk = topk.cpu().numpy()
scores.append(score)
topks.append(topk)
else:
pred, sen_pred = model(**inputs)
sen_pred = sen_pred.cpu().numpy()
sen_pred[np.isnan(sen_pred)] = 0
sen_preds.append(sen_pred)
else:
if args.ensemble_mode != 'none':
inputs['return_score'] = True
pred, score, topk = model(**inputs)
score = score.cpu().numpy() # (bs, )
topk = topk.cpu().numpy()
scores.append(score)
topks.append(topk)
else:
pred, *_ = model(**inputs)
preds = np.concatenate(preds, axis=0).astype(np.float32)
if len(sen_preds) > 0:
sen_preds = np.concatenate(sen_preds, axis=0).astype(np.float32)
if args.ensemble_mode != 'none':
dataloader2 = DataLoader(features2, batch_size=args.test_batch_size, shuffle=False, collate_fn=collate_fn, drop_last=False)
print('Test: Evaluating additional', len(dataloader2), 'samples...')
for batch in tqdm(dataloader2):
model.eval()
inputs = {'input_ids': batch[0].to(args.device),
'attention_mask': batch[1].to(args.device),
'entity_pos': batch[3],
'hts': batch[4],
}
with torch.no_grad():
inputs['return_score'] = True
pred2, score2, topk2 = model(**inputs)
score2 = score2.cpu().numpy() # (bs, )
topk2 = topk2.cpu().numpy()
scores2.append(score2)
topks2.append(topk2)
pred2 = pred2.cpu().numpy()
pred2[np.isnan(pred2)] = 0
preds2.append(pred2)
print('Test: Ensembling two rounds of predictions...')
scores = np.concatenate(scores, axis=0).astype(np.float32)
topks = np.concatenate(topks, axis=0).astype(int)
title2scores = to_score(scores, topks, features)
preds2 = np.concatenate(preds2, axis=0).astype(np.float32)
scores2 = np.concatenate(scores2, axis=0).astype(np.float32)
topks2 = np.concatenate(topks2, axis=0).astype(int)
title2scores2 = to_score(scores2, topks2, features2)
title2gt = extract_gt(args.feature_path, features)
ans, thresh= ensemble_scores(title2scores, title2scores2, thresh=thresh)
else:
if args.ablation in ['eider', 'eider_rule'] and args.evi_eval_mode != 'none':
ans, evi_by_title = to_official(preds, features, sen_preds)
# save predicted evidece to file
output_evi_pred_file = "test_evi_result_"+ args.ablation + "_" + args.model_name_or_path + ".pkl"
if not os.path.exists(output_evi_pred_file):
with open(output_evi_pred_file, "wb") as fh:
pickle.dump(evi_by_title, fh)
else:
ans = to_official(preds, features)
return ans
def main():
parser = argparse.ArgumentParser()
if True:
parser.add_argument("--ablation", default="atlop", type=str, choices=['atlop', 'eider', 'eider_rule'])
parser.add_argument("--name", default="", type=str)
parser.add_argument("--coref_method", default='none', choices=['hoi', 'none'])
parser.add_argument("--ensemble_mode", default='none', choices=['none', '2'])
parser.add_argument("--ensemble_ablation", default="", type=str)
parser.add_argument("--eval_mode", default='dev_only', type=str)
parser.add_argument("--evi_eval_mode", default='none', type=str)
parser.add_argument("--max_sen_num", type=int, default=25)
parser.add_argument("--data_dir", default="dataset/docred", type=str)
parser.add_argument("--transformer_type", default="bert", type=str)
parser.add_argument("--model_name_or_path", default="bert-base-cased", type=str)
parser.add_argument("--train_file", default="train_annotated.json", type=str)
parser.add_argument("--dev_file", default="dev.json", type=str)
parser.add_argument("--test_file", default="test.json", type=str)
parser.add_argument("--rel2htype_file", default="rel2htype.json", type=str)
parser.add_argument("--save_path", default="", type=str)
parser.add_argument("--load_path", default="", type=str)
parser.add_argument("--feature_path", default="", type=str)
parser.add_argument("--evi_pred_file", default='', type=str)
parser.add_argument("--config_name", default="", type=str,
help="Pretrained config name or path if not the same as model_name")
parser.add_argument("--tokenizer_name", default="", type=str,
help="Pretrained tokenizer name or path if not the same as model_name")
parser.add_argument("--max_seq_length", default=1024, type=int,
help="The maximum total input sequence length after tokenization. Sequences longer "
"than this will be truncated, sequences shorter will be padded.")
parser.add_argument("--train_batch_size", default=4, type=int,
help="Batch size for training.")
parser.add_argument("--test_batch_size", default=8, type=int,
help="Batch size for testing.")
parser.add_argument("--gradient_accumulation_steps", default=1, type=int,
help="Number of updates steps to accumulate before performing a backward/update pass.")
parser.add_argument("--num_labels", default=4, type=int,
help="Max number of labels in prediction.")
parser.add_argument("--learning_rate", default=5e-5, type=float,
help="The initial learning rate for transformer layers for Adam.")
parser.add_argument("--grouped_learning_rate", default=1e-4, type=float,
help="The initial learning rate for new layers for Adam.")
parser.add_argument("--adam_epsilon", default=1e-6, type=float,
help="Epsilon for Adam optimizer.")
parser.add_argument("--max_grad_norm", default=1.0, type=float,
help="Max gradient norm.")
parser.add_argument("--warmup_ratio", default=0.06, type=float,
help="Warm up ratio for Adam.")
parser.add_argument("--num_train_epochs", default=30.0, type=float,
help="Total number of training epochs to perform.")
parser.add_argument("--evaluation_steps", default=-1, type=int,
help="Number of training steps between evaluations.")
parser.add_argument("--seed", type=int, default=66,
help="random seed for initialization")
parser.add_argument("--num_class", type=int, default=97,
help="Number of relation types in dataset.")
args = parser.parse_args()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
args.n_gpu = torch.cuda.device_count()
args.device = device
if args.save_path != "":
if not os.path.exists(args.save_path):
os.makedirs(args.save_path)
config = AutoConfig.from_pretrained(
args.config_name if args.config_name else args.model_name_or_path,
num_labels=args.num_class,
)
tokenizer = AutoTokenizer.from_pretrained(
args.tokenizer_name if args.tokenizer_name else args.model_name_or_path,
)
read = read_docred
train_file = os.path.join(args.data_dir, args.train_file)
dev_file = os.path.join(args.data_dir, args.dev_file)
if args.load_path == '':
train_features = read(args, train_file, tokenizer, if_inference=False)
dev_features = read(args, dev_file, tokenizer)
if args.eval_mode == 'dev_only':
test_features = None
else:
test_file = os.path.join(args.data_dir, args.test_file)
test_features = read(args, test_file, tokenizer) #, max_sent_num=3053)
test_features2 = dev_features2 = None
if args.ensemble_mode != 'none':
dev_features2 = read(args, dev_file, tokenizer, ablation=args.ensemble_ablation)
if args.eval_mode != 'dev_only':
test_features2 = read(args, test_file, tokenizer, ablation=args.ensemble_ablation)
model = AutoModel.from_pretrained(
args.model_name_or_path,
from_tf=bool(".ckpt" in args.model_name_or_path),
config=config,
)
config.cls_token_id = tokenizer.cls_token_id
config.sep_token_id = tokenizer.sep_token_id
config.transformer_type = args.transformer_type
if 'eider' in args.load_path:
args.ablation = 'eider'
set_seed(args)
model = DocREModel(config, model, num_labels=args.num_labels, ablation=args.ablation, max_sen_num=args.max_sen_num)
model.to(device)
if args.load_path != '':
name = args.load_path.split('/')[1].split('.')[0].split('EIDER_')[1]
print('Loading from', args.load_path)
model = apex.amp.initialize(model, opt_level="O1", verbosity=0)
model.load_state_dict(torch.load(args.load_path))
if args.ensemble_mode != 'none':
dev_score, dev_output, dev_pred, thresh = evaluate(args, model, dev_features, \
features2=dev_features2)
else:
dev_score, dev_output, dev_pred, thresh = evaluate(args, model, dev_features)
print(dev_output)
with open("dev_result_"+ args.name + '_' + name + '_' + args.model_name_or_path + ".json", "w") as fh:
json.dump(dev_pred, fh)
if test_features is not None:
pred = report(args, model, test_features, features2 = test_features2, thresh=thresh)
with open("test_result_"+ args.name + '_' + name + '_' + args.model_name_or_path + ".json", "w") as fh:
json.dump(pred, fh)
else:
train(args, model, train_features, dev_features, test_features, tokenizer=tokenizer)
if __name__ == "__main__":
main()