forked from ilsenatorov/rindti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretrain.py
51 lines (43 loc) · 1.84 KB
/
pretrain.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
from pytorch_lightning import Trainer, seed_everything
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from pytorch_lightning.loggers import TensorBoardLogger
from torch_geometric.loader import DataLoader
from rindti.data import PfamSampler, PreTrainDataset
from rindti.models import BGRLModel, GraphLogModel, InfoGraphModel, PfamModel
from rindti.utils import MyArgParser, read_config
models = {"graphlog": GraphLogModel, "infograph": InfoGraphModel, "pfam": PfamModel, "bgrl": BGRLModel}
def pretrain(**kwargs):
"""Run pretraining pipeline"""
seed_everything(kwargs["seed"])
dataset = PreTrainDataset(kwargs["data"])
## TODO need a more elegant solution for this
fams = {i.fam for i in dataset}
kwargs["fam_list"] = list(fams)
kwargs.update(dataset.config)
kwargs["feat_dim"] = 20
kwargs["edge_dim"] = 5
logger = TensorBoardLogger("tb_logs", name=kwargs["model"], default_hp_metric=False)
callbacks = [
ModelCheckpoint(monitor="train_loss", save_top_k=3, mode="min"),
EarlyStopping(monitor="train_loss", patience=kwargs["early_stop_patience"], mode="min"),
]
trainer = Trainer(
gpus=kwargs["gpus"],
callbacks=callbacks,
logger=logger,
max_epochs=kwargs["max_epochs"],
num_sanity_val_steps=0,
deterministic=False,
profiler=kwargs["profiler"],
)
model = models[kwargs["model"]](**kwargs)
dl = DataLoader(dataset, batch_size=kwargs["batch_size"], num_workers=kwargs["num_workers"], shuffle=True)
trainer.fit(model, dl)
if __name__ == "__main__":
from pprint import pprint
parser = MyArgParser(prog="Model Trainer")
parser.add_argument("config", type=str, help="Path to YAML config file")
args = parser.parse_args()
config = read_config(args.config)
pprint(config)
pretrain(**config)