-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGNMF_KL_Multi.m
442 lines (404 loc) · 13.2 KB
/
GNMF_KL_Multi.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
function [U_final, V_final, nIter_final, objhistory_final] = GNMF_KL_Multi(X, k, W, options, U, V)
% Graph regularized Non-negative Matrix Factorization with Divergence Formulation
% Locality Preserving Non-negative Matrix Factorization (LPNMF)
%
% where
% X
% Notation:
% X ... (mFea x nSmp) data matrix
% mFea ... number of words (vocabulary size)
% nSmp ... number of documents
% k ... number of hidden factors
% W ... weight matrix of the affinity graph
%
% options ... Structure holding all settings
% options.alpha: manifold regularization prameter (default 100)
% if alpha=0, GNMF_KL boils down to the ordinary
% NMF with KL divergence. Please see [1][2] for details.
%
% You only need to provide the above four inputs.
%
% X = U*V'
%
% References:
% [1] Deng Cai, Xiaofei He, Xuanhui Wang, Hujun Bao, and Jiawei Han.
% "Locality Preserving Nonnegative Matrix Factorization", Proc. 2009 Int.
% Joint Conf. on Arti_cial Intelligence (IJCAI-09), Pasadena, CA, July 2009.
%
% [2] Deng Cai, Xiaofei He, Jiawei Han, Thomas Huang. "Graph Regularized
% Non-negative Matrix Factorization for Data Representation", IEEE
% Transactions on Pattern Analysis and Machine Intelligence, , Vol. 33, No.
% 8, pp. 1548-1560, 2011.
%
%
%
% version 3.0 --Jan/2012
% version 2.0 --April/2009
% version 1.0 --April/2008
%
% Written by Deng Cai (dengcai AT gmail.com)
%
differror = options.error;
maxIter = options.maxIter;
nRepeat = options.nRepeat;
minIter = options.minIter - 1;
if ~isempty(maxIter) && maxIter < minIter+1
minIter = maxIter-1;
end
meanFitRatio = options.meanFitRatio;
kmeansInit = options.kmeansInit;
alpha = options.alpha;
NCWeight = options.NCWeight;
tmpNCWeight = options.tmpNCWeight;
Cons = options.Cons;
if issparse(X)
nz = options.nz;
nzk = options.nzk;
idx = options.idx;
jdx = options.jdx;
vdx = options.vdx;
ldx = options.ldx;
end
Norm = 2;
NormV = 0;
[mFea,nSmp]=size(X);
if alpha > 0
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;
end
L = alpha*L;
L = max(L, L');
else
L = [];
end
selectInit = 1;
if isempty(U)
if kmeansInit
[U,V] = NMF_init(X, k);
else
U = abs(rand(mFea,k));
V = abs(rand(nSmp,k));
end
else
nRepeat = 1;
end
[U,V] = NormalizeUV(U, V, NormV, Norm);
if nRepeat == 1
selectInit = 0;
minIter = 0;
if isempty(maxIter)
if issparse(X)
[obj_NMFhistory, obj_Laphistory] = CalculateObjSparse(Cons, jdx, vdx, ldx, U, V, L, NCWeight);
else
[obj_NMFhistory, obj_Laphistory] = CalculateObj(Cons, X, U, V, L, NCWeight);
end
objhistory = obj_NMFhistory + obj_Laphistory;
meanFit = objhistory*10;
end
end
maxM = 62500000;
mn = numel(X);
if issparse(X)
nBlockNZ = floor(maxM/(k*2));
end
tryNo = 0;
nIter = 0;
while tryNo < nRepeat
tryNo = tryNo+1;
maxErr = 1;
while(maxErr > differror)
if issparse(X)
% ===================== update V ========================
if nzk < maxM
Y = sum(U(idx,:).*V(jdx,:),2);
else
Y = zeros(size(vdx));
for i = 1:ceil(nz/nBlockNZ)
if i == ceil(nz/nBlockNZ)
smpIdx = (i-1)*nBlockNZ+1:nz;
else
smpIdx = (i-1)*nBlockNZ+1:i*nBlockNZ;
end
Y(smpIdx) = sum(U(idx(smpIdx),:).*V(jdx(smpIdx),:),2);
end
end
Y = vdx./max(Y,1e-10);
Y = sparse(idx,jdx,Y,mFea,nSmp);
Y = (U'*Y)';
if alpha > 0
if isempty(NCWeight)
Y = V.*Y;
else
Y = repmat(NCWeight,1,k).*V.*Y;
end
sumU = max(sum(U,1),1e-10);
for i = 1:k
tmpL = L;
tmpD = tmpNCWeight*sumU(i);
tmpL = tmpL+spdiags(tmpD,0,nSmp,nSmp);
V(:,i) = tmpL\Y(:,i);
end
else
sumU = max(sum(U,1),1e-10);
Y = V.*Y;
V = Y./repmat(sumU,nSmp,1);
end
% ===================== update U ========================
if nzk < maxM
Y = sum(U(idx,:).*V(jdx,:),2);
else
Y = zeros(size(vdx));
for i = 1:ceil(nz/nBlockNZ)
if i == ceil(nz/nBlockNZ)
smpIdx = (i-1)*nBlockNZ+1:nz;
else
smpIdx = (i-1)*nBlockNZ+1:i*nBlockNZ;
end
Y(smpIdx) = sum(U(idx(smpIdx),:).*V(jdx(smpIdx),:),2);
end
end
Y = vdx./max(Y,1e-10);
Y = sparse(idx,jdx,Y,mFea,nSmp);
if isempty(NCWeight)
Y = Y*V;
sumV = max(sum(V,1),1e-10);
else
wV = repmat(NCWeight,1,k).*V;
Y = Y*wV;
sumV = max(sum(wV,1),1e-10);
clear wV;
end
U = U.*(Y./repmat(sumV,mFea,1));
else
if mn < maxM
% ===================== update V ========================
Y = U*V';
Y = X./max(Y,1e-10);
Y = Y'*U;
if alpha > 0
if isempty(NCWeight)
Y = V.*Y;
else
Y = repmat(NCWeight,1,k).*V.*Y;
end
sumU = max(sum(U,1),1e-10);
for i = 1:k
tmpL = L;
tmpD = tmpNCWeight*sumU(i);
tmpL = tmpL+spdiags(tmpD,0,nSmp,nSmp);
V(:,i) = tmpL\Y(:,i);
end
else
sumU = max(sum(U,1),1e-10);
Y = V.*Y;
V = Y./repmat(sumU,nSmp,1);
end
% ===================== update U ========================
Y = U*V';
Y = X./max(Y,1e-10);
if isempty(NCWeight)
Y = Y*V;
sumV = max(sum(V,1),1e-10);
else
wV = repmat(NCWeight,1,k).*V;
Y = Y*wV;
sumV = max(sum(wV,1),1e-10);
clear wV;
end
U = U.*(Y./repmat(sumV,mFea,1));
else
error('Not implemented!');
end
end
clear Y sumU sumV;
nIter = nIter + 1;
if nIter > minIter
if selectInit
if issparse(X)
[obj_NMFhistory, obj_Laphistory] = CalculateObjSparse(Cons, jdx, vdx, ldx, U, V, L, NCWeight);
else
[obj_NMFhistory, obj_Laphistory] = CalculateObj(Cons, X, U, V, L, NCWeight);
end
objhistory = obj_NMFhistory + obj_Laphistory;
maxErr = 0;
else
if isempty(maxIter)
if issparse(X)
[obj_NMF, obj_Lap] = CalculateObjSparse(Cons, jdx, vdx, ldx, U, V, L, NCWeight);
else
[obj_NMF, obj_Lap] = CalculateObj(Cons, X, U, V, L, NCWeight);
end
newobj = obj_NMF + obj_Lap;
obj_NMFhistory = [obj_NMFhistory obj_NMF]; %#ok<AGROW>
obj_Laphistory = [obj_Laphistory obj_Lap]; %#ok<AGROW>
objhistory = [objhistory newobj]; %#ok<AGROW>
meanFit = meanFitRatio*meanFit + (1-meanFitRatio)*newobj;
maxErr = (meanFit-newobj)/meanFit;
else
maxErr = 1;
if nIter >= maxIter
maxErr = 0;
objhistory = 0;
end
end
end
end
end
if tryNo == 1
U_final = U;
V_final = V;
nIter_final = nIter;
objhistory_final = objhistory;
else
if objhistory(end) < objhistory_final(end)
U_final = U;
V_final = V;
nIter_final = nIter;
objhistory_final = objhistory;
end
end
if selectInit
if tryNo < nRepeat
if kmeansInit
[U,V] = NMF_init(X, k);
else
U = abs(rand(mFea,k));
V = abs(rand(nSmp,k));
end
[U,V] = NormalizeUV(U, V, NormV, Norm);
nIter = 0;
else
tryNo = tryNo - 1;
nIter = minIter + 1;
selectInit = 0;
U = U_final;
V = V_final;
objhistory = objhistory_final;
meanFit = objhistory*10;
end
end
end
[U_final,V_final] = NormalizeUV(U_final, V_final, NormV, Norm);
%=======================================================
function [obj_NMF, obj_Lap] = CalculateObjSparse(Cons, jdx, vdx, ldx, U, V, L, NCWeight)
ZERO_OFFSET = 1e-200;
maxM = 62500000;
mFea = size(U,1);
nSmp = size(V,1);
mn = mFea*nSmp;
nBlock = floor(maxM/(mFea*2));
if mn < maxM
Y = U*V';
if isempty(NCWeight)
obj_NMF = sum(sum(Y)) - sum(vdx.*log(Y(ldx)+ZERO_OFFSET)); % 14p (p << mn)
else
obj_NMF = sum(NCWeight'.*sum(Y,1)) - sum(NCWeight(jdx).*(vdx.*log(Y(ldx)+ZERO_OFFSET))); % 14p (p << mn)
end
else
obj_NMF = 0;
ldxRemain = ldx;
vdxStart = 1;
for i = 1:ceil(nSmp/nBlock)
if i == ceil(nSmp/nBlock)
smpIdx = (i-1)*nBlock+1:nSmp;
ldxNow = ldxRemain;
vdxNow = vdxStart:length(vdx);
else
smpIdx = (i-1)*nBlock+1:i*nBlock;
ldxLast = find(ldxRemain <= mFea*i*nBlock, 1 ,'last');
ldxNow = ldxRemain(1:ldxLast);
ldxRemain = ldxRemain(ldxLast+1:end);
vdxNow = vdxStart:(vdxStart+ldxLast-1);
vdxStart = vdxStart+ldxLast;
end
Y = U*V(smpIdx,:)';
if isempty(NCWeight)
obj_NMF = obj_NMF + sum(sum(Y)) - sum(vdx(vdxNow).*log(Y(ldxNow-mFea*(i-1)*nBlock)+ZERO_OFFSET));
else
obj_NMF = obj_NMF + sum(NCWeight(smpIdx)'.*sum(Y,1)) - sum(NCWeight(jdx(vdxNow)).*(vdx(vdxNow).*log(Y(ldxNow-mFea*(i-1)*nBlock)+ZERO_OFFSET)));
end
end
end
obj_NMF = obj_NMF + Cons;
Y = log(V + ZERO_OFFSET);
if isempty(L)
obj_Lap = 0;
else
obj_Lap = sum(sum((L*Y).*V));
end
%=======================================================
function [obj_NMF, obj_Lap] = CalculateObj(Cons, X, U, V, L, NCWeight)
ZERO_OFFSET = 1e-200;
[mFea, nSmp] = size(X);
maxM = 62500000;
mn = numel(X);
nBlock = floor(maxM/(mFea*2));
if mn < maxM
Y = U*V'+ZERO_OFFSET;
if isempty(NCWeight)
obj_NMF = sum(sum(Y - X.*log(Y))); % 14mn
else
obj_NMF = sum(NCWeight'.*sum(Y - X.*log(Y),1)); % 14mn
end
else
obj_NMF = 0;
for i = 1:ceil(nSmp/nBlock)
if i == ceil(nSmp/nBlock)
smpIdx = (i-1)*nBlock+1:nSmp;
else
smpIdx = (i-1)*nBlock+1:i*nBlock;
end
Y = U*V(smpIdx,:)'+ZERO_OFFSET;
if isempty(NCWeight)
obj_NMF = obj_NMF + sum(sum(Y - X(:,smpIdx).*log(Y)));
else
obj_NMF = obj_NMF + sum(NCWeight(smpIdx)'.*sum(Y - X(:,smpIdx).*log(Y),1));
end
end
end
obj_NMF = obj_NMF + Cons;
Y = log(V + ZERO_OFFSET);
if isempty(L)
obj_Lap = 0;
else
obj_Lap = sum(sum((L*Y).*V));
end
%=======================================================
function [U, V] = NormalizeUV(U, V, NormV, Norm)
K = size(U,2);
if Norm == 2
if NormV
norms = max(1e-15,sqrt(sum(V.^2,1)))';
V = V*spdiags(norms.^-1,0,K,K);
U = U*spdiags(norms,0,K,K);
else
norms = max(1e-15,sqrt(sum(U.^2,1)))';
U = U*spdiags(norms.^-1,0,K,K);
V = V*spdiags(norms,0,K,K);
end
else
if NormV
norms = max(1e-15,sum(abs(V),1))';
V = V*spdiags(norms.^-1,0,K,K);
U = U*spdiags(norms,0,K,K);
else
norms = max(1e-15,sum(abs(U),1))';
U = U*spdiags(norms.^-1,0,K,K);
V = V*spdiags(norms,0,K,K);
end
end
function [U,V] = NMF_init(X, k)
[label, center] = litekmeans(X',k,'maxIter',10);
center = max(0,center);
U = center';
UTU = U'*U;
UTU = max(UTU,UTU');
UTX = U'*X;
V = max(0,UTU\UTX);
V = V';