-
Notifications
You must be signed in to change notification settings - Fork 105
/
topic_significance_metrics.py
189 lines (146 loc) · 4.93 KB
/
topic_significance_metrics.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
import numpy as np
import octis.configuration.citations as citations
from octis.evaluation_metrics.metrics import AbstractMetric
def _KL(P, Q):
"""
Perform Kullback-Leibler divergence
Parameters
----------
P : distribution P
Q : distribution Q
Returns
-------
divergence : divergence from Q to P
"""
# add epsilon to grant absolute continuity
epsilon = 0.00001
P = P+epsilon
Q = Q+epsilon
divergence = np.sum(P*np.log(P/Q))
return divergence
def _replace_zeros_lines(arr):
zero_lines = np.where(~arr.any(axis=1))[0]
val = 1.0 / len(arr[0])
vett = np.full(len(arr[0]), val)
for zero_line in zero_lines:
arr[zero_line] = vett.copy()
return arr
class KL_uniform(AbstractMetric):
def __init__(self):
"""
Initialize metric
"""
super().__init__()
def info(self):
return {
"citation": citations.em_topic_significance,
"name": "KL_Uniform, Uniform distribution over words"
}
def score(self, model_output, per_topic=False):
"""
Retrieves the score of the metric
Parameters
----------
model_output : dictionary, output of the model
'topic-word-matrix' required
per_topic: if True, it returns the score for each topic
Returns
-------
result : score
"""
phi = _replace_zeros_lines(model_output["topic-word-matrix"].astype(float))
# make uniform distribution
val = 1.0 / len(phi[0])
unif_distr = np.full(len(phi[0]), val)
divergences = []
for topic in range(len(phi)):
# normalize phi, sum up to 1
P = phi[topic] / phi[topic].sum()
divergence = _KL(P, unif_distr)
divergences.append(divergence)
# KL-uniform = mean of the divergences
# between topic-word distributions and uniform distribution
if per_topic:
return divergences
else:
result = np.array(divergences).mean()
return result
class KL_vacuous(AbstractMetric):
def __init__(self):
"""
Initialize metric
"""
super().__init__()
def info(self):
return {
"citation": citations.em_topic_significance,
"name": "KL_Vacuous, Vacuous semantic distribution"
}
def score(self, model_output):
"""
Retrieves the score of the metric
Parameters
----------
model_output : dictionary, output of the model
'topic-word-matrix' required
'topic-document-matrix' required
Returns
-------
result : score
"""
phi = _replace_zeros_lines(model_output["topic-word-matrix"].astype(float))
theta = _replace_zeros_lines(model_output["topic-document-matrix"].astype(float))
vacuous = np.zeros(phi.shape[1])
for topic in range(len(theta)):
# get probability of the topic in the corpus
p_topic = theta[topic].sum()/len(theta[0])
# get probability of the words:
# P(Wi | vacuous_dist) = P(Wi | topic)*P(topic)
vacuous += phi[topic]*p_topic
divergences = []
for topic in range(len(phi)):
# normalize phi, sum up to 1
P = phi[topic] / phi[topic].sum()
divergence = _KL(P, vacuous)
divergences.append(divergence)
# KL-vacuous = mean of the divergences between topic-word distributions and vacuous distribution
result = np.array(divergences).mean()
return result
class KL_background(AbstractMetric):
def __init__(self):
"""
Initialize metric
"""
super().__init__()
def info(self):
return {
"citation": citations.em_topic_significance,
"name": "KL_Background, Background distribution over documents"
}
def score(self, model_output):
"""
Retrieves the score of the metric
Parameters
----------
model_output : dictionary, output of the model
'topic-document-matrix' required
Returns
-------
result : score
"""
theta = _replace_zeros_lines(model_output["topic-document-matrix"].astype(float))
# make uniform distribution
val = 1.0 / len(theta[0])
unif_distr = np.full(len(theta[0]), val)
divergences = []
for topic in range(len(theta)):
# normalize theta, sum up to 1
P = theta[topic] / theta[topic].sum()
divergence = _KL(P, unif_distr)
divergences.append(divergence)
# KL-background = mean of the divergences
# between topic-doc distributions and uniform distribution
result = np.array(divergences).mean()
if np.isnan(result):
return 0
return result