-
Notifications
You must be signed in to change notification settings - Fork 109
/
train.py
378 lines (329 loc) · 14.8 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
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
import re
import os
import sys
import math
import random
import string
import logging
import argparse
from shutil import copyfile
from datetime import datetime
from collections import Counter
import torch
import msgpack
from drqa.model import DocReaderModel
from drqa.utils import str2bool
def main():
args, log = setup()
log.info('[Program starts. Loading data...]')
train, dev, dev_y, embedding, opt = load_data(vars(args))
log.info(opt)
log.info('[Data loaded.]')
if args.save_dawn_logs:
dawn_start = datetime.now()
log.info('dawn_entry: epoch\tf1Score\thours')
if args.resume:
log.info('[loading previous model...]')
checkpoint = torch.load(os.path.join(args.model_dir, args.resume))
if args.resume_options:
opt = checkpoint['config']
state_dict = checkpoint['state_dict']
model = DocReaderModel(opt, embedding, state_dict)
epoch_0 = checkpoint['epoch'] + 1
# synchronize random seed
random.setstate(checkpoint['random_state'])
torch.random.set_rng_state(checkpoint['torch_state'])
if args.cuda:
torch.cuda.set_rng_state(checkpoint['torch_cuda_state'])
if args.reduce_lr:
lr_decay(model.optimizer, lr_decay=args.reduce_lr)
log.info('[learning rate reduced by {}]'.format(args.reduce_lr))
batches = BatchGen(dev, batch_size=args.batch_size, evaluation=True, gpu=args.cuda)
predictions = []
for i, batch in enumerate(batches):
predictions.extend(model.predict(batch))
log.debug('> evaluating [{}/{}]'.format(i, len(batches)))
em, f1 = score(predictions, dev_y)
log.info("[dev EM: {} F1: {}]".format(em, f1))
if math.fabs(em - checkpoint['em']) > 1e-3 or math.fabs(f1 - checkpoint['f1']) > 1e-3:
log.info('Inconsistent: recorded EM: {} F1: {}'.format(checkpoint['em'], checkpoint['f1']))
log.error('Error loading model: current code is inconsistent with code used to train the previous model.')
exit(1)
best_val_score = checkpoint['best_eval']
else:
model = DocReaderModel(opt, embedding)
epoch_0 = 1
best_val_score = 0.0
for epoch in range(epoch_0, epoch_0 + args.epochs):
log.warning('Epoch {}'.format(epoch))
# train
batches = BatchGen(train, batch_size=args.batch_size, gpu=args.cuda)
start = datetime.now()
for i, batch in enumerate(batches):
model.update(batch)
if i % args.log_per_updates == 0:
log.info('> epoch [{0:2}] updates[{1:6}] train loss[{2:.5f}] remaining[{3}]'.format(
epoch, model.updates, model.train_loss.value,
str((datetime.now() - start) / (i + 1) * (len(batches) - i - 1)).split('.')[0]))
log.debug('\n')
# eval
batches = BatchGen(dev, batch_size=args.batch_size, evaluation=True, gpu=args.cuda)
predictions = []
for i, batch in enumerate(batches):
predictions.extend(model.predict(batch))
log.debug('> evaluating [{}/{}]'.format(i, len(batches)))
em, f1 = score(predictions, dev_y)
log.warning("dev EM: {} F1: {}".format(em, f1))
if args.save_dawn_logs:
time_diff = datetime.now() - dawn_start
log.warning("dawn_entry: {}\t{}\t{}".format(epoch, f1/100.0, float(time_diff.total_seconds() / 3600.0)))
# save
if not args.save_last_only or epoch == epoch_0 + args.epochs - 1:
model_file = os.path.join(args.model_dir, 'checkpoint_epoch_{}.pt'.format(epoch))
model.save(model_file, epoch, [em, f1, best_val_score])
if f1 > best_val_score:
best_val_score = f1
copyfile(
model_file,
os.path.join(args.model_dir, 'best_model.pt'))
log.info('[new best model saved.]')
def setup():
parser = argparse.ArgumentParser(
description='Train a Document Reader model.'
)
# system
parser.add_argument('--log_per_updates', type=int, default=3,
help='log model loss per x updates (mini-batches).')
parser.add_argument('--data_file', default='SQuAD/data.msgpack',
help='path to preprocessed data file.')
parser.add_argument('--model_dir', default='models',
help='path to store saved models.')
parser.add_argument('--save_last_only', action='store_true',
help='only save the final models.')
parser.add_argument('--save_dawn_logs', action='store_true',
help='append dawnbench log entries prefixed with dawn_entry:')
parser.add_argument('--seed', type=int, default=1013,
help='random seed for data shuffling, dropout, etc.')
parser.add_argument("--cuda", type=str2bool, nargs='?',
const=True, default=torch.cuda.is_available(),
help='whether to use GPU acceleration.')
# training
parser.add_argument('-e', '--epochs', type=int, default=40)
parser.add_argument('-bs', '--batch_size', type=int, default=32)
parser.add_argument('-rs', '--resume', default='best_model.pt',
help='previous model file name (in `model_dir`). '
'e.g. "checkpoint_epoch_11.pt"')
parser.add_argument('-ro', '--resume_options', action='store_true',
help='use previous model options, ignore the cli and defaults.')
parser.add_argument('-rlr', '--reduce_lr', type=float, default=0.,
help='reduce initial (resumed) learning rate by this factor.')
parser.add_argument('-op', '--optimizer', default='adamax',
help='supported optimizer: adamax, sgd')
parser.add_argument('-gc', '--grad_clipping', type=float, default=10)
parser.add_argument('-wd', '--weight_decay', type=float, default=0)
parser.add_argument('-lr', '--learning_rate', type=float, default=0.1,
help='only applied to SGD.')
parser.add_argument('-mm', '--momentum', type=float, default=0,
help='only applied to SGD.')
parser.add_argument('-tp', '--tune_partial', type=int, default=1000,
help='finetune top-x embeddings.')
parser.add_argument('--fix_embeddings', action='store_true',
help='if true, `tune_partial` will be ignored.')
parser.add_argument('--rnn_padding', action='store_true',
help='perform rnn padding (much slower but more accurate).')
# model
parser.add_argument('--question_merge', default='self_attn')
parser.add_argument('--doc_layers', type=int, default=3)
parser.add_argument('--question_layers', type=int, default=3)
parser.add_argument('--hidden_size', type=int, default=128)
parser.add_argument('--num_features', type=int, default=4)
parser.add_argument('--pos', type=str2bool, nargs='?', const=True, default=True,
help='use pos tags as a feature.')
parser.add_argument('--ner', type=str2bool, nargs='?', const=True, default=True,
help='use named entity tags as a feature.')
parser.add_argument('--use_qemb', type=str2bool, nargs='?', const=True, default=True)
parser.add_argument('--concat_rnn_layers', type=str2bool, nargs='?',
const=True, default=True)
parser.add_argument('--dropout_emb', type=float, default=0.4)
parser.add_argument('--dropout_rnn', type=float, default=0.4)
parser.add_argument('--dropout_rnn_output', type=str2bool, nargs='?',
const=True, default=True)
parser.add_argument('--max_len', type=int, default=15)
parser.add_argument('--rnn_type', default='lstm',
help='supported types: rnn, gru, lstm')
args = parser.parse_args()
# set model dir
model_dir = args.model_dir
os.makedirs(model_dir, exist_ok=True)
args.model_dir = os.path.abspath(model_dir)
if args.resume == 'best_model.pt' and not os.path.exists(os.path.join(args.model_dir, args.resume)):
# means we're starting fresh
args.resume = ''
# set random seed
random.seed(args.seed)
torch.manual_seed(args.seed)
if args.cuda:
torch.cuda.manual_seed(args.seed)
# setup logger
class ProgressHandler(logging.Handler):
def __init__(self, level=logging.NOTSET):
super().__init__(level)
def emit(self, record):
log_entry = self.format(record)
if record.message.startswith('> '):
sys.stdout.write('{}\r'.format(log_entry.rstrip()))
sys.stdout.flush()
else:
sys.stdout.write('{}\n'.format(log_entry))
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
fh = logging.FileHandler(os.path.join(args.model_dir, 'log.txt'))
fh.setLevel(logging.INFO)
ch = ProgressHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter(fmt='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
log.addHandler(fh)
log.addHandler(ch)
return args, log
def lr_decay(optimizer, lr_decay):
for param_group in optimizer.param_groups:
param_group['lr'] *= lr_decay
return optimizer
def load_data(opt):
with open('SQuAD/meta.msgpack', 'rb') as f:
meta = msgpack.load(f, encoding='utf8')
embedding = torch.Tensor(meta['embedding'])
opt['pretrained_words'] = True
opt['vocab_size'] = embedding.size(0)
opt['embedding_dim'] = embedding.size(1)
opt['pos_size'] = len(meta['vocab_tag'])
opt['ner_size'] = len(meta['vocab_ent'])
BatchGen.pos_size = opt['pos_size']
BatchGen.ner_size = opt['ner_size']
with open(opt['data_file'], 'rb') as f:
data = msgpack.load(f, encoding='utf8')
train = data['train']
data['dev'].sort(key=lambda x: len(x[1]))
dev = [x[:-1] for x in data['dev']]
dev_y = [x[-1] for x in data['dev']]
return train, dev, dev_y, embedding, opt
class BatchGen:
pos_size = None
ner_size = None
def __init__(self, data, batch_size, gpu, evaluation=False):
"""
input:
data - list of lists
batch_size - int
"""
self.batch_size = batch_size
self.eval = evaluation
self.gpu = gpu
# sort by len
data = sorted(data, key=lambda x: len(x[1]))
# chunk into batches
data = [data[i:i + batch_size] for i in range(0, len(data), batch_size)]
# shuffle
if not evaluation:
random.shuffle(data)
self.data = data
def __len__(self):
return len(self.data)
def __iter__(self):
for batch in self.data:
batch_size = len(batch)
batch = list(zip(*batch))
if self.eval:
assert len(batch) == 8
else:
assert len(batch) == 10
context_len = max(len(x) for x in batch[1])
context_id = torch.LongTensor(batch_size, context_len).fill_(0)
for i, doc in enumerate(batch[1]):
context_id[i, :len(doc)] = torch.LongTensor(doc)
feature_len = len(batch[2][0][0])
context_feature = torch.Tensor(batch_size, context_len, feature_len).fill_(0)
for i, doc in enumerate(batch[2]):
for j, feature in enumerate(doc):
context_feature[i, j, :] = torch.Tensor(feature)
context_tag = torch.Tensor(batch_size, context_len, self.pos_size).fill_(0)
for i, doc in enumerate(batch[3]):
for j, tag in enumerate(doc):
context_tag[i, j, tag] = 1
context_ent = torch.Tensor(batch_size, context_len, self.ner_size).fill_(0)
for i, doc in enumerate(batch[4]):
for j, ent in enumerate(doc):
context_ent[i, j, ent] = 1
question_len = max(len(x) for x in batch[5])
question_id = torch.LongTensor(batch_size, question_len).fill_(0)
for i, doc in enumerate(batch[5]):
question_id[i, :len(doc)] = torch.LongTensor(doc)
context_mask = torch.eq(context_id, 0)
question_mask = torch.eq(question_id, 0)
text = list(batch[6])
span = list(batch[7])
if not self.eval:
y_s = torch.LongTensor(batch[8])
y_e = torch.LongTensor(batch[9])
if self.gpu:
context_id = context_id.pin_memory()
context_feature = context_feature.pin_memory()
context_tag = context_tag.pin_memory()
context_ent = context_ent.pin_memory()
context_mask = context_mask.pin_memory()
question_id = question_id.pin_memory()
question_mask = question_mask.pin_memory()
if self.eval:
yield (context_id, context_feature, context_tag, context_ent, context_mask,
question_id, question_mask, text, span)
else:
yield (context_id, context_feature, context_tag, context_ent, context_mask,
question_id, question_mask, y_s, y_e, text, span)
def _normalize_answer(s):
def remove_articles(text):
return re.sub(r'\b(a|an|the)\b', ' ', text)
def white_space_fix(text):
return ' '.join(text.split())
def remove_punc(text):
exclude = set(string.punctuation)
return ''.join(ch for ch in text if ch not in exclude)
def lower(text):
return text.lower()
return white_space_fix(remove_articles(remove_punc(lower(s))))
def _exact_match(pred, answers):
if pred is None or answers is None:
return False
pred = _normalize_answer(pred)
for a in answers:
if pred == _normalize_answer(a):
return True
return False
def _f1_score(pred, answers):
def _score(g_tokens, a_tokens):
common = Counter(g_tokens) & Counter(a_tokens)
num_same = sum(common.values())
if num_same == 0:
return 0
precision = 1. * num_same / len(g_tokens)
recall = 1. * num_same / len(a_tokens)
f1 = (2 * precision * recall) / (precision + recall)
return f1
if pred is None or answers is None:
return 0
g_tokens = _normalize_answer(pred).split()
scores = [_score(g_tokens, _normalize_answer(a).split()) for a in answers]
return max(scores)
def score(pred, truth):
assert len(pred) == len(truth)
f1 = em = total = 0
for p, t in zip(pred, truth):
total += 1
em += _exact_match(p, t)
f1 += _f1_score(p, t)
em = 100. * em / total
f1 = 100. * f1 / total
return em, f1
if __name__ == '__main__':
main()