-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtrain.py
executable file
·240 lines (191 loc) · 7.03 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
import sys
import json
import numpy as np
import logging
import argparse
import os
import time
import numpy as np
import glob
import torch
import torch.nn as nn
import torch.optim as optim
from tensorboardX import SummaryWriter
import src.evaluation as evaluation
from src.cuda import CUDA
import src.data as data
import src.models as models
parser = argparse.ArgumentParser()
parser.add_argument(
"--config",
help="path to json config",
required=True
)
parser.add_argument(
"--bleu",
help="do BLEU eval",
action='store_true'
)
parser.add_argument(
"--overfit",
help="train continuously on one batch of data",
action='store_true'
)
args = parser.parse_args()
config = json.load(open(args.config, 'r'))
working_dir = config['data']['working_dir']
if not os.path.exists(working_dir):
os.makedirs(working_dir)
config_path = os.path.join(working_dir, 'config.json')
if not os.path.exists(config_path):
with open(config_path, 'w') as f:
json.dump(config, f)
# set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
filename='%s/train_log' % working_dir,
)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
logging.info('Reading data ...')
src, tgt = data.read_nmt_data(
src=config['data']['src'],
config=config,
tgt=config['data']['tgt'],
attribute_vocab=config['data']['attribute_vocab'],
ngram_attributes=config['data']['ngram_attributes']
)
src_test, tgt_test = data.read_nmt_data(
src=config['data']['src_test'],
config=config,
tgt=config['data']['tgt_test'],
attribute_vocab=config['data']['attribute_vocab'],
ngram_attributes=config['data']['ngram_attributes'],
train_src=src,
train_tgt=tgt
)
logging.info('...done!')
batch_size = config['data']['batch_size']
max_length = config['data']['max_len']
src_vocab_size = len(src['tok2id'])
tgt_vocab_size = len(tgt['tok2id'])
weight_mask = torch.ones(tgt_vocab_size)
weight_mask[tgt['tok2id']['<pad>']] = 0
loss_criterion = nn.CrossEntropyLoss(weight=weight_mask)
if CUDA:
weight_mask = weight_mask.cuda()
loss_criterion = loss_criterion.cuda()
torch.manual_seed(config['training']['random_seed'])
np.random.seed(config['training']['random_seed'])
model = models.SeqModel(
src_vocab_size=src_vocab_size,
tgt_vocab_size=tgt_vocab_size,
pad_id_src=src['tok2id']['<pad>'],
pad_id_tgt=tgt['tok2id']['<pad>'],
config=config
)
logging.info('MODEL HAS %s params' % model.count_params())
model, start_epoch = models.attempt_load_model(
model=model,
checkpoint_dir=working_dir)
if CUDA:
model = model.cuda()
writer = SummaryWriter(working_dir)
if config['training']['optimizer'] == 'adam':
lr = config['training']['learning_rate']
optimizer = optim.Adam(model.parameters(), lr=lr)
elif config['training']['optimizer'] == 'sgd':
lr = config['training']['learning_rate']
optimizer = optim.SGD(model.parameters(), lr=lr)
else:
raise NotImplementedError("Learning method not recommend for task")
epoch_loss = []
start_since_last_report = time.time()
words_since_last_report = 0
losses_since_last_report = []
best_metric = 0.0
best_epoch = 0
cur_metric = 0.0 # log perplexity or BLEU
num_examples = min(len(src['content']), len(tgt['content']))
num_batches = num_examples / batch_size
STEP = 0
for epoch in range(start_epoch, config['training']['epochs']):
if cur_metric > best_metric:
# rm old checkpoint
for ckpt_path in glob.glob(working_dir + '/model.*'):
os.system("rm %s" % ckpt_path)
# replace with new checkpoint
torch.save(model.state_dict(), working_dir + '/model.%s.ckpt' % epoch)
best_metric = cur_metric
best_epoch = epoch - 1
losses = []
for i in range(0, num_examples, batch_size):
if args.overfit:
i = 50
batch_idx = i / batch_size
input_content, input_aux, output = data.minibatch(
src, tgt, i, batch_size, max_length, config['model']['model_type'])
input_lines_src, _, srclens, srcmask, _ = input_content
input_ids_aux, _, auxlens, auxmask, _ = input_aux
input_lines_tgt, output_lines_tgt, _, _, _ = output
decoder_logit, decoder_probs = model(
input_lines_src, input_lines_tgt, srcmask, srclens,
input_ids_aux, auxlens, auxmask)
optimizer.zero_grad()
loss = loss_criterion(
decoder_logit.contiguous().view(-1, tgt_vocab_size),
output_lines_tgt.view(-1)
)
losses.append(loss.item())
losses_since_last_report.append(loss.item())
epoch_loss.append(loss.item())
loss.backward()
norm = nn.utils.clip_grad_norm_(model.parameters(), config['training']['max_norm'])
writer.add_scalar('stats/grad_norm', norm, STEP)
optimizer.step()
if args.overfit or batch_idx % config['training']['batches_per_report'] == 0:
s = float(time.time() - start_since_last_report)
eps = (batch_size * config['training']['batches_per_report']) / s
avg_loss = np.mean(losses_since_last_report)
info = (epoch, batch_idx, num_batches, eps, avg_loss, cur_metric)
writer.add_scalar('stats/EPS', eps, STEP)
writer.add_scalar('stats/loss', avg_loss, STEP)
logging.info('EPOCH: %s ITER: %s/%s EPS: %.2f LOSS: %.4f METRIC: %.4f' % info)
start_since_last_report = time.time()
words_since_last_report = 0
losses_since_last_report = []
# NO SAMPLING!! because weird train-vs-test data stuff would be a pain
STEP += 1
if args.overfit:
continue
logging.info('EPOCH %s COMPLETE. EVALUATING...' % epoch)
start = time.time()
model.eval()
dev_loss = evaluation.evaluate_lpp(
model, src_test, tgt_test, config)
writer.add_scalar('eval/loss', dev_loss, epoch)
if args.bleu and epoch >= config['training'].get('inference_start_epoch', 1):
cur_metric, edit_distance, inputs, preds, golds, auxs = evaluation.inference_metrics(
model, src_test, tgt_test, config)
with open(working_dir + '/auxs.%s' % epoch, 'w') as f:
f.write('\n'.join(auxs) + '\n')
with open(working_dir + '/inputs.%s' % epoch, 'w') as f:
f.write('\n'.join(inputs) + '\n')
with open(working_dir + '/preds.%s' % epoch, 'w') as f:
f.write('\n'.join(preds) + '\n')
with open(working_dir + '/golds.%s' % epoch, 'w') as f:
f.write('\n'.join(golds) + '\n')
writer.add_scalar('eval/edit_distance', edit_distance, epoch)
writer.add_scalar('eval/bleu', cur_metric, epoch)
else:
cur_metric = dev_loss
model.train()
logging.info('METRIC: %s. TIME: %.2fs CHECKPOINTING...' % (
cur_metric, (time.time() - start)))
avg_loss = np.mean(epoch_loss)
epoch_loss = []
writer.close()