-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLapPLSI.m
320 lines (274 loc) · 7.91 KB
/
LapPLSI.m
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
function [Pz_d_final, Pw_z_final, Obj_final, nIter_final] = LapPLSI(X, K, W, options, Pz_d, Pw_z)
% Laplacian Probabilistic Latent Semantic Indexing/Alnalysis (LapPLSI) using generalized EM
%
% where
% X
% Notation:
% X ... (mFea x nSmp) term-document matrix (observed data)
% X(i,j) stores number of occurrences of word i in document j
%
% mFea ... number of words (vocabulary size)
% nSmp ... number of documents
% K ... number of topics
% W ... weight matrix of the affinity graph
%
% options ... Structure holding all settings
% options.lambda: manifold regularization prameter (default 1000)
%
% You only need to provide the above four inputs.
%
% Pz_d ... P(z|d)
% Pw_z ... P(w|z) corresponds to beta parameter in LDA
%
%
% References:
% [1] Deng Cai, Qiaozhu Mei, Jiawei Han, ChengXiang Zhai, "Modeling Hidden
% Topics on Document Manifold", Proc. 2008 ACM Conf. on Information and
% Knowledge Management (CIKM'08), Napa Valley, CA, Oct. 2008.
% [2] Qiaozhu Mei, Deng Cai, Duo Zhang, ChengXiang Zhai, "Topic Modeling
% with Network Regularization", Proceedings of the World Wide Web
% Conference ( WWW'08), 2008
%
%
% This software is based on the implementation of pLSA from
%
% Peter Gehler
% Max Planck Institute for biological Cybernetics
% Feb 2006
% http://www.kyb.mpg.de/bs/people/pgehler/code/index.html
%
% version 2.0 --June/2009
% version 1.1 --June/2008
% version 1.0 --Nov/2007
%
% Written by Deng Cai (dengcai AT gmail.com)
%
maxLoop = 100;
if min(min(X)) < 0
error('Input should be nonnegative!');
end
differror = 1e-7;
if isfield(options,'error')
differror = options.error;
end
maxIter = [];
if isfield(options, 'maxIter')
maxIter = options.maxIter;
end
nRepeat = 10;
if isfield(options,'nRepeat')
nRepeat = options.nRepeat;
end
minIter = 30;
if isfield(options,'minIter')
minIter = options.minIter;
end
meanFitRatio = 0.1;
if isfield(options,'meanFitRatio')
meanFitRatio = options.meanFitRatio;
end
lambda = 1000;
if isfield(options,'lambda')
lambda = options.lambda;
end
gamma = 0.1;
if isfield(options,'gamma')
gamma = options.gamma;
end
nStep = 200;
if isfield(options,'nStep')
nStep = options.nStep;
end
Verbosity = 0;
if isfield(options,'Verbosity')
Verbosity = options.Verbosity;
end
DCol = full(sum(W,2));
D = spdiags(DCol,0,speye(size(W,1)));
L = D - W;
if isfield(options,'NormW') && options.NormW
D_mhalf = DCol.^-.5;
tmpD_mhalf = repmat(D_mhalf,1,nSmp);
L = (tmpD_mhalf.*L).*tmpD_mhalf';
clear D_mhalf tmpD_mhalf;
L = max(L, L');
end
if ~exist('Pz_d','var')
[Pz_d,Pw_z] = pLSA_init(X,K);
else
nRepeat = 1;
end
Pd = sum(X)./sum(X(:));
Pd = full(Pd);
Pw_d = mex_Pw_d(X,Pw_z,Pz_d);
selectInit = 1;
if nRepeat == 1
selectInit = 0;
minIter = 0;
end
tryNo = 0;
[Pw_z,Pz_d] = mex_EMstep(X,Pw_d,Pw_z,Pz_d);
Pw_d = mex_Pw_d(X,Pw_z,Pz_d);
LogL = mex_logL(X,Pw_d,Pd);
ObjLap = sum(sum(Pz_d*L.*Pz_d))*2;
Obj = LogL-ObjLap*lambda;
nIter = 1;
meanFit = Obj*10;
for i=1:nStep
Pz_d = (1-gamma)*Pz_d + gamma*Pz_d*W./repmat(DCol',K,1);
end
Pw_d = mex_Pw_d(X,Pw_z,Pz_d);
LogLNew = mex_logL(X,Pw_d,Pd);
ObjLapNew = sum(sum(Pz_d*L.*Pz_d))*2;
ObjNew = LogLNew-ObjLapNew*lambda;
if ObjNew > Obj
nIter = nIter + 1;
LogL(end+1) = LogLNew;
ObjLap(end+1) = ObjLapNew;
Obj(end+1) = ObjNew;
end
while tryNo < nRepeat
tryNo = tryNo+1;
maxErr = 1;
while(maxErr > differror)
Pw_z_old = Pw_z;
Pz_d_old = Pz_d;
[Pw_z,Pz_d] = mex_EMstep(X,Pw_d,Pw_z,Pz_d);
Pw_d = mex_Pw_d(X,Pw_z,Pz_d);
LogLNew = mex_logL(X,Pw_d,Pd);
deltaLogL = LogLNew-LogL(end);
ObjLapNew = sum(sum(Pz_d*L.*Pz_d))*2;
deltaObjLap = ObjLapNew - ObjLap(end);
deltaObj = deltaLogL-deltaObjLap*lambda;
loopNo = 0;
loopNo2 = 0;
while deltaObj < 0
loopNo = 0;
while deltaObj < 0
for i=1:nStep
Pz_d = (1-gamma)*Pz_d + gamma*Pz_d*W./repmat(DCol',K,1);
end
ObjLapNew = sum(sum(Pz_d*L.*Pz_d))*2;
deltaObjLap = ObjLapNew - ObjLap(end);
deltaObj = deltaLogL-deltaObjLap*lambda;
loopNo = loopNo + 1;
if loopNo > maxLoop
break;
end
end
if loopNo > maxLoop
break;
end
loopNo2 = loopNo2 + 1;
if loopNo2 > maxLoop
break;
end
Pw_d = mex_Pw_d(X,Pw_z,Pz_d);
LogLNew = mex_logL(X,Pw_d,Pd);
deltaLogL = LogLNew-LogL(end);
deltaObj = deltaLogL-deltaObjLap*lambda;
end
IterValid = 1;
StopIter = 0;
if loopNo > maxLoop || loopNo2 > maxLoop
Pw_z = Pw_z_old;
Pz_d = Pz_d_old;
Pw_d = mex_Pw_d(X,Pw_z,Pz_d);
if nStep > 1
nStep = ceil(nStep/2);
IterValid = 0;
elseif gamma > 0.01
gamma = gamma/2;
IterValid = 0;
else
StopIter = 1;
end
else
LogL(end+1) = LogLNew;
ObjLap(end+1) = ObjLapNew;
Obj(end+1) = LogL(end)-ObjLap(end)*lambda;
end
if StopIter
maxErr = 0;
elseif IterValid
nIter = nIter + 1;
if nIter > minIter
if selectInit
maxErr = 0;
else
meanFit = meanFitRatio*meanFit + (1-meanFitRatio)*Obj(end);
maxErr = (meanFit-Obj(end))/meanFit;
if ~isempty(maxIter)
if nIter >= maxIter
maxErr = 0;
end
end
end
end
if Verbosity
if length(LogL) > 1
disp(['tryNo: ',num2str(tryNo),' Iteration: ',num2str(nIter),' LogL: ',num2str(Obj(end)),' deltaLogL: ',num2str(Obj(end)-Obj(end-1)),' maxErr:',num2str(maxErr)]);
else
disp(['tryNo: ',num2str(tryNo),' Iteration: ',num2str(nIter),' LogL: ',num2str(Obj(end)),' maxErr:',num2str(maxErr)]);
end
end
end
end
if tryNo == 1
Pz_d_final = Pz_d;
Pw_z_final = Pw_z;
nIter_final = nIter;
LogL_final = LogL;
ObjLap_final = ObjLap;
Obj_final = Obj;
Pw_d_final = Pw_d;
else
if Obj(end) > Obj_final(end)
Pz_d_final = Pz_d;
Pw_z_final = Pw_z;
nIter_final = nIter;
LogL_final = LogL;
ObjLap_final = ObjLap;
Obj_final = Obj;
Pw_d_final = Pw_d;
end
end
if selectInit
if tryNo < nRepeat
%re-start
[Pz_d,Pw_z] = pLSA_init(X,K);
Pw_d = mex_Pw_d(X,Pw_z,Pz_d);
[Pw_z,Pz_d] = mex_EMstep(X,Pw_d,Pw_z,Pz_d);
Pw_d = mex_Pw_d(X,Pw_z,Pz_d);
LogL = mex_logL(X,Pw_d,Pd);
ObjLap = sum(sum(Pz_d*L.*Pz_d))*2;
Obj = LogL-ObjLap*lambda;
nIter = 1;
else
tryNo = tryNo - 1;
minIter = 0;
selectInit = 0;
Pz_d = Pz_d_final;
Pw_z= Pw_z_final;
LogL = LogL_final;
ObjLap = ObjLap_final;
Obj = Obj_final;
nIter = nIter_final;
meanFit = Obj(end)*10;
Pw_d = Pw_d_final;
end
end
end
function [Pz_d,Pw_z,Pz_q] = pLSA_init(X,K,Xtest)
[mFea,nSmp] = size(X);
Pz_d = rand(K,nSmp);
Pz_d = Pz_d ./ repmat(sum(Pz_d,1),K,1);
% random assignment
Pw_z = rand(mFea,K);
Pw_z = Pw_z./repmat(sum(Pw_z,1),mFea,1);
if nargin > 2
nTest = size(Xtest,2);
Pz_q = rand(K,nTest);
Pz_q = Pz_q ./ repmat(sum(Pz_q),K,1);
end