-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSGFormer.py
More file actions
343 lines (283 loc) · 12.3 KB
/
SGFormer.py
File metadata and controls
343 lines (283 loc) · 12.3 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""
https://github.com/qitianwu/SGFormer
uv pip install torch_sparse==0.6.10 --no-build-isolation
uv pip install torch_geometric==1.7.2 --no-build-isolation
"""
import math
import os
import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
from torch_sparse import SparseTensor, matmul
from torch_geometric.utils import degree
from torch_geometric.nn import GCNConv, GATConv, global_mean_pool, global_max_pool, global_add_pool
class GraphConvLayer(nn.Module):
def __init__(self, in_channels, out_channels, use_weight=True, use_init=False):
super(GraphConvLayer, self).__init__()
self.use_init = use_init
self.use_weight = use_weight
if self.use_init:
in_channels_ = 2 * in_channels
else:
in_channels_ = in_channels
self.W = nn.Linear(in_channels_, out_channels)
def reset_parameters(self):
self.W.reset_parameters()
def forward(self, x, edge_index, x0):
N = x.shape[0]
# Store original device
device = x.device
row, col = edge_index
# torch_sparse requires indices, values, and input matrix to be on CPU
row_cpu = row.cpu()
col_cpu = col.cpu()
x_cpu = x.cpu()
d = degree(col_cpu, N).float()
d_norm_in = (1. / d[col_cpu]).sqrt()
d_norm_out = (1. / d[row_cpu]).sqrt()
value = torch.ones_like(row_cpu) * d_norm_in * d_norm_out
value = torch.nan_to_num(value, nan=0.0, posinf=0.0, neginf=0.0)
# Keep everything on CPU for torch_sparse operations
adj = SparseTensor(row=col_cpu, col=row_cpu, value=value, sparse_sizes=(N, N))
x = matmul(adj, x_cpu) # [N, D]
# Move result back to original device
x = x.to(device)
if self.use_init:
x = torch.cat([x, x0], 1)
x = self.W(x)
elif self.use_weight:
x = self.W(x)
return x
class GraphConv(nn.Module):
def __init__(self, in_channels, hidden_channels, num_layers=2, dropout=0.5, use_bn=True, use_residual=True, use_weight=True, use_init=False, use_act=True):
super(GraphConv, self).__init__()
self.convs = nn.ModuleList()
self.fcs = nn.ModuleList()
self.fcs.append(nn.Linear(in_channels, hidden_channels))
self.bns = nn.ModuleList()
self.bns.append(nn.BatchNorm1d(hidden_channels))
for _ in range(num_layers):
self.convs.append(
GraphConvLayer(hidden_channels, hidden_channels, use_weight, use_init))
self.bns.append(nn.BatchNorm1d(hidden_channels))
self.dropout = dropout
self.activation = F.relu
self.use_bn = use_bn
self.use_residual = use_residual
self.use_act = use_act
def reset_parameters(self):
for conv in self.convs:
conv.reset_parameters()
for bn in self.bns:
bn.reset_parameters()
for fc in self.fcs:
fc.reset_parameters()
def forward(self, x, edge_index):
layer_ = []
x = self.fcs[0](x)
if self.use_bn:
x = self.bns[0](x)
x = self.activation(x)
x = F.dropout(x, p=self.dropout, training=self.training)
layer_.append(x)
for i, conv in enumerate(self.convs):
x = conv(x, edge_index, layer_[0])
if self.use_bn:
x = self.bns[i+1](x)
if self.use_act:
x = self.activation(x)
x = F.dropout(x, p=self.dropout, training=self.training)
if self.use_residual:
x = x + layer_[-1]
return x
class TransConvLayer(nn.Module):
'''
transformer with fast attention
'''
def __init__(self, in_channels,
out_channels,
num_heads,
use_weight=True):
super().__init__()
self.Wk = nn.Linear(in_channels, out_channels * num_heads)
self.Wq = nn.Linear(in_channels, out_channels * num_heads)
if use_weight:
self.Wv = nn.Linear(in_channels, out_channels * num_heads)
self.out_channels = out_channels
self.num_heads = num_heads
self.use_weight = use_weight
def reset_parameters(self):
self.Wk.reset_parameters()
self.Wq.reset_parameters()
if self.use_weight:
self.Wv.reset_parameters()
def forward(self, query_input, source_input, output_attn=False):
# feature transformation
qs = self.Wq(query_input).reshape(-1, self.num_heads, self.out_channels)
ks = self.Wk(source_input).reshape(-1, self.num_heads, self.out_channels)
if self.use_weight:
vs = self.Wv(source_input).reshape(-1, self.num_heads, self.out_channels)
else:
vs = source_input.reshape(-1, 1, self.out_channels)
# normalize input
qs = qs / torch.norm(qs, p=2) # [N, H, M]
ks = ks / torch.norm(ks, p=2) # [L, H, M]
N = qs.shape[0]
# # numerator
# kvs = torch.einsum("lhm,lhd->hmd", ks, vs)
# attention_num = torch.einsum("nhm,hmd->nhd", qs, kvs) # [N, H, D]
# attention_num += N * vs
#
# # denominator
# all_ones = torch.ones([ks.shape[0]]).to(ks.device)
# ks_sum = torch.einsum("lhm,l->hm", ks, all_ones)
# attention_normalizer = torch.einsum("nhm,hm->nh", qs, ks_sum) # [N, H]
#
# # attentive aggregated results
# attention_normalizer = torch.unsqueeze(
# attention_normalizer, len(attention_normalizer.shape)) # [N, H, 1]
# attention_normalizer += torch.ones_like(attention_normalizer) * N
# attn_output = attention_num / attention_normalizer # [N, H, D]
# numerator
attention_num = torch.sigmoid(torch.einsum("nhm,lhm->nlh", qs, ks)) # [N, L, H]
# denominator
all_ones = torch.ones([ks.shape[0]]).to(ks.device)
attention_normalizer = torch.einsum("nlh,l->nh", attention_num, all_ones)
attention_normalizer = attention_normalizer.unsqueeze(1).repeat(1, ks.shape[0], 1) # [N, L, H]
# compute attention and attentive aggregated results
attention = attention_num / attention_normalizer
attn_output = torch.einsum("nlh,lhd->nhd", attention, vs) # [N, H, D]
# compute attention for visualization if needed
if output_attn:
attention = torch.einsum("nhm,lhm->nlh", qs, ks).mean(dim=-1) # [N, N]
normalizer = attention_normalizer.squeeze(dim=-1).mean(dim=-1, keepdims=True) # [N,1]
attention = attention / normalizer
final_output = attn_output.mean(dim=1)
if output_attn:
return final_output, attention
else:
return final_output
class TransConv(nn.Module):
def __init__(self, in_channels, hidden_channels, num_layers=2, num_heads=1,
dropout=0.5, use_bn=True, use_residual=True, use_weight=True, use_act=True):
super().__init__()
self.convs = nn.ModuleList()
self.fcs = nn.ModuleList()
self.fcs.append(nn.Linear(in_channels, hidden_channels))
self.bns = nn.ModuleList()
self.bns.append(nn.LayerNorm(hidden_channels))
for i in range(num_layers):
self.convs.append(
TransConvLayer(hidden_channels, hidden_channels, num_heads=num_heads, use_weight=use_weight))
self.bns.append(nn.LayerNorm(hidden_channels))
self.dropout = dropout
self.activation = F.relu
self.use_bn = use_bn
self.use_residual = use_residual
self.use_act = use_act
def reset_parameters(self):
for conv in self.convs:
conv.reset_parameters()
for bn in self.bns:
bn.reset_parameters()
for fc in self.fcs:
fc.reset_parameters()
def forward(self, x):
layer_ = []
# input MLP layer
x = self.fcs[0](x)
if self.use_bn:
x = self.bns[0](x)
x = self.activation(x)
x = F.dropout(x, p=self.dropout, training=self.training)
# store as residual link
layer_.append(x)
for i, conv in enumerate(self.convs):
# graph convolution with full attention aggregation
x = conv(x, x)
if self.use_residual:
x = (x + layer_[i]) / 2.
if self.use_bn:
x = self.bns[i + 1](x)
if self.use_act:
x = self.activation(x)
x = F.dropout(x, p=self.dropout, training=self.training)
layer_.append(x)
return x
def get_attentions(self, x):
layer_, attentions = [], []
x = self.fcs[0](x)
if self.use_bn:
x = self.bns[0](x)
x = self.activation(x)
layer_.append(x)
for i, conv in enumerate(self.convs):
x, attn = conv(x, x, output_attn=True)
attentions.append(attn)
if self.use_residual:
x = (x + layer_[i]) / 2.
if self.use_bn:
x = self.bns[i + 1](x)
if self.use_act:
x = self.activation(x)
layer_.append(x)
return torch.stack(attentions, dim=0) # [layer num, N, N]
class SGFormer(nn.Module):
def __init__(self, in_channels, hidden_channels, out_channels,
trans_num_layers=1, trans_num_heads=1, trans_dropout=0.5, trans_use_bn=True, trans_use_residual=True, trans_use_weight=True, trans_use_act=True,
gnn_num_layers=1, gnn_dropout=0.5, gnn_use_weight=True, gnn_use_init=False, gnn_use_bn=True, gnn_use_residual=True, gnn_use_act=True,
use_graph=True, graph_weight=0.8, aggregate='add'):
super().__init__()
self.trans_conv = TransConv(in_channels, hidden_channels, trans_num_layers, trans_num_heads, trans_dropout, trans_use_bn, trans_use_residual, trans_use_weight, trans_use_act)
self.graph_conv = GraphConv(in_channels, hidden_channels, gnn_num_layers, gnn_dropout, gnn_use_bn, gnn_use_residual, gnn_use_weight, gnn_use_init, gnn_use_act)
self.use_graph = use_graph
self.graph_weight = graph_weight
self.aggregate = aggregate
if aggregate == 'add':
self.fc = nn.Linear(hidden_channels, out_channels)
elif aggregate == 'cat':
self.fc = nn.Linear(2 * hidden_channels, out_channels)
else:
raise ValueError(f'Invalid aggregate type:{aggregate}')
self.params1 = list(self.trans_conv.parameters())
self.params2 = list(self.graph_conv.parameters()) if self.graph_conv is not None else []
self.params2.extend(list(self.fc.parameters()))
def forward(self, x, edge_index,batch):
x1 = self.trans_conv(x)
if self.use_graph:
x2 = self.graph_conv(x, edge_index)
if self.aggregate == 'add':
x = self.graph_weight * x2 + (1 - self.graph_weight) * x1
else:
x = torch.cat((x1, x2), dim=1)
else:
x = x1
x = self.fc(x)
# 汇聚步骤
if self.use_graph:
x = torch.cat((x1, x2), dim=1) if self.aggregate == 'cat' else self.graph_weight * x2 + (1 - self.graph_weight) * x1
else:
x = x1
# 使用全局平均汇聚
x = global_mean_pool(x, batch)
# 全连接层
x = self.fc(x)
return x
# def forward(self, x, edge_index, batch):
# x = self.trans_conv(x) # 使用 Transformer 部分
# x = global_mean_pool(x, batch) # 汇聚节点特征到图特征
# x = self.fc(x) # 全连接层
# return x
# def forward(self, x, edge_index, batch):
# x = self.graph_conv(x, edge_index) # 使用 GNN 部分
# x = global_mean_pool(x, batch) # 汇聚节点特征到图特征
# x = self.fc(x) # 全连接层
# return x
def get_attentions(self, x):
attns = self.trans_conv.get_attentions(x) # [layer num, N, N]
return attns
def reset_parameters(self):
self.trans_conv.reset_parameters()
if self.use_graph:
self.graph_conv.reset_parameters()