-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
172 lines (149 loc) · 4.67 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
import os
from argparse import ArgumentParser
import torch
import yaml
from torch import nn
from torch.optim import Adam, AdamW
from torch.optim.lr_scheduler import _LRScheduler
from torch.optim.optimizer import Optimizer
from torch.utils.data.dataloader import DataLoader
from tqdm import tqdm
from yaml import Loader
from Transformer.criteration import CrossEntropyWithLabelSmoothing
from Transformer.data import prepare_dataloader
from Transformer.handle import (
TransformerLrScheduler,
handle_device,
ensure_reproducibility,
init_train_options,
)
from Transformer.models import Transformer
def train(
epoch: int,
update_freq: int,
model: nn.Module,
criteration: nn.Module,
train_data: DataLoader,
valid_data: DataLoader,
optim: Optimizer,
scheduler: _LRScheduler,
save_dir: str,
device: torch.device,
):
total_loss = 0
total_sample = 0
model.train()
optim.zero_grad()
nll_loss = 0
training_epoch = 0
for ind, samples in enumerate(tqdm(train_data)): # Training
samples = samples.to(device).get_batch()
ind = ind + 1
loss, logging_info = criteration(model, **samples)
sample_size = logging_info["valid tokens num"]
nll_loss += logging_info["nll_loss"]
training_epoch += 1
loss.backward()
if ind % update_freq == 0:
optim.step()
scheduler.step()
optim.zero_grad()
total_loss += float(loss)
total_sample += int(sample_size)
if (ind // update_freq) % 100 == 0 and ind % update_freq == 0:
total_loss = float(total_loss) / total_sample
nll_loss = float(nll_loss) / total_sample
print(
f"Epoch: {epoch} Training loss: {total_loss} nll loss: {nll_loss} ppl: {2**nll_loss} lr: {float(optim.param_groups[0]['lr'])}"
)
total_loss = 0
total_sample = 0
nll_loss = 0
training_epoch = 0
with torch.no_grad(): # Validating
total_loss = 0
total_sample = 0
nll_loss = 0
model.eval()
for samples in tqdm(valid_data):
samples = samples.to(device).get_batch()
loss, logging_info = criteration(model, **samples)
sample_size = logging_info["valid tokens num"]
nll_loss += logging_info["nll_loss"]
training_epoch += 1
total_loss += loss
total_sample += sample_size
total_loss = float(total_loss) / total_sample
nll_loss = float(nll_loss) / total_sample
print(
f"Epoch: {epoch} Valid loss: {total_loss} nll loss: {nll_loss} ppl: {2**nll_loss}"
)
with open(os.path.join(save_dir, f"epoch{epoch}.pt"), "wb") as fl:
torch.save(model, fl)
def trainer(args):
device = handle_device(args)
ensure_reproducibility(args.seed)
save_dir = args.save_dir.strip()
if save_dir == "":
save_dir = "checkpoint"
os.makedirs(save_dir, exist_ok=True)
valid_data, vocab_info = prepare_dataloader(
args.data,
args.src_lang,
args.tgt_lang,
"valid",
args.max_tokens,
args.batching_strategy,
not args.batching_short_first,
)
with open(args.model_config, "r", encoding="utf-8") as model_config:
model_dict = yaml.load(model_config, Loader=Loader)
model = Transformer(vocab_info, **model_dict).to(device)
train_data, _ = prepare_dataloader(
args.data,
args.src_lang,
args.tgt_lang,
"train",
args.max_tokens,
args.batching_strategy,
not args.batching_short_first,
)
print(model)
if args.optim == "adam":
optim = Adam(
model.parameters(),
lr=args.lr,
betas=args.adam_betas,
eps=args.adam_eps,
weight_decay=args.weight_decay,
)
else:
optim = AdamW(
model.parameters(),
lr=args.lr,
betas=args.adam_betas,
eps=args.adam_eps,
weight_decay=args.weight_decay,
)
scheduler = TransformerLrScheduler(
optim, model_dict["model_dim"], args.warmup_steps
)
criteration = CrossEntropyWithLabelSmoothing(args.label_smoothing_eps)
for epoch in range(args.epoch):
train(
epoch,
args.update_freq,
model,
criteration,
train_data,
valid_data,
optim,
scheduler,
save_dir,
device,
)
if __name__ == "__main__":
parser = ArgumentParser()
parser = init_train_options(parser)
args = parser.parse_args()
trainer(args)