-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCN.py
61 lines (48 loc) · 1.73 KB
/
GCN.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.nn import MessagePassing
from torch_geometric.utils import add_self_loops, degree
class GCNConv(MessagePassing):
def __init__(self, in_channels, out_channels):
super().__init__(aggr='add')
self.lin = nn.Linear(in_channels, out_channels, bias=False)
self.bias = nn.Parameter(torch.empty(out_channels))
self.reset_parameters()
def reset_parameters(self):
self.lin.reset_parameters()
self.bias.data.zero_()
def forward(self, x, edge_index):
edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0))
x = self.lin(x)
row, col = edge_index
deg = degree(col, x.size(0), dtype=x.dtype)
deg_inv_sqrt = deg.pow(-0.5)
deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
norm = deg_inv_sqrt[row] * deg_inv_sqrt[col]
out = self.propagate(edge_index, x=x, norm=norm)
out = out + self.bias
return out
def message(self, x_j, norm):
return norm.view(-1, 1) * x_j
class GCNModel(nn.Module):
"""Graph Convolutional Layer (GCN)"""
def __init__(self, in_channels, out_channels):
super(GCNModel, self).__init__()
self.conv1 = GCNConv(in_channels, 128)
self.conv2 = GCNConv(128, 64)
self.fc = nn.Linear(64, out_channels)
def forward(self, x, edge_index):
"""
Paramters:
x (Tensor):
Node feature matrix
edge_index (LongTensor):
Graph edge connectivity
"""
x = self.conv1(x, edge_index)
x = F.relu(x)
x = self.conv2(x, edge_index)
x = F.relu(x)
x = self.fc(x)
return F.log_softmax(x, dim=1)