-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtrain.py
153 lines (132 loc) · 5.82 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
import os
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
import torch
from tqdm import tqdm
import numpy as np
import random
from model import Transformer
from utils import AverageMeter
from config import config
from dataset import Multi30kDe2En
SEED = 42
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
class Trainer:
def __init__(self, config):
# Configs & Parameters
self.config = config
self.src_vocab_size = config['src_vocab_size']
self.trg_vocab_size = config['trg_vocab_size']
self.ff_hid_dim = config['ff_hid_dim']
self.embed_dim = config['embed_dim']
self.n_blocks = config['n_blocks']
self.n_heads = config['n_heads']
self.max_length = config['max_length']
self.dropout = config['dropout']
self.device = config['device']
self.src_pad_idx = config['src_pad_idx']
self.trg_pad_idx = config['trg_pad_idx']
self.lr = config['lr']
self.clip = config['clip']
# Model
self.model = Transformer(self.src_vocab_size,
self.trg_vocab_size,
self.src_pad_idx,
self.trg_pad_idx,
self.embed_dim,
self.n_blocks,
self.n_heads,
self.ff_hid_dim,
self.max_length,
self.dropout,
self.device)
self._init_weights()
self.model.to(self.device)
# Optimizer
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=self.lr)
# Loss Function
self.criterion = nn.CrossEntropyLoss(ignore_index=self.trg_pad_idx)
self.criterion.to(self.device)
# Metrics
self.loss_tracker = AverageMeter('loss')
# Tensorboard
log_dir = os.path.join(self.config['log_dir'], self.config['name'])
self.writer = SummaryWriter(log_dir=log_dir)
def _init_weights(self):
for p in self.model.parameters():
if p.dim() > 1:
nn.init.xavier_uniform_(p)
def train(self, dataloader, epoch, total_epochs):
self.model.train()
self.loss_tracker.reset()
with tqdm(dataloader, unit="batch", desc=f'Epoch: {epoch}/{total_epochs} ',
bar_format='{desc:<16}{percentage:3.0f}%|{bar:70}{r_bar}', ascii=" #") as iterator:
for src, trg in iterator:
src, trg = src.to(self.device), trg.to(self.device)
output = self.model(src, trg[:, :-1])
output_dim = output.shape[-1]
output = output.contiguous().view(-1, output_dim)
trg = trg[:, 1:].contiguous().view(-1)
loss = self.criterion(output, trg)
loss.backward()
torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.clip)
self.optimizer.step()
self.optimizer.zero_grad()
self.loss_tracker.update(loss.item())
avg_loss = self.loss_tracker.avg
iterator.set_postfix(loss=avg_loss)
return avg_loss
def evaluate(self, dataloader):
self.model.eval()
self.loss_tracker.reset()
with tqdm(dataloader, unit="batch", desc=f'Evaluating... ',
bar_format='{desc:<16}{percentage:3.0f}%|{bar:70}{r_bar}', ascii=" #") as iterator:
with torch.no_grad():
for src, trg in iterator:
src, trg = src.to(self.device), trg.to(self.device)
output = self.model(src, trg[:, :-1])
output_dim = output.shape[-1]
output = output.contiguous().view(-1, output_dim)
trg = trg[:, 1:].contiguous().view(-1)
loss = self.criterion(output, trg)
self.loss_tracker.update(loss.item())
avg_loss = self.loss_tracker.avg
iterator.set_postfix(loss=avg_loss)
return avg_loss
def fit(self, train_loader, valid_loader, epochs):
for epoch in range(1, epochs + 1):
print()
train_loss = self.train(train_loader, epoch, epochs)
val_loss = self.evaluate(valid_loader)
# tensorboard
self.writer.add_scalar('train_loss', train_loss, epoch)
self.writer.add_scalar('val_loss', val_loss, epoch)
should_save_weights = lambda x: not bool(x % self.config['save_interval'])
if should_save_weights(epoch):
save_path = os.path.join(self.config['weights_dir'], f'{epoch}.pt')
torch.save(self.model.state_dict(), save_path)
print(f'Saved Model at {save_path}')
if __name__ == '__main__':
batch_size = config['train_batch_size']
train_dataset = Multi30kDe2En('train')
valid_dataset = Multi30kDe2En('valid')
train_loader = DataLoader(train_dataset,
batch_size=batch_size,
shuffle=True,
collate_fn=Multi30kDe2En.collate_fn)
valid_loader = DataLoader(valid_dataset,
batch_size=batch_size,
shuffle=True,
collate_fn=Multi30kDe2En.collate_fn)
config['src_vocab_size'] = len(train_dataset.de_vocab)
config['trg_vocab_size'] = len(train_dataset.en_vocab)
config['src_pad_idx'] = Multi30kDe2En.PAD_IDX
config['trg_pad_idx'] = Multi30kDe2En.PAD_IDX
trainer = Trainer(config)
trainer.fit(train_loader, valid_loader, config['epochs'])