-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
185 lines (148 loc) · 8.09 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
import torch
import argparse
from torch.utils.data import DataLoader
from transformers import BertModel
from transformers import BertTokenizer
from src.data.dataset import (
MentionEntityAffinityDataset,
MentionMentionAffinityDataset,
MentionEntityBatchSampler,
MentionMentionBatchSampler
)
from src.models.affinity_models import MentionEntityAffinityModel, MentionMentionAffinityModel
from src.models.loss import TripletLosss
from src.utils import (
create_pair_indices,
create_input_sentences,
load_processed_medmention,
create_neg_pair_indices_dict,
load_sentence_tokens,
load_all_entity_descriptions,
tokenize_list_sentences,
load_umls_synonym,
load_umls_semantic_type,
tokenize_all_entitiy_descriptions,
create_pair_indices_mention_entity,
load_all_mentions,
load_negative_candidates
)
from .src.models.candidate_generator import GenerateCandidateModel
from src.data.pre_processing import convert_IOB2_format
from tqdm import tqdm
import logging
import os
import warnings
warnings.filterwarnings("ignore")
LOGGER = logging.getLogger()
def parse_args():
"""
Parse input arguments
"""
parser = argparse.ArgumentParser(description='Train models')
# Required
parser.add_argument('--base_model_name', required=True, help='Name of bert base model')
parser.add_argument('--umls_dir_path', required=True, help='Directory of UMLs dataset')
parser.add_argument('--st21pv_dir_path', required=True, help='Directory of ST21pv dataset')
parser.add_argument('--ab3p_output_file_path', required=True, help='Path of Ab3p output')
parser.add_argument('--output_dir', type=str, required=True, help='Directory for output model weights')
# Optionals
parser.add_argument('--training_sentences_tokens_path', help='Path of training sentence tokens file')
parser.add_argument('--all_entity_description_tokens_path', help='Path of all training description tokens file')
parser.add_argument('--st21pv_corpus_IOB2_format_dir_path', help='Directory of ST21pv corpus IOB2 format')
# Tokenizer settings
parser.add_argument('--max_length', default=256, type=int)
# Train config
parser.add_argument('--learning_rate', help='learning rate', default=0.00001, type=float)
parser.add_argument('--margin', help='margin of triplet loss', default=0.8, type=float)
parser.add_argument('--batch_size', help='train batch size', default=16, type=int)
parser.add_argument('--epoch_mention_entity', help='epoch to train mention entity model', default=2, type= int)
parser.add_argument('--epoch_mention_mention', help='epoch to train mention mention model', default=5, type = int)
args = parser.parse_args()
return args
def init_logging():
LOGGER.setLevel(logging.INFO)
fmt = logging.Formatter('%(asctime)s: [ %(message)s ]',
'%m/%d/%Y %I:%M:%S %p')
console = logging.StreamHandler()
console.setFormatter(fmt)
LOGGER.addHandler(console)
def main(args):
init_logging()
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
try:
tokenizer = BertTokenizer.from_pretrained(args.base_model_name,use_fast=True)
except Exception as e:
LOGGER.info(e.message)
LOGGER.info("Process ST21pv dataset!")
if args.st21pv_corpus_IOB2_format_dir_path is not None:
st21pv_corpus = load_processed_medmention(args.st21pv_corpus_IOB2_format_dir_path)
else:
st21pv_corpus = convert_IOB2_format(args.st21pv_dir_path + "/corpus_pubtator.txt", args.ab3p_output_file_path, args.output_dir)
list_sentences, list_sentence_labels, list_sentence_docids = create_input_sentences(st21pv_corpus, args.st21pv_dir_path + '/corpus_pubtator_pmids_trng.txt')
LOGGER.info("Done processing ST21pv dataset!")
if args.training_sentences_tokens_path is not None:
LOGGER.info("Load exsiting list training sentence tokens!")
training_sentence_tokens, training_mention_pos = load_sentence_tokens(args.training_sentences_tokens_path)
else:
LOGGER.info("Start creating list training sentence tokens!")
training_sentence_tokens = tokenize_list_sentences(list_sentences, tokenizer)
LOGGER.info("Done creating list training sentence tokens!")
if args.all_entity_description_tokens_path is not None:
LOGGER.info("Load all entity description tokens into dictionary!")
all_entity_description_tokens_dict = load_all_entity_descriptions(args.all_entity_description_tokens_path)
else:
LOGGER.info("Start creating dictionary of all entity description tokens!")
umls_synonym_dict = load_umls_synonym(args.umls_dir_path + '/MRCONSO.RRF')
umls_semantic_dict = load_umls_semantic_type(args.umls_dir_path + '/MRSTY.RRF')
all_entity_description_tokens_dict = tokenize_all_entitiy_descriptions(umls_synonym_dict, umls_semantic_dict, tokenizer)
LOGGER.info("Done!")
LOGGER.info("Start trainining mention entity model!")
LOGGER.info("Generate candidates for training set")
corpus =
generate_model = GenerateCandidateModel()
candidate_indices = generate_model
all_mentions, all_mentions_labels, all_docid = load_all_mentions("./data/processed/all_mentions.txt")
all_negative_indices_dict = load_negative_candidates(candidates_file_path, all_entity_labels, list_sentence_labels)
create_pair_indices_mention_entity(list_sentence_docids, all_docid, list_sentence_labels, all_negative_indices_dict)
pair_indices = create_pair_indices(all_mentions_labels, list_sentence_docids)
neg_pair_indices_dict = create_neg_pair_indices_dict(pair_indices)
mention_entity_model = MentionEntityAffinityModel(tokenizer, base_model_path = None)
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
mention_entity_model.to(device)
optimizer = torch.optim.Adam(mention_entity_model.parameters(), lr=1e-5)
loss_func = TripletLosss(margin = 0.8)
BATCH_SIZE = 16
TOP_K_NEG = 1
training_dataset = MentionEntityAffinityDataset(training_mention_tokens = training_sentence_tokens,
training_mention_pos= training_mention_pos,
pair_indices=pair_indices,
all_entity_description_tokens_dict = all_entity_description_tokens_dict)
train_data_loader = DataLoader(training_dataset,
batch_sampler=MentionEntityBatchSampler(model=mention_entity_model,
device= device,
training_mention_tokens = training_sentence_tokens,
training_mention_pos = training_mention_pos,
pair_indices_labels = pair_indices,
neg_pair_indices_dict = neg_pair_indices_dict,
entity_description_tokens_dict = all_entity_description_tokens_dict)
)
with tqdm(train_data_loader, unit="batch") as tepoch:
mention_entity_model.train()
for dataset, _ in tepoch:
pairs = dataset
anchor_pos_batch = pairs[:BATCH_SIZE//TOP_K_NEG].to(device)
anchor_neg_batch = pairs[BATCH_SIZE//TOP_K_NEG:].to(device)
anchor_pos_batch = anchor_pos_batch.to(device)
anchor_neg_batch = anchor_neg_batch.to(device)
for i in range(TOP_K_NEG):
anchor_pos_affin = mention_entity_model(anchor_pos_batch)
anchor_neg_affin = mention_entity_model(anchor_neg_batch[i::TOP_K_NEG]) # anchor_neg_batch_indices[i::TOP_K_NEG]) print(time.time()-s)
loss = loss_func(anchor_pos_affin, anchor_neg_affin)
optimizer.zero_grad()
loss.backward()
optimizer.step()
tepoch.set_postfix(loss=loss.item())
if __name__ == '__main__':
args = parse_args()
main(args)