forked from parshakova/APIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
356 lines (319 loc) · 16.4 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
import os
import sys
import random
import argparse
import time
from shutil import copyfile
from datetime import datetime
import msgpack
import numpy as np
import torch
from torch.autograd import Variable
from tensorboardX import SummaryWriter
from apip import utils
from apip.model import DocReaderModel
parser = argparse.ArgumentParser(
description='Train a Document Reader model.'
)
parser = utils.add_arguments(parser)
args = parser.parse_args()
if not args.drop_nn:
args.dropout_rate = 0.
if args.squad == 2:
if 'data2' in args.data_file:
args.data_file = 'SQuAD2/data2.msgpack'
else:
args.data_file = 'SQuAD2/data.msgpack'
# set model dir
model_dir = args.model_dir
os.makedirs(model_dir, exist_ok=True)
model_dir = os.path.abspath(model_dir)
timestamp = time.strftime("%mm%dd_%H%M%S")
print("timestamp {}".format(timestamp))
current_dir = os.path.join(args.model_dir, timestamp)
os.makedirs(current_dir)
torch.set_printoptions(precision=10)
# save model configuration
s = "\nParameters:\n"
for k in sorted(args.__dict__):
s += "{} = {} \n".format(k.lower(), args.__dict__[k])
with open(os.path.join(args.model_dir, timestamp, "about.txt"),"w") as txtf:
txtf.write(s); print(s)
if args.summary:
writer = SummaryWriter(log_dir=current_dir)
# set random seed
seed = args.seed if args.seed >= 0 else int(random.random()*1000)
print ('seed:', seed)
random.seed(seed)
torch.manual_seed(seed)
if args.cuda:
torch.cuda.manual_seed(seed)
log = utils.setup_logger(__name__, os.path.join(current_dir,args.log_file))
# batch validation size
bs_valid = 100
if args.n_actions > 8:
bs_valid = 50
def main():
log.info('[program starts.]')
train, dev, dev_y, train_y, embedding, opt, q_labels, ql_mask = utils.load_data_train(vars(args), args)
log.info('[Data loaded.ql_mask]')
log.info('vocab size = %d'%opt['vocab_size'])
with open(args.data_file, 'rb') as f:
data = msgpack.load(f, encoding='utf8')
dev_ae = list(data['dev_ans_exists'])
trn_ae = list(data['trn_ans_exists'])
#dev_ae = [1]*len(dev_y); trn_ae = [1]*len(train_y)
if args.resume:
log.info('[loading previous model...]')
checkpoint = torch.load(os.path.join(model_dir, args.restore_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
indices = list(range(len(train)))
for i in range(checkpoint['epoch']):
random.shuffle(indices) # synchronize random seed
train = [train[i] for i in indices]
trn_ae = [trn_ae[i] for i in indices]
train_y = [train_y[i] for i in indices]
q_labels = [q_labels[i] for i in indices]
ql_mask = [ql_mask[i] for i in indices]
if args.reduce_lr:
utils.lr_decay(model.optimizer, args.reduce_lr, log)
else:
model = DocReaderModel(opt, embedding)
epoch_0 = 1
train_y = np.array(train_y) # text answers for training set
q_labels = np.array(q_labels)
ql_mask = np.array(ql_mask)
print("timestamp {}".format(timestamp))
trn_eval_size = len(trn_ae)
dev_y = np.array(dev_y)
if args.cuda:
model.cuda()
# evaluate pre-trained model
if args.resume and not args.debug:
batches = utils.BatchGen(train[:trn_eval_size], batch_size=bs_valid, evaluation=True, gpu=args.cuda)
predictions = []; ae_ta = []
for batch in batches:
if args.squad == 2:
ans_b, _, _, ae_i = model.predict(batch)
ae_ta.extend(ae_i)
predictions.extend(ans_b)
else:
predictions.extend(model.predict(batch)[0])
em_t, f1_t = utils.score(predictions, train_y[:trn_eval_size])
if 'exist' in args.ae_archt:
em_t, f1_t = utils.score_list(predictions, train_y[:trn_eval_size], trn_ae[:trn_eval_size])
n_ae = sum(trn_ae[:trn_eval_size])
n_dae = trn_eval_size - n_ae
print('tot_pos=%d, true_pos=%d, cor_p=%d, cor_n=%d'%(sum(ae_ta), sum(trn_ae[:trn_eval_size]), \
(np.array(trn_ae[:trn_eval_size]).squeeze()*np.array(ae_ta).squeeze()).sum(),\
((np.array(trn_ae[:trn_eval_size]).squeeze()==0)*(np.array(ae_ta).squeeze()==0)).sum()))
log.info("[train EM: {0:.3f} F1: {1:3f}]".format(em_t, f1_t))
batches = utils.BatchGen(dev, batch_size=bs_valid, evaluation=True, gpu=args.cuda)
predictions = []; ae_ta = []
for batch in batches:
if args.squad == 2:
ans_b,_, _, ae_i = model.predict(batch)
ae_ta.extend(ae_i)
predictions.extend(ans_b)
else:
predictions.extend(model.predict(batch)[0])
em_v, f1_v = utils.score(predictions, dev_y)
if 'exist' in args.ae_archt:
em_v, f1_v = utils.score_list(predictions, np.array(dev_y), dev_ae)
n_ae = sum(dev_ae)
n_dae = len(dev_ae) - n_ae
print('tot_pos=%d, true_pos=%d, cor_p=%d, cor_n=%d'%(sum(ae_ta), sum(dev_ae), \
(np.array(dev_ae).squeeze()*np.array(ae_ta).squeeze()).sum(),\
((np.array(dev_ae).squeeze()==0)*(np.array(ae_ta).squeeze()==0)).sum()))
log.info("[val EM: {} F1: {}]".format(em_v, f1_v))
best_val_score = f1_v
if args.summary:
writer.add_scalars('accuracies', {'em_t':em_t, 'f1_t':f1_t, 'em_v':em_v, 'f1_v':f1_v}, epoch_0-1)
else:
best_val_score = 0.0
if 'const' in args.beta:
beta = float(args.beta.split('_')[1])*0.1
if 'const' in args.alpha:
alpha = float(args.alpha.split('_')[1])*0.1
scope = 'pi_q'
if args.select_i:
scope = 'select_i'
dummy_r = np.zeros(args.batch_size); latent_a = None; target_i=None; indices=None # induced interpretation
rewards = dummy_r
# training
for epoch in range(epoch_0, epoch_0 + args.epochs):
log.warn('Epoch {} timestamp {}'.format(epoch, timestamp))
batches = utils.BatchGen(train, batch_size=args.batch_size, gpu=args.cuda)
start = datetime.now()
if args.vae and not args.select_i:
scope = utils.select_scope_update(args, epoch-epoch_0)
print("scope = {} beta = {} alpha = {} ".format(scope, beta, alpha))
for i, batch in enumerate(batches):
inds = batches.indices[i]
# synchronize available interpretations with the current batch
labels = np.take(q_labels, inds, 0)
l_mask = np.take(ql_mask, inds, 0)
if args.vae: # VAE framework
if scope == 'rl':
if args.rl_tuning == 'pgm':
# policy gradient with EM scores for rewards
truth = np.take(train_y, inds, 0)
pred_m, latent_a, indices = model.predict(batch)[:3]
_, f1_m = utils.score_em(None, pred_m, truth)
rewards = f1_m
# normalize rewards over batch
rewards -= rewards.mean(); rewards /= (rewards.std()+1e-08)
elif args.rl_tuning == 'pg':
# policy gradient with F1 scores for rewards
truth = np.take(train_y, inds, 0)
pred_m, latent_a, indices = model.predict(batch)[:3]
_, f1_m = utils.score_sc(None, pred_m, truth)
rewards = f1_m
# normalize rewards over batch
rewards -= rewards.mean(); rewards /= (rewards.std()+1e-08)
elif args.rl_tuning == 'sc':
# reward computed by self-critic
truth = np.take(train_y, inds, 0)
pred_s, pred_m, latent_a, indices = model.predict_self_critic(batch)
rs, rm = utils.score_sc(pred_s, pred_m, truth)
rewards = rs - rm
else:
rewards = dummy_r
if args.select_i:
i_predictions = []
truth = np.take(train_y, batches.indices[i], 0)
for a in range(args.n_actions):
latent_a = Variable(torch.ones(batch[0].size(0))*a).long().cuda()
i_predictions.append(model.predict_inter(batch, latent_a=latent_a)[0])
f1_all = []
for b in range(batch[0].size(0)):
f1_v = []
for a in range(args.n_actions):
_, f1_a = utils.score_test_alli([i_predictions[a][b]], [truth[b]])
f1_v += [f1_a]
f1_all += [f1_v]
target_i = np.argmax(np.array(f1_all), 1)
model.update(batch, q_l=[labels, l_mask], r=rewards, scope=scope, beta=beta, alpha=alpha, \
latent_a=latent_a, target_i=target_i, span=indices)
elif args.self_critic:
# self-critic framework where rewards are computed as difference between the F1 score produced
# by the current model during greedy inference and by sampling
truth = np.take(train_y, inds, 0)
if args.critic_loss:
pred_m, latent_a, indices = model.predict(batch)[:3]
_, f1_m = utils.score_sc(None, pred_m, truth)
rewards = f1_m
else:
pred_s, pred_m, latent_a, indices = model.predict_self_critic(batch)
rs, rm = utils.score_sc(pred_s, pred_m, truth)
rewards = rs - rm
model.update(batch, r=rewards, q_l=[labels, l_mask], latent_a=latent_a)
else:
model.update(batch, q_l=[labels, l_mask])
if i % args.log_per_updates == 0:
# printing
if args.vae and not args.select_i:
log.info('updates[{0:6}] l_p[{1:.3f}] l_q[{2:.3f}] l_rl[{3:.3f}] l_ae[{4:.3f}] l_ce[{5:.3f}] l_cr[{6:.3f}] remaining[{7}]'.format(
model.updates, model.train_loss['p'].avg, model.train_loss['q'].avg, model.train_loss['rl'].avg, model.train_loss['ae'].avg,\
model.train_loss['ce'].avg, model.train_loss['cr'].avg, str((datetime.now() - start) / (i + 1) * (len(batches) - i - 1)).split('.')[0]))
if args.summary:
writer.add_scalars('losses', {'p':model.train_loss['p'].avg, 'q':model.train_loss['q'].avg, 'ce':model.train_loss['ce'].avg, \
'ae':model.train_loss['ae'].avg,'rl':model.train_loss['rl'].avg, 'cr':model.train_loss['cr'].avg,}, (epoch-1)*len(batches)+i)
else:
log.info('updates[{0:6}] train loss[{1:.5f}] remaining[{2}]'.format(
model.updates, model.train_loss.avg,
str((datetime.now() - start) / (i + 1) * (len(batches) - i - 1)).split('.')[0]))
if args.summary:
writer.add_scalar('loss', model.train_loss.avg, (epoch-1)*len(batches)+i)
if scope == 'rl' and (i % 4*args.log_per_updates == 0):
vbatches = utils.BatchGen(dev, batch_size=bs_valid, evaluation=True, gpu=args.cuda)
predictions = []
for batch in vbatches:
predictions.extend(model.predict(batch)[0])
em_v, f1_v = utils.score(predictions, dev_y)
log.warn("val EM: {0:.3f} F1: {1:3f}".format(em_v, f1_v))
# eval
if epoch % args.eval_per_epoch == 0:
batches = utils.BatchGen(dev, batch_size=bs_valid, evaluation=True, gpu=args.cuda)
predictions = []; ae_ta=[]
for i, batch in enumerate(batches):
if args.squad == 2:
ans_b, _, _, ae_i = model.predict(batch)
ae_ta.extend(ae_i)
predictions.extend(ans_b)
else:
predictions.extend(model.predict(batch)[0])
em_v, f1_v = utils.score(predictions, dev_y)
if 'exist' in args.ae_archt:
em_v, f1_v = utils.score_list(predictions, dev_y, dev_ae)
n_ae = sum(dev_ae[:trn_eval_size])
n_dae = len(dev_ae) - n_ae
print('tot_pos=%d, true_pos=%d, cor_p=%d, cor_n=%d'%(sum(ae_ta), sum(dev_ae), \
(np.array(dev_ae).squeeze()*np.array(ae_ta).squeeze()).sum(),\
((np.array(dev_ae).squeeze()==0)*(np.array(ae_ta).squeeze()==0)).sum()))
log.info("[val EM: {} F1: {}]".format(em_v, f1_v))
batches = utils.BatchGen(train[:trn_eval_size], batch_size=bs_valid, evaluation=True, gpu=args.cuda)
predictions = []; ae_ta = []
for batch in batches:
if args.squad == 2:
ans_b, _, _, ae_i = model.predict(batch)
ae_ta.extend(ae_i)
predictions.extend(ans_b)
else:
predictions.extend(model.predict(batch)[0])
em_t, f1_t = utils.score(predictions, train_y[:trn_eval_size])
if 'exist' in args.ae_archt:
em_t, f1_t = utils.score_list(predictions, train_y[:trn_eval_size], trn_ae[:trn_eval_size])
n_ae = sum(trn_ae[:trn_eval_size])
n_dae = trn_eval_size - n_ae
print('tot_pos=%d, true_pos=%d, cor_p=%d, cor_n=%d'%(sum(ae_ta), sum(trn_ae[:trn_eval_size]), \
(np.array(trn_ae[:trn_eval_size]).squeeze()*np.array(ae_ta).squeeze()).sum(),\
((np.array(trn_ae[:trn_eval_size]).squeeze()==0)*(np.array(ae_ta).squeeze()==0)).sum()))
log.info("[train EM: {0:.3f} F1: {1:3f}]".format(em_t, f1_t))
print("current_dir {}".format(current_dir))
if args.summary:
writer.add_scalars('accuracies', {'em_t':em_t, 'f1_t':f1_t, 'em_v':em_v, 'f1_v':f1_v}, epoch)
# save
if not args.save_last_only or epoch == epoch_0 + args.epochs - 1:
try:
os.remove(os.path.join(current_dir, 'checkpoint_epoch_{}.pt'.format(epoch-1)))
except OSError:
pass
model_file = os.path.join(current_dir, 'checkpoint_epoch_{}.pt'.format(epoch))
model.save(model_file, epoch)
if f1_v > best_val_score:
best_val_score = f1_v
copyfile(
model_file,
os.path.join(current_dir, 'best_model.pt'))
log.info('[new best model saved.]')
# load test data that is the development set
train, dev, dev_y, train_y, embedding, opt, q_labels, ql_mask = utils.load_data(vars(args), args)
batches = utils.BatchGen(dev, batch_size=bs_valid, evaluation=True, gpu=args.cuda)
predictions = []; ae_ta = []
for batch in batches:
if args.squad == 2:
ans_b,_, _, ae_i = model.predict(batch)
ae_ta.extend(ae_i)
predictions.extend(ans_b)
else:
predictions.extend(model.predict(batch)[0])
em_v, f1_v = utils.score(predictions, dev_y)
if 'exist' in args.ae_archt:
em_v, f1_v = utils.score_list(predictions, np.array(dev_y), dev_ae)
n_ae = sum(dev_ae)
n_dae = len(dev_ae) - n_ae
print('tot_pos=%d, true_pos=%d, cor_p=%d, cor_n=%d'%(sum(ae_ta), sum(dev_ae), \
(np.array(dev_ae).squeeze()*np.array(ae_ta).squeeze()).sum(),\
((np.array(dev_ae).squeeze()==0)*(np.array(ae_ta).squeeze()==0)).sum()))
log.info("[test EM: {} F1: {}]".format(em_v, f1_v))
if args.summary:
# export scalar data to JSON for external processing
writer.export_scalars_to_json(os.path.join(current_dir,"all_scalars.json"))
writer.close()
if __name__ == '__main__':
main()