-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
53 lines (42 loc) · 1.63 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
from pytorch_lightning.loops import Loop
from utils import get_batch, transfomer_mask
import torch
class TrainingEpochLoop(Loop):
def __init__(self, model, lossfunc, optimizer, data_iter, bptt, device="cpu", clip=0.25):
super().__init__()
self.model = model
self.optimizer = optimizer
self.data_iter = data_iter
self.batch_idx = 0
self.bptt = bptt
self.clip = clip
self.device = device
# Model
self.model.train()
# Loss function
self.loss_func = lossfunc
# transformer Mask
self.transf_mask = transfomer_mask(self.bptt).to(device)
@property
def done(self):
return self.batch_idx >= self.data_iter.size(0)-1
def reset(self) -> None:
self.data = self.data_iter
def advance(self, *args, **kwargs) -> None:
batch = get_batch(self.data, self.batch_idx)
batch[0], batch[1] = batch[0].to(self.device), batch[1].to(self.device)
batch_size = batch[0].size(0)
if batch_size != self.bptt: # only on last batch
self.transf_mask = self.transf_mask[:batch_size, :batch_size]
# Zero the gradients
self.optimizer.zero_grad()
# Call training step function: every call equals to a full loop over the dataset.
loss = self.model.training_step(
batch, self.transf_mask, self.loss_func)
self.batch_idx+=self.bptt
if self.batch_idx %10==0:
print(loss.item())
loss.backward()
# Clip the gradient
torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.clip)
self.optimizer.step()