-
Notifications
You must be signed in to change notification settings - Fork 61
/
prox_l1_and_sum.m
424 lines (338 loc) · 13.2 KB
/
prox_l1_and_sum.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
function op = prox_l1_and_sum( q, b, nColumns, zeroID, useMex )
%PROX_L1_AND_SUM L1 norm with sum(X)=b constraints
% OP = PROX_L1_AND_SUM( Q ) implements the nonsmooth function
% OP(X) = norm(Q.*X,1) with constraints
% Q is optional; if omitted, Q=1 is assumed. But if Q is supplied,
% then it must be a positive real scalar (or must be same size as X).
%
% OP = PROX_L1_AND_SUM( Q, B )
% includes the constraints that sum(X(:)) == B
% (Default: B=1)
%
% OP = PROX_L1_AND_SUM( Q, B, nColumns )
% takes the input vector X and reshapes it to have nColumns
% and applies this prox to every column
%
% OP = PROX_L1_AND_SUM( Q, B, nColumns, zeroID )
% if zeroID == true (it is false by default)
% then after reshaping X, enforces that X(i,i) = 0
%
% OP = PROX_L1_AND_SUM( Q, B, nColumns, zeroID, useMex)
% toggle the usage of prox_l1_and_sum_worker_mex.cc for the computation.
% useMex defaults to true
%
%
% If available and compiled, this uses TFOCS/mexFiles/prox_l1_and_sum_worker_mex.cc.
% It also checks for TFOCS/mexFiles/shrink_mex2.cc to use as a slower backup.
% To control the number of threads these use, run
%
% `prox_l1_and_sum_worker_mex(struct('num_threads', 4))`
%
% when constructing your problem. This will cache the number of threads internally
% and use that number of threads until you restart MATLAB.
%
% Often useful for sparse subpsace clustering (SSC)
% See, e.g., https://github.com/stephenbeckr/SSC
% Nov 2017, [email protected]
% 18 Nov 2018, optimizations by [email protected]
if nargin == 0
q = 1;
elseif ~isnumeric( q ) || ~isreal( q ) || any( q(:) < 0) || all(q(:)==0) || numel( q ) ~= 1
error( 'Argument must be positive.' );
end
if nargin < 2 || isempty(b), b = 1; else, assert( numel(b) == 1 ); end
if nargin < 3 || isempty( nColumns), nColumns = 1;
else assert( numel(nColumns) == 1 && nColumns >= 1 ); end
if nargin < 4 || isempty( zeroID ), zeroID = false; end
if nargin < 5, useMex = true; end
if zeroID && nColumns == 1
warning('TFOCS:prox_l1_and_sum:zeroDiag',...
'You requested enforcing zero diagonals but did not set nColumns>1 which is probably a mistake');
end
if useMex
if 3~=exist('prox_l1_and_sum_worker_mex','file')
addpath( fullfile( tfocs_where, 'mexFiles' ) );
end
if 3==exist('prox_l1_and_sum_worker_mex','file')
op = tfocs_prox( @(x)f(q,x), @(x,t)prox_f_using_worker(q,b,nColumns,zeroID,x,t) , 'vector' );
return;
end
% else fall back to optimized MATLAB approach
end
% 3/15/18, adding:
%JMF 17 Nov 2018: determine if we have shrink_mex once when constructing the prox handle
if useMex
if 3~=exist('shrink_mex2','file')
addpath( fullfile( tfocs_where, 'mexFiles' ) );
end
if 3~=exist('shrink_mex2','file')
useMex = false;
end
end
if useMex
%shrink = @(x,tq) shrink_mex(x,tq); %NOTE: shrink_mex doesn't handle vector tq properly
shrink = @(x,tq) shrink_mex2(x,tq);
shrink_nu = @(x,tq,nu) shrink_mex2(x,tq,nu);
else
shrink = @(x,tq) sign(x).*max( bsxfun(@minus,abs(x),tq), 0 );
shrink_nu = @(x,tq,nu) shrink(bsxfun(@minus,x,nu),tq);
end
% This is Matlab and Octave compatible code
op = tfocs_prox( @(x)f(q,x), @(x,t)prox_f(q,b,nColumns,zeroID,shrink,shrink_nu,x,t) , 'vector' );
end
% These are now subroutines, that are NOT in the same scope
function v = f(qq,x)
v = norm( qq(:).*x(:), 1 );
end
function x = prox_f(qq,b,nColumns,zeroID,shrink,shrink_nu,x,t) % stepsize is t
tq = t .* qq; % March 2012, allowing vectorized stepsizes
tq = reshape(tq, [1 numel(tq)]);
if zeroID && nColumns > 1
x = reshape( x, [], nColumns );
nRows = size(x,1);
if nColumns > nRows
error('Cannot zero out the diagonal if columns > rows');
end
x = prox_l1sum_zeroID_matricized( x, tq, b, shrink_nu );
x = reshape(x, nRows*nColumns, 1);
else
if nColumns > 1
x = reshape( x, [], nColumns );
nRows = size(x,1);
x = prox_l1sum_matricized( x, tq, b, shrink_nu );
x = reshape(x, nRows*nColumns, 1);
else
x = prox_l1sum( x, tq, b, shrink_nu );
end
end
end
function x = prox_f_using_worker(qq,b,nColumns,zeroID,x,t) % stepsize is t
tq = t .* qq; % March 2012, allowing vectorized stepsizes
if nColumns > 1 % matrix
x = reshape(x, [], nColumns);
nRows = size(x,1);
if zeroID && nColumns > nRows
error('Cannot zero out the diagonal if columns > rows');
end
x = prox_l1_and_sum_worker_mex(x, tq, b, zeroID);
x = reshape(x, nRows*nColumns, 1);
else % vector
x = prox_l1_and_sum_worker_mex(x, tq, b);
end
end
% Main algorithmic part: if x0 is length n, takes O(n log n) time
function x = prox_l1sum( x0, lambda, b, shrink_nu )
brk_pts = sort( [x0-lambda;x0+lambda], 'descend' );
xnu = @(nu) shrink_nu( x0 , lambda, nu );
h = @(x) sum(x) - b; % want to solve h(nu) = 0
% Bisection
lwrBnd = 0;
uprBnd = length(brk_pts) + 1;
iMax = ceil( log2(length(brk_pts)) ) + 1;
PRINT = false; % set to "true" for debugging purposes
if PRINT
dispp = @disp;
printf = @fprintf;
else
dispp = @(varargin) 1;
printf = @(varargin) 1;
end
dispp(' ');
for i = 1:iMax
if uprBnd - lwrBnd <= 1
dispp('Bounds are too close; breaking');
break;
end
j = round( (lwrBnd+uprBnd)/2 );
%printf('j is %d (bounds were [%d,%d])\n', j, lwrBnd,uprBnd ); %
if j==lwrBnd
dispp('j==lwrBnd, so increasing');
j = j+1;
elseif j==uprBnd
dispp('j==uprBnd, so increasing');
j = j-1;
end
a = brk_pts(j);
x = xnu(a); % the prox
p = h(x);
if p > 0
uprBnd = j;
elseif p < 0
lwrBnd = j;
end
if PRINT
% Don't rely on redefinition of printf,
% since then we would still calculate find(~x)
% which is slow
printf('i=%2d, a = %6.3f, p = %8.3f, zeros ', i, a, p );
if n < 100, printf('%d ', find(~x) ); end
printf('\n');
end
end
% Now, determine linear part, which we infer from two points.
% If lwr/upr bounds are infinite, we take special care
% e.g., we make a new "a" slightly lower/bigger, and use this
% to extract linear part.
if lwrBnd == 0
a2 = brk_pts( uprBnd );
a1 = a2 - 10; % arbitrary
aBounds = [a1,a2];
elseif uprBnd == length(brk_pts) + 1
a1 = brk_pts( lwrBnd );
a2 = a1 + 10; % arbitrary
aBounds = [a1,a2];
else
% In general case, we can infer linear part from the two break points
a1 = brk_pts( lwrBnd );
a2 = brk_pts( uprBnd );
aBounds = [a1,a2];
end
% Now we have the support, find exact value
x = xnu(( aBounds(1)+aBounds(2))/2 ); % to find the support
supp = find(x);
sgn = sign(x);
nu = ( sum(x0(supp) - lambda*sgn(supp) ) - b )/length(supp);
x = xnu( nu );
end
% This variant can handle several columns at once,
% and it takes exactly log2(n) iterations, as it doesn't stop early
% since different columns might stop at different steps and that's
% not easy to detect efficiently.
function x = prox_l1sum_matricized( x0, lambda, b, shrink_nu )
brk_pts = sort( [x0-lambda;x0+lambda], 'descend' );
xnu = @(nu) shrink_nu( x0 , lambda, nu );
h = @(x) sum(x) - b; % want to solve h(nu) = 0
nCols = size( x0, 2 ); % allow matrices
LDA = size( brk_pts, 1 );
offsets = (0:nCols-1)*LDA;%i.e., [0, LDA, 2*LDA, ... ];
num_brk_pts = LDA;
lwrBnd = zeros(1,nCols);
uprBnd = (length(brk_pts) + 1)*ones(1,nCols);
iMax = ceil( log2(length(brk_pts)) ) + 1;
for i = 1:iMax
j = round(mean([lwrBnd;uprBnd]));
ind = find( j==lwrBnd );
j( ind ) = j( ind ) + 1;
ind = find( j==uprBnd );
j( ind ) = j( ind ) - 1;
a = brk_pts(j+offsets); % need the offsets to correct it here
x = xnu(a); % the prox
p = h(x);
ind = find( p > 0 );
uprBnd(ind) = j(ind);
ind = find( p < 0 );
lwrBnd(ind) = j(ind);
end
[a1,a2] = deal( zeros(1,nCols) );
ind = find( lwrBnd == 0 );
a2(ind) = brk_pts( uprBnd(ind) + offsets(ind) );
a1(ind) = a2(ind) + 10;
ind2 = ind;
ind = find( uprBnd == num_brk_pts + 1 );
a1(ind) = brk_pts( lwrBnd(ind) + offsets(ind) );
a2(ind) = a1(ind) - 10;
indOther = setdiff( 1:nCols, [ind2,ind] );
a1(indOther) = brk_pts( lwrBnd(indOther) + offsets(indOther) );
a2(indOther) = brk_pts( uprBnd(indOther) + offsets(indOther) );
a = mean( [a1;a2] );
x = xnu( a );
nu = zeros(1,nCols);
sgn = sign(x);
if numel(lambda) > 1
for col = 1:nCols
supp = find( sgn(:,col) );
nu(col) = ( sum(x0(supp,col) - lambda(col)*sgn(supp,col) ) - b )/length(supp);
end
else
for col = 1:nCols
supp = find( sgn(:,col) );
nu(col) = ( sum(x0(supp,col) - lambda*sgn(supp,col) ) - b )/length(supp);
end
end
x = xnu( nu );
end
% This variant can handle several columns at once,
% and it takes exactly log2(n) iterations, as it doesn't stop early
% since different columns might stop at different steps and that's
% not easy to detect efficiently.
%
% This hacks "tricks" the implementation into ignoring the diagonal to avoid an extra copy
function x = prox_l1sum_zeroID_matricized( x0, lambda, b, shrink_nu )
[nRows, nCols] = size(x0);
diag_inds = nRows*(0:nCols-1) + (1:nCols);
% Sort all possible break points
% We account for ignoring the diagonal by setting the diagonals to -inf,
% so they're at the bottom of the sorted matrix. We then need to handle the offsets
% appropriately.
brk_pts_minus = x0 - lambda; brk_pts_minus(diag_inds) = -inf;
brk_pts_plus = x0 + lambda; brk_pts_plus(diag_inds) = -inf;
brk_pts = [brk_pts_minus; brk_pts_plus];
%brk_pts = [x0 - lambda; x0 + lambda];
%inf_inds = [2*nRows*(0:nCols-1) + (1:nCols),
% 2*nRows*(0:nCols-1) + (1:nCols) + nRows];
%brk_pts(inf_inds) = -inf;
brk_pts = sort( brk_pts, 'descend' );
xnu = @(nu) shrink_nu( x0, lambda, nu );
h = @(x) sum(x) - x(diag_inds) - b; % want to solve h(nu) = 0
LDA = size( brk_pts, 1 );
offsets = (0:nCols-1)*LDA;%i.e., [0, LDA, 2*LDA, ... ];
num_brk_pts = LDA - 2;
lwrBnd = zeros(1,nCols);
uprBnd = (num_brk_pts + 1 )*ones(1,nCols);
iMax = ceil( log2(num_brk_pts) ) + 1;
for i = 1:iMax
j = round(mean([lwrBnd;uprBnd]));
ind = find( j==lwrBnd );
j( ind ) = j( ind ) + 1;
ind = find( j==uprBnd );
j( ind ) = j( ind ) - 1;
a = brk_pts(j+offsets); % need the offsets to correct it here
x = xnu(a); % the prox
p = h(x);
ind = find( p > 0 );
uprBnd(ind) = j(ind);
ind = find( p < 0 );
lwrBnd(ind) = j(ind);
end
[a1,a2] = deal( zeros(1,nCols) );
ind = find( lwrBnd == 0 );
a2(ind) = brk_pts( uprBnd(ind) + offsets(ind) );
a1(ind) = a2(ind) + 10;
ind2 = ind;
ind = find( uprBnd == num_brk_pts + 1 );
a1(ind) = brk_pts( lwrBnd(ind) + offsets(ind) );
a2(ind) = a1(ind) - 10;
indOther = setdiff( 1:nCols, [ind2,ind] );
a1(indOther) = brk_pts( lwrBnd(indOther) + offsets(indOther) );
a2(indOther) = brk_pts( uprBnd(indOther) + offsets(indOther) );
a = mean( [a1;a2] );
x = xnu( a );
sgn = sign(x);
supp = (sgn ~= 0); supp(diag_inds) = 0;
nu = zeros(1,nCols);
if numel(lambda) > 1
for col = 1:nCols
nu(col) = ( sum(x0(supp(:,col),col) - lambda(col)*sgn(supp(:,col),col) ) - b )/sum(supp(:,col));
end
else
UNSAFE_BUT_FAST = true;
if UNSAFE_BUT_FAST
% some MATLAB hackery to vectorize the below loop
% Note that due to roundoff in the first cumsum, this is not necessarily safe
% With data that are changing signs randomly, the numerical issues should be kept at bay.
% In initial experiments, this seems to be okay.
num_supp = sum(supp,1);
sumthing = cumsum(x0(supp) - lambda*sgn(supp)); % possible numerical issues!
inds = cumsum(num_supp);
numerator = sumthing(inds) - b;
numerator(2:nCols) = numerator(2:nCols) - sumthing(inds(1:nCols-1));
nu = numerator.' ./ num_supp;
else
for col = 1:nCols
nu(col) = ( sum(x0(supp(:,col),col) - lambda*sgn(supp(:,col),col) ) - b )/sum(supp(:,col));
end
end
end
x = xnu( nu );
x(diag_inds) = 0;
end