-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunner.py
executable file
·145 lines (98 loc) · 4.41 KB
/
runner.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
import warnings
import json
import time
import torch
import os
import random
from torch_geometric.utils import add_remaining_self_loops
from sklearn.metrics import accuracy_score
from config import load_arguments
from data import DatasetLoader
from models import EuclideanGNNModel, SPDGNNModel
from pytorchtools import EarlyStopping
class Runner(object):
def __init__(self, model, optimizer, data, args):
self.model = model
self.optimizer = optimizer
self.data = data
self.train_mask = self.data.train_mask
self.val_mask = self.data.val_mask
self.test_mask = self.data.test_mask
self.args = args
self.loss_function = torch.nn.CrossEntropyLoss()
def train_epoch(self):
model.train()
optimizer.zero_grad()
pred_y = model(self.data.x, self.data.edge_index)
loss = self.loss_function(pred_y[self.train_mask], self.data.y[self.train_mask])
loss.backward()
optimizer.step()
tr_loss = loss.item()
return tr_loss
def evaluate(self, mask):
self.model.eval()
with torch.no_grad():
pred_y = model(self.data.x, self.data.edge_index)
loss = self.loss_function(pred_y[mask], self.data.y[mask])
acc = accuracy_score(pred_y[mask].argmax(dim=1).cpu(), self.data.y[mask].cpu())
return loss, acc
def run(self):
checkpoint_path = f'save/model.pt'
early_stopping = EarlyStopping(patience=args.patience, verbose=False, path=checkpoint_path)
for epoch in range(args.epoch):
start = time.perf_counter()
train_loss = self.train_epoch()
exec_time = time.perf_counter() - start
if epoch % self.args.val_every == 0:
val_loss, val_acc = self.evaluate(self.val_mask)
print(f'Epoch {epoch} | train loss: {train_loss:.4f} | total time: {int(exec_time)} secs | valid loss: {val_loss:.4f} | valid acc: {val_acc * 100:.2f}')
early_stopping(val_loss, self.model)
if early_stopping.early_stop:
break
model.load_state_dict(torch.load(checkpoint_path))
model.eval()
test_loss, test_acc = self.evaluate(self.test_mask)
print(f"Final Results | Accuracy: {test_acc * 100:.2f}")
def setup_envs(seed=-1):
warnings.filterwarnings("ignore")
if seed == -1: seed = random.randint(1, 1000)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.set_default_tensor_type(torch.DoubleTensor)
args = load_arguments()
args.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
return args
def build_dataset(args):
data = DatasetLoader(args, os.path.join('data', args.dataset))
data.to(args.device)
args.num_node_features = data.num_node_features
args.num_classes = data.y.max().item() + 1
if len(data.train_mask.shape) > 1: # When dataset contains cross-validation splits, we only use the first one.
data.train_mask = data.train_mask[:, 0]
data.test_mask = data.test_mask[:, 0]
#A = A + Id
data.edge_index, _ = add_remaining_self_loops(data.edge_index)
return data
def load_hyperparameters(args):
with open(f'json/{args.dataset}.json',) as f:
parameters = json.load(f)[args.model]
args.learningrate = parameters.get('learningrate', None)
args.dropout = parameters.get('dropout', None)
args.weight_decay = parameters.get('weight_decay', None)
args.nonlinear = parameters.get('nonlinear', None)
args.hidden_dims = parameters.get('hidden_dims', None)
return args
def GNNFactory(type_str, args):
classes = {
"spd": SPDGNNModel,
"euclidean": EuclideanGNNModel
}
return classes[type_str](args).to(args.device)
if __name__ == "__main__":
args = setup_envs(seed=42)
args = load_hyperparameters(args)
dataset = build_dataset(args)
model = GNNFactory(args.manifold, args)
optimizer = torch.optim.Adam(model.parameters(), lr=args.learningrate, weight_decay=args.weight_decay, amsgrad=False)
runner = Runner(model, optimizer, dataset, args)
runner.run()