forked from pyg-team/pytorch_geometric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproteins_gmt.py
98 lines (76 loc) · 2.87 KB
/
proteins_gmt.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
import os.path as osp
import torch
import torch.nn.functional as F
from torch.nn import Linear
from torch_geometric.datasets import TUDataset
from torch_geometric.loader import DataLoader
from torch_geometric.nn import GCNConv, GraphMultisetTransformer
path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', 'PROTEINS')
dataset = TUDataset(path, name='PROTEINS').shuffle()
avg_num_nodes = int(dataset.data.num_nodes / len(dataset))
n = (len(dataset) + 9) // 10
test_dataset = dataset[:n]
val_dataset = dataset[n:2 * n]
train_dataset = dataset[2 * n:]
test_loader = DataLoader(test_dataset, batch_size=128)
val_loader = DataLoader(val_dataset, batch_size=128)
train_loader = DataLoader(train_dataset, batch_size=128, shuffle=True)
class Net(torch.nn.Module):
def __init__(self):
super().__init__()
self.conv1 = GCNConv(dataset.num_features, 32)
self.conv2 = GCNConv(32, 32)
self.conv3 = GCNConv(32, 32)
self.pool = GraphMultisetTransformer(
in_channels=96,
hidden_channels=64,
out_channels=32,
Conv=GCNConv,
num_nodes=avg_num_nodes,
pooling_ratio=0.25,
pool_sequences=['GMPool_G', 'SelfAtt', 'GMPool_I'],
num_heads=4,
layer_norm=False,
)
self.lin1 = Linear(32, 16)
self.lin2 = Linear(16, dataset.num_classes)
def forward(self, x0, edge_index, batch):
x1 = self.conv1(x0, edge_index).relu()
x2 = self.conv2(x1, edge_index).relu()
x3 = self.conv3(x2, edge_index).relu()
x = torch.cat([x1, x2, x3], dim=-1)
x = self.pool(x, batch, edge_index)
x = self.lin1(x).relu()
x = F.dropout(x, p=0.5, training=self.training)
x = self.lin2(x)
return x.log_softmax(dim=-1)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = Net().to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001, weight_decay=1e-4)
def train():
model.train()
total_loss = 0
for data in train_loader:
data = data.to(device)
optimizer.zero_grad()
out = model(data.x, data.edge_index, data.batch)
loss = F.nll_loss(out, data.y)
loss.backward()
total_loss += data.num_graphs * float(loss)
optimizer.step()
return total_loss / len(train_dataset)
@torch.no_grad()
def test(loader):
model.eval()
total_correct = 0
for data in loader:
data = data.to(device)
out = model(data.x, data.edge_index, data.batch)
total_correct += int((out.argmax(dim=-1) == data.y).sum())
return total_correct / len(loader.dataset)
for epoch in range(1, 201):
train_loss = train()
val_acc = test(val_loader)
test_acc = test(test_loader)
print(f'Epoch: {epoch:03d}, Loss: {train_loss:.4f}, '
f'Val Acc: {val_acc:.4f}, Test Acc: {test_acc:.4f}')