-
Notifications
You must be signed in to change notification settings - Fork 35
/
run_ner_BIO.py
1265 lines (997 loc) · 54.3 KB
/
run_ner_BIO.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
# coding=utf-8
# Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team.
# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Finetuning the library models for sequence classification on GLUE (Bert, XLM, XLNet, RoBERTa)."""
from __future__ import absolute_import, division, print_function
import argparse
import glob
import logging
import os
import random
from collections import defaultdict
import re
import shutil
import numpy as np
import torch
from torch.utils.data import (DataLoader, RandomSampler, SequentialSampler,
TensorDataset)
from torch.utils.data.distributed import DistributedSampler
from tensorboardX import SummaryWriter
from tqdm import tqdm, trange
from transformers import (WEIGHTS_NAME, BertConfig,
BertTokenizer,
RobertaConfig,
RobertaTokenizer,
BertForTokenClassification,
get_linear_schedule_with_warmup,
AdamW,
RobertaForTokenClassification,
AlbertConfig,
AlbertTokenizer
)
from transformers import AutoTokenizer
from torch.utils.data import TensorDataset, Dataset
import json
import pickle
import numpy as np
import unicodedata
import itertools
import seqeval.metrics
import math
import timeit
logger = logging.getLogger(__name__)
ALL_MODELS = sum((tuple(conf.pretrained_config_archive_map.keys()) for conf in (BertConfig, RobertaConfig)), ())
MODEL_CLASSES = {
'bert': (BertConfig, BertForTokenClassification, BertTokenizer),
'roberta': (RobertaConfig, RobertaForTokenClassification, RobertaTokenizer),
}
class CoNLL03Dataset(Dataset):
def __init__(self, tokenizer, args=None, evaluate=False, do_test=False):
if not evaluate:
file_path = os.path.join(args.data_dir, args.train_file)
else:
if do_test:
file_path = os.path.join(args.data_dir, args.test_file)
else:
file_path = os.path.join(args.data_dir, args.dev_file)
assert os.path.isfile(file_path)
self.tokenizer = tokenizer
self.max_seq_length = args.max_seq_length
self.max_mention_length = args.max_mention_length
self.max_pair_length = args.max_pair_length
self.evaluate = evaluate
self.doc_stride = args.doc_stride
self.args = args
self.model_type = args.model_type
self.ori_label_list = ["MISC", "PER", "ORG", "LOC"]
self.label_list = ['O']
for label in self.ori_label_list:
self.label_list.append('B-' + label)
self.label_list.append('I-' + label)
self.label_map = {label: i for i, label in enumerate(self.label_list)}
self.max_entity_length = args.max_pair_length * 2
self.data_info = self._read_data(file_path)
self.initialize()
def _read_data(self, input_file):
data = []
words = []
labels = []
sentence_boundaries = []
with open(input_file) as f:
for line in tqdm(f):
line = line.rstrip()
if line.startswith("-DOCSTART"):
if words:
data.append((words, labels, sentence_boundaries))
if self.args.output_dir.find('test')!=-1:
if len(data) == 5:
return data
assert sentence_boundaries[0] == 0
assert sentence_boundaries[-1] == len(words)
words = []
labels = []
sentence_boundaries = []
continue
if not line:
if not sentence_boundaries or len(words) != sentence_boundaries[-1]:
sentence_boundaries.append(len(words))
else:
parts = line.split(" ")
words.append(parts[0])
labels.append(parts[-1])
if words:
data.append((words, labels, sentence_boundaries))
return data
def is_punctuation(self, char):
# obtained from:
# https://github.com/huggingface/transformers/blob/5f25a5f367497278bf19c9994569db43f96d5278/transformers/tokenization_bert.py#L489
cp = ord(char)
if (cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or (cp >= 91 and cp <= 96) or (cp >= 123 and cp <= 126):
return True
cat = unicodedata.category(char)
if cat.startswith("P"):
return True
return False
def initialize(self):
tokenizer = self.tokenizer
self.data = []
max_num_subwords = self.max_seq_length - 2
def tokenize_word(text):
if (
isinstance(tokenizer, RobertaTokenizer)
and (text[0] != "'")
and (len(text) != 1 or not self.is_punctuation(text))
):
return tokenizer.tokenize(text, add_prefix_space=True)
return tokenizer.tokenize(text)
maxR = 0
for example_index, (words, labels, sentence_boundaries) in enumerate(self.data_info):
tokens = [tokenize_word(w) for w in words]
subwords = [w for li in tokens for w in li]
subword2token = list(itertools.chain(*[[i] * len(li) for i, li in enumerate(tokens)]))
token2subword = [0] + list(itertools.accumulate(len(li) for li in tokens))
subword_start_positions = frozenset(token2subword)
subword_sentence_boundaries = [sum(len(li) for li in tokens[:p]) for p in sentence_boundaries]
assert(len(labels) == len(words))
for n in range(len(subword_sentence_boundaries) - 1):
doc_sent_start, doc_sent_end = subword_sentence_boundaries[n : n + 2]
left_length = doc_sent_start
right_length = len(subwords) - doc_sent_end
sentence_length = doc_sent_end - doc_sent_start
half_context_length = int((max_num_subwords - sentence_length) / 2)
if left_length < right_length:
left_context_length = min(left_length, half_context_length)
right_context_length = min(right_length, max_num_subwords - left_context_length - sentence_length)
else:
right_context_length = min(right_length, half_context_length)
left_context_length = min(left_length, max_num_subwords - right_context_length - sentence_length)
doc_offset = doc_sent_start - left_context_length
target_tokens = subwords[doc_offset : doc_sent_end + right_context_length]
target_tokens = [tokenizer.cls_token] + target_tokens + [tokenizer.sep_token]
word_sent_start, word_sent_end = sentence_boundaries[n: n+2]
sentence_labels = labels[word_sent_start : word_sent_end]
start_positions = []
label_ids = []
for entity_start in range(left_context_length, left_context_length + sentence_length):
doc_entity_start = entity_start + doc_offset
if doc_entity_start in subword_start_positions:
token_idx = subword2token[doc_entity_start]
label_ids.append(self.label_map[labels[token_idx]])
start_positions.append(entity_start+1)
else:
label_ids.append(-1)
label_ids = [-1] * (left_context_length+1) + label_ids + [-1] * (right_context_length+1)
assert(len(sentence_labels) == len(start_positions))
assert(len(label_ids) == len(target_tokens))
item = {
'sentence': target_tokens,
'sentence_labels': sentence_labels,
'label_ids': label_ids,
'start_positions': start_positions,
# 'example_index': (example_index, n)
}
self.data.append(item)
logger.info('maxR: %s', maxR)
def __len__(self):
return len(self.data)
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
entry = self.data[idx]
# left_ctx = entry['left_ctx']
sentence_labels = entry['sentence_labels']
input_ids = self.tokenizer.convert_tokens_to_ids(entry['sentence'])
L = len(input_ids)
input_ids += [0] * (self.max_seq_length - len(input_ids))
attention_mask = [1] * L + [0] * (self.max_seq_length - L)
attention_mask = torch.tensor(attention_mask, dtype=torch.int64)
labels = entry['label_ids'] + [-1] * (self.max_seq_length - L)
item = [torch.tensor(input_ids),
attention_mask,
torch.tensor(labels, dtype=torch.int64),
]
if self.evaluate:
item.append(sentence_labels)
item.append(entry['start_positions'])
# item.append(left_ctx)
return item
@staticmethod
def collate_fn(batch):
fields = [x for x in zip(*batch)]
num_metadata_fields = 2
stacked_fields = [torch.stack(field) for field in fields[:-num_metadata_fields]] # don't stack metadata fields
stacked_fields.extend(fields[-num_metadata_fields:]) # add them as lists not torch tensors
return stacked_fields
class OntonotesDataset(Dataset):
def __init__(self, tokenizer, args=None, evaluate=False, do_test=False):
if not evaluate:
file_path = os.path.join(args.data_dir, args.train_file)
else:
if do_test:
file_path = os.path.join(args.data_dir, args.test_file)
else:
file_path = os.path.join(args.data_dir, args.dev_file)
assert os.path.isfile(file_path)
self.file_path = file_path
self.tokenizer = tokenizer
self.max_seq_length = args.max_seq_length
self.max_mention_length = args.max_mention_length
self.evaluate = evaluate
self.local_rank = args.local_rank
self.args = args
self.model_type = args.model_type
self.ori_label_list = ['CARDINAL', 'DATE', 'EVENT', 'FAC', 'GPE', 'LANGUAGE', 'LAW', 'LOC', 'MONEY', 'NORP', 'ORDINAL', 'ORG', 'PERCENT', 'PERSON', 'PRODUCT', 'QUANTITY', 'TIME', 'WORK_OF_ART']
self.label_list = ['O']
for label in self.ori_label_list:
self.label_list.append('B-' + label)
self.label_list.append('I-' + label)
self.label_map = {label: i for i, label in enumerate(self.label_list)}
self.max_pair_length = args.max_pair_length
self.max_entity_length = args.max_pair_length * 2
self.initialize()
def is_punctuation(self, char):
# obtained from:
# https://github.com/huggingface/transformers/blob/5f25a5f367497278bf19c9994569db43f96d5278/transformers/tokenization_bert.py#L489
cp = ord(char)
if (cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or (cp >= 91 and cp <= 96) or (cp >= 123 and cp <= 126):
return True
cat = unicodedata.category(char)
if cat.startswith("P"):
return True
return False
def get_original_token(self, token):
escape_to_original = {
"-LRB-": "(",
"-RRB-": ")",
"-LSB-": "[",
"-RSB-": "]",
"-LCB-": "{",
"-RCB-": "}",
}
if token in escape_to_original:
token = escape_to_original[token]
return token
def initialize(self):
tokenizer = self.tokenizer
max_num_subwords = self.max_seq_length - 2
def tokenize_word(text):
if (
isinstance(tokenizer, RobertaTokenizer)
and (text[0] != "'")
and (len(text) != 1 or not self.is_punctuation(text))
):
return tokenizer.tokenize(text, add_prefix_space=True)
return tokenizer.tokenize(text)
f = open(self.file_path, "r", encoding='utf-8')
self.data = []
maxL = 0
for l_idx, line in enumerate(f):
data = json.loads(line)
if self.args.output_dir.find('test')!=-1:
if len(self.data) > 5:
break
sentences = data['sentences']
for i in range(len(sentences)):
for j in range(len(sentences[i])):
sentences[i][j] = self.get_original_token(sentences[i][j])
ners = data['ner']
sentence_boundaries = [0]
words = []
L = 0
for i in range(len(sentences)):
L += len(sentences[i])
sentence_boundaries.append(L)
words += sentences[i]
tokens = [tokenize_word(w) for w in words]
subwords = [w for li in tokens for w in li]
maxL = max(len(tokens), maxL)
subword2token = list(itertools.chain(*[[i] * len(li) for i, li in enumerate(tokens)]))
token2subword = [0] + list(itertools.accumulate(len(li) for li in tokens))
subword_start_positions = frozenset(token2subword)
subword_sentence_boundaries = [sum(len(li) for li in tokens[:p]) for p in sentence_boundaries]
for n in range(len(subword_sentence_boundaries) - 1):
sentence_ners = ners[n]
doc_sent_start, doc_sent_end = subword_sentence_boundaries[n : n + 2]
word_sent_start, word_sent_end = sentence_boundaries[n: n+2]
sentence_labels = ["O"] * (word_sent_end-word_sent_start)
for start, end, label in sentence_ners:
start -= word_sent_start
end -= word_sent_start
sentence_labels[start] = "B-" + label
if end - start > 0:
sentence_labels[start + 1 : end+1] = ["I-" + label] * (end - start)
# convert IOB2 -> IOB1
prev_type = None
for n, label in enumerate(sentence_labels):
if (label[0] == "B") and (label[2:] != prev_type):
sentence_labels[n] = "I" + label[1:]
prev_type = label[2:]
left_length = doc_sent_start
right_length = len(subwords) - doc_sent_end
sentence_length = doc_sent_end - doc_sent_start
half_context_length = int((max_num_subwords - sentence_length) / 2)
if left_length < right_length:
left_context_length = min(left_length, half_context_length)
right_context_length = min(right_length, max_num_subwords - left_context_length - sentence_length)
else:
right_context_length = min(right_length, half_context_length)
left_context_length = min(left_length, max_num_subwords - right_context_length - sentence_length)
if self.args.output_dir.find('ctx0')!=-1:
left_context_length = right_context_length = 0 # for debug
doc_offset = doc_sent_start - left_context_length
target_tokens = subwords[doc_offset : doc_sent_end + right_context_length]
assert(len(target_tokens)<=max_num_subwords)
target_tokens = [tokenizer.cls_token] + target_tokens + [tokenizer.sep_token]
start_positions = []
label_ids = []
for entity_start in range(left_context_length, left_context_length + sentence_length):
doc_entity_start = entity_start + doc_offset
if doc_entity_start in subword_start_positions:
token_idx = subword2token[doc_entity_start]
label_ids.append(self.label_map[sentence_labels[ token_idx - word_sent_start ]])
start_positions.append(entity_start+1)
else:
label_ids.append(-1)
label_ids = [-1] * (left_context_length+1) + label_ids + [-1] * (right_context_length+1)
assert(len(sentence_labels) == len(start_positions))
assert(len(label_ids) == len(target_tokens))
item = {
'sentence': target_tokens,
'sentence_labels': sentence_labels,
'label_ids': label_ids,
'start_positions': start_positions,
}
self.data.append(item)
def __len__(self):
return len(self.data)
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
entry = self.data[idx]
# left_ctx = entry['left_ctx']
sentence_labels = entry['sentence_labels']
input_ids = self.tokenizer.convert_tokens_to_ids(entry['sentence'])
L = len(input_ids)
input_ids += [0] * (self.max_seq_length - len(input_ids))
attention_mask = [1] * L + [0] * (self.max_seq_length - L)
attention_mask = torch.tensor(attention_mask, dtype=torch.int64)
labels = entry['label_ids'] + [-1] * (self.max_seq_length - L)
item = [torch.tensor(input_ids),
attention_mask,
torch.tensor(labels, dtype=torch.int64),
]
if self.evaluate:
item.append(sentence_labels)
item.append(entry['start_positions'])
return item
class FewNERDataset(Dataset):
def __init__(self, tokenizer, args=None, evaluate=False, do_test=False):
if not evaluate:
file_path = os.path.join(args.data_dir, args.train_file)
else:
if do_test:
file_path = os.path.join(args.data_dir, args.test_file)
else:
file_path = os.path.join(args.data_dir, args.dev_file)
assert os.path.isfile(file_path)
self.tokenizer = tokenizer
self.max_seq_length = args.max_seq_length
self.max_mention_length = args.max_mention_length
self.max_pair_length = args.max_pair_length
self.max_entity_length = args.max_pair_length * 2
self.evaluate = evaluate
self.file_path = file_path
self.doc_stride = args.doc_stride
self.args = args
self.model_type = args.model_type
ori_label_list = ['art-broadcastprogram', 'art-film', 'art-music', 'art-other', 'art-painting', 'art-writtenart', 'building-airport', 'building-hospital', 'building-hotel', 'building-library', 'building-other', 'building-restaurant', 'building-sportsfacility', 'building-theater', 'event-attack/battle/war/militaryconflict', 'event-disaster', 'event-election', 'event-other', 'event-protest', 'event-sportsevent', 'location-GPE', 'location-bodiesofwater', 'location-island', 'location-mountain', 'location-other', 'location-park', 'location-road/railway/highway/transit', 'organization-company', 'organization-education', 'organization-government/governmentagency', 'organization-media/newspaper', 'organization-other', 'organization-politicalparty', 'organization-religion', 'organization-showorganization', 'organization-sportsleague', 'organization-sportsteam', 'other-astronomything', 'other-award', 'other-biologything', 'other-chemicalthing', 'other-currency', 'other-disease', 'other-educationaldegree', 'other-god', 'other-language', 'other-law', 'other-livingthing', 'other-medical', 'person-actor', 'person-artist/author', 'person-athlete', 'person-director', 'person-other', 'person-politician', 'person-scholar', 'person-soldier', 'product-airplane', 'product-car', 'product-food', 'product-game', 'product-other', 'product-ship', 'product-software', 'product-train', 'product-weapon']
self.ori_label_list = [x.replace('-', '_') for x in ori_label_list]
self.label_list = ['O']
for label in self.ori_label_list:
self.label_list.append('I-' + label)
self.label_map = {label: i for i, label in enumerate(self.label_list)}
self.data_info = self._read_data(file_path)
if self.args.output_dir.find('test')!=-1:
self.data_info = self.data_info[:5]
self.initialize()
def _read_data(self, input_file):
data = []
words = []
labels = []
sentence_boundaries = []
prev_type = None
with open(input_file) as f:
for line in tqdm(f):
line = line.rstrip()
if not line:
if words:
sentence_boundaries = [0, len(words)]
data.append((words, labels, sentence_boundaries))
words = []
labels = []
prev_type = None
else:
parts = line.split("\t")
words.append(parts[0])
label = parts[-1]
if label!='O':
label = parts[-1].replace('-', '_')
label = 'I-' + label
prev_type = label[2:]
labels.append(label)
if words:
data.append((words, labels, sentence_boundaries))
# data = data[:100]
print (len(data))
return data
def is_punctuation(self, char):
# obtained from:
# https://github.com/huggingface/transformers/blob/5f25a5f367497278bf19c9994569db43f96d5278/transformers/tokenization_bert.py#L489
cp = ord(char)
if (cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or (cp >= 91 and cp <= 96) or (cp >= 123 and cp <= 126):
return True
cat = unicodedata.category(char)
if cat.startswith("P"):
return True
return False
def initialize(self):
tokenizer = self.tokenizer
self.data = []
max_num_subwords = self.max_seq_length - 2
label_map = {label: i for i, label in enumerate(self.label_list)}
def tokenize_word(text):
if (
isinstance(tokenizer, RobertaTokenizer)
and (text[0] != "'")
and (len(text) != 1 or not self.is_punctuation(text))
):
return tokenizer.tokenize(text, add_prefix_space=True)
subwords = tokenizer.tokenize(text)
if len(text)>0 and len(subwords)==0: # for \u2063
subwords = ['[UNK]']
return subwords
for example_index, (words, labels, sentence_boundaries) in tqdm(enumerate(self.data_info), disable=self.args.local_rank not in [-1, 0], desc="Processing"):
tokens = [tokenize_word(w) for w in words]
subwords = [w for li in tokens for w in li]
subword2token = list(itertools.chain(*[[i] * len(li) for i, li in enumerate(tokens)]))
token2subword = [0] + list(itertools.accumulate(len(li) for li in tokens))
subword_start_positions = frozenset(token2subword)
subword_sentence_boundaries = [sum(len(li) for li in tokens[:p]) for p in sentence_boundaries]
for n in range(len(subword_sentence_boundaries) - 1):
doc_sent_start, doc_sent_end = subword_sentence_boundaries[n : n + 2]
left_length = doc_sent_start
right_length = len(subwords) - doc_sent_end
sentence_length = doc_sent_end - doc_sent_start
half_context_length = int((max_num_subwords - sentence_length) / 2)
if left_length < right_length:
left_context_length = min(left_length, half_context_length)
right_context_length = min(right_length, max_num_subwords - left_context_length - sentence_length)
else:
right_context_length = min(right_length, half_context_length)
left_context_length = min(left_length, max_num_subwords - right_context_length - sentence_length)
doc_offset = doc_sent_start - left_context_length
target_tokens = subwords[doc_offset : doc_sent_end + right_context_length]
target_tokens = [tokenizer.cls_token] + target_tokens + [tokenizer.sep_token]
word_sent_start, word_sent_end = sentence_boundaries[n: n+2]
sentence_labels = labels[word_sent_start : word_sent_end]
start_positions = []
label_ids = []
for entity_start in range(left_context_length, left_context_length + sentence_length):
doc_entity_start = entity_start + doc_offset
if doc_entity_start in subword_start_positions:
token_idx = subword2token[doc_entity_start]
label_ids.append(self.label_map[labels[token_idx]])
start_positions.append(entity_start+1)
else:
label_ids.append(-1)
label_ids = [-1] * (left_context_length+1) + label_ids + [-1] * (right_context_length+1)
assert(len(sentence_labels) == len(start_positions))
assert(len(label_ids) == len(target_tokens))
item = {
'sentence': target_tokens,
'sentence_labels': sentence_labels,
'label_ids': label_ids,
'start_positions': start_positions,
}
self.data.append(item)
def __len__(self):
return len(self.data)
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
entry = self.data[idx]
sentence_labels = entry['sentence_labels']
input_ids = self.tokenizer.convert_tokens_to_ids(entry['sentence'])
L = len(input_ids)
input_ids += [0] * (self.max_seq_length - len(input_ids))
attention_mask = [1] * L + [0] * (self.max_seq_length - L)
attention_mask = torch.tensor(attention_mask, dtype=torch.int64)
labels = entry['label_ids'] + [-1] * (self.max_seq_length - L)
item = [torch.tensor(input_ids),
attention_mask,
torch.tensor(labels, dtype=torch.int64),
]
if self.evaluate:
item.append(sentence_labels)
item.append(entry['start_positions'])
return item
def set_seed(args):
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if args.n_gpu > 0:
torch.cuda.manual_seed_all(args.seed)
def _rotate_checkpoints(args, checkpoint_prefix, use_mtime=False):
if not args.save_total_limit:
return
if args.save_total_limit <= 0:
return
# Check if we should delete older checkpoint(s)
glob_checkpoints = glob.glob(os.path.join(args.output_dir, '{}-*'.format(checkpoint_prefix)))
if len(glob_checkpoints) <= args.save_total_limit:
return
ordering_and_checkpoint_path = []
for path in glob_checkpoints:
if use_mtime:
ordering_and_checkpoint_path.append((os.path.getmtime(path), path))
else:
regex_match = re.match('.*{}-([0-9]+)'.format(checkpoint_prefix), path)
if regex_match and regex_match.groups():
ordering_and_checkpoint_path.append((int(regex_match.groups()[0]), path))
checkpoints_sorted = sorted(ordering_and_checkpoint_path)
checkpoints_sorted = [checkpoint[1] for checkpoint in checkpoints_sorted]
number_of_checkpoints_to_delete = max(0, len(checkpoints_sorted) - args.save_total_limit)
checkpoints_to_be_deleted = checkpoints_sorted[:number_of_checkpoints_to_delete]
for checkpoint in checkpoints_to_be_deleted:
logger.info("Deleting older checkpoint [{}] due to args.save_total_limit".format(checkpoint))
shutil.rmtree(checkpoint)
def train(args, model, tokenizer):
""" Train the model """
if args.local_rank in [-1, 0]:
tb_writer = SummaryWriter("logs/"+args.data_dir[max(args.data_dir.rfind('/'),0):]+"_ner_logs/"+args.output_dir[args.output_dir.rfind('/'):])
args.train_batch_size = args.per_gpu_train_batch_size * max(1, args.n_gpu)
if args.data_dir.find('fewnerd')!=-1:
train_dataset = FewNERDataset(tokenizer=tokenizer, args=args)
elif args.data_dir.find('ontonotes')!=-1:
train_dataset = OntonotesDataset(tokenizer=tokenizer, args=args)
else:
train_dataset = CoNLL03Dataset(tokenizer=tokenizer, args=args)
train_sampler = RandomSampler(train_dataset) if args.local_rank == -1 else DistributedSampler(train_dataset)
train_dataloader = DataLoader(train_dataset, sampler=train_sampler, batch_size=args.train_batch_size, num_workers=2*int(args.output_dir.find('test')==-1))
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
# Prepare optimizer and schedule (linear warmup and decay)
no_decay = ['bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in model.named_parameters() if 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 any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
optimizer = AdamW(optimizer_grouped_parameters, lr=args.learning_rate, eps=args.adam_epsilon)
if args.warmup_steps==-1:
scheduler = get_linear_schedule_with_warmup(
optimizer, num_warmup_steps=int(0.1*t_total), num_training_steps=t_total
)
else:
scheduler = get_linear_schedule_with_warmup(
optimizer, num_warmup_steps=args.warmup_steps, num_training_steps=t_total
)
if args.fp16:
try:
from apex import amp
except ImportError:
raise ImportError("Please install apex from https://www.github.com/nvidia/apex to use fp16 training.")
model, optimizer = amp.initialize(model, optimizer, opt_level=args.fp16_opt_level)
# multi-gpu training (should be after apex fp16 initialization)
if args.n_gpu > 1:
model = torch.nn.DataParallel(model)
# Distributed training (should be after apex fp16 initialization)
if args.local_rank != -1:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.local_rank],
output_device=args.local_rank,
find_unused_parameters=True)
# Train!
logger.info("***** Running training *****")
logger.info(" Num examples = %d", len(train_dataset))
logger.info(" Num Epochs = %d", args.num_train_epochs)
logger.info(" Instantaneous batch size per GPU = %d", args.per_gpu_train_batch_size)
logger.info(" Total train batch size (w. parallel, distributed & accumulation) = %d",
args.train_batch_size * args.gradient_accumulation_steps * (torch.distributed.get_world_size() if args.local_rank != -1 else 1))
logger.info(" Gradient Accumulation steps = %d", args.gradient_accumulation_steps)
logger.info(" Total optimization steps = %d", t_total)
global_step = 0
tr_loss, logging_loss = 0.0, 0.0
model.zero_grad()
train_iterator = trange(int(args.num_train_epochs), desc="Epoch", disable=args.local_rank not in [-1, 0])
set_seed(args) # Added here for reproductibility (even between python 2 and 3)
best_f1 = 0
for _ in train_iterator:
# if args.randstart:
# if _>0:
# train_dataset.initialize()
epoch_iterator = tqdm(train_dataloader, desc="Iteration", disable=args.local_rank not in [-1, 0])
for step, batch in enumerate(epoch_iterator):
model.train()
batch = tuple(t.to(args.device) for t in batch)
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
# 'position_ids': batch[2],
'labels': batch[2],
}
outputs = model(**inputs)
loss = outputs[0] # model outputs are always tuple in pytorch-transformers (see doc)
if args.n_gpu > 1:
loss = loss.mean() # mean() to average on multi-gpu parallel training
if args.gradient_accumulation_steps > 1:
loss = loss / args.gradient_accumulation_steps
if args.fp16:
with amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
tr_loss += loss.item()
if (step + 1) % args.gradient_accumulation_steps == 0:
if args.max_grad_norm > 0:
if args.fp16:
torch.nn.utils.clip_grad_norm_(amp.master_params(optimizer), args.max_grad_norm)
else:
torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
optimizer.step()
scheduler.step() # Update learning rate schedule
model.zero_grad()
global_step += 1
if args.local_rank in [-1, 0] and args.logging_steps > 0 and global_step % args.logging_steps == 0:
# Log metrics
tb_writer.add_scalar('lr', scheduler.get_lr()[0], global_step)
tb_writer.add_scalar('loss', (tr_loss - logging_loss)/args.logging_steps, global_step)
logging_loss = tr_loss
if args.local_rank in [-1, 0] and args.save_steps > 0 and global_step % args.save_steps == 0:
update = True
# Save model checkpoint
if args.evaluate_during_training: # Only evaluate when single GPU otherwise metrics may not average well
results = evaluate(args, model, tokenizer)
f1 = results['f1']
tb_writer.add_scalar('f1', f1, global_step)
if f1 > best_f1:
best_f1 = f1
print ('Best F1', best_f1)
else:
update = False
if update:
checkpoint_prefix = 'checkpoint'
output_dir = os.path.join(args.output_dir, '{}-{}'.format(checkpoint_prefix, global_step))
if not os.path.exists(output_dir):
os.makedirs(output_dir)
model_to_save = model.module if hasattr(model, 'module') else model # Take care of distributed/parallel training
model_to_save.save_pretrained(output_dir)
torch.save(args, os.path.join(output_dir, 'training_args.bin'))
logger.info("Saving model checkpoint to %s", output_dir)
_rotate_checkpoints(args, checkpoint_prefix)
if args.max_steps > 0 and global_step > args.max_steps:
epoch_iterator.close()
break
if args.max_steps > 0 and global_step > args.max_steps:
train_iterator.close()
break
if args.local_rank in [-1, 0]:
tb_writer.close()
return global_step, tr_loss / global_step, best_f1
def evaluate(args, model, tokenizer, prefix="", do_test=False):
eval_output_dir = args.output_dir
results = {}
if args.data_dir.find('fewnerd')!=-1:
eval_dataset = FewNERDataset(tokenizer=tokenizer, args=args, evaluate=True, do_test=do_test)
elif args.data_dir.find('ontonotes')!=-1:
eval_dataset = OntonotesDataset(tokenizer=tokenizer, args=args, evaluate=True, do_test=do_test)
else:
eval_dataset = CoNLL03Dataset(tokenizer=tokenizer, args=args, evaluate=True, do_test=do_test)
if not os.path.exists(eval_output_dir) and args.local_rank in [-1, 0]:
os.makedirs(eval_output_dir)
label_list = eval_dataset.label_list
args.eval_batch_size = args.per_gpu_eval_batch_size * max(1, args.n_gpu)
# Note that DistributedSampler samples randomly
eval_sampler = SequentialSampler(eval_dataset)
eval_dataloader = DataLoader(eval_dataset, sampler=eval_sampler, batch_size=args.eval_batch_size, collate_fn=CoNLL03Dataset.collate_fn, num_workers=4)
# Eval!
logger.info("***** Running evaluation {} *****".format(prefix))
logger.info(" Num examples = %d", len(eval_dataset))
logger.info(" Batch size = %d", args.eval_batch_size)
all_predictions = defaultdict(dict)
model.eval()
final_labels = []
final_predictions = []
num_ori_labels = len(eval_dataset.ori_label_list)
start_time = timeit.default_timer()
tot_candidates = []
for batch in tqdm(eval_dataloader, desc="Evaluating"):
original_labels = batch[-2]
start_positions = batch[-1]
batch = tuple(t.to(args.device) for t in batch[:-2])
with torch.no_grad():
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
'labels': batch[2]
}
outputs = model(**inputs)
loss = outputs[0]
logits = outputs[1]
probs = torch.softmax(logits, dim=-1).cpu().numpy()
preds = torch.argmax(logits, dim=-1).cpu().numpy()
for i in range(len(start_positions)):
sentence_labels = original_labels[i]
start_position = start_positions[i]
prob = probs[i]
pred = preds[i]
predicted_sequence = []
all_probs = []
for j in range(len(start_position)):
pred_label = label_list[pred[ start_position[j] ]]
predicted_sequence.append(pred_label)
if args.data_dir.find('fewnerd')!=-1:
p = prob[start_position[j]][1:]
else:
p = prob[start_position[j]]
p = p[1:1+num_ori_labels] + p[1+num_ori_labels:]
all_probs.append(p)
if args.output_candidates and do_test:
# if args.data_dir.find('fewnerd')!=-1:
span_prob = []
for l in range(len(all_probs)):
if l == 0:
prob = np.ones(num_ori_labels, dtype=np.float32)
# prob = torch.ones(num_ori_labels).to(all_probs[0])
else:
prob = 1 - all_probs[l-1]
for r in range(l, len(all_probs)):
if r-l+1 > args.max_mention_ori_length:
break
prob = prob * all_probs[r]
if r+1 != len(all_probs):
final_prob = prob * (1 - all_probs[r+1])
else:
final_prob = prob
span_prob.append( ((l, r), final_prob.max()) )
span_prob.sort(key=lambda x: -x[1])
candidates = [x[0] for x in span_prob]
candidates = candidates[:256]
tot_candidates.append(candidates)
assert(len(predicted_sequence)==len(sentence_labels))
# convert IOB2 -> IOB1
prev_type = None
for n, label in enumerate(predicted_sequence):
if (label[0] == "B") and (label[2:] != prev_type):
predicted_sequence[n] = "I" + label[1:]
prev_type = label[2:]
final_predictions.append(predicted_sequence)
final_labels.append(sentence_labels)
evalTime = timeit.default_timer() - start_time
logger.info(" Evaluation done in total %f secs (%f example per second)", evalTime, len(eval_dataset) / evalTime)
assert len(final_predictions) == len(final_labels)
f1 = seqeval.metrics.f1_score(final_labels, final_predictions)
precision_score = seqeval.metrics.precision_score(final_labels, final_predictions)
recall_score = seqeval.metrics.recall_score(final_labels, final_predictions)
results = {'f1': f1, 'precision': precision_score, 'recall_score': recall_score}
logger.info("Result: %s", json.dumps(results))