-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightning_train.py
164 lines (126 loc) · 4.66 KB
/
lightning_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# System imports
import os
import sys
from pprint import pprint as pp
from time import time as tt
import inspect
# External imports
import matplotlib.pyplot as plt
import matplotlib.colors
from sklearn.decomposition import PCA
from sklearn.metrics import auc
import numpy as np
import pandas as pd
import seaborn as sns
import torch
from torch_geometric.data import Data
from torch_geometric.data import DataLoader
import argparse
from itertools import permutations
import pytorch_lightning as pl
from pytorch_lightning import LightningModule, Trainer
from pytorch_lightning.loggers import WandbLogger
from itertools import chain
import trackml.dataset
import ipywidgets as widgets
from ipywidgets import interact, interact_manual
# Pick up local packages
sys.path.append("..")
# Local imports
from prepare import select_hits
from toy_utils import *
from lightning_modules.embedding_scanner import Embedding_Model
# from models import *
# from trainers import *
# Get rid of RuntimeWarnings, gross
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning)
import wandb
device = "cuda" if torch.cuda.is_available() else "cpu"
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser("train.py")
add_arg = parser.add_argument
add_arg("config", nargs="?", default="configs/hello.yaml")
add_arg(
"-r", "--regime", nargs="+", help="Choose regime of [rp, hnm, ci]", default=None
)
add_arg("--hidden-dim", type=int, default=None, help="Hidden layer dimension size")
add_arg(
"--n-graph-iters", type=int, default=None, help="Number of graph iterations"
)
add_arg(
"--emb-dim",
type=int,
default=None,
help="Number of spatial embedding dimensions",
)
add_arg(
"--emb-hidden",
type=int,
default=None,
help="Number of embedding hidden dimensions",
)
add_arg("--nb-layer", type=int, default=None, help="Number of embedding layers")
add_arg("--r-val", type=float, default=None, help="Radius of graph construction")
add_arg("--r-train", type=float, default=None, help="Radius of embedding training")
add_arg("--margin", type=float, default=None, help="Radius of hinge loss")
add_arg("--weight", type=float, default=None, help="Positive weight in AGNN")
add_arg("--randomisation", type=float, default=None, help="Ratio of RP to truth")
add_arg(
"--warmup", type=float, default=None, help="Number of BATCHES to warmup upon"
)
add_arg("--train-size", type=int, default=None, help="Number of train population")
add_arg("--val-size", type=int, default=None, help="Number of validate population")
add_arg("--pt-cut", type=float, default=None, help="Cutoff for momentum")
add_arg("--adjacent", type=bool, default=False, help="Enforce adjacent layers?")
return parser.parse_args()
def setup_wandb(config):
hparams = config["hparams"]
wandb_logger = WandbLogger(
project=config["project"],
group="_".join(hparams["regime"]),
log_model=True,
save_dir=hparams["wandb_save_dir"],
)
wandb_logger.log_hyperparams(hparams)
wandb_logger.log_hyperparams(config["selection"])
return wandb_logger
def get_loaders(config):
# Dataset processing
pt_cut = config["selection"]["pt_min"]
train_number = config["selection"]["train_number"]
test_number = config["selection"]["test_number"]
load_dir = config["input_dir"]
# Construct experiment name
group = str(pt_cut) + "_pt_cut"
if config["selection"]["endcaps"]:
group += "_endcaps"
print("Running with dataset", group, "on device", device)
train_path = os.path.join(load_dir, group, str(train_number) + "_events_train")
test_path = os.path.join(load_dir, group, str(test_number) + "_events_test")
train_dataset = torch.load(train_path)
test_dataset = torch.load(test_path)
train_loader = DataLoader(train_dataset, batch_size=1, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=1, shuffle=True)
return train_loader, test_loader
def main(args):
with open(args.config) as f:
config = yaml.load(f, Loader=yaml.FullLoader)
train_loader, test_loader = get_loaders(config)
hparams = config["hparams"]
model = Embedding_Model(hparams)
wandb_logger = setup_wandb(config)
trainer = Trainer(
gpus=1,
limit_val_batches=0.1,
max_epochs=hparams["max_epochs"],
logger=wandb_logger,
progress_bar_refresh_rate=100,
)
trainer.fit(model, train_loader, test_loader)
if __name__ == "__main__":
# Parse the command line
args = parse_args()
# print(args)
main(args)