-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
49 lines (38 loc) · 1.47 KB
/
model.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
import torch
import torch.nn.functional as F
from torch.nn import Sequential, Linear, BatchNorm1d, ReLU
from torch_geometric.nn import GCNConv
from torch_geometric.nn import global_mean_pool as gap, global_max_pool as gmp
torch.manual_seed(42)
class GCNConvNetwork(torch.nn.Module):
def __init__(self, feature_size):
"""
Graph Neural Network model made with GCN Convolution Layers
Consists of three layers of GCNConv layers, followed by two Linear layers.
Output is two classes.
Parameters - feature_size
"""
super(GCNConvNetwork, self).__init__()
# Hardcoded parameters
num_classes = 2
hidden_channels = 512
# Layers
self.conv1 = GCNConv(feature_size, hidden_channels)
self.conv2 = GCNConv(hidden_channels, hidden_channels)
self.conv3 = GCNConv(hidden_channels, hidden_channels)
self.linear1 = Linear(hidden_channels, 64)
self.linear2 = Linear(64, num_classes)
def forward(self, x, edge_attr, edge_index, batch_index):
#GCN block (Obtain node embeddings)
x = self.conv1(x, edge_index)
x = x.relu()
x = self.conv2(x, edge_index)
x = x.relu()
x = self.conv3(x, edge_index)
# Readout layer
x = gap(x, batch_index)
# Linear block
x = self.linear1(x).relu()
x = F.dropout(x, p=0.5, training=self.training)
x = self.linear2(x)
return x