-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgin.py
274 lines (245 loc) · 11.4 KB
/
gin.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# coding: utf-8
import scipy.sparse as sp
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch_geometric as tg
# Graph Isomorphism Network. For more information, please refer to https://arxiv.org/abs/1810.00826
# We copy and modify GIN code from https://github.com/weihua916/powerful-gnns, and include this method in our graph embedding project framework.
# # Author: jhljx
# # Email: [email protected]
# MLP with lienar output
class MLP(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, layer_num, bias=True):
'''
layer_num: number of layers in the neural networks (EXCLUDING the input layer). If layer_num=1, this reduces to linear model.
input_dim: dimensionality of input features
hidden_dim: dimensionality of hidden units at ALL layers
output_dim: number of classes for prediction
device: which device to use
'''
super(MLP, self).__init__()
self.linear_or_not = True # default is linear model
self.layer_num = layer_num
self.bias = bias
if layer_num < 1:
raise ValueError("number of layers should be positive!")
elif layer_num == 1:
# Linear model
self.linear = nn.Linear(input_dim, output_dim, bias=bias)
else:
# Multi-layer model
self.linear_or_not = False
self.linears = torch.nn.ModuleList()
self.batch_norms = torch.nn.ModuleList()
self.linears.append(nn.Linear(input_dim, hidden_dim, bias=bias))
for layer in range(layer_num - 2):
self.linears.append(nn.Linear(hidden_dim, hidden_dim, bias=bias))
self.linears.append(nn.Linear(hidden_dim, output_dim, bias=bias))
for layer in range(layer_num - 1):
self.batch_norms.append(nn.BatchNorm1d((hidden_dim)))
def forward(self, x):
if self.linear_or_not:
# If linear model
return self.linear(x)
else:
# If MLP
h = x
for layer in range(self.layer_num - 1):
h = F.relu(self.batch_norms[layer](self.linears[layer](h)))
return self.linears[self.layer_num - 1](h)
# Original version of GIN
class GIN(nn.Module):
input_dim: int
hidden_dim: int
output_dim: int
layer_num: int
mlp_layer_num: int
learn_eps: bool
neighbor_pooling_type: str
dropout: float
bias: bool
def __init__(self, input_dim, hidden_dim, output_dim, layer_num, mlp_layer_num, learn_eps, neighbor_pooling_type='sum', dropout=0.5, bias=True):
'''
input_dim: dimensionality of input features
hidden_dim: dimensionality of hidden units at ALL layers
output_dim: number of classes for prediction
layer_num: number of layers in the neural networks
mlp_layer_num: number of layers in mlps
learn_eps: If True, learn epsilon to distinguish center nodes from neighboring nodes. If False, aggregate neighbors and center nodes altogether.
neighbor_pooling_type: how to aggregate neighbors (sum, average, or max)
dropout: dropout ratio on the final linear layer
bias: whether to add bias for MLP
'''
super(GIN, self).__init__()
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.output_dim = output_dim
self.layer_num = layer_num
self.mlp_layer_num = mlp_layer_num
self.learn_eps = learn_eps
self.neighbor_pooling_type = neighbor_pooling_type
self.dropout = dropout
self.bias = bias
self.method_name = 'GIN'
self.eps = nn.Parameter(torch.zeros(self.layer_num))
assert neighbor_pooling_type in ['sum', 'average', 'max']
self.linear = nn.Linear(input_dim, hidden_dim)
self.mlps = torch.nn.ModuleList()
# List of batchnorms applied to the output of MLP (input of the final prediction linear layer)
self.batch_norms = torch.nn.ModuleList()
for layer in range(self.layer_num - 1):
self.mlps.append(MLP(hidden_dim, hidden_dim, hidden_dim, mlp_layer_num, bias=bias))
self.batch_norms.append(nn.BatchNorm1d(hidden_dim))
self.mlps.append(MLP(hidden_dim, hidden_dim, output_dim, mlp_layer_num, bias=bias))
self.batch_norms.append(nn.BatchNorm1d(output_dim))
def __preprocess_neighbors_maxpool(self, adj):
# create padded_neighbor_list in a graph
data = adj._values().cpu().numpy()
row = adj._indices()[0, :].cpu().numpy()
col = adj._indices()[1, :].cpu().numpy()
node_num = adj.shape[0]
sp_neighbors = sp.coo_matrix((data, (row, col)), shape=(node_num, node_num)).tolil()
neighbor_list = sp_neighbors.rows
return neighbor_list
def __preprocess_neighbors_sumavepool(self, adj):
# Add self-loops in the adjacency matrix if learn_eps is False, i.e., aggregate center nodes and neighbor nodes altogether.
node_num = adj.shape[0]
edge_indices = adj._indices()
weights = adj._values()
if not self.learn_eps:
# num_node = start_idx[-1]
self_loop_edge = torch.LongTensor([range(node_num), range(node_num)]).to(adj.device)
elem = torch.ones(node_num).to(adj.device)
Adj_block_idx = torch.cat([edge_indices, self_loop_edge], 1)
Adj_block_elem = torch.cat([weights, elem], 0)
Adj_block = torch.sparse.FloatTensor(Adj_block_idx, Adj_block_elem, torch.Size([node_num, node_num]))
return Adj_block.to(adj.device)
@staticmethod
def maxpool(h, neighbor_list):
neighbor_list_len = len(neighbor_list)
neighbor_feats = torch.zeros_like(h).to(h.device)
for i in range(neighbor_list_len):
if len(neighbor_list[i]) == 0:
continue
neighbor_feats[i] = torch.max(h[neighbor_list[i]], 0)[0]
return neighbor_feats
def next_layer_eps(self, h, layer, neighbor_list=None, Adj_block=None):
# pooling neighboring nodes and center nodes separately by epsilon reweighting.
if self.neighbor_pooling_type == "max":
##If max pooling
pooled = self.maxpool(h, neighbor_list)
else:
# If sum or average pooling
pooled = torch.spmm(Adj_block, h)
if self.neighbor_pooling_type == "average":
# If average pooling
degree = torch.spmm(Adj_block, torch.ones((Adj_block.shape[0], 1)).to(h.device))
pooled = pooled / degree
# Reweights the center node representation when aggregating it with its neighbors
pooled = pooled + (1 + self.eps[layer]) * h
pooled_rep = self.mlps[layer](pooled)
h = self.batch_norms[layer](pooled_rep)
# non-linearity
h = F.relu(h)
return h
def next_layer(self, h, layer, neighbor_list=None, Adj_block=None):
###pooling neighboring nodes and center nodes altogether
if self.neighbor_pooling_type == "max":
##If max pooling
pooled = self.maxpool(h, neighbor_list)
else:
# If sum or average pooling
# As torch.spmm doesn't support matrix multiplication between two sparse tensors, so we have to reduce h into a dense matrix. A linear layer is needed!
pooled = torch.spmm(Adj_block, h)
if self.neighbor_pooling_type == "average":
# If average pooling
degree = torch.spmm(Adj_block, torch.ones((Adj_block.shape[0], 1)).to(h.device))
pooled = pooled / degree
# representation of neighboring and center nodes
pooled_rep = self.mlps[layer](pooled)
h = self.batch_norms[layer](pooled_rep)
# non-linearity
h = F.relu(h)
return h
def forward(self, x, adj):
if isinstance(x, list):
timestamp_num = len(x)
output_list = []
for i in range(timestamp_num):
output_list.append(self.gin(x[i], adj[i]))
return output_list
return self.gin(x, adj)
def gin(self, x, adj):
if self.neighbor_pooling_type == "max":
neighbor_list = self.__preprocess_neighbors_maxpool(adj)
Adj_block = []
else:
neighbor_list = []
Adj_block = self.__preprocess_neighbors_sumavepool(adj)
h = self.linear(x)
for layer in range(self.layer_num):
if self.neighbor_pooling_type == "max" and self.learn_eps:
h = self.next_layer_eps(h, layer, neighbor_list=neighbor_list)
elif not self.neighbor_pooling_type == "max" and self.learn_eps:
h = self.next_layer_eps(h, layer, Adj_block=Adj_block)
elif self.neighbor_pooling_type == "max" and not self.learn_eps:
h = self.next_layer(h, layer, neighbor_list=neighbor_list)
elif not self.neighbor_pooling_type == "max" and not self.learn_eps:
h = self.next_layer(h, layer, Adj_block=Adj_block)
if layer < self.layer_num - 1:
h = F.dropout(h, self.dropout, training=self.training)
return h
# Pytorch-Geometric version of GIN
class TgGIN(torch.nn.Module):
input_dim: int
feature_dim: int
hidden_dim: int
output_dim: int
feature_pre: bool
layer_num: int
dropout: float
bias: bool
method_name: str
def __init__(self, input_dim, feature_dim, hidden_dim, output_dim, feature_pre=True, layer_num=2, dropout=True, bias=True, **kwargs):
super(TgGIN, self).__init__()
self.input_dim = input_dim
self.feature_dim = feature_dim
self.hidden_dim = hidden_dim
self.output_dim = output_dim
self.feature_pre = feature_pre
self.layer_num = layer_num
self.dropout = dropout
self.bias = bias
self.method_name = 'TgGIN'
if feature_pre:
self.linear_pre = nn.Linear(input_dim, feature_dim, bias=bias)
self.conv_first_nn = nn.Linear(feature_dim, hidden_dim, bias=bias)
self.conv_first = tg.nn.GINConv(self.conv_first_nn)
else:
self.conv_first_nn = nn.Linear(input_dim, hidden_dim, bias=bias)
self.conv_first = tg.nn.GINConv(self.conv_first_nn) # default: eps=0, train_eps=False
self.conv_hidden_nn = nn.ModuleList([nn.Linear(hidden_dim, hidden_dim, bias=bias) for i in range(layer_num - 2)])
self.conv_hidden = nn.ModuleList([tg.nn.GINConv(self.conv_hidden_nn[i]) for i in range(layer_num - 2)])
self.conv_out_nn = nn.Linear(hidden_dim, output_dim, bias=bias)
self.conv_out = tg.nn.GINConv(self.conv_out_nn)
def forward(self, x, edge_index):
if isinstance(x, list):
timestamp_num = len(x)
output_list = []
for i in range(timestamp_num):
output_list.append(self.gin(x[i], edge_index[i]))
return output_list
return self.gin(x, edge_index)
def gin(self, x, edge_index):
if self.feature_pre:
x = self.linear_pre(x)
x = self.conv_first(x, edge_index)
x = F.relu(x)
x = F.dropout(x, self.dropout, training=self.training)
for i in range(self.layer_num-2):
x = self.conv_hidden[i](x, edge_index)
x = F.relu(x)
x = F.dropout(x, self.dropout, training=self.training)
x = self.conv_out(x, edge_index)
return x