forked from iotayo/aivivn-tone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
330 lines (267 loc) · 13.6 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
import os
import gc
import random
import torch
import dill
import torch.nn as nn
import numpy as np
from torch.optim import Adam
from torchtext.data import BucketIterator
from dataset import Seq2SeqDataset, PAD, tgt_field_name
from model import Encoder, Decoder, Seq2SeqConcat
from cyclic_lr import CyclicLR
from visualization import Visualization
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
class Trainer:
def __init__(self, src_vocab, tgt_vocab,
max_len=300, hidden_size=300, n_layers=2, clip=5, n_epochs=30):
# hyper-parameters
self.max_len = max_len
self.hidden_size = hidden_size
self.n_layers = n_layers
self.clip = clip
self.n_epochs = n_epochs
# vocab
self.src_vocab = src_vocab
self.tgt_vocab = tgt_vocab
self.pad_idx = self.src_vocab.stoi[PAD]
# prepare model
self.encoder = Encoder(self.src_vocab, self.max_len, self.hidden_size, self.n_layers)
self.decoder = Decoder(self.tgt_vocab, self.max_len, self.hidden_size * 2, self.n_layers)
self.reverse_decoder = Decoder(self.tgt_vocab, self.max_len, self.hidden_size * 2, self.n_layers, reverse=True)
self.model = Seq2SeqConcat(self.encoder, self.decoder, self.reverse_decoder, self.pad_idx)
self.model.to(device)
print(self.model)
print("Total parameters:", sum([p.nelement() for p in self.model.parameters()]))
# initialize weights
for name, param in self.model.named_parameters():
if "lstm.bias" in name:
# set lstm forget gate to 1 (Jozefowicz et al., 2015)
n = param.size(0)
param.data[n//4:n//2].fill_(1.0)
elif "lstm.weight" in name:
nn.init.xavier_uniform_(param)
# prepare loss function; don't calculate loss on PAD tokens
self.criterion = nn.NLLLoss(ignore_index=self.pad_idx)
# prepare optimizer and scheduler
self.optimizer = Adam(self.model.parameters())
self.scheduler = CyclicLR(self.optimizer, base_lr=0.00001, max_lr=0.00005,
step_size_up=4000, step_size_down=4000,
mode="triangular", gamma=1.0, cycle_momentum=False)
# book keeping vars
self.global_iter = 0
self.global_numel = []
self.global_loss = []
self.global_acc = []
# visualization
self.vis_loss = Visualization(env_name="aivivn_tone", xlabel="step", ylabel="loss", title="loss (mean per 300 steps)")
self.vis_acc = Visualization(env_name="aivivn_tone", xlabel="step", ylabel="acc", title="training accuracy (mean per 300 steps)")
def train(self, train_iterator, val_iterator, start_epoch=0, print_every=100):
for epoch in range(start_epoch, self.n_epochs):
self._train_epoch(epoch, train_iterator, train=True, print_every=print_every)
self.save(epoch)
# evaluate on validation set after each epoch
with torch.no_grad():
self._train_epoch(epoch, val_iterator, train=False, print_every=print_every)
def train_in_parts(self, train_parts, val, val_iterator, batch_size, start_epoch=0, print_every=100):
for epoch in range(start_epoch, self.n_epochs):
# shuffle data each epoch
random.shuffle(train_parts)
for train_src_, train_tgt_ in train_parts:
# create train dataset
print("Training part [{}] with target [{}]...".format(train_src_, train_tgt_))
train_ = Seq2SeqDataset.from_file(train_src_, train_tgt_, share_fields_from=val)
# create iterator
train_iterator_ = BucketIterator(dataset=train_, batch_size=batch_size,
sort=False, sort_within_batch=True,
sort_key=lambda x: len(x.src),
shuffle=True, device=device)
# train
self._train_epoch(epoch, train_iterator_, train=True, print_every=print_every)
# clean
del train_
del train_iterator_
gc.collect()
# save
self.save(epoch)
# evaluate on validation set after each epoch
with torch.no_grad():
self._train_epoch(epoch, val_iterator, train=False, print_every=print_every)
def resume(self, train_iterator, val_iterator, save_path):
checkpoint = torch.load(save_path)
self.model.load_state_dict(checkpoint["model_state_dict"])
self.model.to(device)
self.optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
self.scheduler.load_state_dict(checkpoint["scheduler_state_dict"])
start_epoch = checkpoint["epoch"] + 1
self.train(train_iterator, val_iterator, start_epoch)
def resume_in_parts(self, train_parts, val, val_iterator, batch_size, save_path):
checkpoint = torch.load(save_path)
self.model.load_state_dict(checkpoint["model_state_dict"])
self.model.to(device)
self.optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
self.scheduler.load_state_dict(checkpoint["scheduler_state_dict"])
start_epoch = checkpoint["epoch"] + 1
self.train_in_parts(train_parts, val, val_iterator, batch_size, start_epoch=start_epoch)
def _train_epoch(self, epoch, batch_iterator, train=True, print_every=100):
if train:
self.model.train()
else:
self.model.eval()
print("***Evaluating on validation set***")
total_loss = 0
total_correct = 0
total_numel = 0
total_iter = 0
num_batch = len(batch_iterator)
for i, batch in enumerate(batch_iterator):
# forward propagation
# (batch, seq_len, tgt_vocab_size)
if train:
# crude annealing teacher forcing
teacher_forcing = 0.5
if epoch == 0:
teacher_forcing = max(0.5, (num_batch - total_iter) / num_batch)
output, reverse_output, combined_output = self.model(batch, mask_softmax=0.5, teacher_forcing=teacher_forcing)
else:
output, reverse_output, combined_output = self.model(batch, mask_softmax=1.0, teacher_forcing=1.0)
# (batch, seq_len)
target = getattr(batch, tgt_field_name)
# reshape to calculate loss
output = output.view(-1, output.size(-1))
reverse_output = reverse_output.view(-1, reverse_output.size(-1))
combined_output = combined_output.view(-1, combined_output.size(-1))
target = target.view(-1)
# calculate loss
loss = self.criterion(output, target) + self.criterion(reverse_output, target) + self.criterion(combined_output, target)
# backprop
if train:
self.optimizer.zero_grad()
loss.backward()
nn.utils.clip_grad_norm_(self.model.parameters(), self.clip)
self.optimizer.step()
self.scheduler.step()
# calculate accuracy
correct = output.argmax(dim=-1).eq(target).sum().item()
r_correct = reverse_output.argmax(dim=-1).eq(target).sum().item()
c_correct = combined_output.argmax(dim=-1).eq(target).sum().item()
# summarize for each batch
total_loss += loss.item()
total_correct += c_correct
total_numel += target.numel()
total_iter += 1
# add to global summary
if train:
self.global_iter += 1
self.global_numel.append(target.numel())
self.global_loss.append(loss.item())
self.global_acc.append(c_correct)
# visualize
if self.global_iter == 1:
self.vis_loss.plot_line(self.global_loss[0], 1)
self.vis_acc.plot_line(self.global_acc[0]/total_numel, 1)
# update graph every 10 iterations
if self.global_iter % 10 == 0:
# moving average of most recent 300 losses
moving_avg_loss = sum(self.global_loss[max(0, len(self.global_loss)-300):]) / min(300.0, self.global_iter)
moving_avg_acc = sum(self.global_acc[max(0, len(self.global_acc) - 300):]) / sum(self.global_numel[max(0, len(self.global_numel) - 300):])
# visualize
self.vis_loss.plot_line(moving_avg_loss, self.global_iter)
self.vis_acc.plot_line(moving_avg_acc, self.global_iter)
# print
if i % print_every == 0:
template = "epoch = {} iter = {} loss = {:5.3f} correct = {:6.3f} r_correct = {:6.3f} c_correct = {:6.3f}"
print(template.format(epoch,
i,
loss.item(),
correct / target.numel() * 100.0,
r_correct / target.numel() * 100.0,
c_correct / target.numel() * 100.0))
# summarize for each epoch
template = "EPOCH = {} AVG_LOSS = {:5.3f} AVG_CORRECT = {:6.3f}\n"
print(template.format(epoch,
total_loss / total_iter,
total_correct / total_numel * 100.0))
def save(self, epoch, save_path="checkpoint"):
torch.save({
"epoch": epoch,
"model_state_dict": self.model.state_dict(),
"optimizer_state_dict": self.optimizer.state_dict(),
"scheduler_state_dict": self.scheduler.state_dict(),
"global_iter": self.global_iter
}, os.path.join(save_path, "aivivn_tone.model.ep{}".format(epoch)))
def set_seeds(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def load_data(train_src, train_tgt, val_src, val_tgt, batch_size=64, save_path="checkpoint"):
# prepare dataset
print("Reading data...")
train = Seq2SeqDataset.from_file(train_src, train_tgt)
print("Building vocab...")
train.build_vocab(max_size=300)
val = Seq2SeqDataset.from_file(val_src, val_tgt, share_fields_from=train)
src_vocab = train.src_field.vocab
tgt_vocab = train.tgt_field.vocab
# save vocab
with open(os.path.join(save_path, "vocab.src"), "wb") as f:
dill.dump(src_vocab, f)
with open(os.path.join(save_path, "vocab.tgt"), "wb") as f:
dill.dump(tgt_vocab, f)
print("Source vocab size:", len(src_vocab))
print("Target vocab size:", len(tgt_vocab))
# data iterator
# keep sort=False and shuffle=False to speed up training and reduce memory usage
train_iterator = BucketIterator(dataset=train, batch_size=batch_size,
sort=False, sort_within_batch=True,
sort_key=lambda x: len(x.src),
shuffle=False, device=device)
val_iterator = BucketIterator(dataset=val, batch_size=batch_size, train=False,
sort=False, sort_within_batch=True,
sort_key=lambda x: len(x.src),
shuffle=False, device=device)
return src_vocab, tgt_vocab, train_iterator, val_iterator
def load_data_in_parts(train_src, train_tgt, val_src, val_tgt, batch_size=64, save_path="checkpoint"):
# prepare dataset
print("Reading data...")
val = Seq2SeqDataset.from_file(val_src, val_tgt)
print("Building vocab...")
val.build_vocab(max_size=300)
src_vocab = val.src_field.vocab
tgt_vocab = val.tgt_field.vocab
# save vocab
with open(os.path.join(save_path, "vocab.src"), "wb") as f:
dill.dump(src_vocab, f)
with open(os.path.join(save_path, "vocab.tgt"), "wb") as f:
dill.dump(tgt_vocab, f)
print("Source vocab size:", len(src_vocab))
print("Target vocab size:", len(tgt_vocab))
# data iterator
# keep sort=False and shuffle=False to speed up training and reduce memory usage
val_iterator = BucketIterator(dataset=val, batch_size=batch_size, train=False,
sort=False, sort_within_batch=True,
sort_key=lambda x: len(x.src),
shuffle=False, device=device)
return src_vocab, tgt_vocab, list(zip(train_src, train_tgt)), val, val_iterator, batch_size
if __name__ == "__main__":
train_src = ["data/train.src.0", "data/train.src.1", "data/train.src.2", "data/train.src.3"]
train_tgt = ["data/train.tgt.0", "data/train.tgt.1", "data/train.tgt.2", "data/train.tgt.3"]
val_src = "data/val.src"
val_tgt = "data/val.tgt"
# src_vocab_path = "checkpoint/vocab.src"
# tgt_vocab_path = "checkpoint/vocab.tgt"
# set random seeds
set_seeds(420)
# load vocab
# with open(src_vocab_path, "rb") as f:
# src_vocab = dill.load(f)
# with open(tgt_vocab_path, "rb") as f:
# tgt_vocab = dill.load(f)
# load data
src_vocab, tgt_vocab, train_parts, val, val_iterator, batch_size = load_data_in_parts(train_src, train_tgt, val_src, val_tgt)
# prepare trainer
trainer = Trainer(src_vocab, tgt_vocab)
# train
trainer.train_in_parts(train_parts, val, val_iterator, batch_size)
# trainer.resume_in_parts(train_parts, val, val_iterator, batch_size, save_path="checkpoint/aivivn_tone.model.ep19")