-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastWhitenLDA.py
300 lines (227 loc) · 8.15 KB
/
fastWhitenLDA.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
"""
Main script containing the implementation of topic search by whitening algorithm
Copyright (C) 2016 Avik Ray
Code by Avik Ray
Contact: avik@utexas.edu
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import numpy as np
import numpy.matlib
import scipy
import scipy.io
from scipy.sparse.linalg import svds
from utils import *
import sys
import time
import copy
import os
#---------------------------------------------------------------
# Apply filter to word by document matrix
#---------------------------------------------------------------
def applyFilter(corpusMat,filterList):
corpusMat = corpusMat.tocsc()
corpusMat = corpusMat[:,filterList]
return corpusMat.tocsr()
#---------------------------------------------------------------
# Main search by Whitening algo function
#---------------------------------------------------------------
def searchByWhiteningLDA(config, labelWord, corpusMatFile, dictFile):
# Get word index from dictionary
widx = getWordIndex(dictFile, labelWord)
if widx < 0:
print 'Error: Label word not found in dictionary'
return
# Load word by document matrix
M = scipy.io.loadmat(corpusMatFile)['M']
# Make column normalized M and weight vector W
M = M.tocsc()
newdata = np.zeros(M.data.shape[0], dtype=np.float64)
print 'Normalizing columns and computing weight vector ...'
for i in xrange(M.indptr.size-1):
begin = M.indptr[i]
end = M.indptr[i+1]
totWord = np.sum(M.data[begin:end])
if totWord > 0:
newdata[begin:end] = np.divide(M.data[begin:end],float(totWord))
M.data = newdata
# Compute training, validation, test sets
filterList = readFilter(config)
Mtrain = M[:,filterList[0]]
Mval = M[:,filterList[1]]
Mtest = M[:,filterList[2]]
numWords = Mtrain.shape[0]
numDocs = Mtrain.shape[1]
print 'Number of training words = ' + str(numWords)
print 'Number of training documents = ' + str(numDocs)
print 'Number of validation documents =', Mval.shape[1]
print 'Number of test documents =', Mtest.shape[1]
# Consider training set for remaining algo
M = Mtrain
alpha0 = config['alpha0']
# Compute x and mean m
print 'Computing vector x ...'
M = M.tocsr()
m = np.zeros(numWords,dtype=np.float64)
for i in xrange(M.indptr.size-1):
begin = M.indptr[i]
end = M.indptr[i+1]
m[i] = np.sum(M.data[begin:end])/float(numDocs)
x = np.zeros(numWords,dtype=np.float64)
x = alpha0*m
# Compute A
print 'Computing matrix A ...'
T2 = M*M.T
P = T2*(1/float(numDocs))
P = P.todense() - (alpha0/float(1+alpha0))*np.outer(m,m)
A = alpha0*(1+alpha0)*P
# Compute B
# Get distribution
k = config['K']
mu1, alpha1 = fastWhitening(A, M, m, x, k, alpha0, widx)
print 'Topic probability =', alpha1/alpha0
# Compute mu
mu = np.zeros(numWords)
for i in range(numWords):
if i != widx:
mu[i] = mu1[i,0]
p_no_label = np.sum(mu)
if p_no_label > 1:
print 'Normalization error !', p_no_label
else:
mu[widx] = 1 - p_no_label
return mu
#---------------------------------------------------------------
# Fast whitening subroutine
#---------------------------------------------------------------
def fastWhitening(A, M, m, x, k, alpha0, widx):
A = scipy.sparse.csc_matrix(A)
print 'SVD 1 ...'
V, S, Vt = svds(A,k)
# V is size d x k matrix
# Form k x k matrix Dhalf, DhalfInv
Shalf = []
ShalfInv = []
for i in range(k):
s = np.sqrt(S[i])
Shalf.append(s)
ShalfInv.append(1/float(s))
Dhalf = np.diag(Shalf)
DhalfInv = np.diag(ShalfInv)
# Compute R = DhalfInv*Vt*B*V*DhalfInv
W = np.dot(DhalfInv,Vt)
# R is size k x k
print 'Whitening B'
R = computeRLDA(W,M,m,widx,alpha0)
# Compute u the largest singular vector of R
print 'SVD 2 ...'
R = scipy.sparse.csc_matrix(R)
u, s1, ut = svds(R,1)
# Compute w = V*Dhalf*u
w = np.dot(V,np.dot(Dhalf,u))
print 'w row = ', w.shape[0]
print 'w col = ', w.shape[1]
# Estimate a = u'*DhalfInv*Vt*x
xtilde = np.dot(DhalfInv,np.dot(Vt,x))
a = np.dot(u.transpose(),xtilde)
# Compute mu
mu = np.dot(w,1/float(a))
return (mu,a**2)
#---------------------------------------------------------------
# Function to compute R matrix (whitened B matrix)
#---------------------------------------------------------------
def computeRLDA(W,M,m,widx,alpha0):
numWords = M.shape[0]
print '*number of words =', numWords
numDocs = M.shape[1]
print '*number of docs =', numDocs
K = W.shape[0]
print '*K =', K
# Compute weights
print 'Computing matrix R ...'
M = M.tocsr()
L = M[widx,:].todense()
# Compute projected samples
M = M.tocsc()
X = np.zeros((K,numDocs),dtype=np.float64)
X = W*M
# Y2 = W*M*diag(L) = X*diag[L]
Y2 = copy.deepcopy(X)
for i in xrange(numDocs):
#print L[0,i]
Y2[:,i] = Y2[:,i]*L[0,i]
# X1 = W*M*diag(L)*M'*W'/numDocs
X1 = np.zeros((K,K),dtype=np.float64)
X1 = np.dot(Y2,X.transpose())
X1 = X1*(1/float(numDocs))
print 'X1 done.'
# X2 = m[l]*W*M*M'*W'/numDocs
X2 = np.zeros((K,K),dtype=np.float64)
X2 = np.dot(X,X.transpose()/float(numDocs))
X2 = X2*m[widx]
print 'X2 done.'
# X3 = W*M*diag(L)*Mmat'*W'/numDocs
X3 = np.zeros((K,K),dtype=np.float64)
m1 = np.dot(W,m)
x3 = np.zeros((K,1),dtype=np.float64)
for k in range(K):
x3[k] = sum(Y2[k,:])/float(numDocs)
X3 = np.outer(x3,m1)
print 'X3 done.'
# X4 = W*Mmat*diag(L)*M'*W'/numDocs = X3'
X4 = X3.transpose()
print 'X4 done.'
# X5 = m[l]*W*m*m'*W'
X5 = np.zeros((K,K),dtype=np.float64)
X5 = m[widx]*np.outer(m1,m1)
print 'X5 done.'
# B6 = B1 - (alpha0/(alpha0+2))*(B2+B3+B4) + (2*alpha0^2/((alpha0+2)*(alpha0+1)))*B5
X6 = np.zeros((K,K),dtype=np.float64)
X6 = X1 - (alpha0/float(alpha0+2))*(X2+X3+X4) + (2*alpha0**2/float((alpha0+2)*(alpha0+1)))*X5
print 'X6 done.'
# B = (alpha0*(alpha0+1)*(alpha0+2)/2)*B6;
R = (alpha0*(alpha0+1)*(alpha0+2)/float(2))*X6
return R
if __name__=="__main__":
if len(sys.argv) <=2:
print 'Usage: python fastWhitenLDA.py config_file label_word'
else:
configFile = sys.argv[1]
labelWord = sys.argv[2]
#configFile = "configCorpus.txt"
#configFile = "configCorpus20000.txt"
config = loadConfig(configFile)
# Preprocessing
truncMatFile, truncDictFile = preprocess(config)
# Compute Filter
splitData(truncMatFile,config)
# Initialize result directory "res"
directory = 'res'
if not os.path.exists(directory):
os.makedirs(directory)
# Main
K = config['K']
N = config['N']
wordList = [labelWord]
for word in wordList:
print '-----------------------------------'
print 'Labeled word = ' + word
print '-----------------------------------'
# Compute topic likelihood vector via whitening method
tstart = time.time()
mu = searchByWhiteningLDA(config, word, truncMatFile, truncDictFile)
print '>> Total runtime = ', time.time() - tstart, ' sec'
# Print and save top words
top10List = saveTopWords(truncDictFile, N, mu, word)
# Compute PMI
print 'Computing PMI ...'
pmi = computePMIWhiten(word)
print 'Topic PMI score =', pmi