-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLTBM_clean.py
More file actions
329 lines (224 loc) · 10.3 KB
/
LTBM_clean.py
File metadata and controls
329 lines (224 loc) · 10.3 KB
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
import numpy as np
from scipy.special import digamma, gammaln
import time
class LTBM:
def __init__(self, A, W, dictionnary, alpha, Q, L, K, tol=1e-3, YX=None, seed=2023) :
"""
@param A: matrice d'incidence
@param W: dict ; clé: (i, j), ou A[i, j] != 0 ; valeur: liste de liste de mots (! encodés en id 0, 1,... !)
@param dictionnary: list ; liste de tous les mots
@param alpha: vecteur des paramètres de concentration de Dirichlet de thehta (fixé dans le modele)
@param Q: nombre de cluster ligne
@param L: nombre de cluster colonne
@param K: nombre de sujets
"""
np.random.seed(seed)
self.tol = tol
self.A = A
self.T = [(i, j) for i, j in zip(*np.where(A != 0))] # non-zero entries
self.W = W
self.V = len(dictionnary)
self.Q = Q
self.L = L
self.K = K
self.alpha = alpha
self.gln_alpha = gammaln(self.alpha)
self.gln_alpha_sum = gammaln(np.sum(self.alpha))
if not len(alpha) == self.K :
raise ValueError("alpha should be a vector of size K")
if YX is None :
self._init_XY()
else :
self.Y, self.X = YX
self._init_params()
self.lower_bound = -np.inf
def fit(self, max_iter=20, max_iterVEM=100, verbose=1):
greedysucces = True
for iter in range(max_iter):
lb0 = self.lower_bound
time0 = time.time()
# Variational EM
self.VEM(max_iterVEM, verbose=verbose>1)
timeVEM = time.time() - time0
lbVEM = self.lower_bound
deltaVEM = self.lower_bound - lb0
if not greedysucces and deltaVEM < self.tol:
if verbose:
print('\nIteration {}: {:.3f} (deltaVEM: {:.4f})'.format(iter + 1, self.lower_bound, deltaVEM))
print('VEM: {:.2f}s\n'.format(timeVEM))
break
# Greedy search
self.greedy_search_XY()
timeGreedy = time.time() - timeVEM - time0
deltaGreedy = self.lower_bound - lbVEM
if deltaGreedy > self.tol : greedysucces = True
else : greedysucces = False
if verbose>0:
print('\nIteration {}: {:.3f} (deltaVEM: {:.4f}, deltaGreedy: {:.4f})'.format(iter + 1, self.lower_bound, deltaVEM, deltaGreedy))
print('VEM: {:.2f}s, Greedy: {:.2f}s\n'.format(timeVEM, timeGreedy))
if not greedysucces and deltaVEM < self.tol:
break
def A_clust(self):
Y_clust = np.argmax(self.Y, axis=1)
X_clust = np.argmax(self.X, axis=1)
A_clust = np.zeros(self.A.shape)
for i, j in self.T:
A_clust[i, j] = Y_clust[i] + X_clust[j] * self.Q + 1
return A_clust
def _init_XY(self):
self.Y = np.zeros((self.A.shape[0], self.Q))
for i in range(self.A.shape[0]):
self.Y[i, np.random.randint(0, self.Q)] = 1
self.X = np.zeros((self.A.shape[1], self.L))
for j in range(self.A.shape[1]):
self.X[j, np.random.randint(0, self.L)] = 1
def _init_params(self) :
self._update_rho_delta()
self._update_pi()
self.beta = np.random.dirichlet(np.ones(self.V), self.K)
### variational parameters
# parametrise la distribution q(theta) (Dirichlet distrib)
self.gamma = np.ones((self.Q, self.L, self.K))
self.dg_gamma = digamma(self.gamma) - digamma(np.sum(self.gamma, axis=2))[:, :, np.newaxis]
# parametrise la distribution q(Z) (multinomial distrib)
self.phi = [[None for _ in range(self.A.shape[1])] for _ in range(self.A.shape[0])]
for i, j in self.T:
l = []
for doc in self.W[i][j]:
l.append(np.ones((len(doc), self.K)) / self.K)
self.phi[i][j] = l
def VEM(self, max_iter=100, verbose=True):
for i in range(max_iter):
old_lb = self.lower_bound
# E-step
self._update_phi()
self._update_gamma()
# M-step
self._update_rho_delta
self._update_pi()
self._update_beta()
# Compute lower bound
self.compute_lower_bound()
delta = self.lower_bound - old_lb
if verbose:
print('Iteration {}: {:.3f} (delta: {:.4f})'.format(i + 1, self.lower_bound, delta))
if delta < self.tol:
break
def _update_rho_delta(self):
self.rho = np.sum(self.Y, axis=0) / self.A.shape[0]
self.delta = np.sum(self.X, axis=0) / self.A.shape[1]
def _update_pi(self):
self.pi = np.zeros((self.Q, self.L))
for q, l in np.ndindex(self.Q, self.L):
in_clus_ql = self.Y[:, np.newaxis, q] @ self.X[np.newaxis, :, l]
self.pi[q, l] = np.sum(in_clus_ql * self.A) / np.sum(in_clus_ql)
def _update_beta(self):
self.beta = np.zeros((self.K, self.V))
for i, j in self.T:
for doc, phi in zip(self.W[i][j], self.phi[i][j]):
for v, p in zip(doc, phi):
for k in range(self.K):
self.beta[k, v] += p[k]
if not np.all(self.beta>0):
print('Warning: beta has non positive values')
self.beta += 1e-10
self.beta = self.beta / np.sum(self.beta, axis=1)[:, np.newaxis]
self.ln_beta = np.log(self.beta)
# print('beta', self.beta.shape, np.sum(self.beta, axis=1))
def _update_phi(self):
for i, j in self.T:
q = np.where(self.Y[i, :] == 1)[0][0]
l = np.where(self.X[j, :] == 1)[0][0]
for k in range(self.K):
exp_comp = np.exp(self.dg_gamma[q, l, k])
for doc, phi in zip(self.W[i][j], self.phi[i][j]):
for v, p in zip(doc, phi):
p[k] = self.beta[k, v] * exp_comp
# Normalize over topics
for phi in self.phi[i][j] :
if not np.all(phi>0):
print('Warning: phi has non positive values')
phi += [p + 1e-10 for p in phi]
for idx, p in enumerate(phi):
phi[idx] = p / np.sum(p)
def _update_gamma(self):
self.gamma = np.ones((self.Q, self.L, self.K)) * self.alpha
for i, j in self.T:
q = np.where(self.Y[i, :] == 1)[0][0]
l = np.where(self.X[j, :] == 1)[0][0]
for k in range(self.K):
self.gamma[q, l, k] += sum(self.phi[i][j][d][n][k] for d in range(len(self.W[i][j])) for n in range(len(self.W[i][j][d])))
self.dg_gamma = digamma(self.gamma) - digamma(np.sum(self.gamma, axis=2))[:, :, np.newaxis]
self.gln_gamma = gammaln(self.gamma)
self.gln_gamma_sum = gammaln(np.sum(self.gamma, axis=2))
def compute_lower_bound(self):
"""
Compute the variational lower bound
"""
lb = 0
#### L(q(.) | A, Y, X, beta) (refer to Appendix C) ###
for i, j in self.T:
q = np.where(self.Y[i, :] == 1)[0][0]
l = np.where(self.X[j, :] == 1)[0][0]
for doc, phi in zip(self.W[i][j], self.phi[i][j]):
for v, p in zip(doc, phi):
for k in range(self.K):
# 1-st term
lb += p[k] * self.ln_beta[k, v]
# 2-nd term
lb += p[k] * self.dg_gamma[q, l, k]
# 4-rd term
lb -= p[k] * np.log(p[k])
for q, l in np.ndindex(self.Q, self.L):
# 3-rd term
lb += self.gln_alpha_sum - np.sum(self.gln_alpha) + np.sum( (self.alpha-1) * self.dg_gamma[q, l, :])
# 5-th term
lb -= self.gln_gamma_sum[q, l] - np.sum(self.gln_gamma[q, l, :]) + np.sum((self.gamma[q, l, :]-1) * self.dg_gamma[q, l, :])
#### p(A, Y, X | pi, rho, delta) (eq 15) ###
for i, j in self.T:
q = np.where(self.Y[i, :] == 1)[0][0]
l = np.where(self.X[j, :] == 1)[0][0]
lb += np.log(self.pi[q, l] * self.rho[q] * self.delta[l])
# print('lb', lb)
self.lower_bound = lb
def greedy_search_XY(self) :
qs_num = np.sum(self.Y, axis=0)
for i in range(self.A.shape[0]) :
q = np.argmax(self.Y[i, :])
if qs_num[q] > 1 :
current_lb = self.lower_bound
best_lb = current_lb
q_final = q
self.Y[i, q] = 0
for q_try in range(self.Q) :
if q_try == q : continue
self.Y[i, q_try] = 1
self.compute_lower_bound()
if self.lower_bound > best_lb :
q_final = q_try
best_lb = self.lower_bound
self.Y[i, q_try] = 0
self.Y[i, q_final] = 1
self.lower_bound = best_lb
qs_num[q] -= 1
qs_num[q_final] += 1
ls_num = np.sum(self.X, axis=0)
for j in range(self.A.shape[1]) :
l = np.argmax(self.X[j, :])
if ls_num[l] > 1 :
current_lb = self.lower_bound
best_lb = current_lb
l_final = l
self.X[j, l] = 0
for l_try in range(self.L) :
if l_try == l : continue
self.X[j, l_try] = 1
self.compute_lower_bound()
if self.lower_bound > best_lb :
l_final = l_try
best_lb = self.lower_bound
self.X[j, l_try] = 0
self.X[j, l_final] = 1
self.lower_bound = best_lb
ls_num[l] -= 1
ls_num[l_final] += 1