-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathours.py
517 lines (429 loc) · 18.1 KB
/
ours.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import habana_frameworks.torch.core as htcore
import math
import os
from multiprocessing.sharedctypes import Value
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from models import GCN
from torch_geometric.utils import degree
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]
row, col = edge_index
# Create dense adjacency matrix
adj = torch.zeros((N, N), device=x.device)
adj[row, col] = 1.0 # Initialize with unweighted adjacency matrix
# Calculate degree normalization
degree = adj.sum(dim=1) # Row sum gives degree for each node
d_norm = degree.pow(-0.5) # Compute D^(-0.5)
d_norm[torch.isinf(d_norm)] = 0.0 # Replace inf values with 0
# Apply normalization to adjacency matrix
D_norm = torch.diag(d_norm)
adj = torch.matmul(D_norm, torch.matmul(adj, D_norm))
# Dense matrix multiplication
x = torch.matmul(adj, x) # [N, D]
# Apply learnable weight transformation if required
if self.use_init:
x = torch.cat([x, x0], dim=1) # Concatenate with initial feature
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
def full_attention_conv(qs, ks, vs, output_attn=False):
# 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]
# 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
if output_attn:
return attn_output, attention
else:
return attn_output
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, edge_index=None, edge_weight=None, output_attn=False):
# feature transformation
query = self.Wq(query_input).reshape(-1,
self.num_heads, self.out_channels)
key = self.Wk(source_input).reshape(-1,
self.num_heads, self.out_channels)
if self.use_weight:
value = self.Wv(source_input).reshape(-1,
self.num_heads, self.out_channels)
else:
value = source_input.reshape(-1, 1, self.out_channels)
# compute full attentive aggregation
if output_attn:
attention_output, attn = full_attention_conv(
query, key, value, output_attn) # [N, H, D]
else:
attention_output = full_attention_conv(
query, key, value) # [N, H, D]
final_output = attention_output
final_output = final_output.mean(dim=1)
if output_attn:
return final_output, attn
else:
return final_output
class TransConv(nn.Module):
def __init__(self, in_channels, hidden_channels, num_layers=2, num_heads=1,
alpha=0.5, dropout=0.5, use_bn=True, use_residual=True, use_weight=True, use_act=False):
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.residual = use_residual
self.alpha = alpha
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, data):
x = data.graph['node_feat']
edge_index = data.graph['edge_index']
edge_weight = data.graph['edge_weight'] if 'edge_weight' in data.graph else None
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)
layer_.append(x)
for i, conv in enumerate(self.convs):
# graph convolution with full attention aggregation
x = conv(x, x, edge_index, edge_weight)
if self.residual:
x = self.alpha * x + (1-self.alpha) * layer_[i]
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.residual:
x = self.alpha * x + (1 - self.alpha) * layer_[i]
if self.use_bn:
x = self.bns[i + 1](x)
layer_.append(x)
return torch.stack(attentions, dim=0) # [layer num, N, N]
class TransConvLayerLarge(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]
# 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 TransConvLarge(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(
TransConvLayerLarge(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, num_layers=2, num_heads=1,
alpha=0.5, dropout=0.5, use_bn=True, use_residual=True, use_weight=True, use_graph=True, use_act=False, graph_weight=0.8, gnn=None, aggregate='add'):
super().__init__()
self.trans_conv=TransConv(in_channels,hidden_channels,num_layers,num_heads,alpha,dropout,use_bn,use_residual,use_weight)
self.gnn=gnn
self.use_graph=use_graph
self.graph_weight=graph_weight
self.use_act=use_act
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.gnn.parameters()) if self.gnn is not None else []
self.params2.extend(list(self.fc.parameters()) )
def forward(self,data):
x1=self.trans_conv(data)
if self.use_graph:
x2=self.gnn(data)
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)
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.gnn.reset_parameters()
class SGFormer_large(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 = TransConvLarge(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):
# Convert edge_index to dense adjacency matrix
N = x.shape[0]
# row, col = edge_index
# adj_dense = torch.zeros((N, N), device=x.device)
# adj_dense[row, col] = 1.0
# TransConv uses dense adjacency internally
x1 = self.trans_conv(x)
# GraphConv now uses dense adjacency
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)
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()