-
Notifications
You must be signed in to change notification settings - Fork 0
/
BART_SemEval_Train.py
1296 lines (1121 loc) · 57.8 KB
/
BART_SemEval_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
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import collections
import logging
import math
import os
import pickle
import re
import sys
import warnings
from datetime import datetime
from multiprocessing import Pool
import pandas as pd
import simpletransformers.seq2seq.seq2seq_utils
import torch.cuda
from dataclasses import asdict
from torch import nn
from torchcrf import CRF
from BART_SemEval_Test_ASD import convert_pred_to_TAS_format as ASD
from BART_SemEval_Test_TAD import convert_pred_to_TAS_format as TAD
from BART_SemEval_Test_TSD import convert_pred_to_TAS_format as TSD
try:
import wandb
wandb_available = True
except ImportError:
wandb_available = False
from simpletransformers.seq2seq import Seq2SeqModel, Seq2SeqArgs
from tensorboardX import SummaryWriter
from torch.utils.data import Dataset, RandomSampler, DataLoader
from tqdm.auto import tqdm, trange
from simpletransformers.seq2seq.seq2seq_utils import (
Seq2SeqDataset,
load_hf_dataset,
)
from transformers import AdamW, Adafactor, get_constant_schedule, get_constant_schedule_with_warmup, \
get_linear_schedule_with_warmup, get_cosine_schedule_with_warmup, \
get_cosine_with_hard_restarts_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup
warnings.filterwarnings('ignore')
logging.basicConfig(level=logging.INFO)
transformers_logger = logging.getLogger("transformers")
transformers_logger.setLevel(logging.ERROR)
logger = logging.getLogger(__name__)
def clean_str(string):
"""
Tokenization/string cleaning for all datasets except for SST.
Original taken from https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py
"""
string = re.sub(r"[^A-Za-z0-9(),!?.$*+;/:@&#%\"=\-'`–’é]", " ", string)
# string = " ".join(re.split("[^a-zA-Z]", string.lower())).strip()
string = re.sub(r"\'s", " \' s", string)
string = re.sub(r"\'ve", " \' ve", string)
string = re.sub(r"\'t", " \' t", string)
string = re.sub(r"\'re", " \' re", string)
string = re.sub(r"\'d", " \' d", string)
string = re.sub(r"\'ll", " \' ll", string)
string = re.sub(r",", " , ", string)
string = re.sub(r"!", " ! ", string)
string = re.sub(r"\(", " ( ", string)
string = re.sub(r"\)", " ) ", string)
string = re.sub(r"\?", " ? ", string)
string = re.sub(r"\+", " + ", string)
string = re.sub(r"\$", " $ ", string)
string = re.sub(r"\*", " * ", string)
string = re.sub(r"\.", " . ", string)
string = re.sub(r"-", " - ", string)
string = re.sub(r"\;", " ; ", string)
string = re.sub(r"\/", " / ", string)
string = re.sub(r"\:", " : ", string)
string = re.sub(r"\@", " @ ", string)
string = re.sub(r"\#", " # ", string)
string = re.sub(r"\%", " % ", string)
string = re.sub(r"\"", " \" ", string)
string = re.sub(r"\&", " & ", string)
string = re.sub(r"=", " = ", string)
string = re.sub(r"–", " – ", string)
string = re.sub(r"’", " \’ ", string)
string = re.sub(r"\s{2,}", " ", string)
return string.strip()
def longestCommonPrefix(strs):
"""
:type strs: List[str]
:rtype: str
"""
if len(strs) == 0:
return ""
current = strs[0]
for i in range(1, len(strs)):
temp = ""
if len(current) == 0:
break
for j in range(len(strs[i])):
if j < len(current) and current[j] == strs[i][j]:
temp += current[j]
else:
break
current = temp
return current
def convert_pred_to_TAS_format(truth, preds):
new_preds = []
trimmed_preds = []
num_trimmed_sentences = 0
match_count = 0
for pred, gold in zip(preds, truth):
new_pred = []
trim_flag = True
for p in [pred]:
match = re.match(
r"(([\sA-Za-z0-9(),!?.\$\*\+;/:@&#%\"=\-'`–’é]+ ~ ((food|drinks|service|ambience|location|restaurant)"
r"(\s)(general|prices|quality|style_options|miscellaneous)) ~ (positive|negative|neutral)( ~~ )?)+)",
p)
if match:
match_count += 1
out = match.groups()[0].strip().strip("~~").strip()
new_pred.append(out)
else:
new_pred.append("")
opinion_patterns = re.findall(
r"([\sA-Za-z0-9(),!?.\$\*\+;/:@&#%\"=\-'`–’é]+(\s)?~(\s)?(((f|F)ood|(d|D)rinks|(s|S)ervice|(a|A)mbience|(l|L)ocation|(r|R)estaurant)(\s)?("
r"(g|G)eneral|(p|P)rices|(q|Q)uality|(s|S)tyle_options|(m|M)iscellaneous))(\s)?~(\s)?(positive|negative|neutral))",
p)
if len(opinion_patterns) > 0:
# to reformat the opinion phrase into correct format based on correct opinions
opinion_patterns = " ~~ ".join(
[" ~ ".join([each_part.strip() for each_part in each_op_pat[0].split("~")]) for each_op_pat in
opinion_patterns])
new_opinion_pattern = []
for each_op_pat in opinion_patterns.split(" ~~ "):
op_aspect = each_op_pat.split(" ~ ")[1].lower()
if len(op_aspect.split()) == 1:
# to reformat the aspect category if there is a missing space or inconsistent capitalization
op_aspect = " ".join([each.strip() for each in
re.split(r"(food|drinks|service|ambience|location|restaurant)",
op_aspect)
if each.strip() != ''])
each_op_part = each_op_pat.split(" ~ ")
each_op_part[1] = op_aspect
new_opinion_pattern.append(" ~ ".join(each_op_part))
opinion_patterns = " ~~ ".join(new_opinion_pattern)
else:
opinion_patterns = ""
if p != new_pred[-1] or p != opinion_patterns:
if new_pred[-1] == "":
trimmed_preds.append(f"Truth:\n{gold}\n--------------")
# if opinion_patterns != "":
# new_pred[-1] = opinion_patterns
else:
trimmed_preds.append(
f"Truth:\n\t{gold}\nActual Prediction:\n\t{p}\nchanged pred: \n\t{new_pred[-1]}\nopinion patterns: \n\t{opinion_patterns}")
# if new_pred[-1] != opinion_patterns:
# new_pred[-1] = opinion_patterns
if trim_flag:
trim_flag = False
num_trimmed_sentences += 1
new_preds.append(new_pred[0])
with open(f'results/{task}{run}_{dir_prefix}/trimmed_preds.txt', "w+") as f:
f.write(f"Number of trimmed sentences={num_trimmed_sentences}\n\n")
f.write("\n\n".join(trimmed_preds))
# exit(0)
preds = new_preds
# Saving the predictions if needed
with open(f"predictions/{task}{run}_{dir_prefix}_predictions_{datetime.now()}.txt", "w") as f:
for i, text in enumerate(df["input_text"].tolist()):
f.write(str(text) + "\n\n")
f.write("Truth:\n")
f.write(truth[i] + "\n\n")
f.write("Prediction:\n")
f.write(str(preds[i]) + "\n")
f.write("________________________________________________________________________________\n")
# print(match_count)
# exit(1)
def getsubidx(x, y):
l1, l2 = len(x), len(y)
for i in range(l1):
if x[i:i + l2] == y:
return i
return -1
num_of_comb = 36 if dataset == 'semeval-2016' else 39
# get the gold annotations for the aspect-sentiment, yes_no, ner_tags from the TAS-BERT test file
gold_df = pd.read_csv(f'data/{dataset}/test_TAS.tsv', sep="\t")
gold_aspect_sentiment_list = gold_df["aspect_sentiment"].tolist()[:num_of_comb]
gold_aspect_sentiment_dict = {v: k for k, v in enumerate(gold_aspect_sentiment_list)}
gold_yes_no = gold_df["yes_no"].tolist()
gold_ner = gold_df["ner_tags"].tolist()
# get the input text ids, and input text from the text_gen test set for this task
input_text_ids = df["input_text_ids"].tolist()
input_text = df["input_text"].tolist()
yes_no, yes_no_pred, text, true_ner, predict_ner = [], [], [], [], []
wrong_count = 0
dup_count = 0
longest_prefix_count = 0
for idx, inp_text in enumerate(input_text):
wrong_flag = False
# set the values of true yes_no values, true_ner, true_text of a sentence from the gold annotations
# loaded earlier
sentence_yes_no = gold_yes_no[idx * num_of_comb: (idx + 1) * num_of_comb]
sentence_true_ner = gold_ner[idx * num_of_comb: (idx + 1) * num_of_comb]
sentence_text = (['[CLS] ' + inp_text]) * num_of_comb
# After running the regex, if the prediction string is empty, then, directly assign the default values to
# the prediction yes_no, prediction ner
sent_ner_len = len(inp_text.split())
sentence_yes_no_pred = [0] * num_of_comb
sentence_predict_ner = [" ".join(['O'] * sent_ner_len)] * num_of_comb
if preds[idx] != "":
assert len(sentence_predict_ner) == len(sentence_true_ner)
assert len(sentence_predict_ner[0]) == len(sentence_true_ner[0])
assert len(sentence_yes_no_pred) == len(sentence_yes_no)
true_aspects = [each_op.split(" ~ ")[1] for each_op in truth[idx].split(" ~~ ")]
true_pol = [each_op.split(" ~ ")[2] for each_op in truth[idx].split(" ~~ ")]
pred_aspects = [each_op.split(" ~ ")[1] for each_op in preds[idx].split(" ~~ ")]
pred_pol = [each_op.split(" ~ ")[2] for each_op in preds[idx].split(" ~~ ")]
# pred_aspects = [tgt_asp_pol for op_idx, tgt_asp_pol in enumerate(preds[idx][pred_offset].split(" ")) if
# op_idx % 3 == 1]
# pred_pol = [tgt_asp_pol for op_idx, tgt_asp_pol in enumerate(preds[idx][pred_offset].split(" ")) if
# op_idx % 3 == 2]
# pred_pol = [[each_pol for each_pol in each_op.split(" ~ ")[2]] for each_op in preds[idx][pred_offset].split(" ~~ ")]
assert len(true_pol) == len(true_aspects)
assert len(pred_pol) == len(pred_aspects)
# combine the aspect categories and the polarities to form true and predicted aspect-sentiment
# strings
true_aspect_pol = [true_aspects[i] + " " + true_pol[i] for i in range(len(true_pol))]
pred_aspect_pol = [pred_aspects[i] + " " + pred_pol[i] for i in range(len(pred_pol))]
# find the indexes of the aspect categories based on the dict of num_of_comb values
# This can be used to select the particular TAS tuple out of the num_of_comb possibilities of a sentence
true_aspect_pol_idx = [gold_aspect_sentiment_dict[each] for each in true_aspect_pol if
each in gold_aspect_sentiment_dict]
pred_aspect_pol_idx = [gold_aspect_sentiment_dict[each] for each in pred_aspect_pol if
each in gold_aspect_sentiment_dict]
if len(pred_aspect_pol_idx) > 0:
# similarly, get the indexes of the gold aspect categories of the sentence
# this is used to verify whether the true_indices == gold_indices for aspect-sentiments
gold_yes_no_idx = [i for i, val in
enumerate(gold_yes_no[idx * num_of_comb: (idx + 1) * num_of_comb]) if val == 1]
assert collections.Counter(set(gold_yes_no_idx)) == collections.Counter(
set(true_aspect_pol_idx))
true_target = [each_op.split(" ~ ")[0] for each_op in truth[idx].split(" ~~ ")]
pred_target = [each_op.split(" ~ ")[0] for each_op in preds[idx].split(" ~~ ")]
# pred_target = [tgt_asp_pol for op_idx, tgt_asp_pol in enumerate(preds[idx][pred_offset].split(" "))
# if
# op_idx % 3 == 0]
# if len(pred_target) > 1:
# print(pred_target)
# If any aspect polarity is dropped by any chance, then, we have to exclude that respective
# target also
if len(pred_aspect_pol_idx) != len(pred_target):
pred_target = pred_target[:len(pred_aspect_pol_idx)]
true_target_idx = []
for each_target in true_target:
if each_target != 'NULL':
sub_idx = getsubidx(inp_text.split(), each_target.split())
if inp_text.count(each_target) > 1:
dup_count += 1
# print(f"{dup_count}: Target: {each_target}\nText: {inp_text}\n\n")
if sub_idx != -1:
true_target_idx.append(
[it for it in range(sub_idx, (sub_idx + len(each_target.split())))])
else:
true_target_idx.append([])
else:
true_target_idx.append([])
# exit(1)
pred_target_idx = []
for each_target in pred_target:
if each_target != 'NULL':
# clean the target word before finding it's index
# The intuition is changing the word "Ray' s" ----> "Ray ' s"
tgt = clean_str(each_target)
if each_target != tgt:
# print(f"changing '{each_target}' to '{tgt}'\n")
each_target = tgt
# match the longest prefix from the sentence for each target word and replace the word
# with the one from the sentence if there >80% match compared to the target word
# else don't change
# new_target_str = ""
# for each_target_word in each_target.split():
# if each_target_word not in inp_text.split():
# new_each_target_word = []
# for each_inp_word in inp_text.split():
# if (len(longestCommonPrefix(
# [each_inp_word, each_target_word])) / len(each_target_word)) > 0.8:
# new_each_target_word.append(each_inp_word)
# if len(new_each_target_word) == 0:
# new_target_str += f" {each_target_word}"
# else:
# new_target_str += " ".join(new_each_target_word)
# else:
# new_target_str += f" {each_target_word}"
# new_target_str = new_target_str.strip()
# if new_target_str != each_target:
# longest_prefix_count += 1
# print(f"{longest_prefix_count} Longest Prefix Match Changes - {each_target}: {new_target_str}\n{inp_text}\n")
# each_target = new_target_str
# Find the indices of the target expression in the sentence
sub_idx = getsubidx(inp_text.split(), each_target.split())
if sub_idx != -1:
pred_target_idx.append(
[it for it in range(sub_idx, (sub_idx + len(each_target.split())))])
else:
pred_target_idx.append([])
else:
pred_target_idx.append([])
# verify if number of polarities == number of targets
assert len(pred_aspect_pol_idx) == len(pred_target_idx)
# find the gold target indexes to verify the correctness of true_target_idx
gold_target_idx = [sorted(
[each_ner_tag_idx for each_ner_tag_idx, each_ner_tag in
enumerate(sentence_true_ner[each_idx].split())
if
each_ner_tag != 'O']) for each_idx in gold_yes_no_idx]
assert len([item for sublist in gold_target_idx for item in sublist]) == \
len([item for sublist in true_target_idx for item in sublist])
# assert collections.Counter([item for sublist in gold_target_idx for item in sublist]) == \
# collections.Counter([item for sublist in true_target_idx for item in sublist])
for each_asp_idx, each_tgt_idx in zip(pred_aspect_pol_idx, pred_target_idx):
sentence_yes_no_pred[each_asp_idx] = 1
# if true_aspect_pol_idx == pred_aspect_pol_idx and true_target_idx == pred_target_idx:
# sentence_predict_ner = sentence_true_ner
# if not wrong_flag:
# wrong_flag = True
# # print(input_text[idx])
# if len(pred_target_idx) == 1 and len(pred_target_idx[0]) == 0:
# wrong_count += 1
# else:
tgt_ner_loc = sentence_predict_ner[each_asp_idx].split()
for each_idx in each_tgt_idx:
tgt_ner_loc[each_idx] = 'T'
sentence_predict_ner[each_asp_idx] = " ".join(tgt_ner_loc).strip()
sentence_predict_ner = ['[CLS] ' + each for each in sentence_predict_ner]
sentence_true_ner = ['[CLS] ' + each for each in sentence_true_ner]
assert len(sentence_yes_no) == len(sentence_yes_no_pred)
assert len(sentence_yes_no_pred) == len(sentence_text)
assert len(sentence_text) == len(sentence_true_ner)
assert len(sentence_predict_ner) == len(sentence_true_ner)
yes_no.extend(sentence_yes_no)
yes_no_pred.extend(sentence_yes_no_pred)
text.extend(sentence_text)
true_ner.extend(sentence_true_ner)
predict_ner.extend(sentence_predict_ner)
# print(wrong_count)
out_df = pd.DataFrame(yes_no, columns=['yes_not'])
out_df['yes_not_pred'] = yes_no_pred
out_df['sentence'] = text
out_df['true_ner'] = true_ner
out_df['predict_ner'] = predict_ner
out_df.to_csv(f"results/{task}{run}_{dir_prefix}/converted_predictions0.txt",
sep="\t", index=False, header=True)
def construct_ner_tags(target_texts, ner_tags):
for tgt_txt in target_texts:
sentence_ner_tags = []
for op_idx, each_op in enumerate(tgt_txt.split(" ~~ ")):
for part_idx, each_part in enumerate(each_op.split(" ~ ")):
if task == "TASD":
for tok_idx, each_token in enumerate(each_part.split()):
if part_idx == 0:
if tok_idx == 0:
sentence_ner_tags.append("B-tgt")
else:
sentence_ner_tags.append("I-tgt")
elif part_idx == 1:
if tok_idx == 0:
sentence_ner_tags.append("B-asp")
else:
sentence_ner_tags.append("I-asp")
else:
sentence_ner_tags.append("B-pol")
if part_idx != 2:
sentence_ner_tags.append("B-sep")
elif task == "ASD":
for tok_idx, each_token in enumerate(each_part.split()):
if part_idx == 0:
if tok_idx == 0:
sentence_ner_tags.append("B-asp")
else:
sentence_ner_tags.append("I-asp")
else:
sentence_ner_tags.append("B-pol")
if part_idx != 1:
sentence_ner_tags.append("B-sep")
elif task == "AD":
for tok_idx, each_token in enumerate(each_part.split()):
if tok_idx == 0:
sentence_ner_tags.append("B-asp")
else:
sentence_ner_tags.append("I-asp")
elif task == "TD":
for tok_idx, each_token in enumerate(each_part.split()):
if tok_idx == 0:
sentence_ner_tags.append("B-tgt")
else:
sentence_ner_tags.append("I-tgt")
elif task == "TSD":
for tok_idx, each_token in enumerate(each_part.split()):
if part_idx == 0:
if tok_idx == 0:
sentence_ner_tags.append("B-tgt")
else:
sentence_ner_tags.append("I-tgt")
else:
sentence_ner_tags.append("B-pol")
if part_idx != 1:
sentence_ner_tags.append("B-sep")
elif task == "TAD":
for tok_idx, each_token in enumerate(each_part.split()):
if part_idx == 0:
if tok_idx == 0:
sentence_ner_tags.append("B-tgt")
else:
sentence_ner_tags.append("I-tgt")
elif part_idx == 1:
if tok_idx == 0:
sentence_ner_tags.append("B-asp")
else:
sentence_ner_tags.append("I-asp")
if part_idx != 1:
sentence_ner_tags.append("B-sep")
if op_idx < (len(tgt_txt.split(" ~~ ")) - 1):
sentence_ner_tags.append("B-opsep")
ner_tags.append(" ".join(sentence_ner_tags))
return ner_tags
def reconstruct_ner_tags_for_encoded_targets(target_text, ner_tags, tokenizer, args):
# text = "staff ~ service general ~ positive ~~ restaurant ~ restaurant general ~ negative"
# ner_tags = "B-tgt I-tgt I-tgt B-sep B-asp I-asp B-sep B-pol B-opsep B-tgt I-tgt I-tgt I-tgt I-tgt B-sep B-asp I-asp B-sep B-pol"
# encoded_text = tokenizer.encode(text)[1:-1]
text = target_text
encoded_text_org = \
tokenizer.batch_encode_plus([text], max_length=args.max_seq_length, padding="max_length", return_tensors="pt",
truncation=True)["input_ids"].squeeze().tolist()
encoded_text = encoded_text_org[1:] # to get rid of the start token
enc_i = 0
i = 0
# new_ner_tags = ["[START]"]
new_ner_tags = []
while i < len(text.split()):
if tokenizer.decode([encoded_text[enc_i]]).lstrip(' ') == text.split()[i]:
# print(text.split()[i])
new_ner_tags.append(ner_tags.split()[i])
enc_i += 1
else:
enc_str = ""
pos = 0
while enc_str != text.split()[i]:
enc_str = tokenizer.decode(encoded_text[enc_i: (enc_i + pos + 1)]).lstrip(' ')
if pos == 0:
new_ner_tags.append(ner_tags.split()[i])
else:
if "B-" in ner_tags.split()[i]:
tg = "I-" + ner_tags.split()[i].split("B-")[-1]
else:
tg = ner_tags.split()[i]
new_ner_tags.append(tg)
pos += 1
enc_i += pos
# print(text.split()[i])
i += 1
# print(enc_i - i)
new_ner_tags.append("[END]")
new_ner_tags.extend(["[PAD]"] * (args.max_seq_length - (enc_i + 2)))
text_to_tag = []
for e_text, tag in zip(encoded_text_org, new_ner_tags):
text_to_tag.append(f"{tokenizer.decode([e_text]).lstrip(' ')} : {tag2idx[tag]}")
# print("\n".join(text_to_tag))
# print(len(text_to_tag))
return torch.LongTensor([tag2idx[each] for each in new_ner_tags])
def preprocess_data_bart(data):
input_text, target_text, ner_tags, tokenizer, args = data
input_ids = tokenizer.batch_encode_plus(
[input_text], max_length=args.max_seq_length, padding="max_length", return_tensors="pt", truncation=True
)
target_ids = tokenizer.batch_encode_plus(
[target_text], max_length=args.max_seq_length, padding="max_length", return_tensors="pt", truncation=True
)
target_ner_tags = reconstruct_ner_tags_for_encoded_targets(target_text, ner_tags, tokenizer, args)
target_ner_mask = (target_ner_tags != 1).float()
return {
"source_ids": input_ids["input_ids"].squeeze(),
"source_mask": input_ids["attention_mask"].squeeze(),
"target_ids": target_ids["input_ids"].squeeze(),
"target_ner_tags": target_ner_tags,
"target_ner_mask": target_ner_mask,
}
class SimpleSummarizationDataset(Dataset):
def __init__(self, tokenizer, args, data, mode):
self.tokenizer = tokenizer
cached_features_file = os.path.join(
args.cache_dir, args.model_name + "_cached_" + str(args.max_seq_length) + str(len(data))
)
if os.path.exists(cached_features_file) and (
(not args.reprocess_input_data and not args.no_cache)
or (mode == "dev" and args.use_cached_eval_features and not args.no_cache)
):
logger.info(" Loading features from cached file %s", cached_features_file)
with open(cached_features_file, "rb") as handle:
self.examples = pickle.load(handle)
else:
logger.info(" Creating features from dataset file at %s", args.cache_dir)
data = [
(input_text, target_text, ner_tags, tokenizer, args)
for input_text, target_text, ner_tags in zip(data["input_text"], data["target_text"], data["ner_tags"])
]
preprocess_fn = preprocess_data_bart
if (mode == "train" and args.use_multiprocessing) or (
mode == "dev" and args.use_multiprocessing_for_evaluation
):
if args.multiprocessing_chunksize == -1:
chunksize = max(len(data) // (args.process_count * 2), 500)
else:
chunksize = args.multiprocessing_chunksize
with Pool(args.process_count) as p:
self.examples = list(
tqdm(p.imap(preprocess_fn, data, chunksize=chunksize), total=len(data), disable=args.silent, )
)
else:
self.examples = [preprocess_fn(d) for d in tqdm(data, disable=args.silent)]
def __len__(self):
return len(self.examples)
def __getitem__(self, index):
return self.examples[index]
class MultiTaskLossWrapper(nn.Module):
def __init__(self, task_num):
super(MultiTaskLossWrapper, self).__init__()
self.task_num = task_num
self.log_vars = nn.Parameter(torch.zeros((task_num)))
def forward(self, losses):
loss0 = losses[0]
loss1 = losses[1]
precision0 = torch.exp(-self.log_vars[0])
loss0 = precision0 * loss0 + self.log_vars[0]
precision1 = torch.exp(-self.log_vars[1])
loss1 = precision1 * loss1 + self.log_vars[1]
return loss0 + loss1
def load_and_cache_examples(self, data, evaluate=False, no_cache=False, verbose=True, silent=False):
"""
Creates a T5Dataset from data.
Utility function for train() and eval() methods. Not intended to be used directly.
"""
encoder_tokenizer = self.encoder_tokenizer
decoder_tokenizer = self.decoder_tokenizer
args = self.args
if not no_cache:
no_cache = args.no_cache
if not no_cache:
os.makedirs(self.args.cache_dir, exist_ok=True)
mode = "dev" if evaluate else "train"
if self.args.use_hf_datasets:
dataset = load_hf_dataset(data, encoder_tokenizer, decoder_tokenizer, self.args)
return dataset
else:
if args.dataset_class:
CustomDataset = args.dataset_class
return CustomDataset(encoder_tokenizer, decoder_tokenizer, args, data, mode)
else:
if args.model_type in ["bart", "mbart", "marian"]:
return SimpleSummarizationDataset(encoder_tokenizer, self.args, data, mode)
else:
return Seq2SeqDataset(encoder_tokenizer, decoder_tokenizer, self.args, data, mode, )
def train_loop(
self, train_dataset, output_dir, show_running_loss=True, eval_data=None, verbose=True, **kwargs,
):
"""
Trains the model on train_dataset.
Utility function to be used by the train_model() method. Not intended to be used directly.
"""
if use_crf: # global value
self.ner_hidden2tag = nn.Linear(len(self.encoder_tokenizer.vocab),
len(idx2tag)).to(self.device) # num_ner_labels is the type sum of ner labels: TO or BIO etc
self.num_ner_labels = len(idx2tag)
# CRF
self.CRF_model = CRF(len(idx2tag), batch_first=True).to(self.device)
self.loss_wrapper = MultiTaskLossWrapper(task_num=2)
model = self.model
args = self.args
tb_writer = SummaryWriter(logdir=args.tensorboard_dir)
train_sampler = RandomSampler(train_dataset)
train_dataloader = DataLoader(
train_dataset,
sampler=train_sampler,
batch_size=args.train_batch_size,
num_workers=self.args.dataloader_num_workers,
)
if args.max_steps > 0:
t_total = args.max_steps
args.num_train_epochs = args.max_steps // (len(train_dataloader) // args.gradient_accumulation_steps) + 1
else:
t_total = len(train_dataloader) // args.gradient_accumulation_steps * args.num_train_epochs
no_decay = ["bias", "LayerNorm.weight"]
optimizer_grouped_parameters = []
custom_parameter_names = set()
for group in self.args.custom_parameter_groups:
params = group.pop("params")
custom_parameter_names.update(params)
param_group = {**group}
param_group["params"] = [p for n, p in model.named_parameters() if n in params]
optimizer_grouped_parameters.append(param_group)
for group in self.args.custom_layer_parameters:
layer_number = group.pop("layer")
layer = f"layer.{layer_number}."
group_d = {**group}
group_nd = {**group}
group_nd["weight_decay"] = 0.0
params_d = []
params_nd = []
for n, p in model.named_parameters():
if n not in custom_parameter_names and layer in n:
if any(nd in n for nd in no_decay):
params_nd.append(p)
else:
params_d.append(p)
custom_parameter_names.add(n)
group_d["params"] = params_d
group_nd["params"] = params_nd
optimizer_grouped_parameters.append(group_d)
optimizer_grouped_parameters.append(group_nd)
if not self.args.train_custom_parameters_only:
optimizer_grouped_parameters.extend(
[
{
"params": [
p
for n, p in model.named_parameters()
if n not in custom_parameter_names and not any(nd in n for nd in no_decay)
],
"weight_decay": args.weight_decay,
},
{
"params": [
p
for n, p in model.named_parameters()
if n not in custom_parameter_names and any(nd in n for nd in no_decay)
],
"weight_decay": 0.0,
},
]
)
warmup_steps = math.ceil(t_total * args.warmup_ratio)
args.warmup_steps = warmup_steps if args.warmup_steps == 0 else args.warmup_steps
# TODO: Use custom optimizer like with BertSum?
if args.optimizer == "AdamW":
optimizer = AdamW(optimizer_grouped_parameters, lr=args.learning_rate, eps=args.adam_epsilon)
elif args.optimizer == "Adafactor":
optimizer = Adafactor(
optimizer_grouped_parameters,
lr=args.learning_rate,
eps=args.adafactor_eps,
clip_threshold=args.adafactor_clip_threshold,
decay_rate=args.adafactor_decay_rate,
beta1=args.adafactor_beta1,
weight_decay=args.weight_decay,
scale_parameter=args.adafactor_scale_parameter,
relative_step=args.adafactor_relative_step,
warmup_init=args.adafactor_warmup_init,
)
print("Using Adafactor for T5")
else:
raise ValueError(
"{} is not a valid optimizer class. Please use one of ('AdamW', 'Adafactor') instead.".format(
args.optimizer
)
)
if args.scheduler == "constant_schedule":
scheduler = get_constant_schedule(optimizer)
elif args.scheduler == "constant_schedule_with_warmup":
scheduler = get_constant_schedule_with_warmup(optimizer, num_warmup_steps=args.warmup_steps)
elif args.scheduler == "linear_schedule_with_warmup":
scheduler = get_linear_schedule_with_warmup(
optimizer, num_warmup_steps=args.warmup_steps, num_training_steps=t_total
)
elif args.scheduler == "cosine_schedule_with_warmup":
scheduler = get_cosine_schedule_with_warmup(
optimizer,
num_warmup_steps=args.warmup_steps,
num_training_steps=t_total,
num_cycles=args.cosine_schedule_num_cycles,
)
elif args.scheduler == "cosine_with_hard_restarts_schedule_with_warmup":
scheduler = get_cosine_with_hard_restarts_schedule_with_warmup(
optimizer,
num_warmup_steps=args.warmup_steps,
num_training_steps=t_total,
num_cycles=args.cosine_schedule_num_cycles,
)
elif args.scheduler == "polynomial_decay_schedule_with_warmup":
scheduler = get_polynomial_decay_schedule_with_warmup(
optimizer,
num_warmup_steps=args.warmup_steps,
num_training_steps=t_total,
lr_end=args.polynomial_decay_schedule_lr_end,
power=args.polynomial_decay_schedule_power,
)
else:
raise ValueError("{} is not a valid scheduler.".format(args.scheduler))
if (
args.model_name
and os.path.isfile(os.path.join(args.model_name, "optimizer.pt"))
and os.path.isfile(os.path.join(args.model_name, "scheduler.pt"))
):
# Load in optimizer and scheduler states
optimizer.load_state_dict(torch.load(os.path.join(args.model_name, "optimizer.pt")))
scheduler.load_state_dict(torch.load(os.path.join(args.model_name, "scheduler.pt")))
if args.n_gpu > 1:
model = torch.nn.DataParallel(model)
logger.info(" Training started")
global_step = 0
training_progress_scores = None
tr_loss, logging_loss = 0.0, 0.0
model.zero_grad()
train_iterator = trange(int(args.num_train_epochs), desc="Epoch", disable=args.silent, mininterval=0)
epoch_number = 0
best_eval_metric = None
early_stopping_counter = 0
steps_trained_in_current_epoch = 0
epochs_trained = 0
if args.model_name and os.path.exists(args.model_name):
try:
# set global_step to gobal_step of last saved checkpoint from model path
checkpoint_suffix = args.model_name.split("/")[-1].split("-")
if len(checkpoint_suffix) > 2:
checkpoint_suffix = checkpoint_suffix[1]
else:
checkpoint_suffix = checkpoint_suffix[-1]
global_step = int(checkpoint_suffix)
epochs_trained = global_step // (len(train_dataloader) // args.gradient_accumulation_steps)
steps_trained_in_current_epoch = global_step % (
len(train_dataloader) // args.gradient_accumulation_steps
)
logger.info(" Continuing training from checkpoint, will skip to saved global_step")
logger.info(" Continuing training from epoch %d", epochs_trained)
logger.info(" Continuing training from global step %d", global_step)
logger.info(" Will skip the first %d steps in the current epoch", steps_trained_in_current_epoch)
except ValueError:
logger.info(" Starting fine-tuning.")
if args.evaluate_during_training:
training_progress_scores = self._create_training_progress_scores(**kwargs)
if args.wandb_project:
wandb.init(project=args.wandb_project, config={**asdict(args)}, **args.wandb_kwargs)
wandb.watch(self.model)
if args.fp16:
from torch.cuda import amp
scaler = amp.GradScaler()
for current_epoch in train_iterator:
model.train()
if epochs_trained > 0:
epochs_trained -= 1
continue
train_iterator.set_description(f"Epoch {epoch_number + 1} of {args.num_train_epochs}")
batch_iterator = tqdm(
train_dataloader,
desc=f"Running Epoch {epoch_number} of {args.num_train_epochs}",
disable=args.silent,
mininterval=0,
)
for step, batch in enumerate(batch_iterator):
# if step == 0:
# print("\n****** inside batch loop *****\n")
if steps_trained_in_current_epoch > 0:
steps_trained_in_current_epoch -= 1
continue
# batch = tuple(t.to(device) for t in batch)
inputs = self._get_inputs_dict(batch)
if args.fp16:
# if step == 0:
# print("\n****** inside FP16 *****\n")
with amp.autocast():
outputs = model(**inputs)
# model outputs are always tuple in pytorch-transformers (see doc)
loss = outputs[0]
else:
# if step == 0:
# print("\n****** inside normal execution *******\n")
outputs = model(**inputs)
# model outputs are always tuple in pytorch-transformers (see doc)
loss = outputs[0]
ner_tags, ner_mask = batch["target_ner_tags"], batch["target_ner_mask"]
ner_logits = self.ner_hidden2tag(outputs[1].to(self.device)) # (batch, seq, vocab_size) --> (batch, seq, num_ner_labels)
ner_loss_list = self.CRF_model(ner_logits.to(self.device), ner_tags.to(self.device),
ner_mask.type(torch.ByteTensor).to(self.device),
reduction='none')
ner_loss = torch.mean(-ner_loss_list)
# ner_predict = self.CRF_model.decode(ner_logits, ner_mask.type(torch.ByteTensor))
if args.n_gpu > 1:
loss = loss.mean() # mean() to average on multi-gpu parallel training
ner_loss = ner_loss.mean()
current_loss = loss.item() + ner_loss.item()
if show_running_loss:
batch_iterator.set_description(
f"Epochs {epoch_number}/{args.num_train_epochs}. Running Loss: {current_loss:9.4f}"
)
if args.gradient_accumulation_steps > 1:
loss = loss / args.gradient_accumulation_steps
if args.fp16:
combined_loss = self.loss_wrapper([loss, ner_loss])
scaler.scale(combined_loss).backward()
# scaler.scale(loss).backward(retain_graph=True)
# loss.backward(retain_graph=True)
# scaler.scale(ner_loss).backward()
else:
combined_loss = self.loss_wrapper([loss, ner_loss])
combined_loss.backward()
# loss.backward()
# loss.backward(retain_graph=True)
# ner_loss.backward()
# tr_loss += loss.item()
tr_loss += current_loss
if (step + 1) % args.gradient_accumulation_steps == 0:
if args.fp16:
scaler.unscale_(optimizer)
if args.optimizer == "AdamW":
torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
if args.fp16:
scaler.step(optimizer)
scaler.update()
else:
optimizer.step()
scheduler.step() # Update learning rate schedule
model.zero_grad()
global_step += 1
if args.logging_steps > 0 and global_step % args.logging_steps == 0:
# Log metrics
tb_writer.add_scalar("lr", scheduler.get_last_lr()[0], global_step)
tb_writer.add_scalar("loss", (tr_loss - logging_loss) / args.logging_steps, global_step)
logging_loss = tr_loss
if args.wandb_project or self.is_sweeping:
wandb.log(
{
"Training loss": current_loss,
"lr": scheduler.get_last_lr()[0],
"global_step": global_step,
}
)
if args.save_steps > 0 and global_step % args.save_steps == 0:
# Save model checkpoint
output_dir_current = os.path.join(output_dir, "checkpoint-{}".format(global_step))
self.save_model(output_dir_current, optimizer, scheduler, model=model)
if args.evaluate_during_training and (
args.evaluate_during_training_steps > 0
and global_step % args.evaluate_during_training_steps == 0
):
# Only evaluate when single GPU otherwise metrics may not average well
results = self.eval_model(
eval_data,
verbose=verbose and args.evaluate_during_training_verbose,
silent=args.evaluate_during_training_silent,
**kwargs,
)
for key, value in results.items():
tb_writer.add_scalar("eval_{}".format(key), value, global_step)
output_dir_current = os.path.join(output_dir, "checkpoint-{}".format(global_step))
if args.save_eval_checkpoints:
self.save_model(output_dir_current, optimizer, scheduler, model=model, results=results)
training_progress_scores["global_step"].append(global_step)
training_progress_scores["train_loss"].append(current_loss)
for key in results:
training_progress_scores[key].append(results[key])
report = pd.DataFrame(training_progress_scores)
report.to_csv(
os.path.join(args.output_dir, "training_progress_scores.csv"), index=False,
)
if args.wandb_project or self.is_sweeping:
wandb.log(self._get_last_metrics(training_progress_scores))
if not best_eval_metric:
best_eval_metric = results[args.early_stopping_metric]
if args.save_best_model:
self.save_model(
args.best_model_dir, optimizer, scheduler, model=model, results=results
)
if best_eval_metric and args.early_stopping_metric_minimize:
if results[args.early_stopping_metric] - best_eval_metric < args.early_stopping_delta:
best_eval_metric = results[args.early_stopping_metric]