forked from snap-stanford/GraphGym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
76 lines (64 loc) · 2.6 KB
/
example.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
import logging
import time
import torch
from graphgym.checkpoint import clean_ckpt, load_ckpt, save_ckpt
from graphgym.config import cfg
from graphgym.loss import compute_loss
from graphgym.register import register_train
from graphgym.utils.epoch import is_ckpt_epoch, is_eval_epoch
def train_epoch(logger, loader, model, optimizer, scheduler):
model.train()
time_start = time.time()
for batch in loader:
optimizer.zero_grad()
batch.to(torch.device(cfg.device))
pred, true = model(batch)
loss, pred_score = compute_loss(pred, true)
loss.backward()
optimizer.step()
logger.update_stats(true=true.detach().cpu(),
pred=pred_score.detach().cpu(),
loss=loss.item(),
lr=scheduler.get_last_lr()[0],
time_used=time.time() - time_start,
params=cfg.params)
time_start = time.time()
scheduler.step()
def eval_epoch(logger, loader, model):
model.eval()
time_start = time.time()
for batch in loader:
batch.to(torch.device(cfg.device))
pred, true = model(batch)
loss, pred_score = compute_loss(pred, true)
logger.update_stats(true=true.detach().cpu(),
pred=pred_score.detach().cpu(),
loss=loss.item(),
lr=0,
time_used=time.time() - time_start,
params=cfg.params)
time_start = time.time()
def train_example(loggers, loaders, model, optimizer, scheduler):
start_epoch = 0
if cfg.train.auto_resume:
start_epoch = load_ckpt(model, optimizer, scheduler)
if start_epoch == cfg.optim.max_epoch:
logging.info('Checkpoint found, Task already done')
else:
logging.info('Start from epoch {}'.format(start_epoch))
num_splits = len(loggers)
for cur_epoch in range(start_epoch, cfg.optim.max_epoch):
train_epoch(loggers[0], loaders[0], model, optimizer, scheduler)
loggers[0].write_epoch(cur_epoch)
if is_eval_epoch(cur_epoch):
for i in range(1, num_splits):
eval_epoch(loggers[i], loaders[i], model)
loggers[i].write_epoch(cur_epoch)
if is_ckpt_epoch(cur_epoch):
save_ckpt(model, optimizer, scheduler, cur_epoch)
for logger in loggers:
logger.close()
if cfg.train.ckpt_clean:
clean_ckpt()
logging.info('Task done, results saved in {}'.format(cfg.out_dir))
register_train('example', train_example)