-
Notifications
You must be signed in to change notification settings - Fork 0
/
different_toptokens_example.py
136 lines (98 loc) · 4.02 KB
/
different_toptokens_example.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
import pickle
import numpy
import matplotlib.pyplot as plt
import pandas as pd
import model_utils
from palmettopy.palmetto import Palmetto
palmetto = Palmetto()
def measure_coherence(displayed_words):
displayed_words = displayed_words[-10:]
result = palmetto.get_coherence(displayed_words, coherence_type="cv")
return result
model, batch_vectorizer, dictionary = model_utils.example_model()
model.initialize(dictionary=dictionary)
model.num_document_passes = 1
model.fit_offline(batch_vectorizer=batch_vectorizer, num_collection_passes=1)
plsa_theta = model.get_theta()
df_phi = model.get_phi()
num_2_token = list(df_phi.index)
plsa_phi = numpy.array(df_phi)
def calc_weighted_pk(phi, theta):
'''
p(k) is prob(topic = k), defined as p(k) = sum_k n_k / n,
calculation of n_k is a bit tricky:
n_t = sum_d sum_w n_wd p_tdw = sum_d theta_td
alternatively:
n_t = sum_w n_wt
(where n_wt = sum_d n_wd p_tdw)
(so I don't actually need theta here, but using it a bit more convenient)
if we fix some word w, we can calculate weighted_pk:
wp_k = p(k) p(w|k)
'''
n_k = numpy.sum(theta, axis=1)
p_k = n_k / numpy.sum(n_k)
print p_k[:, numpy.newaxis].shape, phi.transpose().shape
weighted_pk = p_k[:, numpy.newaxis] * phi.transpose()
return weighted_pk, p_k
def calc_ptw(phi, theta):
weighted_pk, p_k = calc_weighted_pk(phi, theta)
return weighted_pk / numpy.sum(weighted_pk, axis=0) # sum by all T
def calc_LR_vectorised(phi, theta):
"""
Likelihood ratio is defined as
L = phi_wt / sum_k p(k)/p(!t) phi_wk
equivalently:
L = phi_wt * p(!t) / sum_k!=t p(k) phi_wk
after some numpy magic, you can get:
L = phi[topic, id] * (1 - p_k[topic]) / {(sum(weighted_pk) - weighted_pk[topic])}
numerator and denominator are calculated separately
"""
weighted_pk, p_k = calc_weighted_pk(phi, theta)
print phi.transpose().shape, (1 - p_k[:, numpy.newaxis]).shape
numerator = phi.transpose() * (1 - p_k[:, numpy.newaxis])
denominator = (numpy.sum(weighted_pk, axis=0) - weighted_pk)
target_values = numerator / denominator
target_values[denominator == 0] = float("-inf") # infinite likelihood ratios aren't interesting
return target_values
def calc_blei_scores(plsa_phi):
'''
score
phi[wt] * [log(phi[wt]) - 1/T sum_k log(phi[wk])]
'''
T = plsa_phi.shape[0]
blei_eps = 1e-42
log_phi = numpy.log(plsa_phi + blei_eps)
denom = numpy.sum(log_phi, axis=0)
denom = denom[numpy.newaxis, :]
score = plsa_phi * (log_phi - denom/T)
return score.transpose()
words_ids = numpy.array(range(len(num_2_token)))
LR_vector = calc_LR_vectorised(plsa_phi, plsa_theta)
blei_scores = calc_blei_scores(plsa_phi)
ptw_vector = calc_ptw(plsa_phi, plsa_theta)
methods = ["top", "lr", "blei"]
alpha_x = numpy.arange(0.1, 0.3, 0.01)
alpha_x = numpy.array([0.16, 1])
for alpha_val in alpha_x:
methods.append ("ptw_alpha_{}".format(alpha_val))
coherence_avg = {m: 0 for m in methods}
for topic_index, topic_name in enumerate(model.topic_names):
print "-------------"
for description in methods:
if description == "top":
target_values = plsa_phi[:, topic_index]
elif description == "lr":
target_values = LR_vector[topic_index, :]
elif description == "blei":
target_values = blei_scores[topic_index, :]
elif "ptw_alpha" in description:
alpha = float(description[10:])
target_values = alpha * ptw_vector[topic_index, :] + (1-alpha) * plsa_phi[:, topic_index]
else:
raise NameError
displayed_word_ids = model_utils.get_top_indices(target_values, 10)
displayed_words = [num_2_token[id] for id in displayed_word_ids]
print "{}: {}".format(description, displayed_words)
coh = measure_coherence(displayed_words)
coherence_avg[description] += coh
print "Coherence = {}".format(coh)