-
Notifications
You must be signed in to change notification settings - Fork 61
/
demo_full.m
141 lines (138 loc) · 5.02 KB
/
demo_full.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
function X = demo_full()
addpath('spams/build');
addpath('proj');
addpath('utils');
d = 300; % data dimension
N = 70; % number of samples
k = 100; % dictionary size
lambda = 0.01;
Y = normc(rand(d, N));
D = normc(rand(d, k));
opts.max_iter = 500;
opts.verbose = false;
opts.check_grad = false;
opts.tol = 1e-8;
opts.lambda = lambda;
%% cost function
function cost = calc_F(X)
if numel(lambda) == 1 % scalar
cost = 1/2 *normF2(Y - D*X) + lambda*norm1(X);
elseif numel(lambda) == numel(X)
cost = 1/2 *normF2(Y - D*X) + norm1(lambda.*X);
end
end
fprintf('********************Full demo**********************\n');
fprintf('A toy example:\n')
fprintf('Data dimension : %d\n', size(Y, 1));
fprintf('No. of samples : %d\n', size(Y, 2));
fprintf('No. of atoms in the dictionary: %d\n', size(D, 2));
fprintf('=====================================================\n')
fprintf('Lasso FISTA solution vs SPAMS solution,\n');
fprintf(' both of the following values should be close to 0.\n');
% param for mex
param.lambda = lambda;
param.lambda2 = 0;
param.numThreads = 1;
param.mode = 2;
% mex solution and optimal value
param.pos = true;
opts.pos = param.pos;
X_fista = fista_lasso(Y, D, [], opts);
X_spams = mexLasso(Y, D, param);
fprintf('1. average(norm1(X_fista - X_spams)) = %5f\n', ...
norm1(X_fista - X_spams)/numel(X_spams));
costmex = calc_F(X_spams);
costfista = calc_F(X_fista);
fprintf('2. costfista - cost_spams = %5f\n', ...
costfista - costmex);
if costfista < costmex
fprintf('FISTA provides a better cost.\n');
else
fprintf('SPAMS provides a better cost.\n');
end
%% lasso_weighted test
lambda = rand(size(X_spams));
opts.lambda = lambda;
opts.pos = false;
X_fista = fista_lasso(Y, D, [], opts);
param.lambda = 1;
param.lambda2 = 0;
param.numThreads = 1;
param.mode = 2;
param.pos = opts.pos;
W = lambda;
% mex solution and optimal value
X_spams = mexLassoWeighted(Y, D, W, param);
fprintf('=====================================================\n')
fprintf('Lasso Weighted FISTA solution vs SPAMS solution,\n');
fprintf(' both of the following values should be close to 0.\n');
fprintf('1. average(norm1(X_fista - X_spams)) = %5f\n', ...
norm1(X_fista - X_spams)/numel(X_fista));
costmex = calc_F(X_fista);
costfista = calc_F(X_spams);
fprintf('2. costfista - cost_spams = %5f\n', ...
costfista - costmex);
if costfista < costmex
fprintf('FISTA provides a better cost.\n');
else
fprintf('SPAMS provides a better cost.\n');
end
%% with positive constraint on X
fprintf('================Positive Constraint===================\n')
fprintf('Lasso FISTA solution vs SPAMS solution,\n');
fprintf(' both of the following values should be close to 0.\n');
% opts for fista
opts.lambda = 0.1;
opts.pos = true;
X_fista = fista_lasso(Y, D, [], opts);
% param for mex
param.lambda = opts.lambda;
param.lambda2 = 0;
param.numThreads = 1;
param.mode = 2;
param.pos = opts.pos;
% mex solution and optimal value
X_spams = mexLasso(Y, D, param);
fprintf('1. average(norm1(X_fista - X_spams)) = %5f\n', ...
norm1(X_fista - X_spams)/numel(X_spams));
costmex = calc_F(X_spams);
costfista = calc_F(X_fista);
fprintf('2. costfista - cost_spams = %5f\n', ...
costfista - costmex);
if costfista < costmex
fprintf('FISTA provides a better cost.\n');
else
fprintf('SPAMS provides a better cost.\n');
end
%% lasso_weighted test
fprintf('================Positive Constraint===================\n')
% opts for fista
lambda = rand(size(X_spams));
opts.lambda = lambda;
opts.pos = true;
% X_fista = lasso_fista(Y, D, [], opts);
X_fista = fista_lasso(Y, D, [], opts);
% param for mex
param.lambda = 1;
param.lambda2 = 0;
param.numThreads = 1;
param.mode = 2;
param.pos = opts.pos;
W = opts.lambda;
% mex solution and optimal value
X_spams = mexLassoWeighted(Y, D, W, param);
fprintf('Lasso Weighted FISTA solution vs SPAMS solution,\n');
fprintf(' both of the following values should be close to 0.\n');
fprintf('1. average(norm1(X_fista - X_spams)) = %5f\n', ...
norm1(X_fista - X_spams)/numel(X_fista));
costmex = calc_F(X_fista);
costfista = calc_F(X_spams);
fprintf('2. costfista - cost_spams = %5f\n', ...
costfista - costmex);
if costfista < costmex
fprintf('FISTA provides a better cost.\n');
else
fprintf('SPAMS provides a better cost.\n');
end
X = [];
end