-
Notifications
You must be signed in to change notification settings - Fork 0
/
RBQR_BKSVD.py
245 lines (201 loc) · 7.52 KB
/
RBQR_BKSVD.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
# encoding=utf8
import os
import numpy as np
import networkx as nx
import scipy as scipy
import scipy.sparse
import scipy.sparse as sp
from scipy import linalg
import scipy.io
from sklearn import preprocessing
import argparse
import time
class RBQR():
def __init__(self, graph_file, emb_file1, emb_file2, dimension):
self.graph = graph_file
self.emb1 = emb_file1
self.emb2 = emb_file2
self.dimension = dimension
data = scipy.io.loadmat(graph_file)
self.matrix0 = data['network'].astype('float32')
self.node_number = self.matrix0.shape[0]
self.randmatrix = np.random.normal(0, 1./np.sqrt(self.node_number),size=[self.node_number, dimension]).astype(dtype='float32')
self.node_number = self.matrix0.shape[0]
print(self.matrix0.shape)
def get_embedding_rand(self, matrix, dimension, blockSize):
# Sparse randomized tSVD for fast embedding
i=0
matrix_ = matrix
Q = None
omg = self.randmatrix
for j in range(0, 3):
omg = matrix_ * omg
omg = np.hsplit(omg, int(dimension/blockSize))
for i in range(0, int(dimension/blockSize)):
q,_ = np.linalg.qr(omg[i])
if i > 0:
q,_ = np.linalg.qr(q-Q.dot(Q.T.dot(q)))
b = q.T * matrix_
Q = np.concatenate((Q, q), 1)
B = np.concatenate((B, b), 0)
else:
Q = q
B = q.T * matrix_
i += blockSize
features_matrix = B/np.linalg.norm(B, axis=0, keepdims=1)
return features_matrix.T
# def get_embedding_rand(self, matrix, dimension, blockSize):
# # Sparse randomized tSVD for fast embedding
# i=0
# matrix_ = matrix
# Q = None
# omg = self.randmatrix
# for j in range(0, 3):
# omg = matrix_ * omg
# omg = np.hsplit(omg, int(dimension/blockSize))
# randmatrix = np.hsplit(self.randmatrix, int(dimension / blockSize))
# for i in range(0, int(dimension/blockSize)):
# if i > 0:
# q, _ = np.linalg.qr(omg[i]-Q.dot(B.dot(randmatrix[i])))
# q,_ = np.linalg.qr(q-Q.dot(Q.T.dot(q)))
# b = q.T * matrix_
# Q = np.concatenate((Q, q), 1)
# B = np.concatenate((B, b), 0)
# else:
# q, _ = np.linalg.qr(omg[i])
# Q = q
# B = q.T * matrix_
# i += blockSize
# features_matrix = B/np.linalg.norm(B, axis=0, keepdims=1)
# return features_matrix.T
def get_embedding_dense(self, matrix, dimension):
# get dense embedding via SVD
t1 = time.time()
#U, s, Vh = linalg.svd(matrix, full_matrices=False, check_finite=False, overwrite_a=True)
U, s, Vh = self.bksvd(matrix)
U = np.array(U)
U = U[:, :dimension]
s = s[:dimension]
s = np.sqrt(s)
U = U * s
U = U / np.linalg.norm(U, axis=1, keepdims=1)
#U = preprocessing.normalize(U, "l2")
print('densesvd time', time.time() - t1)
return U
def pre_factorization(self, tran, mask):
#Network Embedding as Sparse Matrix Factorization
t1 = time.time()
l1 = 0.75
C1 = preprocessing.normalize(tran, "l1")
neg = np.array(C1.sum(axis=0))[0] ** l1
neg = neg / neg.sum()
neg = scipy.sparse.diags(neg, format="csr")
neg = mask.dot(neg)
print("neg", time.time() - t1)
C1.data[C1.data <= 0] = 1
neg.data[neg.data <= 0] = 1
C1.data = np.log(C1.data)
neg.data = np.log(neg.data)
C1 -= neg
F = C1
t1 = time.time()
# features_matrix = self.rank1_deepwalk_matrix(F,self.dimension)
features_matrix = self.get_embedding_rand(F, self.dimension, 16)
print('sparse proximity time', time.time() - t1)
return features_matrix
def bksvd(self,A,k,iter=3,bsize=8):
u = np.zeros(1,A.shape[1])
l = ones(A.shape[0],1)
n = A.shape[0]
K = zeros(A.shape[0],bsize*iter)
block = np.random.normal(A.shape[1],bsize)
block,_ = np.linalg.qr(block)
T = np.zeros(A.shape[1],bsize)
for i in range(iter):
T = A*block - l*(u*block)
block= A.t*T - u.t*(l.t*T)
block,_ = qr(block)
K[:, (i - 1) * bsize + 1:i * bsize] = block
Q,_ = qr(K)
T = A*Q - l*(u*Q)
Ut,St,Vt = np.linalg.svd(T)
S = St[1:k,1:k]
U = Ut[:,1:k]
V = Q*Vt[:,1:k]
return U,S,V
# def taylor_expansion(self, A, a):
# # NE Enhancement via Spectral Propagation
# print('taylor Series -----------------')
# t1 = time.time()
#
#
# A = sp.eye(self.node_number,dtype='float32') + A
# DA = preprocessing.normalize(A, norm='l1')
# L = sp.eye(self.node_number,dtype='float32') - DA
#
# Lx1 = L.dot(a)
# Lx1 = 0.5 * L.dot(Lx1) - a
#
# mm = DA.dot(Lx1)
# emb = self.get_embedding_dense(mm, self.dimension)
# return emb
def taylor_expansion(self, A, a):
# NE Enhancement via Spectral Propagation
print('taylor Series -----------------')
t1 = time.time()
A = sp.eye(self.node_number, dtype='float32') + A
DA = preprocessing.normalize(A, norm='l1')
Lx1 = DA.dot(a)
Lx2 = DA.dot(Lx1)
Lx3 = DA.dot(Lx2)
mm = Lx3 - 2*Lx2 - Lx1
emb = self.get_embedding_dense(mm, self.dimension)
return emb
# def taylor_expansion(self, A, a):
# # NE Enhancement via Spectral Propagation
# print('taylor Series -----------------')
# t1 = time.time()
#
#
# A = sp.eye(self.node_number,dtype='float32') + A
# DA = preprocessing.normalize(A, norm='l1')
# mm = a
# Lx1 = DA.dot(a)
# mm = Lx1
# # Lx1 = DA.dot(Lx1)
# # mm+= Lx1
# # Lx1 = DA.dot(Lx1)
# # mm+= Lx1
# emb = self.get_embedding_dense(mm, self.dimension)
# return emb
def save_embedding(emb_file, features):
np.save(emb_file, features, allow_pickle=False)
def parse_args():
parser = argparse.ArgumentParser(description="Run ProNE.")
parser.add_argument('-graph', nargs='?', default='data/blogcatalog.mat',
help='Graph path')
parser.add_argument('-emb1', nargs='?', default='emb/blogcatalog.emb',
help='Output path of sparse embeddings')
parser.add_argument('-emb2', nargs='?', default='emb/blogcatalog_spectral.emb',
help='Output path of enhanced embeddings')
parser.add_argument('-dimension', type=int, default=128,
help='Number of dimensions. Default is 128.')
return parser.parse_args()
def main():
args = parse_args()
t_0 = time.time()
model = RBQR(args.graph, args.emb1, args.emb2, args.dimension)
t_1 = time.time()
features_matrix = model.pre_factorization(model.matrix0, model.matrix0)
t_2 = time.time()
embeddings_matrix = model.taylor_expansion(model.matrix0, features_matrix)
t_3 = time.time()
print('---', model.node_number)
print('total time', t_3 - t_0)
print('sparse NE time', t_2 - t_1)
print('spectral Pro time', t_3 - t_2)
save_embedding(args.emb1, features_matrix)
save_embedding(args.emb2, embeddings_matrix)
print('save embedding done')
if __name__ == '__main__':
main()