-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_module.py
198 lines (161 loc) · 5.76 KB
/
filter_module.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
import math
import numpy as np
import scipy.sparse as sp
from scipy.sparse.linalg import expm
from scipy.special import iv
from sklearn import preprocessing
class HeatKernel(object):
def __init__(self, t=0.5, theta0=0.6, theta1=0.4):
self.t = t
self.theta0 = theta0
self.theta1 = theta1
def prop_adjacency(self, mx):
mx_norm = preprocessing.normalize(mx.transpose(), "l1").transpose()
adj = self.t * mx_norm
adj.data = np.exp(adj.data)
return adj / np.exp(self.t)
def prop(self, mx, emb):
adj = self.prop_adjacency(mx)
return self.theta0 * emb + self.theta1 * adj.dot(emb)
class HeatKernelApproximation(object):
def __init__(self, t=0.2, k=5):
self.t = t
self.k = k
def taylor(self, mx_norm, emb):
result = [math.exp(self.t) * emb]
for i in range(self.k - 1):
temp_mx = self.t * mx_norm.dot(result[-1]) / (i + 1)
result.append(temp_mx)
return sum(result)
def chebyshev(self, mx, emb):
mx = mx + sp.eye(emb.shape[0])
mx = preprocessing.normalize(mx, "l1")
conv = iv(0, self.t) * emb
laplacian = sp.eye(emb.shape[0]) - mx
Lx0 = emb
Lx1 = laplacian.dot(emb)
conv -= 2 * iv(1, self.t) * Lx1
for i in range(2, self.k):
Lx2 = 2 * laplacian.dot(Lx1) - Lx0
conv += (-1) ** i * 2 * iv(i, self.t) * Lx2
Lx0 = Lx1
Lx1 = Lx2
return conv
def prop(self, mx, emb):
return self.chebyshev(mx, emb)
class Gaussian(object):
def __init__(self, mu=0.5, theta=1, rescale=False, k=3):
self.theta = theta
self.mu = mu
self.k = k
self.rescale = rescale
self.coefs = [(-1) ** i * iv(i, self.theta) for i in range(k+3)]
self.coefs[0] = self.coefs[0] / 2
# adj: 1 mul + 3 add, emb: 2*k mul, 3*k add
def prop(self, mx_norm, emb):
row_num, col_sum = mx_norm.shape
mx_hat = (1 - self.mu) * sp.eye(row_num) - mx_norm
Lx0 = emb
Lx1 = mx_hat.dot(emb)
Lx1 = 0.5 * mx_hat.dot(Lx1) - emb
conv = iv(0, self.theta) * Lx0
conv -= 2 * iv(1, self.theta) * Lx1
for i in range(2, self.k):
Lx2 = mx_hat.dot(Lx1)
Lx2 = (mx_hat.dot(Lx2) - 2 * Lx1) - Lx0
# Lx2 = 2 * mx_hat.dot(Lx1) - Lx0
conv += (-1) ** i * 2 * iv(i, self.theta) * Lx2
Lx0 = Lx1
Lx1 = Lx2
if self.rescale:
conv = mx.dot(emb - conv)
return conv
class PPR(object):
"""
applying sparsification to accelerate computation
"""
def __init__(self, alpha=0.5, k=10):
self.alpha = alpha
self.k = k
self.alpha_list = [self.alpha * (1 - self.alpha) ** i for i in range(self.k)]
self.epsilon = 1e-3
def prop(self, mx, emb):
row_num, col_num = mx.shape
if row_num < 1000:
# stable state
row, col = mx.nonzero
degree = mx.sum(1).A.squeeze()
degree_sqrt_inv_mx = sp.csc_matrix((np.sqrt(1. / degree), (row, col)), shape=(row, col))
identity = sp.csc_matrix((np.ones(row_num), (np.arange(row), np.arange(col_num))), shape=(row, col))
ppr_mx = identity - (1 - self.alpha) * degree_sqrt_inv_mx.dot(mx).dot(degree_sqrt_inv_mx)
return self.alpha * sp.linalg.inv(ppr_mx).dot(emb)
else:
# k-1 add, k-1 mul
mx_norm = preprocessing.normalize(mx, "l1")
Lx = emb
conv = self.alpha * Lx
for i in range(1, self.k):
Lx = (1 - self.alpha) * mx_norm.dot(Lx)
conv += Lx
return conv
class SignalRescaling(object):
"""
- rescale signal of each node according to the degree of the node:
- sigmoid(degree)
- sigmoid(1/degree)
"""
def __init__(self):
pass
def prop(self, mx, emb):
mx = preprocessing.normalize(mx, "l1")
degree = mx.sum(1).A.squeeze()
degree_inv = 1. / degree
signal_val = 1./(1+np.exp(-degree_inv))
row_num, col_num = mx.shape
q_ = sp.csc_matrix((signal_val, (np.arange(row_num), np.arange(col_num))), shape=(row_num, col_num))
adj_norm = mx.dot(q_)
adj_norm = preprocessing.normalize(adj_norm, "l1")
conv = adj_norm.dot(emb)
return conv
class ProNE(object):
def __call__(self, A, a, order=10, mu=0.1, s=0.5):
# NE Enhancement via Spectral Propagation
print('Chebyshev Series -----------------')
if order == 1:
return a
node_number = a.shape[0]
A = sp.eye(node_number) + A
DA = preprocessing.normalize(A, norm='l1')
L = sp.eye(node_number) - DA
M = L - mu * sp.eye(node_number)
Lx0 = a
Lx1 = M.dot(a)
Lx1 = 0.5 * M.dot(Lx1) - a
conv = iv(0, s) * Lx0
conv -= 2 * iv(1, s) * Lx1
for i in range(2, order):
Lx2 = M.dot(Lx1)
Lx2 = (M.dot(Lx2) - 2 * Lx1) - Lx0
# Lx2 = 2*L.dot(Lx1) - Lx0
if i % 2 == 0:
conv += 2 * iv(i, s) * Lx2
else:
conv -= 2 * iv(i, s) * Lx2
Lx0 = Lx1
Lx1 = Lx2
del Lx2
mm = A.dot(a - conv)
return mm
class NodeAdaptiveEncoder(object):
"""
- shrink negative values in signal/feature matrix
- no learning
"""
@staticmethod
def prop(signal):
mean_signal = signal.mean(1)
mean_signal = 1./(1 + np.exp(-mean_signal))
sel_row, sel_col = np.where(signal < 0)
mean_signal = mean_signal[sel_row]
signal[sel_row, sel_col] = signal[sel_row, sel_col] * mean_signal
return signal