forked from ChrisWu1997/PQ-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
66 lines (49 loc) · 1.91 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
from collections import OrderedDict
from tqdm import tqdm
from dataset import get_dataloader
from config import get_config
from util.utils import cycle
from agent import get_agent
def main():
# create experiment config
config = get_config('pqnet')('train')
# create network and training agent
tr_agent = get_agent(config)
# load from checkpoint if provided
if config.cont:
tr_agent.load_ckpt(config.ckpt)
# create dataloader
train_loader = get_dataloader('train', config)
val_loader = get_dataloader('val', config)
val_loader = cycle(val_loader)
# start training
clock = tr_agent.clock
for e in range(clock.epoch, config.nr_epochs):
# begin iteration
pbar = tqdm(train_loader)
for b, data in enumerate(pbar):
# train step
outputs, losses = tr_agent.train_func(data)
# visualize
if config.vis and clock.step % config.vis_frequency == 0:
tr_agent.visualize_batch(data, 'train', outputs=outputs)
pbar.set_description("EPOCH[{}][{}]".format(e, b))
pbar.set_postfix(OrderedDict({k: v.item() for k, v in losses.items()}))
# validation step
if clock.step % config.val_frequency == 0:
data = next(val_loader)
outputs, losses = tr_agent.val_func(data)
if config.vis and clock.step % config.vis_frequency == 0:
tr_agent.visualize_batch(data, 'validation', outputs=outputs)
clock.tick()
# update lr by scheduler
tr_agent.update_learning_rate()
# update teacher forcing ratio
if config.module == 'seq2seq':
tr_agent.update_teacher_forcing_ratio()
clock.tock()
if clock.epoch % config.save_frequency == 0:
tr_agent.save_ckpt()
tr_agent.save_ckpt('latest')
if __name__ == '__main__':
main()