-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn.py
executable file
·220 lines (190 loc) · 7.85 KB
/
learn.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
#!/usr/bin/python2
# -*- coding: utf-8 -*-
from __future__ import division
import argparse,glob,os,cPickle,numpy as np,time,learnC as lc,bottleneck as bn
#Parse feature file to generate a list
def featureFileNoLabelToList(filename):
f=open(filename,'r')
content = [line.rstrip() for line in f]
featureList=[]
for lines in content:
tokens=lines.split(' ')
featureDict={}
for feature in tokens:
kv=feature.split(':')
#print kv
featureDict[int(kv[0])]=int(kv[1])
featureList.append(featureDict)
#print featureList
return featureList
#normalize 1d array to sum=1
def normalize1(a):
s=np.sum(a)
if s!=0:
normed=np.divide(a,s)
return normed
#nomalize 2d arry to col sum=1
# def normalize2(a):
# normed=np.empty_like(a)
# #at=a.T
# #normedT=np.empty_like(at)
# for i in range(len(a)):
# normed[i]=normalize1(a[i])
# return normed
def normalize2(a):
#normed=np.empty_like(a)
at=a.T
normedT=np.empty_like(at)
for i in range(len(at)):
normedT[i]=normalize1(at[i])
normed=normedT.T
return normed
def timing(f):
def wrap(*args):
time1 = time.time()
ret = f(*args)
time2 = time.time()
print '%s function took %0.3f ms' % (f.func_name, (time2-time1)*1000.0)
return ret
return wrap
#plsi class that contains plsi routines and p values
class plsi(object):
def __init__(self):
self.p_dz_n=None
self.p_wz_n=None
self.p_z_n=None
self.log_like=0
self.time=0
#self.new_log_like=0
#initialize p arrays
#@timing
def initialize(self,d,w,z):
self.p_dz_n=normalize2(np.random.rand(d,z))
self.p_wz_n=normalize2(np.random.rand(w,z))
self.p_z_n=normalize1(np.random.rand(z))
# self.p_dz_n=np.random.rand(d,z)
# self.p_wz_n=(np.random.rand(w,z))
# self.p_z_n=(np.random.rand(z))
# self.numerator_p_dz_n=np.zeros((d,z),dtype=np.float64)
# self.denominator_p_dz_n=np.zeros(z,dtype=np.float64)
# self.numerator_p_wz_n=np.zeros((w,z),dtype=np.float64)
# self.denominator_p_wz_n=np.zeros(z,dtype=np.float64)
# self.numerator_p_z_n=np.zeros(z,dtype=np.float64)
# self.denominator_p_z_n=np.zeros(z,dtype=np.float64)
self.time=0
self.log_like=0
#print self.p_z_n
#print self.p_dz_n
#@timing
def train(self,featureList,docNum,wordNum,z):
self.initialize(docNum,wordNum,z)
#new_log_like=self.loglikelihood(featureList,docNum,wordNum,z)
#self.log_like=new_log_like
#cycle=0
time1 = time.time()
while True:
self.update(featureList,docNum,wordNum,z)
new_log_like=self.loglikelihood(featureList,docNum,wordNum,z)#self.loglikelihood(featureList,docNum,wordNum,z)
print "old log_like:"+str(self.log_like)
print "new log_like:"+str(new_log_like)
delta=new_log_like-self.log_like
print "delta:"+str(delta)
self.log_like=new_log_like
if np.abs(delta)<1:
break
#cycle+=1
time2 = time.time()
self.time=(time2-time1)
#@timing
def update(self,featureList,docNum,wordNum,z):
#call C++ routine for speed
res=lc.update(featureList,docNum,wordNum,z,self.p_wz_n.tolist(),self.p_dz_n.tolist(),self.p_z_n.tolist())
self.p_wz_n=np.array(res[0]);
self.p_dz_n=np.array(res[1]);
self.p_z_n=np.array(res[2]);
# print "npsums:\n"
# print np.sum(self.p_wz_n,axis=0)
# print np.sum(self.p_dz_n,axis=0)
# print np.sum(self.p_z_n)
#print res
#update
# for d in range(docNum):
# for w in featureList[d]:
# w=w-1
# denominator=0
# #equivalent vector expression:
# numerator=self.p_dz_n[d][:]*self.p_wz_n[w][:]*self.p_z_n[:]
# denominator=np.sum(numerator)
# P_z_condition_d_w=numerator/denominator
# tfwd=featureList[d][w+1]
# self.numerator_p_dz_n[d][:]+=tfwd*P_z_condition_d_w[:]
# self.denominator_p_dz_n[:]+=tfwd*P_z_condition_d_w[:]
# self.numerator_p_wz_n[w][:]+=tfwd*P_z_condition_d_w[:]
# self.denominator_p_wz_n[:]+=tfwd*P_z_condition_d_w[:]
# self.numerator_p_z_n[:]+=tfwd*P_z_condition_d_w[:]
# self.denominator_p_z_n[:]+=tfwd
# for d in range(docNum):
# self.p_dz_n[d][:]=self.numerator_p_dz_n[d][:]/self.denominator_p_dz_n[:]
# for w in range(wordNum):
# self.p_wz_n[w][:]=self.numerator_p_wz_n[w][:]/self.denominator_p_wz_n[:]
# self.p_z_n[:]=self.numerator_p_z_n[:]/self.denominator_p_z_n[:]
#@timing
def loglikelihood(self,featureList,docNum,wordNum,z):
#call C++ routine for speed
new_log_like=lc.loglikelihood(featureList,docNum,wordNum,z,self.p_wz_n.tolist(),self.p_dz_n.tolist(),self.p_z_n.tolist())
# original numpy implementation, a bit slow
# new_log_like=0
# for d in range(docNum):
# for w in featureList[d]:
# w=w-1
# tfwd=featureList[d][w+1]
# p_d_w=np.sum(self.p_wz_n[w][:]*self.p_dz_n[d][:]*self.p_z_n[:])
# new_log_like+=tfwd*np.log(p_d_w)
return new_log_like
def output(self,outpath,words,z):
#topIndices=bn.argpartsort(-self.p_wz_n[:][z])
with open(os.path.join(outpath,str(z)+"-topic.txt"),'w') as outfile:
for i in range(z):
#print self.p_wz_n.shape
#print self.p_wz_n[:,i]
topIndices=bn.argpartsort(-self.p_wz_n[:,i],20)[:20]
# print topIndices
topList=[]
for index in topIndices:
topList.append([index,self.p_wz_n[index,i]])
sortedList=sorted(topList,key=lambda x:(-x[1]))
outfile.write("Topic "+str(i)+":\n")
for w in sortedList:
outfile.write(words[w[0]+1]+":"+str(w[1])+"\n")
with open(os.path.join(outpath,"k-likelihood-time.txt"),'a') as outfile:
outfile.write(str(z)+' '+str(self.log_like)+' '+str(self.time)+'\n')
if __name__=="__main__":
parser = argparse.ArgumentParser("Learn PLSI")
parser.add_argument('-f','--feature_file',help='Input feature file path',required=True)
parser.add_argument('-o','--output_folder',help='Output file path',required=True)
parser.add_argument('-w','--word_file',help='Word file name',required=True)
parser.add_argument('-k1','--k_min',help='Topic number k min',required=True)
parser.add_argument('-k2','--k_max',help='Topic number k max',required=True)
parser.add_argument('-ks','--k_step',help='Topic number k incremental step',required=True)
args=parser.parse_args()
featureList=featureFileNoLabelToList(args.feature_file)
docNum=len(featureList)
print "doc number:"+str(docNum)
#open word index file
wordIndex=cPickle.load(open(args.word_file,'rb'))
#swap wordIndex dict to generate index-word dict
words=dict((wordIndex[k][0],k) for k in wordIndex)
#print words
wordNum=len(words)
print "word number:"+str(wordNum)
if not os.path.exists(args.output_folder):
try:
os.makedirs(args.output_folder)
except OSError,why:
print "Failed: %s"%str(why)
if os.path.exists(os.path.join(args.output_folder,"k-likelihood-time.txt")):
os.remove(os.path.join(args.output_folder,"k-likelihood-time.txt"))
for k in range(int(args.k_min),int(args.k_max),int(args.k_step)):
plsiOb=plsi()
plsiOb.train(featureList,docNum,wordNum,k)
plsiOb.output(args.output_folder,words,k)