-
Notifications
You must be signed in to change notification settings - Fork 0
/
bert_ned_full_pipeline.py
326 lines (257 loc) · 9.92 KB
/
bert_ned_full_pipeline.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
"""
Author: Amund Faller Råheim
Script to run full pipeline:
* candidate generation,
* input data generation,
* training and
* evaluation
Configurable through config.ini and commandline arguments
"""
from configparser import ConfigParser
import argparse
import time
from src.conll_candidates_generator import ConllCandidatesGenerator
from src.input_data_generator import InputDataGenerator
from src.dataset_generator import DatasetGenerator
from src.bert_model import BertBinaryClassification, load_bert_from_file, \
save_bert_to_file
from src.trainer import ModelTrainer
from src.evaluation import plot_training_stats
import torch
import numpy as np
import random
# -- Setup --------------------------------------------------------------------
# Set the seed value everywhere to make this reproducible
seed_val = 42
random.seed(seed_val)
np.random.seed(seed_val)
torch.manual_seed(seed_val)
torch.cuda.manual_seed_all(seed_val)
# Wrapper function to time executions
def timer(func):
def wrapper(*args, **kwargs):
print("----\n Timing function ...\n----")
t0 = time.time()
res = func(*args, **kwargs)
t1 = time.time()
d_t_str = time.strftime("%H:%M:%S hh:mm:ss", time.gmtime(t1-t0))
print(f"----\n Function ran from "
f"{time.ctime(t0)} to {time.ctime(t1)}")
print(f" Took: {d_t_str}\n----")
return res
return wrapper
config = ConfigParser()
config.read('config.ini')
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=__doc__
)
parser.add_argument(
"-r", "--read_input_data", action="store_true",
help="whether to use paths in 'config.ini' "
"to read previously generated input data"
)
parser.add_argument(
"-e", "--evaluate", action="store_true",
help="no training, only evaluate model read from path in 'config.ini'"
)
parser.add_argument(
"-m", "--new_model", action="store_true",
help="get pretrained model from Huggingface, "
"and do not read model from disk"
)
args = parser.parse_args()
print(f"args: {args}")
# -- Generate candidates for mentions in CoNLL dataset ------------------------
@timer
def candidate_generation():
candidate_generator = ConllCandidatesGenerator(
spacy_nlp_vocab_dir=config['DATA']['Spacy Vocab Dir'],
spacy_kb_file=config['DATA']['Spacy KB']
)
if args.read_input_data:
candidate_generator.read_entities_info(
config['DATA']['Candidate Info']
)
else:
docs_entities = candidate_generator.get_docs_entities(
config['DATA']['Conll Annotated']
)
candidate_generator.write_entities_info(
config['DATA']['Candidate Info']
)
candidate_generator.print_candidate_stats()
docs_entities = candidate_generator.get_docs_entities()
docs = candidate_generator.get_docs(config['DATA']['Conll Annotated'])
del candidate_generator
return docs_entities, docs
print(' 1. Candidate generation ...')
docs_entities, docs = candidate_generation()
print(' ... Candidate generation done!')
# -- Generate input vectors from CoNLL docs and Wikipedia abstracts -----------
@timer
def input_data_generation():
input_data_generator = InputDataGenerator(
wikipedia_abstracts_file=config['DATA']['Wikipedia Abstracts'],
tokenizer_pretrained_id=config['BERT']['Model ID']
)
input_vectors = input_data_generator.generate_for_conll_data(
docs=docs,
docs_entities=docs_entities,
max_len=int(config['BERT']['Max Sequence Length']),
progress=True
)
del input_data_generator
return input_vectors
input_vectors = None
if args.read_input_data:
print(' 2. Input data generation ... Skipping!')
else:
print(' 2. Input data generation ...')
input_vectors = input_data_generation()
print(' ... Input data generation done!')
# -- Read or generate BERT input vectors, and other info ----------------------
@timer
def dataset_generation():
use_balanced_dataset = config.getboolean(
'INPUT VECTORS',
'Use Balanced Dataset'
)
# Recommended CoNLL split, reverse engineered to ratios.
split_ratios = [0.6799, 0.1557, 0.1644]
use_default_split = config.getboolean('TRAINING', 'Use Default Split')
if not use_default_split:
split_ratios = [float(config['TRAINING']['Training Set Size']),
float(config['TRAINING']['Validation Set Size']),
float(config['TRAINING']['Test Set Size'])]
dataset_generator = DatasetGenerator()
# If input vectors were generated in previous step,
# and no instructions to read from file
if not args.read_input_data and input_vectors:
dataset_generator = DatasetGenerator(*input_vectors)
# Use balanced dataset
if use_balanced_dataset:
balanced_dataset_dir = config['INPUT VECTORS']['Balanced Dataset Dir']
# Reading previously generated dataset from file
if args.read_input_data and balanced_dataset_dir:
print("Reading balanced dataset from files ...")
dataset_generator.read_balanced_dataset(balanced_dataset_dir)
# Generate balanced dataset
else:
n_neg_samples = config['INPUT VECTORS']['N Negative Samples']
n_neg_samples = int(n_neg_samples) if n_neg_samples else 1
print(f"Generating balanced dataset with "
f"ratio 1:{n_neg_samples} ...")
dataset_generator.get_balanced_dataset(
docs_entities,
n_neg_samples
)
if balanced_dataset_dir:
print("Writing balanced dataset to files ...")
dataset_generator.write_balanced_dataset_to_files(
balanced_dataset_dir
)
print("Splitting dataset ...")
dataset_generator.get_split_dataset(split_ratios, dataset='balanced')
dataset_to_doc = dataset_generator.balanced_dataset_to_doc
dataset_to_mention = dataset_generator.balanced_dataset_to_entity
dataset_to_candidate = dataset_generator.balanced_dataset_to_candidate
# Use full dataset
else:
vec_dir = config['INPUT VECTORS']['Input Vectors Dir']
# Reading previously generated dataset from file
if vec_dir:
if args.read_input_data:
print("Reading vectors ...")
dataset_generator.read_from_directory(vec_dir)
elif input_vectors:
print(f"Writing vectors to directory {vec_dir} ...")
dataset_generator.write_to_files(vec_dir)
else:
print("\nERROR: Got no vectors!")
exit()
print("Splitting dataset ...")
dataset_generator.get_split_dataset(
split_ratios,
dataset='full',
docs_entities=docs_entities
)
dataset_to_doc, dataset_to_mention, dataset_to_candidate = \
dataset_generator.get_dataset_to_x(docs_entities)
print("Getting DataLoaders ...")
train_loader, val_loader, test_loader = \
dataset_generator.get_data_loaders(
batch_size=int(config['TRAINING']['Batch Size'])
)
neg, pos = dataset_generator.get_dataset_balance_info()
return train_loader, val_loader, test_loader, dataset_to_doc, \
dataset_to_mention, dataset_to_candidate, pos, neg
print(' 3. Data loader generation ...')
train_loader, val_loader, test_loader, dataset_to_doc, \
dataset_to_mention, dataset_to_candidate, pos, neg = dataset_generation()
print(' ... Data loader generation done!')
# -- Generate BERT model ------------------------------------------------------
@timer
def model_generation():
model_dir = config['BERT']['Bert Model Dir']
if not model_dir or args.new_model:
model_path = config['BERT']['Model ID']
model = BertBinaryClassification.from_pretrained(
model_path
)
else:
model = load_bert_from_file(model_dir)
freeze_n_transformers = config['TRAINING']['Freeze N Transformers']
if freeze_n_transformers:
freeze_n_transformers = int(freeze_n_transformers)
else:
freeze_n_transformers = 12
model.freeze_n_transformers(freeze_n_transformers)
print(f" pos:neg = {pos}:{neg}")
model.set_class_weights(neg/pos * torch.ones([1]))
return model
print(' 4. Model generation ...')
model = model_generation()
print(' ... Model generation done!')
# -- Train and test -----------------------------------------------------------
@timer
def training():
epochs = int(config['TRAINING']['Epochs'])
save_dir = config['BERT']['Save Model Dir']
train_update_freq = int(config['VERBOSITY']['Training Update Frequency'])
validation_update_freq = int(
config['VERBOSITY']['Validation Update Frequency']
)
test_update_freq = int(config['VERBOSITY']['Test Update Frequency'])
train = bool(epochs > 0 and not args.evaluate)
handler = ModelTrainer(
model,
train_loader,
val_loader,
test_loader,
epochs
)
if train:
dataset_to_x = (
dataset_to_doc,
dataset_to_mention,
dataset_to_candidate
)
training_stats = handler.train(
train_update_freq,
validation_update_freq,
dataset_to_x
)
if save_dir:
save_bert_to_file(model, save_dir)
if len(training_stats) > 1:
plot_training_stats(training_stats)
handler.test(
dataset_to_doc,
dataset_to_mention,
test_update_freq,
dataset_to_candidate
)
print(' 5. Training ...')
training()
print(' ... All done!')