-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGNMF_Multi.m
281 lines (250 loc) · 7.11 KB
/
GNMF_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
function [U_final, V_final, nIter_final, objhistory_final] = GNMF_Multi(X, k, W, options, U, V)
% Graph regularized Non-negative Matrix Factorization (GNMF) with
% multiplicative update
%
% 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
%
% You only need to provide the above four inputs.
%
% X = U*V'
%
% References:
% [1] Deng Cai, Xiaofei He, Xiaoyun Wu, and Jiawei Han. "Non-negative
% Matrix Factorization on Manifold", Proc. 2008 Int. Conf. on Data Mining
% (ICDM'08), Pisa, Italy, Dec. 2008.
%
% [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 2.1 --Dec./2011
% 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
minIter = maxIter;
end
meanFitRatio = options.meanFitRatio;
alpha = options.alpha;
Norm = 2;
NormV = 0;
[mFea,nSmp]=size(X);
if alpha > 0
W = alpha*W;
DCol = full(sum(W,2));
D = spdiags(DCol,0,nSmp,nSmp);
L = D - W;
if isfield(options,'NormW') && options.NormW
D_mhalf = spdiags(DCol.^-.5,0,nSmp,nSmp) ;
L = D_mhalf*L*D_mhalf;
end
else
L = [];
end
selectInit = 1;
if isempty(U)
U = abs(rand(mFea,k));
V = abs(rand(nSmp,k));
else
nRepeat = 1;
end
[U,V] = NormalizeUV(U, V, NormV, Norm);
if nRepeat == 1
selectInit = 0;
minIter = 0;
if isempty(maxIter)
objhistory = CalculateObj(X, U, V, L);
meanFit = objhistory*10;
else
if isfield(options,'Converge') && options.Converge
objhistory = CalculateObj(X, U, V, L);
end
end
else
if isfield(options,'Converge') && options.Converge
error('Not implemented!');
end
end
tryNo = 0;
nIter = 0;
while tryNo < nRepeat
tryNo = tryNo+1;
maxErr = 1;
while(maxErr > differror)
% ===================== update V ========================
XU = X'*U; % mnk or pk (p<<mn)
UU = U'*U; % mk^2
VUU = V*UU; % nk^2
if alpha > 0
WV = W*V;
DV = D*V;
XU = XU + WV;
VUU = VUU + DV;
end
V = V.*(XU./max(VUU,1e-10));
% ===================== update U ========================
XV = X*V; % mnk or pk (p<<mn)
VV = V'*V; % nk^2
UVV = U*VV; % mk^2
U = U.*(XV./max(UVV,1e-10)); % 3mk
nIter = nIter + 1;
if nIter > minIter
if selectInit
objhistory = CalculateObj(X, U, V, L);
maxErr = 0;
else
if isempty(maxIter)
newobj = CalculateObj(X, U, V, L);
objhistory = [objhistory newobj]; %#ok<AGROW>
meanFit = meanFitRatio*meanFit + (1-meanFitRatio)*newobj;
maxErr = (meanFit-newobj)/meanFit;
else
if isfield(options,'Converge') && options.Converge
newobj = CalculateObj(X, U, V, L);
objhistory = [objhistory newobj]; %#ok<AGROW>
end
maxErr = 1;
if nIter >= maxIter
maxErr = 0;
if isfield(options,'Converge') && options.Converge
else
objhistory = 0;
end
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
%re-start
U = abs(rand(mFea,k));
V = abs(rand(nSmp,k));
[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, dV] = CalculateObj(X, U, V, L, deltaVU, dVordU)
MAXARRAY = 500*1024*1024/8; % 500M. You can modify this number based on your machine's computational power.
if ~exist('deltaVU','var')
deltaVU = 0;
end
if ~exist('dVordU','var')
dVordU = 1;
end
dV = [];
nSmp = size(X,2);
mn = numel(X);
nBlock = ceil(mn/MAXARRAY);
if mn < MAXARRAY
dX = U*V'-X;
obj_NMF = sum(sum(dX.^2));
if deltaVU
if dVordU
dV = dX'*U + L*V;
else
dV = dX*V;
end
end
else
obj_NMF = 0;
if deltaVU
if dVordU
dV = zeros(size(V));
else
dV = zeros(size(U));
end
end
PatchSize = ceil(nSmp/nBlock);
for i = 1:nBlock
if i*PatchSize > nSmp
smpIdx = (i-1)*PatchSize+1:nSmp;
else
smpIdx = (i-1)*PatchSize+1:i*PatchSize;
end
dX = U*V(smpIdx,:)'-X(:,smpIdx);
obj_NMF = obj_NMF + sum(sum(dX.^2));
if deltaVU
if dVordU
dV(smpIdx,:) = dX'*U;
else
dV = dU+dX*V(smpIdx,:);
end
end
end
if deltaVU
if dVordU
dV = dV + L*V;
end
end
end
if isempty(L)
obj_Lap = 0;
else
obj_Lap = sum(sum((V'*L).*V'));
end
obj = obj_NMF+obj_Lap;
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