forked from MurtyShikhar/robustqa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
312 lines (278 loc) · 14.9 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
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
import argparse
import json
import os
from collections import OrderedDict
import torch
import csv
import util
from transformers import DistilBertTokenizerFast
from transformers import DistilBertForQuestionAnswering
from transformers import AdamW
from tensorboardX import SummaryWriter
from torch.utils.data import DataLoader
from torch.utils.data.sampler import RandomSampler, SequentialSampler
from args import get_train_test_args
from tqdm import tqdm
def prepare_eval_data(dataset_dict, tokenizer):
tokenized_examples = tokenizer(dataset_dict['question'],
dataset_dict['context'],
truncation="only_second",
stride=128,
max_length=384,
return_overflowing_tokens=True,
return_offsets_mapping=True,
padding='max_length')
# Since one example might give us several features if it has a long context, we need a map from a feature to
# its corresponding example. This key gives us just that.
sample_mapping = tokenized_examples.pop("overflow_to_sample_mapping")
# For evaluation, we will need to convert our predictions to substrings of the context, so we keep the
# corresponding example_id and we will store the offset mappings.
tokenized_examples["id"] = []
for i in tqdm(range(len(tokenized_examples["input_ids"]))):
# Grab the sequence corresponding to that example (to know what is the context and what is the question).
sequence_ids = tokenized_examples.sequence_ids(i)
# One example can give several spans, this is the index of the example containing this span of text.
sample_index = sample_mapping[i]
tokenized_examples["id"].append(dataset_dict["id"][sample_index])
# Set to None the offset_mapping that are not part of the context so it's easy to determine if a token
# position is part of the context or not.
tokenized_examples["offset_mapping"][i] = [
(o if sequence_ids[k] == 1 else None)
for k, o in enumerate(tokenized_examples["offset_mapping"][i])
]
return tokenized_examples
def prepare_train_data(dataset_dict, tokenizer):
tokenized_examples = tokenizer(dataset_dict['question'],
dataset_dict['context'],
truncation="only_second",
stride=128,
max_length=384,
return_overflowing_tokens=True,
return_offsets_mapping=True,
padding='max_length')
sample_mapping = tokenized_examples["overflow_to_sample_mapping"]
offset_mapping = tokenized_examples["offset_mapping"]
# Let's label those examples!
tokenized_examples["start_positions"] = []
tokenized_examples["end_positions"] = []
tokenized_examples['id'] = []
inaccurate = 0
for i, offsets in enumerate(tqdm(offset_mapping)):
# We will label impossible answers with the index of the CLS token.
input_ids = tokenized_examples["input_ids"][i]
cls_index = input_ids.index(tokenizer.cls_token_id)
# Grab the sequence corresponding to that example (to know what is the context and what is the question).
sequence_ids = tokenized_examples.sequence_ids(i)
# One example can give several spans, this is the index of the example containing this span of text.
sample_index = sample_mapping[i]
answer = dataset_dict['answer'][sample_index]
# Start/end character index of the answer in the text.
start_char = answer['answer_start'][0]
end_char = start_char + len(answer['text'][0])
tokenized_examples['id'].append(dataset_dict['id'][sample_index])
# Start token index of the current span in the text.
token_start_index = 0
while sequence_ids[token_start_index] != 1:
token_start_index += 1
# End token index of the current span in the text.
token_end_index = len(input_ids) - 1
while sequence_ids[token_end_index] != 1:
token_end_index -= 1
# Detect if the answer is out of the span (in which case this feature is labeled with the CLS index).
if not (offsets[token_start_index][0] <= start_char and offsets[token_end_index][1] >= end_char):
tokenized_examples["start_positions"].append(cls_index)
tokenized_examples["end_positions"].append(cls_index)
else:
# Otherwise move the token_start_index and token_end_index to the two ends of the answer.
# Note: we could go after the last offset if the answer is the last word (edge case).
while token_start_index < len(offsets) and offsets[token_start_index][0] <= start_char:
token_start_index += 1
tokenized_examples["start_positions"].append(token_start_index - 1)
while offsets[token_end_index][1] >= end_char:
token_end_index -= 1
tokenized_examples["end_positions"].append(token_end_index + 1)
# assertion to check if this checks out
context = dataset_dict['context'][sample_index]
offset_st = offsets[tokenized_examples['start_positions'][-1]][0]
offset_en = offsets[tokenized_examples['end_positions'][-1]][1]
if context[offset_st : offset_en] != answer['text'][0]:
inaccurate += 1
total = len(tokenized_examples['id'])
print(f"Preprocessing not completely accurate for {inaccurate}/{total} instances")
return tokenized_examples
def read_and_process(args, tokenizer, dataset_dict, dir_name, dataset_name, split):
#TODO: cache this if possible
cache_path = f'{dir_name}/{dataset_name}_encodings.pt'
if os.path.exists(cache_path) and not args.recompute_features:
tokenized_examples = util.load_pickle(cache_path)
else:
if split=='train':
tokenized_examples = prepare_train_data(dataset_dict, tokenizer)
else:
tokenized_examples = prepare_eval_data(dataset_dict, tokenizer)
util.save_pickle(tokenized_examples, cache_path)
return tokenized_examples
#TODO: use a logger, use tensorboard
class Trainer():
def __init__(self, args, log):
self.lr = args.lr
self.num_epochs = args.num_epochs
self.device = args.device
self.eval_every = args.eval_every
self.path = os.path.join(args.save_dir, 'checkpoint')
self.num_visuals = args.num_visuals
self.save_dir = args.save_dir
self.log = log
self.visualize_predictions = args.visualize_predictions
if not os.path.exists(self.path):
os.makedirs(self.path)
def save(self, model):
model.save_pretrained(self.path)
def evaluate(self, model, data_loader, data_dict, return_preds=False, split='validation'):
device = self.device
model.eval()
pred_dict = {}
all_start_logits = []
all_end_logits = []
with torch.no_grad(), \
tqdm(total=len(data_loader.dataset)) as progress_bar:
for batch in data_loader:
# Setup for forward
input_ids = batch['input_ids'].to(device)
attention_mask = batch['attention_mask'].to(device)
batch_size = len(input_ids)
outputs = model(input_ids, attention_mask=attention_mask)
# Forward
start_logits, end_logits = outputs.start_logits, outputs.end_logits
# TODO: compute loss
all_start_logits.append(start_logits)
all_end_logits.append(end_logits)
progress_bar.update(batch_size)
# Get F1 and EM scores
start_logits = torch.cat(all_start_logits).cpu().numpy()
end_logits = torch.cat(all_end_logits).cpu().numpy()
preds = util.postprocess_qa_predictions(data_dict,
data_loader.dataset.encodings,
(start_logits, end_logits))
if split == 'validation':
results = util.eval_dicts(data_dict, preds)
results_list = [('F1', results['F1']),
('EM', results['EM'])]
else:
results_list = [('F1', -1.0),
('EM', -1.0)]
results = OrderedDict(results_list)
if return_preds:
return preds, results
return results
def train(self, model, train_dataloader, eval_dataloader, val_dict):
device = self.device
model.to(device)
optim = AdamW(model.parameters(), lr=self.lr)
global_idx = 0
best_scores = {'F1': -1.0, 'EM': -1.0}
tbx = SummaryWriter(self.save_dir)
for epoch_num in range(self.num_epochs):
self.log.info(f'Epoch: {epoch_num}')
with torch.enable_grad(), tqdm(total=len(train_dataloader.dataset)) as progress_bar:
for batch in train_dataloader:
optim.zero_grad()
model.train()
input_ids = batch['input_ids'].to(device)
attention_mask = batch['attention_mask'].to(device)
start_positions = batch['start_positions'].to(device)
end_positions = batch['end_positions'].to(device)
outputs = model(input_ids, attention_mask=attention_mask,
start_positions=start_positions,
end_positions=end_positions)
loss = outputs[0]
loss.backward()
optim.step()
progress_bar.update(len(input_ids))
progress_bar.set_postfix(epoch=epoch_num, NLL=loss.item())
tbx.add_scalar('train/NLL', loss.item(), global_idx)
if (global_idx % self.eval_every) == 0:
self.log.info(f'Evaluating at step {global_idx}...')
preds, curr_score = self.evaluate(model, eval_dataloader, val_dict, return_preds=True)
results_str = ', '.join(f'{k}: {v:05.2f}' for k, v in curr_score.items())
self.log.info('Visualizing in TensorBoard...')
for k, v in curr_score.items():
tbx.add_scalar(f'val/{k}', v, global_idx)
self.log.info(f'Eval {results_str}')
if self.visualize_predictions:
util.visualize(tbx,
pred_dict=preds,
gold_dict=val_dict,
step=global_idx,
split='val',
num_visuals=self.num_visuals)
if curr_score['F1'] >= best_scores['F1']:
best_scores = curr_score
self.save(model)
global_idx += 1
return best_scores
def get_dataset(args, datasets, data_dir, tokenizer, split_name):
datasets = datasets.split(',')
dataset_dict = None
dataset_name=''
for dataset in datasets:
dataset_name += f'_{dataset}'
dataset_dict_curr = util.read_squad(f'{data_dir}/{dataset}')
dataset_dict = util.merge(dataset_dict, dataset_dict_curr)
data_encodings = read_and_process(args, tokenizer, dataset_dict, data_dir, dataset_name, split_name)
return util.QADataset(data_encodings, train=(split_name=='train')), dataset_dict
def main():
# define parser and arguments
args = get_train_test_args()
util.set_seed(args.seed)
model = DistilBertForQuestionAnswering.from_pretrained("distilbert-base-uncased")
tokenizer = DistilBertTokenizerFast.from_pretrained('distilbert-base-uncased')
if args.do_train:
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
args.save_dir = util.get_save_dir(args.save_dir, args.run_name)
log = util.get_logger(args.save_dir, 'log_train')
log.info(f'Args: {json.dumps(vars(args), indent=4, sort_keys=True)}')
log.info("Preparing Training Data...")
args.device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
trainer = Trainer(args, log)
train_dataset, _ = get_dataset(args, args.train_datasets, args.train_dir, tokenizer, 'train')
log.info("Preparing Validation Data...")
val_dataset, val_dict = get_dataset(args, args.train_datasets, args.val_dir, tokenizer, 'val')
train_loader = DataLoader(train_dataset,
batch_size=args.batch_size,
sampler=RandomSampler(train_dataset))
val_loader = DataLoader(val_dataset,
batch_size=args.batch_size,
sampler=SequentialSampler(val_dataset))
if (args.checkpoint != 'none'):
checkpoint_path = os.path.join(args.checkpoint, 'checkpoint')
model = DistilBertForQuestionAnswering.from_pretrained(checkpoint_path)
best_scores = trainer.train(model, train_loader, val_loader, val_dict)
if args.do_eval:
args.device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
split_name = 'test' if 'test' in args.eval_dir else 'validation'
log = util.get_logger(args.save_dir, f'log_{split_name}')
trainer = Trainer(args, log)
checkpoint_path = os.path.join(args.save_dir, 'checkpoint')
model = DistilBertForQuestionAnswering.from_pretrained(checkpoint_path)
model.to(args.device)
eval_dataset, eval_dict = get_dataset(args, args.eval_datasets, args.eval_dir, tokenizer, split_name)
eval_loader = DataLoader(eval_dataset,
batch_size=args.batch_size,
sampler=SequentialSampler(eval_dataset))
eval_preds, eval_scores = trainer.evaluate(model, eval_loader,
eval_dict, return_preds=True,
split=split_name)
results_str = ', '.join(f'{k}: {v:05.2f}' for k, v in eval_scores.items())
log.info(f'Eval {results_str}')
# Write submission file
sub_path = os.path.join(args.save_dir, split_name + '_' + args.sub_file)
log.info(f'Writing submission file to {sub_path}...')
with open(sub_path, 'w', newline='', encoding='utf-8') as csv_fh:
csv_writer = csv.writer(csv_fh, delimiter=',')
csv_writer.writerow(['Id', 'Predicted'])
for uuid in sorted(eval_preds):
csv_writer.writerow([uuid, eval_preds[uuid]])
if __name__ == '__main__':
main()