-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_sr.py
322 lines (275 loc) · 11.4 KB
/
run_sr.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""
script to train on SR classification dataset
"""
import argparse
import os
import shutil
import time
from json import dumps
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import Adam
from torch_geometric.loader import DataLoader
from torch_geometric.nn import DataParallel
from torch_geometric.seed import seed_everything
from models.gnn_count import DR2FWL2Kernel
from pygmmpp.datasets import SRDataset
from data_utils.batch import collate
from pygmmpp.data import DataLoader
from data_utils.preprocess import drfwl2_transform, drfwl3_transform
from models.pool import GraphLevelPooling
from pygmmpp.utils import compose
def train(loader, model, optimizer, device, parallel=False):
model.train()
total_loss = 0
for data in loader:
optimizer.zero_grad()
if parallel:
num_graphs = len(data)
y = torch.cat([d.y for d in data]).to(device)
else:
num_graphs = data.num_graphs
data = data.to(device)
y = data.y
out = model(data).squeeze()
loss = torch.nn.NLLLoss()(out, y)
loss.backward()
total_loss += loss.item() * num_graphs
optimizer.step()
return total_loss / len(loader.dataset)
@torch.no_grad()
def test(loader, model, device, parallel=False):
model.train() # eliminate the effect of BN
y_preds, y_trues = [], []
for data in loader:
if parallel:
y = torch.cat([d.y for d in data]).to(device)
else:
data = data.to(device)
y = data.y
y_preds.append(torch.argmax(model(data), dim=-1))
y_trues.append(y)
y_preds = torch.cat(y_preds, -1)
y_trues = torch.cat(y_trues, -1)
return (y_preds == y_trues).float().mean()
class SRModel3(nn.Module):
def __init__(self,
hidden_channels: int,
num_layers: int,
add_0: bool = True,
add_112: bool = True,
add_212: bool = True,
add_222: bool = True,
eps: float = 0.,
train_eps: bool = False,
norm_type: str = "batch_norm",
norm_between_layers: str = "batch_norm",
residual: str = "none",
drop_prob: float = 0.0):
super().__init__()
self.hidden_channels = hidden_channels
self.num_layers = num_layers
self.add_0 = add_0
self.add_112 = add_112
self.add_212 = add_212
self.add_222 = add_222
self.initial_eps = eps
self.train_eps = train_eps
self.norm_type = norm_type
self.residual = residual
self.drop_prob = drop_prob
self.node_transform = nn.Linear(1, self.hidden_channels)
self.ker = DR2FWL2Kernel(self.hidden_channels,
self.num_layers,
self.initial_eps,
self.train_eps,
self.norm_type,
norm_between_layers,
self.residual,
self.drop_prob)
self.pool = GraphLevelPooling(hidden_channels)
self.post_mlp = nn.Sequential(nn.Linear(hidden_channels, hidden_channels // 2),
nn.ELU(),
nn.Linear(hidden_channels // 2, 15))
self.ker.add_aggr(1, 1, 1)
if self.add_0:
self.ker.add_aggr(0, 1, 1)
self.ker.add_aggr(0, 2, 2)
if self.add_112:
self.ker.add_aggr(1, 1, 2)
if self.add_212:
self.ker.add_aggr(2, 2, 1)
if self.add_222:
self.ker.add_aggr(2, 2, 2)
self.ker.add_aggr(1, 2, 3)
self.ker.add_aggr(3, 3, 1)
self.ker.add_aggr(2, 2, 3)
self.ker.add_aggr(3, 3, 2)
self.ker.add_aggr(3, 3, 3)
self.ker.add_aggr(0, 3, 3)
self.reset_parameters()
def reset_parameters(self):
self.node_transform.reset_parameters()
self.ker.reset_parameters()
for m in self.post_mlp:
if hasattr(m, 'reset_parameters'):
m.reset_parameters()
def forward(self, batch) -> torch.Tensor:
edge_indices = [batch.edge_index, batch.edge_index2, batch.edge_index3]
edge_attrs = [self.node_transform(batch.x),
self.node_transform(batch.x[batch.edge_index[0]]) +
self.node_transform(batch.x[batch.edge_index[1]]),
self.node_transform(batch.x[batch.edge_index2[0]]) +
self.node_transform(batch.x[batch.edge_index2[1]]),
self.node_transform(batch.x[batch.edge_index3[0]]) +
self.node_transform(batch.x[batch.edge_index3[1]])
]
triangles = {
(1, 1, 1): batch.triangle_1_1_1,
(1, 1, 2): batch.triangle_1_1_2,
(2, 2, 1): batch.triangle_2_2_1,
(2, 2, 2): batch.triangle_2_2_2,
(1, 2, 3): batch.triangle_1_2_3,
(3, 3, 1): batch.triangle_3_3_1,
(2, 2, 3): batch.triangle_2_2_3,
(3, 3, 2): batch.triangle_3_3_2,
(3, 3, 3): batch.triangle_3_3_3,
}
inverse_edges = [batch.inverse_edge_1, batch.inverse_edge_2, batch.inverse_edge_3]
edge_attrs = self.ker(edge_attrs,
edge_indices,
triangles,
inverse_edges)
x = self.pool(edge_attrs, edge_indices, batch.num_nodes, batch.batch0)
x = self.post_mlp(x)
x = F.log_softmax(x, dim=1)
return x
class SRModel(nn.Module):
def __init__(self,
hidden_channels: int,
num_layers: int,
add_0: bool = True,
add_112: bool = True,
add_212: bool = True,
add_222: bool = True,
eps: float = 0.,
train_eps: bool = False,
norm_type: str = "batch_norm",
norm_between_layers: str = "batch_norm",
residual: str = "none",
drop_prob: float = 0.0):
super().__init__()
self.hidden_channels = hidden_channels
self.num_layers = num_layers
self.add_0 = add_0
self.add_112 = add_112
self.add_212 = add_212
self.add_222 = add_222
self.initial_eps = eps
self.train_eps = train_eps
self.norm_type = norm_type
self.residual = residual
self.drop_prob = drop_prob
self.node_transform = nn.Linear(1, self.hidden_channels)
self.ker = DR2FWL2Kernel(self.hidden_channels,
self.num_layers,
self.initial_eps,
self.train_eps,
self.norm_type,
norm_between_layers,
self.residual,
self.drop_prob)
self.pool = GraphLevelPooling(hidden_channels)
self.post_mlp = nn.Sequential(nn.Linear(hidden_channels, hidden_channels // 2),
nn.ELU(),
nn.Linear(hidden_channels // 2, 15))
self.ker.add_aggr(1, 1, 1)
if self.add_0:
self.ker.add_aggr(0, 1, 1)
self.ker.add_aggr(0, 2, 2)
if self.add_112:
self.ker.add_aggr(1, 1, 2)
if self.add_212:
self.ker.add_aggr(2, 2, 1)
if self.add_222:
self.ker.add_aggr(2, 2, 2)
self.reset_parameters()
def reset_parameters(self):
self.node_transform.reset_parameters()
self.ker.reset_parameters()
for m in self.post_mlp:
if hasattr(m, 'reset_parameters'):
m.reset_parameters()
def forward(self, batch) -> torch.Tensor:
edge_indices = [batch.edge_index, batch.edge_index2]
edge_attrs = [self.node_transform(batch.x),
self.node_transform(batch.x[batch.edge_index[0]]) +
self.node_transform(batch.x[batch.edge_index[1]]),
self.node_transform(batch.x[batch.edge_index2[0]]) +
self.node_transform(batch.x[batch.edge_index2[1]])
]
triangles = {
(1, 1, 1): batch.triangle_1_1_1,
(1, 1, 2): batch.triangle_1_1_2,
(2, 2, 1): batch.triangle_2_2_1,
(2, 2, 2): batch.triangle_2_2_2,
}
inverse_edges = [batch.inverse_edge_1, batch.inverse_edge_2]
edge_attrs = self.ker(edge_attrs,
edge_indices,
triangles,
inverse_edges)
x = self.pool(edge_attrs, edge_indices, batch.num_nodes, batch.batch0)
x = self.post_mlp(x)
x = F.log_softmax(x, dim=1)
return x
def main():
parser = argparse.ArgumentParser(f'arguments for training and testing')
parser.add_argument('--seed', type=int, default=42)
parser.add_argument('--root', type=str, default='datasets/sr25')
parser.add_argument('--hidden', type=int, default=64)
parser.add_argument('--layer', type=int, default=5)
parser.add_argument('--cuda', type=int, default=0)
parser.add_argument('--batch-size', type=int, default=64)
parser.add_argument('--lr', type=float, default=0.001)
parser.add_argument('--l2-wd', type=float, default=0.0)
parser.add_argument('--num-epochs', type=int, default=100)
parser.add_argument('--use_3', action='store_true', help='3-DRFWL(2)')
args = parser.parse_args()
seed_everything(args.seed)
dataset = SRDataset(args.root, pre_transform=drfwl2_transform() if not args.use_3 else drfwl3_transform())
dataset.data_batch.y = torch.arange(len(dataset.data_batch.y)).long() # each graph is a unique class
train_dataset = dataset
val_dataset = dataset
test_dataset = dataset
# 2. create loader
train_loader = DataLoader(train_dataset, args.batch_size, shuffle=True, collator=collate)
test_loader = DataLoader(test_dataset, args.batch_size, shuffle=False, collator=collate)
device = f'cuda:{args.cuda}' if args.cuda != -1 else 'cpu'
model = (SRModel if not args.use_3 else SRModel3)(
args.hidden,
args.layer,
norm_type='none',
norm_between_layers='none',
residual='last'
).to(device)
optimizer = Adam(model.parameters(), lr=args.lr, weight_decay=args.l2_wd)
best_test_acc = 0
start_outer = time.time()
for epoch in range(args.num_epochs):
start = time.time()
train_loss = train(train_loader, model, optimizer, device=device)
lr = optimizer.param_groups[0]['lr']
test_acc = test(test_loader, model, device=device)
if test_acc >= best_test_acc:
best_test_acc = test_acc
time_per_epoch = time.time() - start
print(f'Epoch: {epoch + 1:03d}, LR: {lr:7f}, Train Loss: {train_loss:.4f}, Test Acc: {test_acc:.4f}, '
f'Best Test Acc: {best_test_acc:.4f}, Seconds: {time_per_epoch:.4f}')
torch.cuda.empty_cache() # empty test part memory cost
time_average_epoch = time.time() - start_outer
print(
f'Loss: {train_loss:.4f}, Best test: {best_test_acc:.4f}, Seconds/epoch: {time_average_epoch / (epoch + 1):.4f}')
if __name__ == "__main__":
main()