-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphNN_GraphSAGE.py
225 lines (191 loc) · 7.53 KB
/
graphNN_GraphSAGE.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 5 16:19:11 2023
test PG implementation of GraphSAGE convolution, for graph classification
@author: hari
"""
import torch
# dataset
from torch_geometric.data import InMemoryDataset
from torch_geometric.loader import DataLoader # graph batches
from torch_geometric.transforms import ToDevice
# NN model definition
from torch_geometric.nn.conv import SAGEConv
import torch.nn.functional as F
# readout layer for graph class
from torch_geometric.nn import global_mean_pool
# from torch_geometric.nn.aggr import SoftmaxAggregation
from joblib import Parallel, delayed
import datetime as dt
import os.path as osp
from tqdm import tqdm
# configuration and logging
import tomllib
from pprint import pprint
import argparse
# %% load parameters from toml file
parser = argparse.ArgumentParser()
parser.add_argument("configFilePath")
tmp = parser.parse_args()
print(tmp.configFilePath)
with open(tmp.configFilePath,'rb') as f:
ARGS = tomllib.load(f)
# %% load dataset (to device)
composition = ToDevice(ARGS['device'])
dataset = InMemoryDataset(transform= composition)
dataset.load(osp.join(ARGS['root_folder'], ARGS['input_name'],
'processed', 'data.pt'))
# %% define ML model
class GCN(torch.nn.Module):
def __init__(self):
# TODO tomls all parameters of the
super().__init__()
hDim = ARGS['conv2']['hidden_dim']
# layer 1
self.conv1 = SAGEConv(in_channels= dataset.num_node_features,
out_channels= hDim if hDim else dataset.num_classes,
aggr= ARGS['conv1']['aggr'],
)
if ARGS['conv1']['dropout']:
self.drop1= F.dropout
if ARGS['conv1']['activation']:
self.activ1= F.relu
# layer 2
if hDim:
self.conv2 = SAGEConv(in_channels= hDim,
out_channels= dataset.num_classes,
aggr= ARGS['conv1']['aggr'],
)
if ARGS['conv2']['dropout']:
self.drop2= F.dropout
if ARGS['conv2']['activation']:
self.activ2= F.relu
def forward(self, data):
x, edge_index, batch = data.x, data.edge_index, data.batch
# 1. node embeddings
x = self.conv1(x, edge_index)
if hasattr(self, 'drop1'):
x = self.drop1(x, p=ARGS['conv1']['dropout'], training=self.training)
if hasattr(self, 'activ1'):
x = self.activ1(x)
if hasattr(self, 'conv2'):
x = self.conv2(x, edge_index)
if hasattr(self, 'drop2'):
x = self.drop2(x, p=ARGS['conv2']['dropout'], training=self.training)
if hasattr(self, 'activ2'):
x = self.activ2(x)
# 2. Readout layer (graph classification)
# TODO is there only this batch function ?
x = global_mean_pool(x, batch)
# 3. final classifier
return F.log_softmax(x, dim=1)
def trainLoss(model, optimizer, criterion, loader):
"""
loader data needs to be already in the correct device
"""
model.train()
# Iterate in batches over the training dataset.
lossMean = 0
for data in loader:
optimizer.zero_grad() # Clear gradients.
# Perform a single forward pass.
out = model(data)
# Compute the loss.
loss = criterion(out, data.y)
lossMean += loss/len(loader)
# Derive gradients.
loss.backward()
# Update parameters based on gradients.
optimizer.step()
# FIXME loss from multiple batches ? mean ?
return lossMean
def testAccuracy(model,loader):
"""
loader data needs to be already in the correct device
"""
model.eval()
count = 0
# Iterate in batches over the training/test dataset.
for data in loader:
# compute model prediction
out = model(data)
# Use the class with highest probability.
pred = out.argmax(dim=1)
# Check against ground-truth labels.
count += int((pred == data.y).sum())
# Compute ratio of correct predictions.
return count / len(loader.dataset)
def modelValidate(model, dataset)-> torch.tensor:
model.eval()
tmpLoader = DataLoader(dataset, batch_size=1, shuffle=False)
out= torch.cat([model(data) for data in tmpLoader], dim=0)
# predixtions are computed upon export
return out
#%% optimization setup and random seed sequences
def learningLoop(randomSeed:int)-> torch.tensor:
# define object
model = GCN().to(ARGS['device'])
criterion = torch.nn.CrossEntropyLoss()
match ARGS['opt']['name'].lower():
case 'adam':
# https://pytorch.org/docs/stable/generated/torch.optim.Adam.html
optimizer = torch.optim.Adam(model.parameters(),
lr=ARGS['opt']['learning_rate'],
weight_decay= ARGS['opt']['weight_decay'],
)
case 'sgd':
# https://pytorch.org/docs/stable/generated/torch.optim.SGD.html
optimizer = torch.optim.SGD(model.parameters(),
lr= ARGS['opt']['learning_rate'],
nesterov= ARGS['opt']['nesterov'],
momentum= ARGS['opt']['momentum'],
dampening= ARGS['opt']['dampening'],
weight_decay= ARGS['opt']['weight_decay'],
)
# shuffle the hole dataset
randGen = torch.manual_seed(int(randomSeed))
perm = torch.randperm(len(dataset), generator=randGen)
# train- test split
b = int(len(dataset)*(1-ARGS['test_share']))
dataTrain = dataset[perm][:b]
dataTest = dataset[perm][b:]
# graph mini-batch
trainLoader = DataLoader(dataTrain, batch_size=ARGS['batch_size'],
shuffle=False)
testLoader = DataLoader(dataTest, batch_size=ARGS['batch_size'],
shuffle=False)
epochs= tqdm(range(ARGS['max_epochs']), ncols=100)
accWindow = torch.zeros([ARGS['ma_window']])
for e in epochs:
loss = trainLoss(model, optimizer, criterion, trainLoader)
accT = testAccuracy(model, trainLoader)
accV = testAccuracy(model, testLoader)
desc = f"{e: 4d}| L:{loss:4.3f}| T:{accT:4.3f}| V:{accV:4.3f}"
epochs.set_description(desc)
# early stopping with moving average update threshold
accWindow = accWindow.roll(-1)
accWindow[-1]=accV
if (accWindow.max()-accWindow.min())< ARGS['ma_threshold']:
break
# FIXME dataset global variable
return modelValidate(model, dataset)
# %% MAIN loop
with open(osp.join(ARGS['root_folder'],ARGS['seed_name']+'.txt'),'r') as f:
seeds = f.readlines()
dtype=torch.long
seeds = [int(s) for s in seeds]
para = Parallel(n_jobs=2, return_as='generator')
outGen = para(delayed(learningLoop)(s) for s in seeds)
outProbs = list(tqdm(outGen, ncols=100, total=len(seeds)))
# %% export clasification performance (to binaty .pt file)
if ARGS['save']:
# the information score requires predicted probabilities!
result = torch.stack(outProbs)
outVersion= dt.datetime.now().strftime("%y%m%d-%H%M%S")
outPath= osp.join(ARGS['root_folder'], ARGS['out_folder'], outVersion)
torch.save(result, outPath+'.pt')
# print training information
with open(outPath+'.log','w') as f :
pprint(ARGS, stream=f)
pprint(GCN(), stream=f)