-
Notifications
You must be signed in to change notification settings - Fork 1
/
gsl.py
129 lines (114 loc) · 4.62 KB
/
gsl.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
import torch
import torch.nn as nn
import torch.nn.functional as F
# A = ReLu(W)
class Graph_ReLu_W(nn.Module):
def __init__(self, n_nodes, k, device):
super(Graph_ReLu_W, self).__init__()
self.num_nodes = n_nodes
self.k = k
self.A = nn.Parameter(torch.randn(n_nodes, n_nodes).to(device),
requires_grad=True).to(device)
def forward(self, idx):
adj = F.relu(self.A)
if self.k:
mask = torch.zeros(idx.size(0), idx.size(0)).to(self.device)
mask.fill_(float('0'))
v, id = (adj + torch.rand_like(adj)*0.01).topk(self.k, 1)
mask.scatter_(1, id, v.fill_(1))
adj = adj*mask
return adj
# A for Directed graphs:
class Graph_Directed_A(nn.Module):
def __init__(self, n_nodes, window_size, alpha, k, device):
super(Graph_Directed_A, self).__init__()
self.alpha = alpha
self.k = k
self.device = device
self.e1 = nn.Embedding(n_nodes, window_size)
self.e2 = nn.Embedding(n_nodes, window_size)
self.l1 = nn.Linear(window_size, window_size)
self.l2 = nn.Linear(window_size, window_size)
def forward(self, idx):
m1 = torch.tanh(self.alpha*self.l1(self.e1(idx)))
m2 = torch.tanh(self.alpha*self.l2(self.e2(idx)))
adj = F.relu(torch.tanh(self.alpha*torch.mm(m1, m2.transpose(1, 0))))
if self.k:
mask = torch.zeros(idx.size(0), idx.size(0)).to(self.device)
mask.fill_(float('0'))
v, id = (adj + torch.rand_like(adj)*0.01).topk(self.k, 1)
mask.scatter_(1, id, v.fill_(1))
adj = adj*mask
return adj
# A for Uni-directed graphs:
class Graph_Uni_Directed_A(nn.Module):
def __init__(self, n_nodes, window_size, alpha, k, device):
super(Graph_Directed_A, self).__init__()
self.alpha = alpha
self.k = k
self.device = device
self.e1 = nn.Embedding(n_nodes, window_size)
self.e2 = nn.Embedding(n_nodes, window_size)
self.l1 = nn.Linear(window_size, window_size)
self.l2 = nn.Linear(window_size, window_size)
def forward(self, idx):
m1 = torch.tanh(self.alpha*self.l1(self.e1(idx)))
m2 = torch.tanh(self.alpha*self.l2(self.e2(idx)))
adj = F.relu(torch.tanh(self.alpha*(torch.mm(m1, m2.transpose(1, 0))
- torch.mm(m2, m1.transpose(1, 0)))))
if self.k:
mask = torch.zeros(idx.size(0), idx.size(0)).to(self.device)
mask.fill_(float('0'))
v, id = (adj + torch.rand_like(adj)*0.01).topk(self.k, 1)
mask.scatter_(1, id, v.fill_(1))
adj = adj*mask
return adj
# A for Undirected graphs:
class Graph_Undirected_A(nn.Module):
def __init__(self, n_nodes, window_size, alpha, k, device):
super(Graph_Directed_A, self).__init__()
self.alpha = alpha
self.k = k
self.device = device
self.e1 = nn.Embedding(n_nodes, window_size)
self.l1 = nn.Linear(window_size, window_size)
def forward(self, idx):
m1 = torch.tanh(self.alpha*self.l1(self.e1(idx)))
m2 = torch.tanh(self.alpha*self.l1(self.e1(idx)))
adj = F.relu(torch.tanh(self.alpha*torch.mm(m1, m2.transpose(1, 0))))
if self.k:
mask = torch.zeros(idx.size(0), idx.size(0)).to(self.device)
mask.fill_(float('0'))
v, id = (adj + torch.rand_like(adj)*0.01).topk(self.k, 1)
mask.scatter_(1, id, v.fill_(1))
adj = adj*mask
return adj
class GSL(nn.Module):
"""
Graph structure learning block.
"""
def __init__(
self,
gsl_type,
n_nodes,
window_size,
alpha,
k,
device):
super(GSL, self).__init__()
self.gsl_layer = None
if gsl_type == 'relu':
self.gsl_layer = Graph_ReLu_W(n_nodes, k, device)
elif gsl_type == 'directed':
self.self.gsl_layer = Graph_Directed_A(n_nodes, window_size,
alpha, k, device)
elif gsl_type == 'unidirected':
self.self.gsl_layer = Graph_Uni_Directed_A(n_nodes, window_size,
alpha, k, device)
elif gsl_type == 'undirected':
self.self.gsl_layer = Graph_Undirected_A(n_nodes, window_size,
alpha, k, device)
else:
print('Wrong name of graph structure learning layer!')
def forward(self, idx):
return self.gsl_layer(idx)