-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmodels.py
587 lines (433 loc) · 19.1 KB
/
models.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import torch.nn as nn
import torch.nn.functional as F
import math
import torch
from torch.nn.parameter import Parameter
from torch.nn.modules.module import Module
from torch.nn import init
import ipdb
#--------------
### layers###
#--------------
#GCN layer
class GraphConvolution(Module):
"""
Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
"""
def __init__(self, in_features, out_features, bias=True):
super(GraphConvolution, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = Parameter(torch.FloatTensor(in_features, out_features))
if bias:
self.bias = Parameter(torch.FloatTensor(out_features))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def forward(self, input, adj):
support = torch.mm(input, self.weight)
output = torch.spmm(adj, support)
#for 3_D batch, need a loop!!!
if self.bias is not None:
return output + self.bias
else:
return output
#Multihead attention layer
class MultiHead(Module):#currently, allowed for only one sample each time. As no padding mask is required.
def __init__(
self,
input_dim,
num_heads,
kdim=None,
vdim=None,
embed_dim = 128,#should equal num_heads*head dim
v_embed_dim = None,
dropout=0.1,
bias=True,
):
super(MultiHead, self).__init__()
self.input_dim = input_dim
self.kdim = kdim if kdim is not None else input_dim
self.vdim = vdim if vdim is not None else input_dim
self.num_heads = num_heads
self.embed_dim = embed_dim
self.v_embed_dim = v_embed_dim if v_embed_dim is not None else embed_dim
self.dropout = dropout
self.head_dim = embed_dim // num_heads
self.bias = bias
assert (
self.head_dim * num_heads == self.embed_dim
), "embed_dim must be divisible by num_heads"
assert self.v_embed_dim % num_heads ==0, "v_embed_dim must be divisible by num_heads"
self.scaling = self.head_dim ** -0.5
self.q_proj = nn.Linear(self.input_dim, self.embed_dim, bias=bias)
self.k_proj = nn.Linear(self.kdim, self.embed_dim, bias=bias)
self.v_proj = nn.Linear(self.vdim, self.v_embed_dim, bias=bias)
self.out_proj = nn.Linear(self.v_embed_dim, self.v_embed_dim//self.num_heads, bias=bias)
self.reset_parameters()
def reset_parameters(self):
if True:
# Empirically observed the convergence to be much better with
# the scaled initialization
nn.init.normal_(self.k_proj.weight)
nn.init.normal_(self.v_proj.weight)
nn.init.normal_(self.q_proj.weight)
else:
nn.init.normal_(self.k_proj.weight)
nn.init.normal_(self.v_proj.weight)
nn.init.normal_(self.q_proj.weight)
nn.init.normal_(self.out_proj.weight)
if self.out_proj.bias is not None:
nn.init.constant_(self.out_proj.bias, 0.)
if self.bias:
nn.init.constant_(self.k_proj.bias, 0.)
nn.init.constant_(self.v_proj.bias, 0.)
nn.init.constant_(self.q_proj.bias, 0.)
def forward(
self,
query,
key,
value,
need_weights: bool = False,
need_head_weights: bool = False,
):
"""Input shape: Time x Batch x Channel
Args:
need_weights (bool, optional): return the attention weights,
averaged over heads (default: False).
need_head_weights (bool, optional): return the attention
weights for each head. Implies *need_weights*. Default:
return the average attention weights over all heads.
"""
if need_head_weights:
need_weights = True
batch_num, node_num, input_dim = query.size()
assert key is not None and value is not None
#project input
q = self.q_proj(query)
k = self.k_proj(key)
v = self.v_proj(value)
q = q * self.scaling
#compute attention
q = q.view(batch_num, node_num, self.num_heads, self.head_dim).transpose(-2,-3).contiguous().view(batch_num*self.num_heads, node_num, self.head_dim)
k = k.view(batch_num, node_num, self.num_heads, self.head_dim).transpose(-2,-3).contiguous().view(batch_num*self.num_heads, node_num, self.head_dim)
v = v.view(batch_num, node_num, self.num_heads, self.vdim).transpose(-2,-3).contiguous().view(batch_num*self.num_heads, node_num, self.vdim)
attn_output_weights = torch.bmm(q, k.transpose(-1,-2))
attn_output_weights = F.softmax(attn_output_weights, dim=-1)
#drop out
attn_output_weights = F.dropout(attn_output_weights, p=self.dropout, training=self.training)
#collect output
attn_output = torch.bmm(attn_output_weights, v)
attn_output = attn_output.view(batch_num, self.num_heads, node_num, self.vdim).transpose(-2,-3).contiguous().view(batch_num, node_num, self.v_embed_dim)
attn_output = self.out_proj(attn_output)
if need_weights:
attn_output_weights = attn_output_weights #view: (batch_num, num_heads, node_num, node_num)
return attn_output, attn_output_weights.sum(dim=1) / self.num_heads
else:
return attn_output
#Graphsage layer
class SageConv(Module):
"""
Simple Graphsage layer
"""
def __init__(self, in_features, out_features, bias=False):
super(SageConv, self).__init__()
self.proj = nn.Linear(in_features*2, out_features, bias=bias)
self.reset_parameters()
#print("note: for dense graph in graphsage, require it normalized.")
def reset_parameters(self):
nn.init.normal_(self.proj.weight)
if self.proj.bias is not None:
nn.init.constant_(self.proj.bias, 0.)
def forward(self, features, adj):
"""
Args:
adj: can be sparse or dense matrix.
"""
#fuse info from neighbors. to be added:
if adj.layout != torch.sparse_coo:
if len(adj.shape) == 3:
neigh_feature = torch.bmm(adj, features) / (adj.sum(dim=1).reshape((adj.shape[0], adj.shape[1],-1))+1)
else:
neigh_feature = torch.mm(adj, features) / (adj.sum(dim=1).reshape(adj.shape[0], -1)+1)
else:
#print("spmm not implemented for batch training. Note!")
neigh_feature = torch.spmm(adj, features) / (adj.to_dense().sum(dim=1).reshape(adj.shape[0], -1)+1)
#perform conv
data = torch.cat([features,neigh_feature], dim=-1)
combined = self.proj(data)
return combined
#GraphAT layers
class GraphAttentionLayer(nn.Module):
"""
Simple GAT layer, similar to https://arxiv.org/abs/1710.10903
"""
def __init__(self, in_features, out_features, dropout, alpha, concat=True):
super(GraphAttentionLayer, self).__init__()
self.dropout = dropout
self.in_features = in_features
self.out_features = out_features
self.alpha = alpha
self.concat = concat
self.W = nn.Parameter(torch.zeros(size=(in_features, out_features)))
nn.init.xavier_uniform_(self.W.data, gain=1.414)
self.a = nn.Parameter(torch.zeros(size=(2*out_features, 1)))
nn.init.xavier_uniform_(self.a.data, gain=1.414)
self.leakyrelu = nn.LeakyReLU(self.alpha)
def forward(self, input, adj):
if isinstance(adj, torch.sparse.FloatTensor):
adj = adj.to_dense()
h = torch.mm(input, self.W)
N = h.size()[0]
a_input = torch.cat([h.repeat(1, N).view(N * N, -1), h.repeat(N, 1)], dim=1).view(N, -1, 2 * self.out_features)
e = self.leakyrelu(torch.matmul(a_input, self.a).squeeze(2))
zero_vec = -9e15*torch.ones_like(e)
attention = torch.where(adj > 0, e, zero_vec)
attention = F.softmax(attention, dim=1)
attention = F.dropout(attention, self.dropout, training=self.training)
h_prime = torch.matmul(attention, h)
if self.concat:
return F.elu(h_prime)
else:
return h_prime
def __repr__(self):
return self.__class__.__name__ + ' (' + str(self.in_features) + ' -> ' + str(self.out_features) + ')'
class SpecialSpmmFunction(torch.autograd.Function):
"""Special function for only sparse region backpropataion layer."""
@staticmethod
def forward(ctx, indices, values, shape, b):
assert indices.requires_grad == False
a = torch.sparse_coo_tensor(indices, values, shape)
ctx.save_for_backward(a, b)
ctx.N = shape[0]
return torch.matmul(a, b)
@staticmethod
def backward(ctx, grad_output):
a, b = ctx.saved_tensors
grad_values = grad_b = None
if ctx.needs_input_grad[1]:
grad_a_dense = grad_output.matmul(b.t())
edge_idx = a._indices()[0, :] * ctx.N + a._indices()[1, :]
grad_values = grad_a_dense.view(-1)[edge_idx]
if ctx.needs_input_grad[3]:
grad_b = a.t().matmul(grad_output)
return None, grad_values, None, grad_b
class SpecialSpmm(nn.Module):
def forward(self, indices, values, shape, b):
return SpecialSpmmFunction.apply(indices, values, shape, b)
class SpGraphAttentionLayer(nn.Module):
"""
Sparse version GAT layer, similar to https://arxiv.org/abs/1710.10903
"""
def __init__(self, in_features, out_features, dropout, alpha, concat=True):
super(SpGraphAttentionLayer, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.alpha = alpha
self.concat = concat
self.W = nn.Parameter(torch.zeros(size=(in_features, out_features)))
nn.init.xavier_normal_(self.W.data, gain=1.414)
self.a = nn.Parameter(torch.zeros(size=(1, 2*out_features)))
nn.init.xavier_normal_(self.a.data, gain=1.414)
self.dropout = nn.Dropout(dropout)
self.leakyrelu = nn.LeakyReLU(self.alpha)
self.special_spmm = SpecialSpmm()
def forward(self, input, adj):
dv = 'cuda' if input.is_cuda else 'cpu'
N = input.size()[0]
edge = adj.nonzero().t()
h = torch.mm(input, self.W)
# h: N x out
assert not torch.isnan(h).any()
# Self-attention on the nodes - Shared attention mechanism
edge_h = torch.cat((h[edge[0, :], :], h[edge[1, :], :]), dim=1).t()
# edge: 2*D x E
edge_e = torch.exp(-self.leakyrelu(self.a.mm(edge_h).squeeze()))
assert not torch.isnan(edge_e).any()
# edge_e: E
e_rowsum = self.special_spmm(edge, edge_e, torch.Size([N, N]), torch.ones(size=(N,1), device=dv))
# e_rowsum: N x 1
edge_e = self.dropout(edge_e)
# edge_e: E
h_prime = self.special_spmm(edge, edge_e, torch.Size([N, N]), h)
assert not torch.isnan(h_prime).any()
# h_prime: N x out
h_prime = h_prime.div(e_rowsum)
# h_prime: N x out
assert not torch.isnan(h_prime).any()
if self.concat:
# if this layer is not last layer,
return F.elu(h_prime)
else:
# if this layer is last layer,
return h_prime
def __repr__(self):
return self.__class__.__name__ + ' (' + str(self.in_features) + ' -> ' + str(self.out_features) + ')'
#--------------
### models ###
#--------------
#gcn_encode
class GCN_En(nn.Module):
def __init__(self, nfeat, nhid, nembed, dropout):
super(GCN_En, self).__init__()
self.gc1 = GraphConvolution(nfeat, nhid)
self.dropout = dropout
def forward(self, x, adj):
x = F.relu(self.gc1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
return x
class GCN_En2(nn.Module):
def __init__(self, nfeat, nhid, nembed, dropout):
super(GCN_En2, self).__init__()
self.gc1 = GraphConvolution(nfeat, nhid)
self.gc2 = GraphConvolution(nhid, nembed)
self.dropout = dropout
def forward(self, x, adj):
x = F.relu(self.gc1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = F.relu(self.gc2(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
return x
class GCN_Classifier(nn.Module):
def __init__(self, nembed, nhid, nclass, dropout):
super(GCN_Classifier, self).__init__()
self.gc1 = GraphConvolution(nembed, nhid)
self.mlp = nn.Linear(nhid, nclass)
self.dropout = dropout
self.reset_parameters()
def reset_parameters(self):
nn.init.normal_(self.mlp.weight,std=0.05)
def forward(self, x, adj):
x = F.relu(self.gc1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = self.mlp(x)
return x
#sage model
class Sage_En(nn.Module):
def __init__(self, nfeat, nhid, nembed, dropout):
super(Sage_En, self).__init__()
self.sage1 = SageConv(nfeat, nembed)
self.dropout = dropout
def forward(self, x, adj):
x = F.relu(self.sage1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
return x
class Sage_En2(nn.Module):
def __init__(self, nfeat, nhid, nembed, dropout):
super(Sage_En2, self).__init__()
self.sage1 = SageConv(nfeat, nhid)
self.sage2 = SageConv(nhid, nembed)
self.dropout = dropout
def forward(self, x, adj):
x = F.relu(self.sage1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = F.relu(self.sage2(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
return x
class Sage_Classifier(nn.Module):
def __init__(self, nembed, nhid, nclass, dropout):
super(Sage_Classifier, self).__init__()
self.sage1 = SageConv(nembed, nhid)
self.mlp = nn.Linear(nhid, nclass)
self.dropout = dropout
self.reset_parameters()
def reset_parameters(self):
nn.init.normal_(self.mlp.weight,std=0.05)
def forward(self, x, adj):
x = F.relu(self.sage1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = self.mlp(x)
return x
#GAT model
class GAT_En(nn.Module):
def __init__(self, nfeat, nhid, nembed, dropout, alpha=0.2, nheads=8):
super(GAT_En, self).__init__()
self.attentions = [GraphAttentionLayer(nfeat, nhid, dropout=dropout, alpha=alpha, concat=True) for _ in range(nheads)]
for i, attention in enumerate(self.attentions):
self.add_module('attention_{}'.format(i), attention)
self.out_proj = nn.Linear(nhid * nheads, nembed)
self.dropout = dropout
self.reset_parameters()
def reset_parameters(self):
nn.init.normal_(self.out_proj.weight,std=0.05)
def forward(self, x, adj):
x = torch.cat([att(x, adj) for att in self.attentions], dim=1)
x = F.dropout(x, self.dropout, training=self.training)
x = F.elu(self.out_proj(x))
return x
class GAT_En2(nn.Module):
def __init__(self, nfeat, nhid, nembed, dropout, alpha=0.2, nheads=8):
super(GAT_En2, self).__init__()
self.attentions = [GraphAttentionLayer(nfeat, nhid, dropout=dropout, alpha=alpha, concat=True) for _ in range(nheads)]
for i, attention in enumerate(self.attentions):
self.add_module('attention_{}'.format(i), attention)
self.out_proj = nn.Linear(nhid * nheads, nembed)
self.dropout = dropout
self.attentions_2 = [GraphAttentionLayer(nembed, nembed, dropout=dropout, alpha=alpha, concat=True) for _ in range(nheads)]
for i, attention in enumerate(self.attentions_2):
self.add_module('attention2_{}'.format(i), attention)
self.out_proj_2 = nn.Linear(nembed * nheads, nembed)
self.reset_parameters()
def reset_parameters(self):
nn.init.normal_(self.out_proj.weight,std=0.05)
nn.init.normal_(self.out_proj_2.weight,std=0.05)
def forward(self, x, adj):
x = torch.cat([att(x, adj) for att in self.attentions], dim=1)
x = F.dropout(x, self.dropout, training=self.training)
x = F.elu(self.out_proj(x))
x = torch.cat([att(x, adj) for att in self.attentions_2], dim=1)
x = F.dropout(x, self.dropout, training=self.training)
x = F.elu(self.out_proj_2(x))
return x
class GAT_Classifier(nn.Module):
def __init__(self, nembed, nhid, nclass, dropout, alpha=0.2, nheads=8):
super(GAT_Classifier, self).__init__()
self.attentions = [GraphAttentionLayer(nembed, nhid, dropout=dropout, alpha=alpha, concat=True) for _ in range(nheads)]
for i, attention in enumerate(self.attentions):
self.add_module('attention_{}'.format(i), attention)
self.out_proj = nn.Linear(nhid * nheads, nhid)
self.dropout = dropout
self.mlp = nn.Linear(nhid, nclass)
self.dropout = dropout
self.reset_parameters()
def reset_parameters(self):
nn.init.normal_(self.mlp.weight,std=0.05)
nn.init.normal_(self.out_proj.weight,std=0.05)
def forward(self, x, adj):
x = torch.cat([att(x, adj) for att in self.attentions], dim=1)
x = F.dropout(x, self.dropout, training=self.training)
x = F.elu(self.out_proj(x))
x = self.mlp(x)
return x
class Classifier(nn.Module):
def __init__(self, nembed, nhid, nclass, dropout):
super(Classifier, self).__init__()
self.mlp = nn.Linear(nhid, nclass)
self.dropout = dropout
self.reset_parameters()
def reset_parameters(self):
nn.init.normal_(self.mlp.weight,std=0.05)
def forward(self, x, adj):
x = self.mlp(x)
return x
class Decoder(Module):
"""
Simple Graphsage layer
"""
def __init__(self, nembed, dropout=0.1):
super(Decoder, self).__init__()
self.dropout = dropout
self.de_weight = Parameter(torch.FloatTensor(nembed, nembed))
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.de_weight.size(1))
self.de_weight.data.uniform_(-stdv, stdv)
def forward(self, node_embed):
combine = F.linear(node_embed, self.de_weight)
adj_out = torch.sigmoid(torch.mm(combine, combine.transpose(-1,-2)))
return adj_out