-
Notifications
You must be signed in to change notification settings - Fork 4
/
train.py
195 lines (153 loc) · 5.66 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
# -*- coding: utf-8 -*-
import argparse
import math
import os
import dill
from collections import OrderedDict
from tqdm import tqdm
from torchtext import data
from torchtext import datasets
from torchtext.vocab import Vectors
import torch
import torch.nn as nn
import torch.optim as optim
from options import train_opts
from options import model_opts
from model import Seq2seqAttn
class Trainer(object):
def __init__(
self, model, criterion, optimizer, scheduler, clip):
self.model = model
self.criterion = criterion
self.optimizer = optimizer
self.scheduler = scheduler
self.clip = clip
self.n_updates = 0
def get_lr(self):
return self.optimizer.param_groups[0]['lr']
def step(self, samples, tf_ratio):
self.optimizer.zero_grad()
bsz = samples.src.size(1)
outs = self.model(samples.src, samples.tgt, tf_ratio)
loss = self.criterion(outs.view(-1, outs.size(2)), samples.tgt.view(-1))
if self.model.training:
loss.backward()
nn.utils.clip_grad_norm_(self.model.parameters(), self.clip)
self.optimizer.step()
self.n_updates += 1
return loss
def save_model(save_vars, filename):
model_path = os.path.join(args.savedir, filename)
torch.save(save_vars, model_path)
def save_vocab(savedir, fields):
name, field = fields
save_path = os.path.join(savedir, f"{name}_vocab.txt")
with open(save_path, 'w') as fout:
for w in field.vocab.itos:
fout.write(w + '\n')
def save_field(savedir, fields):
name, field = fields
save_path = os.path.join(savedir, f"{name}.field")
with open(save_path, 'wb') as fout:
dill.dump(field, fout)
def main(args):
device = torch.device('cuda' if args.gpu else 'cpu')
# load data and construct vocabulary dictionary
SRC = data.Field(lower=True)
TGT = data.Field(lower=True, eos_token='<eos>')
fields = [('src', SRC), ('tgt', TGT)]
train_data = data.TabularDataset(
path=args.train,
format='tsv',
fields=fields,
)
valid_data = data.TabularDataset(
path=args.valid,
format='tsv',
fields=fields,
)
SRC.build_vocab(train_data, min_freq=args.src_min_freq)
TGT.build_vocab(train_data, min_freq=args.tgt_min_freq)
if not os.path.exists(args.savedir):
os.mkdir(args.savedir)
# save field and vocabulary
for field in fields:
save_field(args.savedir, field)
save_vocab(args.savedir, field)
# set iterator
train_iter, valid_iter = data.BucketIterator.splits(
(train_data, valid_data),
batch_size=args.batch_size,
sort_within_batch=True,
sort_key= lambda x: len(x.src),
repeat=False,
device=device
)
model = Seq2seqAttn(args, fields, device).to(device)
print(model)
print('')
criterion = nn.CrossEntropyLoss(ignore_index=TGT.vocab.stoi['<pad>'])
optimizer = optim.SGD(model.parameters(), lr=args.lr)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min')
trainer = Trainer(model, criterion, optimizer, scheduler, args.clip)
epoch = 1
max_epoch = args.max_epoch or math.inf
max_update = args.max_update or math.inf
best_loss = math.inf
while epoch < max_epoch and trainer.n_updates < max_update \
and args.min_lr < trainer.get_lr():
# training
with tqdm(train_iter, dynamic_ncols=True) as pbar:
train_loss = 0.0
trainer.model.train()
for samples in pbar:
bsz = samples.src.size(1)
loss = trainer.step(samples, args.tf_ratio)
train_loss += loss.item()
# setting of progressbar
pbar.set_description(f"epoch {str(epoch).zfill(3)}")
progress_state = OrderedDict(
loss=loss.item(),
ppl=math.exp(loss.item()),
bsz=len(samples),
lr=trainer.get_lr(),
clip=args.clip,
num_updates=trainer.n_updates)
pbar.set_postfix(progress_state)
train_loss /= len(train_iter)
print(f"| epoch {str(epoch).zfill(3)} | train ", end="")
print(f"| loss {train_loss:.{4}} ", end="")
print(f"| ppl {math.exp(train_loss):.{4}} ", end="")
print(f"| lr {trainer.get_lr():.1e} ", end="")
print(f"| clip {args.clip} ", end="")
print(f"| num_updates {trainer.n_updates} |")
# validation
valid_loss = 0.0
trainer.model.eval()
for samples in valid_iter:
bsz = samples.src.size(1)
loss = trainer.step(samples, tf_ratio=0.0)
valid_loss += loss.item()
valid_loss /= len(valid_iter)
print(f"| epoch {str(epoch).zfill(3)} | valid ", end="")
print(f"| loss {valid_loss:.{4}} ", end="")
print(f"| ppl {math.exp(valid_loss):.{4}} ", end="")
print(f"| lr {trainer.get_lr():.1e} ", end="")
print(f"| clip {args.clip} ", end="")
print(f"| num_updates {trainer.n_updates} |")
# saving model
save_vars = {"train_args": args,
"state_dict": model.state_dict()}
if valid_loss < best_loss:
best_loss = valid_loss
save_model(save_vars, 'checkpoint_best.pt')
save_model(save_vars, "checkpoint_last.pt")
# update
trainer.scheduler.step(valid_loss)
epoch += 1
if __name__ == '__main__':
parser = argparse.ArgumentParser('')
train_opts(parser)
model_opts(parser)
args = parser.parse_args()
main(args)