-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
407 lines (314 loc) · 12.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
'''
Train the question generation model
'''
import argparse
import math
import time
import random
from tqdm import tqdm
import torch
import torch.nn.functional as F
import torch.optim as optim
import torch.utils.data
import transformer.Constants as Constants
from dataset import SEGDataset, paired_collate_fn
from transformer.Model import Transformer
from transformer.Optim import ScheduledOptim
import os
def cal_performance(pred, gold, smoothing=False):
'''
:param pred:
:param gold:
:param smoothing:
:return:
'''
''' use label smoothing if needed'''
loss = cal_loss(pred, gold, smoothing)
pred = pred.max(1)[1]
# print('pred:', pred[:10])
gold = gold.contiguous().view(-1)
non_pad_mask = gold.ne(Constants.PAD)
n_correct = pred.eq(gold)
n_correct = n_correct.masked_select(non_pad_mask).sum().item()
return loss, n_correct, pred
def cal_loss(pred, gold, smoothing):
'''
:param pred:
:param gold:
:param smoothing:
:return:
'''
''' Calculate cross entropy loss, apply label smoothing if needed. '''
gold = gold.contiguous().view(-1)
if smoothing:
eps = 0.1
n_class = pred.size(1)
one_hot = torch.zeros_like(pred).scatter(1, gold.view(-1, 1), 1)
one_hot = one_hot * (1 - eps) + (1 - one_hot) * eps / (n_class - 1)
log_prb = F.log_softmax(pred, dim=1)
non_pad_mask = gold.ne(Constants.PAD)
loss = -(one_hot * log_prb).sum(dim=1)
loss = loss.masked_select(non_pad_mask).sum() # average later
else:
loss = F.cross_entropy(pred, gold, ignore_index=Constants.PAD, reduction='sum')
return loss
def train_epoch(model, training_data, optimizer, device, smoothing):
'''
:param model:
:param training_data:
:param optimizer:
:param device:
:param smoothing:
:return:
'''
''' Train epoch'''
model.train()
total_loss = 0
n_word_total = 0
n_word_correct = 0
for batch in tqdm(
training_data, mininterval=2,
desc=' - (Training) ', leave=False):
# prepare data
src1_seq, src1_pos, \
src2_seq, src2_pos,\
src3_seq, src3_pos,\
src4_seq, src4_pos,\
src1_emo, \
src2_emo, \
src3_emo, \
src4_emo, \
tgt_seq, tgt_pos = map(lambda x: x.to(device), batch)
gold = tgt_seq[:, 1:]
src = src1_seq, src1_pos, src2_seq, src2_pos, src3_seq, src3_pos, src4_seq, src4_pos, \
src1_emo, src2_emo, src3_emo, src4_emo
# forward
optimizer.zero_grad()
pred = model(src, tgt_seq, tgt_pos)
# backward
loss, n_correct, prediction = cal_performance(pred, gold, smoothing=smoothing)
loss.backward()
# update parameters
optimizer.step_and_update_lr()
# note keeping
total_loss += loss.item()
non_pad_mask = gold.ne(Constants.PAD)
n_word = non_pad_mask.sum().item()
n_word_total += n_word
n_word_correct += n_correct
loss_per_word = total_loss/n_word_total
accuracy = n_word_correct/n_word_total
return loss_per_word, accuracy
def eval_epoch(model, validation_data, device, opt, epoch_i):
'''
:param model:
:param validation_data:
:param device:
:return:
'''
''' Evaluate each epoch'''
model.eval()
total_loss = 0
n_word_total = 0
n_word_correct = 0
data = torch.load(opt.data)
tgt_index2word = {idx: word for word, idx in data['dict']['tgt'].items()}
word_list = []
with torch.no_grad():
for batch in tqdm(validation_data, mininterval=2, desc=' - (Validation) ', leave=False):
# prepare data
src1_seq, src1_pos, \
src2_seq, src2_pos, \
src3_seq, src3_pos, \
src4_seq, src4_pos, \
src1_emo, \
src2_emo, \
src3_emo, \
src4_emo, \
tgt_seq, tgt_pos = map(lambda x: x.to(device), batch)
gold = tgt_seq[:, 1:]
src = src1_seq, src1_pos, src2_seq, src2_pos, src3_seq, src3_pos, src4_seq, src4_pos, \
src1_emo, src2_emo, src3_emo, src4_emo,\
# forward
# optimizer.zero_grad()
pred = model(src, tgt_seq, tgt_pos)
# tgt_index2word = validation_data.tgt_idx2word()
loss, n_correct, prediction = cal_performance(pred, gold, smoothing=False)
# print('batch_prediction:', prediction.shape)
prediction = prediction.data.cpu().numpy()
for pred in prediction:
word = tgt_index2word[pred]
word_list.append(word)
# note keeping
total_loss += loss.item()
non_pad_mask = gold.ne(Constants.PAD)
n_word = non_pad_mask.sum().item()
n_word_total += n_word
n_word_correct += n_correct
loss_per_word = total_loss/n_word_total
accuracy = n_word_correct/n_word_total
return loss_per_word, accuracy
def train(model, training_data, validation_data, optimizer, device, opt, seed):
'''
:param model:
:param training_data:
:param validation_data:
:param optimizer:
:param device:
:param opt:
:return:
'''
''' Start training '''
log_train_file = None
log_valid_file = None
if opt.log:
log_train_file = opt.log + str(seed) + '_train.log'
log_valid_file = opt.log + str(seed) + '_valid.log'
print('[Info] Training performance will be written to file: {} and {}'.format(
log_train_file, log_valid_file))
if not os.path.exists(opt.log):
os.mkdir(opt.log)
with open(log_train_file, 'w') as log_tf, open(log_valid_file, 'w') as log_vf:
log_tf.write('epoch, loss,ppl, accuracy\n')
log_vf.write('epoch, loss,ppl, accuracy\n')
valid_accus = []
for epoch_i in range(opt.epoch):
print('[ Epoch', epoch_i, ']')
start = time.time()
train_loss, train_accu = train_epoch(
model, training_data, optimizer, device, smoothing=opt.label_smoothing)
print('\n')
print(' - (Training) ppl: {ppl: 8.5f}, accuracy: {accu:3.3f} lr: {lr:.4f}%, '\
'elapse: {elapse:3.3f} min'.format(
ppl=math.exp(min(train_loss, 100)), accu=100*train_accu,
elapse=(time.time()-start)/60, lr=opt.lr))
start = time.time()
valid_loss, valid_accu = eval_epoch(model, validation_data, device, opt, epoch_i)
print('\n')
print(' - (Validation) ppl: {ppl: 8.5f}, accuracy: {accu:3.3f} lr: {lr:.4f}%, '\
'elapse: {elapse:3.3f} min'.format(
ppl=math.exp(min(valid_loss, 100)), accu=100*valid_accu,
elapse=(time.time()-start)/60, lr=opt.lr))
valid_accus += [valid_accu]
model_state_dict = model.state_dict()
checkpoint = {
'model': model_state_dict,
'settings': opt,
'epoch': epoch_i}
if not os.path.exists(opt.save_model):
os.mkdir(opt.save_model)
if opt.save_model:
if opt.save_mode == 'all':
model_name = opt.save_model + str(seed) + '_%.3f.ckpt' % math.exp(min(valid_loss, 100))
torch.save(checkpoint, model_name)
elif opt.save_mode == 'best':
model_name = opt.save_model + str(seed) + '_%.3f.ckpt' % math.exp(min(valid_loss, 100))
# if math.exp(min(valid_loss, 100)) >= max(math.exp(min(valid_loss, 100))):
# torch.save(checkpoint, model_name)
if valid_accu >= max(valid_accus):
torch.save(checkpoint, model_name)
print('\n')
print(' - [Info] The checkpoint file has been updated.')
if log_train_file and log_valid_file:
with open(log_train_file, 'a') as log_tf, open(log_valid_file, 'a') as log_vf:
log_tf.write('{epoch},{loss: 8.5f},{ppl: 8.5f},{accu:3.3f}\n'.format(
epoch=epoch_i, loss=train_loss,
ppl=math.exp(min(train_loss, 100)), accu=100*train_accu))
log_vf.write('{epoch},{loss: 8.5f},{ppl: 8.5f},{accu:3.3f}, {lr: .4f}\n'.format(
epoch=epoch_i, loss=valid_loss,
ppl=math.exp(min(valid_loss, 100)), accu=100*valid_accu, lr=opt.lr))
def main():
''' Main function '''
parser = argparse.ArgumentParser()
parser.add_argument('-data', type=str, default='data/final_gcn_data')
parser.add_argument('-epoch', type=int, default=20)
parser.add_argument('-batch_size', type=int, default=64)
parser.add_argument('-gpu', type=int, default=0)
parser.add_argument('-d_word_vec', type=int, default=300)
parser.add_argument('-d_model', type=int, default=300)
parser.add_argument('-d_inner', type=int, default=2048)
parser.add_argument('-d_k', type=int, default=64)
parser.add_argument('-d_v', type=int, default=64)
parser.add_argument('-n_head', type=int, default=6)
parser.add_argument('-n_layers', type=int, default=2)
parser.add_argument('-n_warmup_steps', type=int, default=400)
parser.add_argument('-dropout', type=float, default=0.1)
parser.add_argument('-embs_share_weight', action='store_true')
parser.add_argument('-proj_share_weight', action='store_true')
parser.add_argument('-finetune', type=bool, default=True)
parser.add_argument('-usepretrained', type=bool, default=True)
parser.add_argument('-use_pretrained_model', type=bool, default=False)
parser.add_argument('-lr', type=float, default=0.005)
parser.add_argument('-log', default='log/')
parser.add_argument('-save_model', default='ckpt/')
parser.add_argument('-save_mode', type=str, choices=['all', 'best'], default='best')
parser.add_argument('-no_cuda', action='store_true')
parser.add_argument('-label_smoothing', action='store_true')
# gcn setting
parser.add_argument('-hidden_size', type=int, default=300)
parser.add_argument('-flat_glimpses', type=int, default=1)
opt = parser.parse_args()
opt.cuda = not opt.no_cuda
opt.d_word_vec = opt.d_word_vec
os.environ["CUDA_VISIBLE_DEVICES"] = str(opt.gpu)
torch.backends.cudnn.enabled = True
#========= Loading Dataset =========#
data = torch.load(opt.data)
opt.max_src_token_seq_len = data['settings'].max_src_token_seq_len
opt.max_tgt_token_seq_len = data['settings'].max_tgt_token_seq_len
device = torch.device('cuda' if opt.cuda else 'cpu')
seed = random.randint(0, 999)
# seed = 1152
opt.seed = seed
# load the data from dataset
training_data, validation_data = prepare_dataloaders(data, opt)
opt.src_vocab_size = training_data.dataset.src_vocab_size
opt.tgt_vocab_size = training_data.dataset.tgt_vocab_size
# initialize the model
transformer = Transformer(opt, opt.src_vocab_size, opt.tgt_vocab_size).to(device)
if opt.embs_share_weight:
assert training_data.dataset.src_word2idx == training_data.dataset.tgt_word2idx, \
'The src/tgt word2idx table are different but asked to share word embedding.'
optimizer = ScheduledOptim(optim.Adam(
filter(lambda x: x.requires_grad, transformer.parameters()),
betas=(0.9, 0.98), eps=1e-09), opt)
print(opt)
train(transformer, training_data, validation_data, optimizer, device, opt, seed)
def prepare_dataloaders(data, opt):
# ========= Preparing DataLoader =========#
train_loader = torch.utils.data.DataLoader(
SEGDataset(
src_word2idx=data['dict']['src'],
tgt_word2idx=data['dict']['tgt'],
src1_insts=data['train']['src1'],
src2_insts=data['train']['src2'],
src3_insts=data['train']['src3'],
src4_insts=data['train']['src4'],
src1_emo=data['train']['adj1'],
src2_emo=data['train']['adj2'],
src3_emo=data['train']['adj3'],
src4_emo=data['train']['adj4'],
tgt_insts=data['train']['tgt'][0]),
num_workers=0,
batch_size=opt.batch_size,
collate_fn=paired_collate_fn,
shuffle=True)
valid_loader = torch.utils.data.DataLoader(
SEGDataset(
src_word2idx=data['dict']['src'],
tgt_word2idx=data['dict']['tgt'],
src1_insts=data['valid']['src1'],
src2_insts=data['valid']['src2'],
src3_insts=data['valid']['src3'],
src4_insts=data['valid']['src4'],
src1_emo=data['valid']['adj1'],
src2_emo=data['valid']['adj2'],
src3_emo=data['valid']['adj3'],
src4_emo=data['valid']['adj4'],
tgt_insts=data['valid']['tgt'][0]),
num_workers=0,
batch_size=opt.batch_size,
collate_fn=paired_collate_fn)
return train_loader, valid_loader
if __name__ == '__main__':
main()