-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlda_t.py
246 lines (201 loc) · 5.33 KB
/
lda_t.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
#!/usr/bin/python
# Heinrich G. Parameter estimation for text analysis, version 2.9.
# <http://arbylon.net/publications/text-est2.pdf>, 2009.
import random
import sys
doc_set = []
M = K = V = 0
## count variables
n_mk = [] # document-topic count
n_m = [] # document-topic sum
n_kt = [] # topic-term count
n_k = [] # topic-term sum
z = []
num_it = 0 # num of iteration
max_doc_len = 0
alpha = 1
beta = 0.1
# vocabulary
voc_itms = []
voc_map = {}
def get_wids(words):
global voc_itms, voc_map
res = []
for word in words:
word = word.strip()
if len(word) == 0:
continue
if not voc_map.has_key(word): # insert into voc
voc_map[word] = len(voc_itms)
voc_itms.append(word)
res.append(voc_map[word]) # insert into res
return res
def save_dic(fn_dic):
global voc_itms
fo = open(fn_dic, "w")
for i in range(len(voc_itms)):
fo.write("%d\t%s\n" % (i, voc_itms[i]))
fo.close()
def save_doc(fn_doc):
global doc_set
fo = open(fn_doc, "w")
for i in range(len(doc_set)):
fo.write("%d\t" % i)
for wid in doc_set[i]:
fo.write("%d " % wid)
fo.write("\n")
fo.close()
def read_docs(fnd):
global doc_set, M, V, max_doc_len
fi = open(fnd, "r")
print "[Reading in docs]"
for line in fi:
li = line.split("\t")
if len(li) != 2:
print "illegal doc: %s" % line
continue
doc_no = int(li[0].strip())
words = li[1].strip().split(" ")
word_ids = get_wids(words)
if len(word_ids) == 0:
continue
doc_set.append(word_ids)
M += 1
if len(word_ids) > max_doc_len:
max_doc_len = len(word_ids)
fi.close()
V = len(voc_itms)
def zero_count():
global n_mk, n_m, n_kt, n_k, M, K, V
for i in range(M):
n_m.append(0)
n_mk.append([])
for i in range(K):
n_k.append(0)
n_kt.append([])
for i in range(M):
for j in range(K):
n_mk[i].append(0)
for i in range(K):
for j in range(V):
n_kt[i].append(0)
def init():
global doc_set, M, K, z, n_mk, n_m, n_kt, n_k
# zero all count variable
zero_count()
for m in range(M):
z.append([])
for n in range(len(doc_set[m])):
t = doc_set[m][n]
k = random.randint(0,K-1)
z[m].append(k)
n_mk[m][k] += 1
n_m[m] += 1
n_kt[k][t] += 1
n_k[k] += 1
def inverse_df(pm):
tmp = pm[:]
i = 1
while i < len(pm):
tmp[i] += tmp[i-1]
i += 1
u = random.uniform(0,1) * tmp[len(tmp)-1]
for index in range(len(tmp)):
if tmp[index] > u:
break
return index
def compute_p(m, t):
global K, V, n_mk, n_m, n_kt, n_k, alpha, beta
ret = []
for k in range(K):
n1 = float(beta) + n_kt[k][t]
d1 = float(beta) + n_k[k]
n2 = float(alpha) + n_mk[m][k]
ret.append(n1/d1 * n2)
return ret
def gibbs_samp():
global num_it, doc_set, z, M, n_mk, n_m, n_kt, n_k
for i in range(num_it):
for m in range(M):
for n in range(len(doc_set[m])):
t = doc_set[m][n]
k = z[m][n]
n_mk[m][k] -= 1
n_m[m] -= 1
n_kt[k][t] -= 1
n_k[k] -= 1
p = compute_p(m, t)
k_tilde = inverse_df(p)
z[m][n] = k_tilde
n_mk[m][k_tilde] += 1
n_m[m] += 1
n_kt[k_tilde][t] += 1
n_k[k_tilde] += 1
#if is_converge():
# break
def export_phi(fn):
global n_kt, n_k, beta, K, V
fo = open(fn, "w")
for k in range(K):
fo.write("%d " % k)
for t in range(V):
n = n_kt[k][t] + beta
d = n_k[k] + beta
fo.write("%f " % (float(n)/d))
fo.write("\n")
fo.close()
def export_theta(fn):
global n_mk, n_m, M, K, alpha
fo = open(fn, "w")
for m in range(M):
fo.write("%d " % m)
for k in range(K):
n = n_mk[m][k] + alpha
d = n_m[m] + alpha
fo.write("%f " % (float(n)/d))
fo.write("\n")
fo.close()
def export_z(fn):
global z, M
fo = open(fn, "w")
for m in range(M):
fo.write("%d " % m)
for topic_id in z[m]:
fo.write("%d " % topic_id)
fo.write("\n")
fo.close()
def save_stat(fn):
global K, V, n_kt, n_k
fo = open(fn, "w")
fo.write("%d %d\n" % (K, V))
for k in range(K):
fo.write("%d " % n_k[k])
fo.write("\n")
for k in range(K):
for t in range(V):
fo.write("%d " % n_kt[k][t])
fo.write("\n")
fo.close()
def main():
global K, num_it
path = "./data/"
fn_doc_set = path + "raws.dat"
fn_dic = path + "dic.dat"
fn_doc = path + "docs.dat"
fn_theta = path + "theta.dat"
fn_phi = path + "phi.dat"
fn_tassign = path + "tassign.dat"
fn_stat = path + "stat.dat"
K = 50
num_it = 20
read_docs(fn_doc_set)
save_dic(fn_dic)
save_doc(fn_doc)
init()
gibbs_samp()
export_z(fn_tassign)
export_phi(fn_phi)
export_theta(fn_theta)
save_stat(fn_stat) # for predict
if __name__ == "__main__":
main()