-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminimal_example.py
More file actions
127 lines (103 loc) · 3.5 KB
/
minimal_example.py
File metadata and controls
127 lines (103 loc) · 3.5 KB
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
import os
import os.path as osp
import torch
import torch.nn.functional as F
from torch_geometric.nn import GINConv
from torch_scatter import scatter_mean
from source.data import TUDataModule
from source.layers.maxcutpool.modules import MaxCutPool
# Setup paths
path = osp.join('data', 'MUTAG')
os.makedirs(path, exist_ok=True)
# Configuration
args = type('Args', (), {
'dataset': 'MUTAG',
'seed': 42,
'n_folds': 10,
'fold_id': 0,
'batch_size': 20
})
# Initialize data module
data_module = TUDataModule(args)
# Define the GNN model
class Net(torch.nn.Module):
def __init__(self):
super().__init__()
num_features = data_module.dataset.num_features
num_classes = data_module.dataset.num_classes
hidden_channels = 32
# First GINConv layer
self.conv1 = GINConv(
torch.nn.Sequential(
torch.nn.Linear(num_features, hidden_channels),
torch.nn.ReLU(),
torch.nn.Linear(hidden_channels, hidden_channels),
)
)
# MaxCutPool layer
pool_kwargs = {'ratio': 0.5, 'beta': 1.0}
score_net_kwargs = {
'mp_units': [hidden_channels],
'mp_act': 'ReLU',
'mlp_units': [32]*4,
'mlp_act': 'ReLU'
}
self.pool = MaxCutPool(hidden_channels, **pool_kwargs, **score_net_kwargs)
# Second GINConv layer
self.conv2 = GINConv(
torch.nn.Sequential(
torch.nn.Linear(hidden_channels, hidden_channels),
torch.nn.ReLU(),
torch.nn.Linear(hidden_channels, hidden_channels),
)
)
# Readout layer
self.lin = torch.nn.Linear(hidden_channels, num_classes)
def forward(self, x, edge_index, batch=None):
# First MP layer
x = self.conv1(x, edge_index)
# MaxCutPool layer
x, edge_index, _, batch, _, _, mc_loss, _, _ = self.pool(
x, edge_index, edge_weight=None, batch=batch)
# Second MP layer
x = self.conv2(x, edge_index)
# Global pooling
x = scatter_mean(x, batch, dim=0)
# Readout layer
x = self.lin(x)
return F.log_softmax(x, dim=-1), mc_loss
# Device setup
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = Net().to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=5e-4)
def train():
model.train()
loss_all = 0
for data in data_module.train_dataloader():
data = data.to(device)
optimizer.zero_grad()
output, mc_loss = model(data.x, data.edge_index, data.batch)
loss = F.nll_loss(output, data.y.view(-1)) + mc_loss
loss.backward()
loss_all += data.y.size(0) * float(loss)
optimizer.step()
return loss_all / len(data_module.train_dataloader().dataset)
@torch.no_grad()
def test(loader):
model.eval()
correct = 0
for data in loader:
data = data.to(device)
pred = model(data.x, data.edge_index, data.batch)[0].max(dim=1)[1]
correct += int(pred.eq(data.y.view(-1)).sum())
return correct / len(loader.dataset)
# Training loop
best_val_acc = test_acc = 0
for epoch in range(1, 151):
train_loss = train()
val_acc = test(data_module.val_dataloader())
if val_acc > best_val_acc:
test_acc = test(data_module.test_dataloader())
best_val_acc = val_acc
print(f'Epoch: {epoch:03d}, Train Loss: {train_loss:.4f}, '
f'Val Acc: {val_acc:.4f}, Test Acc: {test_acc:.4f}')