-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhybrid.py
executable file
·498 lines (394 loc) · 16.6 KB
/
hybrid.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import random
import string
import time
import nltk
import nltk.corpus
import numpy
import scipy
import scipy.io
import scipy.sparse
import scipy.special
# import nchar
"""
Implements online variational Bayesian for LDA.
"""
class Hybrid():
"""
"""
def __init__(self,
minimum_word_length=3,
maximum_word_length=20,
dict_list=None,
N=3,
word_model_smooth=1e6,
char_list=string.ascii_lowercase,
# char_list=string.lowercase + string.digits
ranking_statistics_scale=1e3
):
from nltk.stem.porter import PorterStemmer
self._stemmer = PorterStemmer()
self._minimum_word_length = minimum_word_length
self._maximum_word_length = maximum_word_length
'''
self._word_model_smooth = word_model_smooth
self._char_list = char_list
self._n_char_model = N
if dict_list != None:
tokens = []
for line in open(dict_list, 'r'):
line = line.strip()
if len(line) <= 0:
continue
#tokens.append(line)
tokens.append(self._stemmer.stem(line))
#tokens = set(tokens)
#self._word_model = nchar.NcharModel(self._n_char_model, tokens, self._word_model_smooth, self._maximum_word_length, self._minimum_word_length, self._char_list)
else:
self._word_model = None
'''
self._word_model = None
self._ranking_statistics_scale = ranking_statistics_scale
"""
"""
def _initialize(self,
vocab,
number_of_topics,
number_of_documents,
batch_size,
expected_truncation_size,
alpha_theta=1e-2,
alpha_beta=1e6,
tau=1.,
kappa=0.5,
refine_vocab_interval=10,
save_word_trace=False,
ranking_smooth_factor=1e-12,
# gamma_converge_threshold=1e-3,
# number_of_samples=50,
number_of_samples=10,
# burn_in_sweeps=2
burn_in_sweeps=5
):
self._number_of_topics = number_of_topics
self._number_of_documents = number_of_documents
self._batch_size = batch_size
self._word_to_index = {}
self._index_to_word = {}
for word in set(vocab):
self._index_to_word[len(self._index_to_word)] = word
self._word_to_index[word] = len(self._word_to_index)
vocab = list(self._index_to_word.keys())
self._new_words = [len(self._index_to_word)]
self._word_trace = None
if save_word_trace:
self._word_trace = []
for index in vocab:
self._word_trace.append(
numpy.zeros((self._number_of_topics, self._number_of_documents // self._batch_size + 1),
dtype='int32') + numpy.iinfo(numpy.int32).max)
self._index_to_nupos = []
self._nupos_to_index = []
for k in range(self._number_of_topics):
self._index_to_nupos.append(dict())
self._nupos_to_index.append(dict())
random.shuffle(vocab)
for index in vocab:
self._nupos_to_index[k][len(self._nupos_to_index[k])] = index
self._index_to_nupos[k][index] = len(self._index_to_nupos[k])
self._alpha_theta = alpha_theta
self._alpha_beta = alpha_beta
self._tau = tau
self._kappa = kappa
self._truncation_size = []
self._truncation_size_prime = []
self._nu_1 = {}
self._nu_2 = {}
for k in range(self._number_of_topics):
self._truncation_size.append(len(self._index_to_nupos[k]))
self._truncation_size_prime.append(len(self._index_to_nupos[k]))
self._nu_1[k] = numpy.ones((1, self._truncation_size[k]))
self._nu_2[k] = numpy.ones((1, self._truncation_size[k])) * self._alpha_beta
self._expected_truncation_size = expected_truncation_size
# self._gamma_converge_threshold = gamma_converge_threshold
self._number_of_samples = number_of_samples
self._burn_in_sweeps = burn_in_sweeps
assert (self._burn_in_sweeps < self._number_of_samples)
self._ranking_smooth_factor = ranking_smooth_factor
self._reorder_vocab_interval = refine_vocab_interval
self._counter = 0
self._ranking_statistics = []
for k in range(self._number_of_topics):
self._ranking_statistics.append(nltk.probability.FreqDist())
for index in self._index_to_nupos[k]:
# self._ranking_statistics[k].inc(index, self._ranking_smooth_factor)
self._ranking_statistics[k][index] += self._ranking_smooth_factor
'''
if self._word_model != None:
self._ranking_statistics[k].inc(index, self._word_model.probability(self._index_to_word[index]) * self._ranking_statistics_scale)
else:
self._ranking_statistics[k].inc(index, self._ranking_smooth_factor)
'''
self._document_topic_distribution = None
if self._word_trace != None:
self.update_word_trace()
def update_word_trace(self):
if self._counter > self._number_of_documents / self._batch_size:
return
for topic_index in range(self._number_of_topics):
temp_keys = list(self._ranking_statistics[topic_index].keys())
for word_rank in range(len(temp_keys)):
self._word_trace[temp_keys[word_rank]][topic_index, self._counter:] = word_rank + 1
def parse_doc_list(self, docs):
if (type(docs).__name__ == 'str'):
temp = list()
temp.append(docs)
docs = temp
assert self._batch_size == len(docs)
batch_documents = []
for d in range(self._batch_size):
'''
docs[d] = docs[d].lower()
docs[d] = re.sub(r'-', ' ', docs[d])
docs[d] = re.sub(r'[^a-z ]', '', docs[d])
docs[d] = re.sub(r'[^a-z0-9 ]', '', docs[d])
docs[d] = re.sub(r' +', ' ', docs[d])
words = []
for word in docs[d].split():
if word in nltk.corpus.stopwords.words('english'):
continue
word = self._stemmer.stem(word)
if word in nltk.corpus.stopwords.words('english'):
continue
if len(word)>=self.maximum_word_length or len(word)<=self._minimum_word_length
continue
words.append(word)
'''
words = [word for word in docs[d].split() if
len(word) <= self._maximum_word_length and len(word) >= self._minimum_word_length]
document_topics = numpy.zeros((self._number_of_topics, len(words)), dtype='int32')
for word_index in range(len(words)):
word = words[word_index]
# valid only if limiting the ranking statistics
if word not in self._word_to_index:
# if this word never appeared before
index = len(self._word_to_index)
self._index_to_word[len(self._index_to_word)] = word
self._word_to_index[word] = len(self._word_to_index)
if self._word_trace != None:
self._word_trace.append(
numpy.zeros((self._number_of_topics, self._number_of_documents // self._batch_size + 1),
dtype='int32') + numpy.iinfo(numpy.int32).max)
for topic in range(self._number_of_topics):
# self._ranking_statistics[topic].inc(index, self._ranking_smooth_factor)
self._ranking_statistics[topic][index] += self._ranking_smooth_factor
else:
index = self._word_to_index[word]
for topic in range(self._number_of_topics):
if index not in self._index_to_nupos[topic]:
# if this word is not in current vocabulary
self._nupos_to_index[topic][len(self._nupos_to_index[topic])] = index
self._index_to_nupos[topic][index] = len(self._index_to_nupos[topic])
self._truncation_size_prime[topic] += 1
document_topics[topic, word_index] = self._index_to_nupos[topic][index]
batch_documents.append(document_topics)
if self._word_trace != None:
self.update_word_trace()
self._new_words.append(len(self._word_to_index))
return batch_documents
"""
Compute the aggregate digamma values, for phi update.
"""
def compute_exp_weights(self):
exp_weights = {}
exp_oov_weights = {}
for k in range(self._number_of_topics):
psi_nu_1_k = scipy.special.psi(self._nu_1[k])
psi_nu_2_k = scipy.special.psi(self._nu_2[k])
psi_nu_all_k = scipy.special.psi(self._nu_1[k] + self._nu_2[k])
aggregate_psi_nu_2_minus_psi_nu_all_k = numpy.cumsum(psi_nu_2_k - psi_nu_all_k, axis=1)
exp_oov_weights[k] = numpy.exp(aggregate_psi_nu_2_minus_psi_nu_all_k[0, -1])
aggregate_psi_nu_2_minus_psi_nu_all_k = numpy.hstack(
(numpy.zeros((1, 1)), aggregate_psi_nu_2_minus_psi_nu_all_k[:, :-1]))
assert (aggregate_psi_nu_2_minus_psi_nu_all_k.shape == psi_nu_1_k.shape)
exp_weights[k] = numpy.exp(psi_nu_1_k - psi_nu_all_k + aggregate_psi_nu_2_minus_psi_nu_all_k)
return exp_weights, exp_oov_weights
"""
"""
def e_step(self, wordids, directory=None):
batch_size = len(wordids)
sufficient_statistics = {}
for k in range(self._number_of_topics):
sufficient_statistics[k] = numpy.zeros((1, self._truncation_size_prime[k]))
batch_document_topic_distribution = numpy.zeros((batch_size, self._number_of_topics))
# batch_document_topic_distribution = scipy.sparse.dok_matrix((batch_size, self._number_of_topics), dtype='int16')
# log_likelihood = 0
exp_weights, exp_oov_weights = self.compute_exp_weights()
# Now, for each document document_index update that document's phi_d for every words
for document_index in range(batch_size):
phi = numpy.random.random(wordids[document_index].shape)
phi = phi / numpy.sum(phi, axis=0)[numpy.newaxis, :]
phi_sum = numpy.sum(phi, axis=1)[:, numpy.newaxis]
# assert(phi_sum.shape == (self.number_of_topics, 1))
for sample_index in range(self._number_of_samples):
for word_index in range(wordids[document_index].shape[1]):
phi_sum -= phi[:, word_index][:, numpy.newaxis]
# this is to get rid of the underflow error from the above summation, ideally, phi will become all integers after few iterations
phi_sum *= phi_sum > 0
# assert(numpy.all(phi_sum >= 0))
temp_phi = phi_sum + self._alpha_theta
# assert(temp_phi.shape == (self.number_of_topics, 1))
for k in range(self._number_of_topics):
id = wordids[document_index][k, word_index]
if id >= self._truncation_size[k]:
# if this word is an out-of-vocabulary term
temp_phi[k, 0] *= exp_oov_weights[k]
else:
# if this word is inside current vocabulary
temp_phi[k, 0] *= exp_weights[k][0, id]
temp_phi /= numpy.sum(temp_phi)
# assert(temp_phi.shape == (self.number_of_topics, 1))
# sample a topic for this word
temp_phi = temp_phi.T[0]
temp_phi = numpy.random.multinomial(1, temp_phi)[:, numpy.newaxis]
# assert(temp_phi.shape == (self.number_of_topics, 1))
phi[:, word_index][:, numpy.newaxis] = temp_phi
phi_sum += temp_phi
# assert(numpy.all(phi_sum >= 0))
# discard the first few burn-in sweeps
if sample_index >= self._burn_in_sweeps:
for k in range(self._number_of_topics):
id = wordids[document_index][k, word_index]
sufficient_statistics[k][0, id] += temp_phi[k, 0]
batch_document_topic_distribution[document_index, :] = self._alpha_theta + phi_sum.T[0, :]
for k in range(self._number_of_topics):
sufficient_statistics[k] /= (self._number_of_samples - self._burn_in_sweeps)
return sufficient_statistics, batch_document_topic_distribution
"""
"""
def m_step(self, batch_size, sufficient_statistics, close_form_updates=False):
# sufficient_statistics = self.sort_sufficient_statistics(sufficient_statistics)
reverse_cumulated_phi = {}
for k in range(self._number_of_topics):
reverse_cumulated_phi[k] = self.reverse_cumulative_sum_matrix_over_axis(sufficient_statistics[k], 1)
if close_form_updates:
self._nu_1 = 1 + sufficient_statistics
self._nu_2 = self._alpha_beta + reverse_cumulated_phi
else:
# Epsilon will be between 0 and 1, and says how much to weight the information we got from this mini-batch.
self._epsilon = pow(self._tau + self._counter, -self._kappa)
self.update_accumulate_sufficient_statistics(sufficient_statistics)
for k in range(self._number_of_topics):
if self._truncation_size[k] < self._truncation_size_prime[k]:
self._nu_1[k] = numpy.append(self._nu_1[k], numpy.ones(
(1, self._truncation_size_prime[k] - self._truncation_size[k])), 1)
self._nu_2[k] = numpy.append(self._nu_2[k], numpy.ones(
(1, self._truncation_size_prime[k] - self._truncation_size[k])), 1)
self._truncation_size[k] = self._truncation_size_prime[k]
self._nu_1[k] += self._epsilon * (
self._number_of_documents / batch_size * sufficient_statistics[k] + 1 - self._nu_1[k])
self._nu_2[k] += self._epsilon * (
self._alpha_beta + self._number_of_documents / batch_size * reverse_cumulated_phi[k] -
self._nu_2[k])
"""
"""
def update_accumulate_sufficient_statistics(self, sufficient_statistics):
for k in range(self._number_of_topics):
for index in self._index_to_word:
# self._ranking_statistics[k].inc(index, -self._epsilon*self._ranking_statistics[k][index])
self._ranking_statistics[k][index] += -self._epsilon * self._ranking_statistics[k][index]
for index in self._index_to_nupos[k]:
if self._word_model != None:
adjustment = self._word_model.probability(
self._index_to_word[index]) * self._ranking_statistics_scale
else:
adjustment = 1.
# self._ranking_statistics[k].inc(index, self._epsilon*adjustment*sufficient_statistics[k][0, self._index_to_nupos[k][index]])
self._ranking_statistics[k][index] += self._epsilon * adjustment * sufficient_statistics[k][
0, self._index_to_nupos[k][index]]
"""
"""
def prune_vocabulary(self):
# Re-order the nu values
new_index_to_nupos = []
new_nupos_to_index = []
new_nu_1 = {}
new_nu_2 = {}
for k in range(self._number_of_topics):
if len(self._index_to_nupos[k]) < self._expected_truncation_size:
new_nu_1[k] = numpy.zeros((1, len(self._index_to_nupos[k])))
new_nu_2[k] = numpy.zeros((1, len(self._index_to_nupos[k])))
else:
new_nu_1[k] = numpy.zeros((1, self._expected_truncation_size))
new_nu_2[k] = numpy.zeros((1, self._expected_truncation_size))
new_index_to_nupos.append(dict())
new_nupos_to_index.append(dict())
for index in list(self._ranking_statistics[k].keys()):
if len(new_index_to_nupos[k]) >= min(len(self._index_to_nupos[k]), self._expected_truncation_size):
break
# if index in words_to_keep and index in self._index_to_nupos[k].keys():
new_nupos_to_index[k][len(new_index_to_nupos[k])] = index
new_index_to_nupos[k][index] = len(new_index_to_nupos[k])
if index not in self._index_to_nupos[k]:
# TODO: this statement is never reached.
new_nu_1[k][0, new_index_to_nupos[k][index]] = 1
new_nu_2[k][0, new_index_to_nupos[k][index]] = 1
else:
new_nu_1[k][0, new_index_to_nupos[k][index]] = self._nu_1[k][0, self._index_to_nupos[k][index]]
new_nu_2[k][0, new_index_to_nupos[k][index]] = self._nu_2[k][0, self._index_to_nupos[k][index]]
self._truncation_size[k] = len(new_index_to_nupos[k])
self._truncation_size_prime[k] = self._truncation_size[k]
self._index_to_nupos = new_index_to_nupos
self._nupos_to_index = new_nupos_to_index
self._nu_1 = new_nu_1
self._nu_2 = new_nu_2
"""
"""
def learning(self, batch):
self._counter += 1
# This is to handle the case where someone just hands us a single document, not in a list.
if (type(batch).__name__ == 'string'):
temp = list()
temp.append(batch)
batch = temp
batch_size = len(batch)
# Parse the document mini-batch
clock = time.time()
wordids = self.parse_doc_list(batch)
clock_p_step = time.time() - clock
# E-step: hybrid approach, sample empirical topic assignment
clock = time.time()
sufficient_statistics, batch_document_topic_distribution = self.e_step(wordids)
clock_e_step = time.time() - clock
# M-step: online variational inference
clock = time.time()
self.m_step(batch_size, sufficient_statistics)
if self._counter % self._reorder_vocab_interval == 0:
self.prune_vocabulary()
clock_m_step = time.time() - clock
print('P-step, E-step and M-step take %d, %d, %d seconds respectively...' % (
clock_p_step, clock_e_step, clock_m_step))
return batch_document_topic_distribution
"""
"""
def reverse_cumulative_sum_matrix_over_axis(self, matrix, axis):
cumulative_sum = numpy.zeros(matrix.shape)
(k, n) = matrix.shape
if axis == 1:
for j in range(n - 2, -1, -1):
cumulative_sum[:, j] = cumulative_sum[:, j + 1] + matrix[:, j + 1]
elif axis == 0:
for i in range(k - 2, -1, -1):
cumulative_sum[i, :] = cumulative_sum[i + 1, :] + matrix[i + 1, :]
return cumulative_sum
def export_beta(self, exp_beta_path, top_display=-1):
exp_weights, exp_oov_weights = self.compute_exp_weights()
output = open(exp_beta_path, 'w')
for k in range(self._number_of_topics):
output.write("==========\t%d\t==========\n" % (k))
i = 0
for type_index in reversed(numpy.argsort(exp_weights[k][0, :])):
i += 1
output.write("%s\t%g\n" % (self._index_to_word[type_index], exp_weights[k][0, type_index]))
if top_display > 0 and i >= top_display:
break
output.close()