-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnns.py
156 lines (123 loc) · 4.71 KB
/
gnns.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
Code for Dir-GNN and GCN.
Adapted from https://github.com/emalgorithm/directed-graph-neural-network
"""
import torch
from torch import nn
from torch_sparse import SparseTensor
import torch.nn.functional as F
from torch.nn import ModuleList, Linear
from torch_geometric.nn.conv.gcn_conv import gcn_norm
from torch_sparse import sum as sparsesum
from torch_sparse import mul
from torch_geometric.nn import (
SAGEConv,
GCNConv,
GATConv,
)
def row_norm(adj):
"""
Applies the row-wise normalization:
\mathbf{D}_{out}^{-1} \mathbf{A}
"""
row_sum = sparsesum(adj, dim=1)
return mul(adj, 1 / row_sum.view(-1, 1))
def directed_norm(adj):
"""
Applies the normalization for directed graphs:
\mathbf{D}_{out}^{-1/2} \mathbf{A} \mathbf{D}_{in}^{-1/2}.
"""
in_deg = sparsesum(adj, dim=0)
in_deg_inv_sqrt = in_deg.pow_(-0.5)
in_deg_inv_sqrt.masked_fill_(in_deg_inv_sqrt == float("inf"), 0.0)
out_deg = sparsesum(adj, dim=1)
out_deg_inv_sqrt = out_deg.pow_(-0.5)
out_deg_inv_sqrt.masked_fill_(out_deg_inv_sqrt == float("inf"), 0.0)
adj = mul(adj, out_deg_inv_sqrt.view(-1, 1))
adj = mul(adj, in_deg_inv_sqrt.view(1, -1))
return adj
def get_norm_adj(adj, norm):
if norm == "sym":
return gcn_norm(adj, add_self_loops=False)
elif norm == "row":
return row_norm(adj)
elif norm == "dir":
return directed_norm(adj)
else:
raise ValueError(f"{norm} normalization is not supported")
class DirGCNConv(torch.nn.Module):
def __init__(self, input_dim, output_dim, alpha):
super(DirGCNConv, self).__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.lin_src_to_dst = Linear(input_dim, output_dim)
self.lin_dst_to_src = Linear(input_dim, output_dim)
self.alpha = alpha
self.adj_norm, self.adj_t_norm = None, None
def forward(self, x, edge_index):
if self.adj_norm is None:
row, col = edge_index
num_nodes = x.shape[1]
adj = SparseTensor(row=row, col=col, sparse_sizes=(num_nodes, num_nodes))
self.adj_norm = get_norm_adj(adj, norm="dir").to_dense()
adj_t = SparseTensor(row=col, col=row, sparse_sizes=(num_nodes, num_nodes))
self.adj_t_norm = get_norm_adj(adj_t, norm="dir").to_dense()
out = self.alpha * self.lin_src_to_dst(torch.matmul(self.adj_norm, x)) + (1 - self.alpha) * self.lin_dst_to_src(torch.matmul(self.adj_t_norm, x))
return out
def get_conv(conv_type, input_dim, output_dim, alpha):
if conv_type == "gcn":
return GCNConv(input_dim, output_dim, add_self_loops=False)
elif conv_type == "sage":
return SAGEConv(input_dim, output_dim)
elif conv_type == "gat":
return GATConv(input_dim, output_dim, heads=1)
elif conv_type == "dir-gcn":
return DirGCNConv(input_dim, output_dim, alpha)
else:
raise ValueError(f"Convolution type {conv_type} not supported")
class GNN(torch.nn.Module):
def __init__(
self,
num_features,
num_classes,
hidden_dim,
num_layers=2,
dropout=0,
conv_type="dir-gcn",
jumping_knowledge=False,
normalize=False,
alpha=1 / 2,
learn_alpha=False,
):
super(GNN, self).__init__()
self.alpha = nn.Parameter(torch.ones(1) * alpha, requires_grad=learn_alpha)
output_dim = hidden_dim
if num_layers == 1:
self.convs = ModuleList([get_conv(conv_type, num_features, output_dim, self.alpha)])
else:
self.convs = ModuleList([get_conv(conv_type, num_features, hidden_dim, self.alpha)])
for _ in range(num_layers - 2):
self.convs.append(get_conv(conv_type, hidden_dim, hidden_dim, self.alpha))
self.convs.append(get_conv(conv_type, hidden_dim, output_dim, self.alpha))
self.lin = torch.nn.Sequential(
torch.nn.Linear(hidden_dim, hidden_dim),
torch.nn.LeakyReLU(),
torch.nn.Linear(hidden_dim, num_classes)
)
self.num_layers = num_layers
self.dropout = dropout
self.jumping_knowledge = jumping_knowledge
self.normalize = normalize
self.conv_type = conv_type
def forward(self, x, edge_index):
xs = []
for i, conv in enumerate(self.convs):
x = conv(x, edge_index)
if i != len(self.convs) - 1 or self.jumping_knowledge:
x = F.relu(x)
x = F.dropout(x, p=self.dropout, training=self.training)
xs += [x]
## Readout - maxpool
x = x.max(1)[0]
x = self.lin(x)
return torch.nn.functional.log_softmax(x, dim=1)