-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminimal_example.py
126 lines (99 loc) · 3.85 KB
/
minimal_example.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
import torch
import torch.nn.functional as F
from torch_geometric.nn import GINConv, DenseGINConv
from torch_geometric.utils import to_dense_batch
from torch_geometric.loader import DataLoader
from torch_geometric.datasets import TUDataset
# Local imports
from source.layers.bnpool import BNPool
from source.utils import NormalizeAdjSparse_with_ea
from source.utils.misc import TensorsCache
from source.utils.data import get_train_val_test_datasets
### Get the data
dataset = TUDataset(root="data/TUDataset", name='MUTAG', pre_transform=NormalizeAdjSparse_with_ea())
train_dataset, val_dataset, test_dataset = get_train_val_test_datasets(dataset, seed=777, n_folds=10, fold_id=0)
tr_dataloader = DataLoader(train_dataset, batch_size=32, shuffle=True)
val_dataloader = DataLoader(val_dataset, batch_size=32)
test_dataloader = DataLoader(test_dataset, batch_size=32)
### Model definition
class Net(torch.nn.Module):
def __init__(self):
super().__init__()
num_features = dataset.num_features
num_classes = dataset.num_classes
hidden_channels = 64
self.cache = TensorsCache(use_cache=False)
# First MP layer
self.conv1 = GINConv(
torch.nn.Sequential(
torch.nn.Linear(num_features, hidden_channels),
torch.nn.ReLU(),
torch.nn.Linear(hidden_channels, hidden_channels),
)
)
# BNPool layer
self.pool = BNPool(emb_size=hidden_channels)
# Second MP layer
self.conv2 = DenseGINConv(
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)
# Transform to dense batch
x, mask = to_dense_batch(x, batch)
adj = self.cache.get_and_cache_A(edge_index, batch)
# BNPool layer
pos_weight = self.cache.get_and_cache_pos_weight(adj, mask)
_, x, adj, _, loss_d = self.pool(x, adj=adj, node_mask=mask,
pos_weight=pos_weight,
return_coarsened_graph=True)
aux_loss = loss_d['quality'] + loss_d['kl'] + loss_d['K_prior']
# Second MP layer
x = self.conv2(x, adj)
# Global pooling
x = x.mean(dim=1)
# Readout layer
x = self.lin(x)
return F.log_softmax(x, dim=-1), aux_loss
### Model 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 tr_dataloader:
data = data.to(device)
optimizer.zero_grad()
output, aux_loss = model(data.x, data.edge_index, data.batch)
loss = F.nll_loss(output, data.y.view(-1)) + aux_loss
loss.backward()
loss_all += data.y.size(0) * float(loss)
optimizer.step()
return loss_all / len(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(val_dataloader)
if val_acc > best_val_acc:
test_acc = test(test_dataloader)
best_val_acc = val_acc
print(f'Epoch: {epoch:03d}, Train Loss: {train_loss:.1f}, '
f'Val Acc: {val_acc:.3f}, Test Acc: {test_acc:.3f}')