forked from lancopku/superAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
343 lines (290 loc) · 11.3 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
'''
@Author: Shuming Ma
@mail: [email protected]
@homepage : shumingma.com
'''
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.utils.data
import torch.backends.cudnn as cudnn
from torch.nn.utils.rnn import pack_padded_sequence as pack
from torch.utils.serialization import load_lua
import models
import data.dataloader as dataloader
import data.utils as utils
import data.dict as dict
from optims import Optim
#from predict import eval
import os
import argparse
import time
import math
import collections
import codecs
#config
parser = argparse.ArgumentParser(description='train.py')
parser.add_argument('-config', default='default.yaml', type=str,
help="config file")
parser.add_argument('-gpus', default=[], nargs='+', type=int,
help="Use CUDA on the listed devices.")
parser.add_argument('-restore', default='', type=str,
help="restore checkpoint")
parser.add_argument('-seed', type=int, default=1234,
help="Random seed")
parser.add_argument('-model', default='seq2seq', type=str,
help="Model selection")
parser.add_argument('-score', default='', type=str,
help="score_fn")
parser.add_argument('-pretrain', action='store_true',
help="load pretrain embedding")
parser.add_argument('-single_pass', action='store_true',
help="train or not")
parser.add_argument('-limit', type=int, default=0,
help="data limit")
parser.add_argument('-log', default='', type=str,
help="log directory")
parser.add_argument('-unk', action='store_true',
help="replace unk")
parser.add_argument('-reduce', action='store_true',
help="reduce redundancy")
parser.add_argument('-group', action='store_true',
help="group evaluation")
parser.add_argument('-loss', default='', type=str,
help="loss function")
parser.add_argument('-weight', type=float, default=0.0,
help="weight")
parser.add_argument('-update', type=int, default=0,
help="pretrain updates")
opt = parser.parse_args()
config = utils.read_config(opt.config)
torch.manual_seed(opt.seed)
#checkpoint
if opt.restore:
print('loading checkpoint...\n')
checkpoints = torch.load(opt.restore)
config = checkpoints['config']
#cuda
use_cuda = torch.cuda.is_available() and len(opt.gpus)>0
if use_cuda:
torch.cuda.set_device(opt.gpus[0])
torch.cuda.manual_seed(opt.seed)
#print(opt.gpus)
#os.environ["CUDA_VISIBLE_DEVICES"] = ",".join(opt.gpus)
#cudnn.benchmark = True
#data
print('loading data...\n')
start_time = time.time()
datas = torch.load(config.data)
print('loading time cost: %.3f' % (time.time()-start_time))
trainset, validset = datas['train'], datas['valid']
src_vocab, tgt_vocab = datas['dicts']['src'], datas['dicts']['tgt']
if not (hasattr(config, 'src_vocab') or hasattr(config, 'tgt_vocab')):
config.src_vocab = src_vocab.size()
config.tgt_vocab = tgt_vocab.size()
if opt.limit > 0:
trainset.src = trainset.src[:opt.limit]
validset = trainset
if hasattr(config, 'eval_batch_size'):
eval_batch_size = config.eval_batch_size
else:
eval_batch_size = config.batch_size
if 'copy' in opt.score:
padding = dataloader.pg_padding
else:
padding = dataloader.padding
trainloader = dataloader.get_loader(trainset, batch_size=config.batch_size, shuffle=True, num_workers=0, padding=padding)
validloader = dataloader.get_loader(validset, batch_size=eval_batch_size, shuffle=False, num_workers=0, padding=padding)
if opt.pretrain:
pretrain_embed = torch.load(config.emb_file)
else:
pretrain_embed = None
#model
print('building model...\n')
model = getattr(models, opt.model)(config, config.src_vocab, config.tgt_vocab, use_cuda,
w2v=pretrain_embed, score_fn=opt.score, weight=opt.weight, pretrain_updates=opt.update,
extend_vocab_size=tgt_vocab.size()-config.tgt_vocab, device_ids=opt.gpus)
if opt.restore:
model.load_state_dict(checkpoints['model'])
if use_cuda:
model.cuda()
if len(opt.gpus) > 1:
model = nn.DataParallel(model, device_ids=opt.gpus, dim=1)
model_module = model.module
else:
model_module = model
#optimizer
if opt.restore:
optim = checkpoints['optim']
else:
optim = Optim(config.optim, config.learning_rate, config.max_grad_norm,
lr_decay=config.learning_rate_decay,start_decay_at=config.start_decay_at)
optim.set_parameters(model.parameters())
param_count = 0
for param in model.parameters():
param_count += param.view(-1).size()[0]
#log
if not os.path.exists(config.log):
os.mkdir(config.log)
if opt.log == '':
log_path = config.log + str(int(time.time() * 1000)) + '/'
else:
log_path = config.log + opt.log + '/'
if not os.path.exists(log_path):
os.mkdir(log_path)
logging = utils.logging(log_path+'log.txt')
for k, v in config.items():
logging("%s:\t%s\n" % (str(k), str(v)))
logging("\n")
logging(repr(model)+"\n\n")
logging('total number of parameters: %d\n\n' % param_count)
logging('score function is %s\n\n' % opt.score)
#checkpoint
if opt.restore:
updates = checkpoints['updates']
else:
updates = 0
total_loss, start_time = 0, time.time()
report_total, report_correct = 0, 0
scores = [[] for metric in config.metric]
scores = collections.OrderedDict(zip(config.metric, scores))
#train
def train(epoch):
model.train()
if opt.model == 'gated':
model.current_epoch = epoch
global updates, total_loss, start_time, report_correct, report_total, report_tot_vocab, report_vocab
for batch in trainloader:
model.zero_grad()
src, src_len, tgt, tgt_len = batch['src'], batch['src_len'], batch['tgt'], batch['tgt_len']
if 'num_oovs' in batch.keys():
num_oovs = batch['num_oovs']
#print(num_oovs)
else:
num_oovs = 0
loss, num_total, num_correct = model.train_model(src, src_len, tgt, tgt_len, opt.loss, updates, optim, num_oovs=num_oovs)
total_loss += loss
report_correct += num_correct
report_total += num_total
#optim.step()
utils.progress_bar(updates, config.eval_interval)
updates += 1
if updates % config.eval_interval == 0:
logging("epoch: %3d, ppl: %6.3f, time: %6.3f, updates: %8d, accuracy: %2.2f\n"
% (epoch, math.exp(total_loss / report_total), time.time()-start_time, updates,
report_correct * 100.0 / report_total))
print('evaluating after %d updates...\r' % updates)
score = eval(epoch)
for metric in config.metric:
scores[metric].append(score[metric])
if score[metric] >= max(scores[metric]):
save_model(log_path+'best_'+metric+'_checkpoint.pt')
if metric == 'bleu':
with codecs.open(log_path+'best_'+metric+'_prediction.txt','w','utf-8') as f:
f.write(codecs.open(log_path+'candidate.txt','r','utf-8').read())
model.train()
total_loss, start_time = 0, time.time()
report_correct, report_total = 0, 0
report_vocab, report_tot_vocab = 0, 0
if updates % config.save_interval == 0:
save_model(log_path+'checkpoint.pt')
optim.updateLearningRate(score=0, epoch=epoch)
#evaluate
def eval(epoch):
model.eval()
reference, candidate, source, alignments = [], [], [], []
count, total_count = 0, len(validset)
for batch in validloader:
raw_src, src, src_len, raw_tgt, tgt, tgt_len = \
batch['raw_src'], batch['src'], batch['src_len'], batch['raw_tgt'], batch['tgt'], batch['tgt_len']
if 'num_oovs' in batch.keys():
num_oovs = batch['num_oovs']
oovs = batch['oovs']
else:
num_oovs = 0
oovs = None
if config.beam_size == 1:
samples, alignment = model.sample(src, src_len, num_oovs=num_oovs)
else:
samples, alignment = model.beam_sample(src, src_len, beam_size=config.beam_size)
if oovs is not None:
candidate += [tgt_vocab.convertToLabels(s, dict.EOS, oovs=oov) for s, oov in zip(samples, oovs)]
else:
candidate += [tgt_vocab.convertToLabels(s, dict.EOS) for s in samples]
source += raw_src
reference += raw_tgt
alignments += [align for align in alignment]
count += len(raw_src)
utils.progress_bar(count, total_count)
if opt.unk:
###replace unk
cands = []
for s, c, align in zip(source, candidate, alignments):
cand = []
for word, idx in zip(c, align):
if word == dict.UNK_WORD and idx < len(s):
try:
cand.append(s[idx])
except:
cand.append(word)
print("%d %d\n" % (len(s), idx))
else:
cand.append(word)
cands.append(cand)
candidate = cands
score = {}
if hasattr(config,'convert'):
candidate = utils.convert_to_char(candidate)
reference = utils.convert_to_char(reference)
if 'bleu' in config.metric:
result = utils.eval_bleu(reference, candidate, log_path, config)
score['bleu'] = float(result.split()[2][:-1])
logging(result)
if 'rouge' in config.metric:
result = utils.eval_rouge(reference, candidate, log_path)
try:
score['rouge'] = result['F_measure'][0]
logging("F_measure: %s Recall: %s Precision: %s\n"
% (str(result['F_measure']), str(result['recall']), str(result['precision'])))
#optim.updateLearningRate(score=score['rouge'], epoch=epoch)
except:
logging("Failed to compute rouge score.\n")
score['rouge'] = 0.0
if 'multi_rouge' in config.metric:
result = utils.eval_multi_rouge(reference, candidate, log_path)
try:
score['multi_rouge'] = result['F_measure'][0]
logging("F_measure: %s Recall: %s Precision: %s\n"
% (str(result['F_measure']), str(result['recall']), str(result['precision'])))
except:
logging("Failed to compute rouge score.\n")
score['multi_rouge'] = 0.0
if 'SARI' in config.metric:
result = utils.eval_SARI(source, reference, candidate, log_path, config)
logging("SARI score is: %.2f\n" % result)
score['SARI'] = result
return score
def save_model(path):
global updates
#model_state_dict = model_module.state_dict() if len(opt.gpus) > 1 else model.state_dict()
model_state_dict = model.state_dict()
checkpoints = {
'model': model_state_dict,
'config': config,
'optim': optim,
'updates': updates}
torch.save(checkpoints, path)
def main():
for i in range(1, config.epoch+1):
if not opt.single_pass:
train(i)
else:
eval(i)
return
for metric in config.metric:
logging("Best %s score: %.2f\n" % (metric, max(scores[metric])))
with open(log_path+metric+'.txt', 'w') as f:
for i, score in enumerate(scores[metric]):
f.write(str(i)+','+str(score)+'\n')
if __name__ == '__main__':
main()