forked from graphnet-team/graphnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model.py
153 lines (132 loc) · 4.12 KB
/
train_model.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
import os.path
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import EarlyStopping
from pytorch_lightning.loggers import WandbLogger
import torch
from torch.optim.adam import Adam
from graphnet.components.loss_functions import LogCoshLoss
from graphnet.data.constants import FEATURES, TRUTH
from graphnet.data.utils import get_equal_proportion_neutrino_indices
from graphnet.models import Model
from graphnet.models.detector.icecube import IceCubeDeepCore
from graphnet.models.gnn import DynEdge
from graphnet.models.graph_builders import KNNGraphBuilder
from graphnet.models.task.reconstruction import EnergyReconstruction
from graphnet.models.training.callbacks import ProgressBar, PiecewiseLinearLR
from graphnet.models.training.utils import (
get_predictions,
make_train_validation_dataloader,
save_results,
)
from graphnet.utilities.logging import get_logger
logger = get_logger()
# Configurations
torch.multiprocessing.set_sharing_strategy("file_system")
# Constants
features = FEATURES.DEEPCORE
truth = TRUTH.DEEPCORE
# Initialise Weights & Biases (W&B) run
wandb_logger = WandbLogger(
project="example-script",
entity="graphnet-team",
save_dir="./wandb/",
log_model=True,
)
# Main function definition
def main():
logger.info(f"features: {features}")
logger.info(f"truth: {truth}")
# Configuration
config = {
"db": "/groups/icecube/asogaard/data/sqlite/dev_lvl7_robustness_muon_neutrino_0000/data/dev_lvl7_robustness_muon_neutrino_0000.db",
"pulsemap": "SRTTWOfflinePulsesDC",
"batch_size": 512,
"num_workers": 10,
"gpus": [1],
"target": "energy",
"n_epochs": 5,
"patience": 5,
}
archive = "/groups/icecube/asogaard/gnn/results/"
run_name = "dynedge_{}_example".format(config["target"])
# Log configuration to W&B
wandb_logger.experiment.config.update(config)
# Common variables
train_selection, _ = get_equal_proportion_neutrino_indices(config["db"])
train_selection = train_selection[0:50000]
(
training_dataloader,
validation_dataloader,
) = make_train_validation_dataloader(
config["db"],
train_selection,
config["pulsemap"],
features,
truth,
batch_size=config["batch_size"],
num_workers=config["num_workers"],
)
# Building model
detector = IceCubeDeepCore(
graph_builder=KNNGraphBuilder(nb_nearest_neighbours=8),
)
gnn = DynEdge(
nb_inputs=detector.nb_outputs,
)
task = EnergyReconstruction(
hidden_size=gnn.nb_outputs,
target_labels=config["target"],
loss_function=LogCoshLoss(),
transform_prediction_and_target=torch.log10,
)
model = Model(
detector=detector,
gnn=gnn,
tasks=[task],
optimizer_class=Adam,
optimizer_kwargs={"lr": 1e-03, "eps": 1e-03},
scheduler_class=PiecewiseLinearLR,
scheduler_kwargs={
"milestones": [
0,
len(training_dataloader) / 2,
len(training_dataloader) * config["n_epochs"],
],
"factors": [1e-2, 1, 1e-02],
},
scheduler_config={
"interval": "step",
},
)
# Training model
callbacks = [
EarlyStopping(
monitor="val_loss",
patience=config["patience"],
),
ProgressBar(),
]
trainer = Trainer(
gpus=config["gpus"],
max_epochs=config["n_epochs"],
callbacks=callbacks,
log_every_n_steps=1,
logger=wandb_logger,
)
try:
trainer.fit(model, training_dataloader, validation_dataloader)
except KeyboardInterrupt:
logger.warning("[ctrl+c] Exiting gracefully.")
pass
# Saving predictions to file
results = get_predictions(
trainer,
model,
validation_dataloader,
[config["target"] + "_pred"],
additional_attributes=[config["target"], "event_no"],
)
save_results(config["db"], run_name, results, archive, model)
# Main function call
if __name__ == "__main__":
main()