-
Notifications
You must be signed in to change notification settings - Fork 5
/
sparseNNLS.m
236 lines (193 loc) · 6.99 KB
/
sparseNNLS.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
function H = sparseNNLS(X,W,WtW,WtX,L,M,initH)
%
% H = sparseNNLS(X,W,WtW,WtX,L,M,initH)
%
% Performs nonnegative least squares with multiple rhs [2], until maximal
% M basis vectors (columns of W) are collected, i.e. until we have maximal M
% nonnegative coefficients in each column of H. Then iterativly discard
% smallest coefficient and perform the NNLS "correction loop" until maximal
% L nonzero coefficients remain in each column of H.
% This function unifies several methods, depending on parameters L and M:
%
% 1) for L = M = size(W,2), this function returns the nonnegative least
% squares solution for multiple rhs, i.e. it solves w.r.t. H
%
% minimize ||X - W * H||_F
% s.t. W(:) >= 0
% H(:) >= 0
%
% This is the algorithm described in [2].
%
% 2) for L = M < size(W,2), this function returns an approximate solution
% for the nonnegative sparse coding problem, i.e.
%
% minimize ||X - W * H||_F
% s.t. W(:) >= 0
% H(:) >= 0
% sum(H(:,k) > 0) <= L for all k
%
% This is the sparse NNLS (sNNLS) algorithm described in [3].
%
% 3) for L < M = size(W,2), this function returns an approximate solution
% for the nonnegative sparse coding problem, i.e.
%
% minimize ||X - W * H||_F
% s.t. W(:) >= 0
% H(:) >= 0
% sum(H(:,k) > 0) <= L for all k
%
% This is the reverse sparse NNLS (rsNNLS) algorithm described in [3].
%
% For L < M < size(W,2), we get a trade-off solution between sNNLS and rsNNLS.
%
%
% input:
% X: D x N data matrix (should be nonnegative).
% W: D x K dictionary matrix (should be negative) .
% WtW: W'*W (if [] is passed, this will be calculated).
% WtX: W'*X (if [] is passed, this will be calculated).
% L: maximal allowed number of nonzeros per column of H.
% M: "forward" NNLS will break after M collected nonzeros
% initH: initial H, for "warmstart"
%
% output:
% H: coding matrix, will be nonnegative and contain maximal
% L nonzeros per column
%
% References:
%
% [1] Lawson and Hanson, "Solving Least Squares Problems", Prentice-Hall, 1974.
%
% [2] M. H. Van Benthem and M. R. Keenan, "Fast algorithm for the solution of
% large-scale non-negativity-constrained least squares problems", Journal
% of Chemometrics, 2004; 18: 441-450.
%
% [3] R. Peharz and F. Pernkopf, "Sparse nonnegative matrix factorization with
% ℓ0-constraints", Neurocomputing, 2012.
%
% Robert Peharz, 2011
%
tol = 10*eps*norm(W,1)*length(W);
[~,K] = size(W);
[D,N] = size(X);
maxIter = 5*K;
iterCount = 0;
tmpVar = zeros(K,N);
tmpAlpha = inf(K,N);
G = zeros(K,N);
if nargin < 3 || isempty(WtW)
WtW = W'*W;
end
if nargin < 4 || isempty(WtX)
WtX = W'*X;
end
if nargin < 5 || isempty(L)
L = K;
end
if nargin < 6 || isempty(M)
M = L;
end
M = min([M,K,D]);
% Initialize in-active set of active columns to all
if nargin < 7 || size(initH,1) ~= K || size(initH,2) ~= N || any(initH(:) < 0)
P = false(K,N);
H = zeros(K,N);
else
P = initH > 0;
H = initH;
G = combinatorialLS(WtW, WtX, P);
negSet = find(any(G <= tol & P))';
%%% correction loop
while ~isempty(negSet)
[rowIdx, colIdx] = find(P(:,negSet) & G(:,negSet) < tol);
globCol = negSet(colIdx);
idx = sub2ind([K,N], rowIdx, globCol);
tmpAlpha(idx) = H(idx) ./ (H(idx) - G(idx));
alpha = min(tmpAlpha(:,negSet));
tmpAlpha(idx) = Inf;
H(:,negSet) = H(:,negSet) + repmat(alpha,K,1) .* (G(:,negSet) - H(:,negSet));
% Reset Z and P given intermediate values of H
P(:,negSet) = abs(H(:,negSet)) >= tol & P(:,negSet);
% Re-solve for G
G(:,negSet) = combinatorialLS(WtW, WtX(:,negSet), P(:,negSet));
negSet = find(any(G <= tol & P))';
end
H = G;
end
grad = WtX - WtW*H;
inIdx = find(any(grad > tol & ~P));
%%% -------------------------------------------------------------------
%%% Forward stage: active set NNLS, break when M nonzeros are
%%% collected (sNNLS)
%%% -------------------------------------------------------------------
%%% outer loop: select next variable for active set
while ~isempty(inIdx)
tmpVar(P) = -Inf;
tmpVar(~P) = grad(~P);
% Find variable with largest Lagrange multiplier
[~,maxIdx] = max(tmpVar(:,inIdx));
% Move variables zero set to positive set
idx = sub2ind([K,N], maxIdx, inIdx);
P(idx) = true;
% Compute intermediate solution using only variables in positive set
G(:,inIdx) = combinatorialLS(WtW, WtX(:,inIdx), P(:,inIdx));
negSet = any(G(:,inIdx) <= tol & P(:,inIdx));
negSet = inIdx(negSet)';
%%% correction loop
while ~isempty(negSet)
iterCount = iterCount + 1;
[rowIdx, colIdx] = find(P(:,negSet) & G(:,negSet) < tol);
globCol = negSet(colIdx);
idx = sub2ind([K,N], rowIdx, globCol);
tmpAlpha(idx) = H(idx) ./ (H(idx) - G(idx));
alpha = min(tmpAlpha(:,negSet));
tmpAlpha(idx) = Inf;
H(:,negSet) = H(:,negSet) + repmat(alpha,K,1) .* (G(:,negSet) - H(:,negSet));
% Reset Z and P given intermediate values of H
P(:,negSet) = abs(H(:,negSet)) >= tol & P(:,negSet);
% Re-solve for G
G(:,negSet) = combinatorialLS(WtW, WtX(:,negSet), P(:,negSet));
negSet = any(G(:,inIdx) <= tol & P(:,inIdx));
negSet = inIdx(negSet)';
end
H = G;
grad = WtX - WtW*H;
inIdx = find(any(grad > tol & ~P) & sum(P) < M);
if iterCount > maxIter
% fprintf('MAX ITERCOUNT\n')
break
end
end
%%% -----------------------------------------------------------------------
%%% Reverse stage: remove coefficients until maximal L nonzeros
%%% remain (rsNNLS)
%%% -----------------------------------------------------------------------
inIdx = find(sum(P) > L);
while ~isempty(inIdx)
tmpVar = H;
tmpVar(~P) = Inf;
[~,minIdx] = min(tmpVar(:,inIdx));
idx = sub2ind([K,N], minIdx, inIdx);
P(idx) = false;
G(:,inIdx) = combinatorialLS(WtW, WtX(:,inIdx), P(:,inIdx));
negSet = any(G(:,inIdx) <= tol & P(:,inIdx));
negSet = inIdx(negSet)';
%%% correction loop
while ~isempty(negSet)
[rowIdx, colIdx] = find(P(:,negSet) & G(:,negSet) < tol);
globCol = negSet(colIdx);
idx = sub2ind([K,N], rowIdx, globCol);
tmpAlpha(idx) = H(idx) ./ (H(idx) - G(idx));
alpha = min(tmpAlpha(:,negSet));
tmpAlpha(idx) = Inf;
H(:,negSet) = H(:,negSet) + repmat(alpha,K,1) .* (G(:,negSet) - H(:,negSet));
% Reset Z and P given intermediate values of H
P(:,negSet) = abs(H(:,negSet)) >= tol & P(:,negSet);
% Re-solve for G
G(:,negSet) = combinatorialLS(WtW, WtX(:,negSet), P(:,negSet));
negSet = any(G(:,inIdx) <= tol & P(:,inIdx));
negSet = inIdx(negSet)';
end
H = G;
inIdx = find(sum(P) > L);
end