-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.py
More file actions
70 lines (54 loc) · 2.03 KB
/
train.py
File metadata and controls
70 lines (54 loc) · 2.03 KB
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
import os
import logging
import hydra
from pyprojroot import here
import numpy as np
from pathlib import Path
import graph_tool
import torch
import pytorch_lightning as pl
from omegaconf import OmegaConf
from autograph.models.seq_models import SequenceModel
torch.backends.cuda.matmul.allow_tf32 = True # Default False in PyTorch 1.12+
torch.backends.cudnn.allow_tf32 = True # Default True
OmegaConf.register_new_resolver('eval', eval)
log = logging.getLogger(__name__)
@hydra.main(
version_base="1.3", config_path=str(here() / "configs"), config_name="train"
)
def main(cfg):
log.info(f"Configs:\n{OmegaConf.to_yaml(cfg)}")
pl.seed_everything(cfg.seed, workers=True)
if cfg.model.pretrained_path is None:
model = SequenceModel(cfg)
else:
log.info(f"Loading model from {cfg.model.pretrained_path}...")
model = SequenceModel.load_from_checkpoint(cfg.model.pretrained_path, model=cfg.model)
os.symlink(
Path(cfg.model.pretrained_path).resolve(),
Path(cfg.logs.path) / "pretrained.ckpt",
)
model.update_cfg(cfg)
datamodule = model._datamodule
logger = []
if cfg.wandb:
wandb_logger = pl.loggers.WandbLogger(project="AutoGraph", config=OmegaConf.to_container(cfg, resolve=True))
logger.append(wandb_logger)
logger.append(pl.loggers.CSVLogger(cfg.logs.path, name="csv_logs"))
model_ckpt_cls = pl.callbacks.ModelCheckpoint
callbacks = [
pl.callbacks.LearningRateMonitor(),
model_ckpt_cls(
monitor=f'val/{datamodule.val_metric[0]}',
dirpath=cfg.logs.path,
filename=cfg.model.model_name,
mode=f'{datamodule.val_metric[1]}',
)
]
trainer = hydra.utils.instantiate(cfg.trainer, logger=logger, callbacks=callbacks)
trainer.fit(model, datamodule)
trainer.save_checkpoint(f"{cfg.logs.path}/{cfg.model.model_name}-last.ckpt")
model.cfg.sampling.num_samples = -1 # balanced number
trainer.test(model, datamodule)
if __name__ == "__main__":
main()