-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflair_api.py
165 lines (123 loc) · 5.41 KB
/
flair_api.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
from flair.embeddings import TransformerWordEmbeddings, ELMoEmbeddings, StackedEmbeddings
from flair.embeddings.legacy import BertTokenizer
import os
from torch.optim import SGD, Adam, AdamW, ASGD
from flair.models import SequenceTagger
from flair.data import Corpus
from flair.datasets import ColumnCorpus
from flair.trainers import ModelTrainer
import pandas as pd
from glob import glob
import flair
import torch
import yaml
import argparse
from pymedext_eds.extract.termino import TerminologyEmbeddings
print("initialized")
def run_exp(data_folder,
columns,
ter_params,
lm_param,
optimizer_type,
tagger_params,
trainer_params,
exp_device,
ratio = 0
):
flair.device = torch.device(exp_device)
assert set(columns.keys()) == set(['text', 'ner']) , "Columns shoud only has 2 value, text and ner"
#check every tsv in directory has the same number of columns
check_cols = [(fn, len(pd.read_csv(fn, sep="\t", index_col=False, nrows=1).columns)) for fn in glob(data_folder + "*.tsv")]
assert len(set([t[1] for t in check_cols])) == 1, check_cols
#load headers
headers = {v:i for i,v in enumerate(pd.read_csv(data_folder + "trainheaders.tsv", sep="\t").columns.tolist())}
#define tag from params
tag_type = 'ner'
#make columns format for flair
columns = {headers[v]:k for k,v in columns.items()}
ter_cols = {headers[v['field']]:v['field'] for v in ter_params}
merged_columns = {**columns, **ter_cols}
assert len(merged_columns) == len(columns) + len(ter_cols), "Terminolgy dict cannot be the columns to be tagged"
if optimizer_type == "ASGD":
optimizer = ASGD
elif optimizer_type == "AdamW":
optimizer = AdamW
else:
optimizer = SGD
print("Loading file withe columns:", merged_columns)
print("TAG-type:", tag_type)
# init a corpus using column format, data folder and the names of the train, dev and test files
corpus: Corpus = ColumnCorpus(data_folder, merged_columns,
train_file='train.tsv',
test_file='test.tsv',
dev_file='dev.tsv')
#oversampling
if ratio > 0:
print("oversample with ratio of 1 postive example for {}".format(ratio))
count = 0
oversampled_sentences = []
for sent in corpus.train.sentences:
if sent.get_spans("ner"):
count += ratio
oversampled_sentences.append(sent)
else:
if count >=0:
oversampled_sentences.append(sent)
count -= 1
corpus.train.sentences = oversampled_sentences
corpus.train.total_sentence_count = len(corpus.train.sentences)
del oversampled_sentences
# 3. make the tag dictionary from the corpus
tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type)
print("Tagging objective:", tag_dictionary.idx2item)
lm_type = lm_param["lm_type"]
if lm_type == "bert":
if "lm_path" in lm_param:
lm_path = lm_param["lm_path"]
else:
lm_path = 'data/embeddings/bert-base-medical-huge-cased/checkpoint-800000/'
bert_embedding = TransformerWordEmbeddings(lm_path)
if "lm_tokenizer_path" in lm_param:
tok_path = lm_param['lm_tokenizer_path']
else:
tok_path = 'data/embeddings/bert-base-multilingual-cased/'
bert_embedding.tokenizer = BertTokenizer.from_pretrained(tok_path, do_lower_case=False)
lm_embedding = [bert_embedding]
elif lm_type == "camembert":
if "lm_path" in lm_param:
lm_path = lm_param["lm_path"]
else:
lm_path = "data/embeddings/camembert-base/"
bert_embedding = TransformerWordEmbeddings(lm_path)
lm_embedding = [bert_embedding]
elif lm_type == "elmo":
if "lm_path" in lm_param:
lm_path = lm_param["lm_path"]
else:
lm_path = '../ivan_nested_NER/data/embeddings/nck_elmo/'
lm_embedding = [ELMoEmbeddings(options_file=os.path.join(lm_path, "options.json"), weight_file= os.path.join(lm_path, "weights.hdf5"))]
else:
lm_embedding = []
terminology_embeddings = [TerminologyEmbeddings(corpus=corpus, **t) for t in ter_params]
# if terminology_embeddings:
embeddings: StackedEmbeddings = StackedEmbeddings([ *lm_embedding, *terminology_embeddings])
# else:
# embeddings = lm_embedding[0]
tagger_params = {**tagger_params,
**{"embeddings":embeddings,
"tag_dictionary":tag_dictionary,
"tag_type":tag_type}
}
# 5. initialize sequence tagger
tagger: SequenceTagger = SequenceTagger(**tagger_params)
# 6. initialize trainer
trainer: ModelTrainer = ModelTrainer(tagger, corpus, optimizer=optimizer)
trainer.train(**trainer_params)
return tagger, trainer
if __name__ == "__main__":
# Arguments
parser = argparse.ArgumentParser(description='Training flair Sequence tagger')
parser.add_argument('--config', type=str, help='Path to experiment config.')
paras = parser.parse_args()
config = yaml.load(open(paras.config,'r'), Loader=yaml.FullLoader)
tagger, trainer = run_exp(**config)