-
Notifications
You must be signed in to change notification settings - Fork 106
/
train.py
213 lines (171 loc) · 7.72 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
import argparse
import os
import time
import torch
from tensorboardX import SummaryWriter
from tqdm import tqdm
from dataset import problem
from utils.optimizer import LRScheduler
from utils import utils
def summarize_train(writer, global_step, last_time, model, opt,
inputs, targets, optimizer, loss, pred, ans):
if opt.summary_grad:
for name, param in model.named_parameters():
if not param.requires_grad:
continue
norm = torch.norm(param.grad.data.view(-1))
writer.add_scalar('gradient_norm/' + name, norm,
global_step)
writer.add_scalar('input_stats/batch_size',
targets.size(0), global_step)
if inputs is not None:
writer.add_scalar('input_stats/input_length',
inputs.size(1), global_step)
i_nonpad = (inputs != opt.src_pad_idx).view(-1).type(torch.float32)
writer.add_scalar('input_stats/inputs_nonpadding_frac',
i_nonpad.mean(), global_step)
writer.add_scalar('input_stats/target_length',
targets.size(1), global_step)
t_nonpad = (targets != opt.trg_pad_idx).view(-1).type(torch.float32)
writer.add_scalar('input_stats/target_nonpadding_frac',
t_nonpad.mean(), global_step)
writer.add_scalar('optimizer/learning_rate',
optimizer.learning_rate(), global_step)
writer.add_scalar('loss', loss.item(), global_step)
acc = utils.get_accuracy(pred, ans, opt.trg_pad_idx)
writer.add_scalar('training/accuracy',
acc, global_step)
steps_per_sec = 100.0 / (time.time() - last_time)
writer.add_scalar('global_step/sec', steps_per_sec,
global_step)
def train(train_data, model, opt, global_step, optimizer, t_vocab_size,
label_smoothing, writer):
model.train()
last_time = time.time()
pbar = tqdm(total=len(train_data.dataset), ascii=True)
for batch in train_data:
inputs = None
if opt.has_inputs:
inputs = batch.src
targets = batch.trg
pred = model(inputs, targets)
pred = pred.view(-1, pred.size(-1))
ans = targets.view(-1)
loss = utils.get_loss(pred, ans, t_vocab_size,
label_smoothing, opt.trg_pad_idx)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if global_step % 100 == 0:
summarize_train(writer, global_step, last_time, model, opt,
inputs, targets, optimizer, loss, pred, ans)
last_time = time.time()
pbar.set_description('[Loss: {:.4f}]'.format(loss.item()))
global_step += 1
pbar.update(targets.size(0))
pbar.close()
train_data.reload_examples()
return global_step
def validation(validation_data, model, global_step, t_vocab_size, val_writer,
opt):
model.eval()
total_loss = 0.0
total_cnt = 0
for batch in validation_data:
inputs = None
if opt.has_inputs:
inputs = batch.src
targets = batch.trg
with torch.no_grad():
pred = model(inputs, targets)
pred = pred.view(-1, pred.size(-1))
ans = targets.view(-1)
loss = utils.get_loss(pred, ans, t_vocab_size, 0,
opt.trg_pad_idx)
total_loss += loss.item() * len(batch)
total_cnt += len(batch)
val_loss = total_loss / total_cnt
print("Validation Loss", val_loss)
val_writer.add_scalar('loss', val_loss, global_step)
return val_loss
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--problem', required=True)
parser.add_argument('--train_step', type=int, default=200)
parser.add_argument('--batch_size', type=int, default=4096)
parser.add_argument('--max_length', type=int, default=100)
parser.add_argument('--n_layers', type=int, default=6)
parser.add_argument('--hidden_size', type=int, default=512)
parser.add_argument('--filter_size', type=int, default=2048)
parser.add_argument('--warmup', type=int, default=16000)
parser.add_argument('--val_every', type=int, default=5)
parser.add_argument('--dropout', type=float, default=0.1)
parser.add_argument('--label_smoothing', type=float, default=0.1)
parser.add_argument('--model', type=str, default='transformer')
parser.add_argument('--output_dir', type=str, default='./output')
parser.add_argument('--data_dir', type=str, default='./data')
parser.add_argument('--no_cuda', action='store_true')
parser.add_argument('--parallel', action='store_true')
parser.add_argument('--summary_grad', action='store_true')
opt = parser.parse_args()
device = torch.device('cpu' if opt.no_cuda else 'cuda')
if not os.path.exists(opt.output_dir + '/last/models'):
os.makedirs(opt.output_dir + '/last/models')
if not os.path.exists(opt.data_dir):
os.makedirs(opt.data_dir)
train_data, validation_data, i_vocab_size, t_vocab_size, opt = \
problem.prepare(opt.problem, opt.data_dir, opt.max_length,
opt.batch_size, device, opt)
if i_vocab_size is not None:
print("# of vocabs (input):", i_vocab_size)
print("# of vocabs (target):", t_vocab_size)
if opt.model == 'transformer':
from model.transformer import Transformer
model_fn = Transformer
elif opt.model == 'fast_transformer':
from model.fast_transformer import FastTransformer
model_fn = FastTransformer
if os.path.exists(opt.output_dir + '/last/models/last_model.pt'):
print("Load a checkpoint...")
last_model_path = opt.output_dir + '/last/models'
model, global_step = utils.load_checkpoint(last_model_path, device,
is_eval=False)
else:
model = model_fn(i_vocab_size, t_vocab_size,
n_layers=opt.n_layers,
hidden_size=opt.hidden_size,
filter_size=opt.filter_size,
dropout_rate=opt.dropout,
share_target_embedding=opt.share_target_embedding,
has_inputs=opt.has_inputs,
src_pad_idx=opt.src_pad_idx,
trg_pad_idx=opt.trg_pad_idx)
model = model.to(device=device)
global_step = 0
if opt.parallel:
print("Use", torch.cuda.device_count(), "GPUs")
model = torch.nn.DataParallel(model)
num_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print("# of parameters: {}".format(num_params))
optimizer = LRScheduler(
filter(lambda x: x.requires_grad, model.parameters()),
opt.hidden_size, opt.warmup, step=global_step)
writer = SummaryWriter(opt.output_dir + '/last')
val_writer = SummaryWriter(opt.output_dir + '/last/val')
best_val_loss = float('inf')
for t_step in range(opt.train_step):
print("Epoch", t_step)
start_epoch_time = time.time()
global_step = train(train_data, model, opt, global_step,
optimizer, t_vocab_size, opt.label_smoothing,
writer)
print("Epoch Time: {:.2f} sec".format(time.time() - start_epoch_time))
if t_step % opt.val_every != 0:
continue
val_loss = validation(validation_data, model, global_step,
t_vocab_size, val_writer, opt)
utils.save_checkpoint(model, opt.output_dir + '/last/models',
global_step, val_loss < best_val_loss)
best_val_loss = min(val_loss, best_val_loss)
if __name__ == '__main__':
main()