-
Notifications
You must be signed in to change notification settings - Fork 1
/
belief_propagation_community_detection.py
274 lines (176 loc) · 8.17 KB
/
belief_propagation_community_detection.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
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse import dok_matrix
from scipy.sparse import random
from scipy.sparse import diags
import networkx as nx
from networkx.algorithms.community.quality import modularity
import math
def get_degree_vector(g: nx.Graph):
#compute the vector of node degrees
return csr_matrix(np.array([g.degree()[i] for i in range(g.number_of_nodes())]))
def init_beliefs(q: int, g: nx.Graph):
#initialize the beliefs
n_n = g.number_of_nodes()
b = random(n_n, q, density=1, data_rvs=lambda x: np.random.random(x), format='csr')
b = csr_matrix(b/b.sum(axis=1))
return b
def init_messages(q: int, g: nx.Graph):
#initialize the messages
n_n = g.number_of_nodes()
messages = dok_matrix((n_n*n_n, q))
for i in range(q):
for node in g.nodes:
for neighbor in g.neighbors(node):
messages[node*n_n + neighbor, i] = np.random.random()
sum = messages.sum(axis=1).transpose().tolist()[0]
norm = diags(sum, format='csr')
norm[norm.nonzero()] = 1/norm[norm.nonzero()]
messages = messages.tocsr(copy=True)
return norm.dot(messages)
def compute_theta(beliefs: csr_matrix, degrees: csr_matrix):
# compute_theta(beliefs, degrees)[t,0] returns theta of community t
return degrees.dot(beliefs)
def update_matrix(g: nx.Graph):
#compute the matrix needed to update the messages
n_n = g.number_of_nodes()
update_mat = dok_matrix((n_n*n_n, n_n*n_n), dtype=np.int8)
for node in g.nodes:
for neighbor in g.neighbors(node):
for node_neighbor in g.neighbors(node):
if node_neighbor != neighbor:
update_mat[node*n_n+neighbor, node_neighbor*n_n + node] = 1
update_mat = update_mat.tocsr(copy=True)
return update_mat
def belief_matrix(g: nx.Graph):
#comupte the matrix needed to calculate the beliefs
n_n = g.number_of_nodes()
update_mat = dok_matrix((n_n, n_n * n_n), dtype=np.int8)
for node in g.nodes:
for neighbor in g.neighbors(node):
update_mat[node, neighbor*n_n + node] = 1
update_mat = update_mat.tocsr(copy=True)
return update_mat
def get_external_field_beliefs(theta: csr_matrix, g: nx.Graph, beta: float):
#compute the external field based on theta for the computation of the beliefs
m = g.number_of_edges()
degrees = get_degree_vector(g)
ext_field = -(beta/(2*m))*degrees.transpose().dot(theta)
return ext_field
def external_field_update_matrix(g: nx.Graph, beta: float, q: int):
#compute the matrix needed to update the external field
n_n = g.number_of_nodes()
m = g.number_of_edges()
ext_field_update_matrix = dok_matrix((n_n*n_n, n_n))
for i in range(q):
for node in g.nodes:
for neighbor in g.neighbors(node):
ext_field_update_matrix[(node)*n_n + neighbor, neighbor] = -((beta*g.degree()[node])/(2*m))
ext_field_update_matrix = ext_field_update_matrix.tocsr()
return ext_field_update_matrix
def get_external_field(theta: csr_matrix, ext_update: csr_matrix):
#update the external field for the message update
n_n = ext_update.shape[1]
ones_vec = csr_matrix(np.ones((n_n, 1)))
theta_transformed = ones_vec*theta
ext_field = ext_update*theta_transformed
return ext_field
def run_bp_community_detection(g: nx.Graph, q: int = 2, beta: float = 1.012, max_it: int = 30, eps: float = 0.0001,
reruns_if_not_conv: int = 20):
#run belief propagation community detection with given parameters
n_n = g.number_of_nodes()
#building update_matrix, belief_update_matrix, degree_vector
update_mat = update_matrix(g)
belief_update_mat = belief_matrix(g)
degrees = get_degree_vector(g)
#building external_field_update_matrix
ext_field_update_matrix = external_field_update_matrix(g=g, beta=beta, q=q)
converged = False
runs = 0
for i in range(reruns_if_not_conv):
if converged:
break
runs = runs+1
# initializing beliefs and messages
beliefs = init_beliefs(q, g)
messages = init_messages(q, g)
# building a vector with ones at certain positions, needed for computation
ones_vector = dok_matrix((n_n * n_n, q))
ones_vector[messages.nonzero()] = 1
ones_vector = ones_vector.tocsr(copy=True)
print('Run: ', i+1)
belief_diff = 1
iterations = 0
for j in range(max_it):
iterations = iterations+1
if belief_diff < eps:
converged = True
break
theta = compute_theta(beliefs, degrees)
# update beliefs with updated messages
new_messages = ones_vector + (messages * (np.exp(beta) - 1))
new_messages.data = np.log(new_messages.data)
updated_beliefs = belief_update_mat.dot(new_messages)
old_beliefs = beliefs.copy()
beliefs = updated_beliefs + get_external_field_beliefs(theta=theta, g=g, beta=beta)
beliefs.data = np.exp(beliefs.data)
b_sum = beliefs.sum(axis=1).transpose().tolist()[0]
b_norm = diags(b_sum, format='csr')
b_norm.data = 1 / b_norm.data
beliefs = b_norm.dot(beliefs)
beliefs = beliefs.tocsr(copy=True)
# update messages
external_field = get_external_field(theta=theta, ext_update=ext_field_update_matrix)
#new_messages = ones_vector+(messages*(np.exp(beta) - 1))
#new_messages.data = np.log(new_messages.data)
updated_messages = update_mat.dot(new_messages)
messages = external_field + updated_messages
messages.data = np.exp(messages.data)
m_sum = messages.sum(axis=1).transpose().tolist()[0]
m_norm = diags(m_sum, format='csr')
m_norm.data = 1 / m_norm.data
messages = m_norm.dot(messages)
messages = messages.tocsr(copy=True)
belief_diff = np.linalg.norm(beliefs.todense() - old_beliefs)
#print(belief_diff)
detected_communities = [np.argmax(beliefs[i, :]) for i in range(beliefs.shape[0])]
return beliefs, detected_communities, runs, iterations, converged
def compute_opt_beta(q: int, c: int):
# compute the optimal value for the parameter beta
return math.log(q/(math.sqrt(c)-1) + 1)
def detect_communities(g: nx.Graph, max_it: int = 100, eps: float = 0.0001, reruns_if_not_conv: int = 5,
threshold: float = 0.005, q_max: int = 7):
#determine number of optimal communities and run community detection for a given network
#The nodes have to be labeled form 0 to n
modularity_0 = 0
modularity_1 = threshold
q = 1
c = 2*g.number_of_edges()/g.number_of_nodes()
partition = ()
#run belief propagation community detection with increasing number of communities until the modularity of the
#detected partition does not increase more then given threshold
while modularity_1 - modularity_0 >= threshold:
old_partition = partition
beta = compute_opt_beta(q,c)
modularity_0 = modularity_1
partition = run_bp_community_detection(g=g, q=q, beta=beta, max_it=max_it, eps=eps,
reruns_if_not_conv=reruns_if_not_conv)
modularity_1 = modularity(g, [{i for i in range(len(partition[1])) if partition[1][i] == j}
for j in set(partition[1])])
if not partition[4]:
curr_partition = partition
partition = old_partition
modularity_1 = modularity_0
modularity_0 = modularity_1 - threshold
if q == 1:
modularity_0 = modularity_1 - threshold
if q > q_max:
break
print(q)
q = q + 1
if len(old_partition) != 0:
return q-1, modularity_0, old_partition[1], old_partition[2], old_partition[3], old_partition[4]
else:
return q-1, modularity_0, curr_partition[1], curr_partition[2], curr_partition[3], curr_partition[4]
if __name__ == '__main__':
None