annotate toolboxes/alps/ALPSbenchmark_errornorm_vs_iter.m @ 177:714fa7b8c1ad danieleb

added ramirez dl (to be completed) and MOCOD dictionary update
author Daniele Barchiesi <daniele.barchiesi@eecs.qmul.ac.uk>
date Thu, 17 Nov 2011 11:18:25 +0000
parents 0de08f68256b
children
rev   line source
ivan@154 1 % =========================================================================
ivan@154 2 % Algebraic Pursuit algorithms - Demo
ivan@154 3 % =========================================================================
ivan@154 4 % Algebraic Pursuit (ALPS) algorithms are accelerated hard thresholding methods
ivan@154 5 % on sparse recovery of linear inverse systems. In particular, let
ivan@154 6 % Phi : M x N real matrix (M < N)
ivan@154 7 % x* : N x 1 K-sparse data vector
ivan@154 8 % n : N x 1 additive noise vector
ivan@154 9 % then, y = Phi x* + n is the undersampled M x 1 measurement vector. ALPS
ivan@154 10 % solve the following minimization problem
ivan@154 11 % minimize ||y - Phi x||^2 subject to x is K-sparse vector.
ivan@154 12 %
ivan@154 13 % Detailed discussion on the algorithm can be found in
ivan@154 14 % [1] "On Accelerated Hard Thresholding Methods for Sparse Approximation", written
ivan@154 15 % by Volkan Cevher, Technical Report, 2011.
ivan@154 16 % =========================================================================
ivan@154 17 % 01/04/2011, by Anastasios Kyrillidis. anastasios.kyrillidis@epfl.ch, EPFL.
ivan@154 18 % =========================================================================
ivan@154 19
ivan@154 20 clear all
ivan@154 21 close all
ivan@154 22 clc;
ivan@154 23
ivan@154 24 addpath ALPS/;
ivan@154 25
ivan@154 26 % General parameters
ivan@154 27 N = 4000; % Size of sparse vector x
ivan@154 28 M = 800; % Number of measurements
ivan@154 29 K = ceil(M*(0.2)); % Sparsity of x*
ivan@154 30 sigma = 0.0000; % Additive noise variance
ivan@154 31 verbose = 1; % Print information
ivan@154 32
ivan@154 33 % ALPS parameters
ivan@154 34 its = 600; % Maximum number of iterations
ivan@154 35 Ttol= 1e-6; % Convergence tolerance
ivan@154 36 tau = -1; % Momentum term
ivan@154 37 mu = 0; % Step size selection
ivan@154 38 MC = 10; % Number of Monte-Carlo iterations
ivan@154 39 useCG = 0; % Conjugate Gradient
ivan@154 40
ivan@154 41 % Please refer to generate_matrix.m and generate_vector.m files
ivan@154 42 m_ensemble = 'Gaussian'; % Measurement matrix ensemble type
ivan@154 43 x_ensemble = 'Gaussian'; % Sparse vector ensemble type
ivan@154 44
ivan@154 45 max_its = 0;
ivan@154 46
ivan@154 47 for i = 1:MC
ivan@154 48 x = generate_vector(N, K, x_ensemble);
ivan@154 49 Phi = generate_matrix(M, N, m_ensemble);
ivan@154 50 noise = sigma*randn(M,1);
ivan@154 51 y = Phi*x + noise;
ivan@154 52
ivan@154 53 % 0-ALPS(0)
ivan@154 54 disp('=====================================================================');
ivan@154 55 [x_hat, numiter, time, x_path] = AlgebraicPursuit(y, Phi, K, 'memory', 0, 'mode', 0, 'tolerance', Ttol, 'ALPSiterations', its, 'verbose', verbose, 'tau', tau, 'useCG', useCG, 'mu', mu);
ivan@154 56 str = sprintf('Error norm: %f ', norm(x-x_hat));
ivan@154 57 disp(str);
ivan@154 58 num_iter(1, i) = numiter;
ivan@154 59 error(1,i,1:size(x_path,2))=sqrt(sum(abs(x*ones(1,size(x_path,2))-x_path).^2));
ivan@154 60 total_time(1, i) = time;
ivan@154 61
ivan@154 62 if (numiter > max_its)
ivan@154 63 max_its = numiter;
ivan@154 64 end;
ivan@154 65
ivan@154 66 % 0-ALPS(2)
ivan@154 67 disp('=====================================================================');
ivan@154 68 [x_hat, numiter, time, x_path] = AlgebraicPursuit(y, Phi, K, 'memory', 0, 'mode', 2, 'tolerance', Ttol, 'ALPSiterations', its, 'verbose', verbose, 'tau', tau, 'useCG', useCG, 'mu', mu);
ivan@154 69 str = sprintf('Error norm: %f ', norm(x-x_hat));
ivan@154 70 disp(str);
ivan@154 71 num_iter(2, i) = numiter;
ivan@154 72 error(2,i,1:size(x_path,2))=sqrt(sum(abs(x*ones(1,size(x_path,2))-x_path).^2));
ivan@154 73 total_time(2, i) = time;
ivan@154 74
ivan@154 75 if (numiter > max_its)
ivan@154 76 max_its = numiter;
ivan@154 77 end;
ivan@154 78
ivan@154 79 % 0-ALPS(4)
ivan@154 80 disp('=====================================================================');
ivan@154 81 [x_hat, numiter, time, x_path] = AlgebraicPursuit(y, Phi, K, 'memory', 0, 'mode', 4, 'tolerance', Ttol, 'ALPSiterations', its, 'verbose', verbose, 'tau', tau, 'useCG', useCG, 'mu', mu);
ivan@154 82 str = sprintf('Error norm: %f ', norm(x-x_hat));
ivan@154 83 disp(str);
ivan@154 84 num_iter(3, i) = numiter;
ivan@154 85 error(3,i,1:size(x_path,2))=sqrt(sum(abs(x*ones(1,size(x_path,2))-x_path).^2));
ivan@154 86 total_time(3, i) = time;
ivan@154 87
ivan@154 88 if (numiter > max_its)
ivan@154 89 max_its = numiter;
ivan@154 90 end;
ivan@154 91
ivan@154 92 % 0-ALPS(5)
ivan@154 93 disp('=====================================================================');
ivan@154 94 [x_hat, numiter, time, x_path] = AlgebraicPursuit(y, Phi, K, 'memory', 0, 'mode', 5, 'tolerance', Ttol, 'ALPSiterations', its, 'verbose', verbose, 'tau', tau, 'useCG', useCG, 'mu', mu);
ivan@154 95 str = sprintf('Error norm: %f ', norm(x-x_hat));
ivan@154 96 disp(str);
ivan@154 97 num_iter(4, i) = numiter;
ivan@154 98 error(4,i,1:size(x_path,2))=sqrt(sum(abs(x*ones(1,size(x_path,2))-x_path).^2));
ivan@154 99 total_time(4, i) = time;
ivan@154 100
ivan@154 101 if (numiter > max_its)
ivan@154 102 max_its = numiter;
ivan@154 103 end;
ivan@154 104
ivan@154 105 % 1-ALPS(2)
ivan@154 106 disp('=====================================================================');
ivan@154 107 [x_hat, numiter, time, x_path] = AlgebraicPursuit(y, Phi, K, 'memory', 1, 'mode', 2, 'tolerance', Ttol, 'ALPSiterations', its, 'verbose', verbose, 'tau', tau, 'useCG', useCG, 'mu', mu);
ivan@154 108 str = sprintf('Error norm: %f ', norm(x-x_hat));
ivan@154 109 disp(str);
ivan@154 110 num_iter(5, i) = numiter;
ivan@154 111 error(5,i,1:size(x_path,2))=sqrt(sum(abs(x*ones(1,size(x_path,2))-x_path).^2));
ivan@154 112 total_time(5, i) = time;
ivan@154 113
ivan@154 114 if (numiter > max_its)
ivan@154 115 max_its = numiter;
ivan@154 116 end;
ivan@154 117
ivan@154 118 % 1-ALPS(4)
ivan@154 119 disp('=====================================================================');
ivan@154 120 [x_hat, numiter, time, x_path] = AlgebraicPursuit(y, Phi, K, 'memory', 1, 'mode', 4, 'tolerance', Ttol, 'ALPSiterations', its, 'verbose', verbose, 'tau', tau, 'useCG', useCG, 'mu', mu);
ivan@154 121 str = sprintf('Error norm: %f ', norm(x-x_hat));
ivan@154 122 disp(str);
ivan@154 123 num_iter(6, i) = numiter;
ivan@154 124 error(6,i,1:size(x_path,2))=sqrt(sum(abs(x*ones(1,size(x_path,2))-x_path).^2));
ivan@154 125 total_time(6, i) = time;
ivan@154 126
ivan@154 127 if (numiter > max_its)
ivan@154 128 max_its = numiter;
ivan@154 129 end;
ivan@154 130
ivan@154 131 % 1-ALPS(5)
ivan@154 132 disp('=====================================================================');
ivan@154 133 [x_hat, numiter, time, x_path] = AlgebraicPursuit(y, Phi, K, 'memory', 1, 'mode', 5, 'tolerance', Ttol, 'ALPSiterations', its, 'verbose', verbose, 'tau', tau, 'useCG', useCG, 'mu', mu);
ivan@154 134 str = sprintf('Error norm: %f ', norm(x-x_hat));
ivan@154 135 disp(str);
ivan@154 136 num_iter(7, i) = numiter;
ivan@154 137 error(7,i,1:size(x_path,2))=sqrt(sum(abs(x*ones(1,size(x_path,2))-x_path).^2));
ivan@154 138 total_time(7,i) = time;
ivan@154 139
ivan@154 140 if (numiter > max_its)
ivan@154 141 max_its = numiter;
ivan@154 142 end;
ivan@154 143
ivan@154 144 % 2-ALPS(2)
ivan@154 145 disp('=====================================================================');
ivan@154 146 [x_hat, numiter, time, x_path] = AlgebraicPursuit(y, Phi, K, 'memory', 2, 'mode', 2, 'tolerance', Ttol, 'ALPSiterations', its, 'verbose', verbose, 'tau', tau, 'useCG', useCG, 'mu', mu);
ivan@154 147 str = sprintf('Error norm: %f ', norm(x-x_hat));
ivan@154 148 disp(str);
ivan@154 149 num_iter(8, i) = numiter;
ivan@154 150 error(8,i,1:size(x_path,2))=sqrt(sum(abs(x*ones(1,size(x_path,2))-x_path).^2));
ivan@154 151 total_time(8, i) = time;
ivan@154 152
ivan@154 153 if (numiter > max_its)
ivan@154 154 max_its = numiter;
ivan@154 155 end;
ivan@154 156
ivan@154 157 % 2-ALPS(4)
ivan@154 158 disp('=====================================================================');
ivan@154 159 [x_hat, numiter, time, x_path] = AlgebraicPursuit(y, Phi, K, 'memory', 2, 'mode', 4, 'tolerance', Ttol, 'ALPSiterations', its, 'verbose', verbose, 'tau', tau, 'useCG', useCG, 'mu', mu);
ivan@154 160 str = sprintf('Error norm: %f ', norm(x-x_hat));
ivan@154 161 disp(str);
ivan@154 162 num_iter(9, i) = numiter;
ivan@154 163 error(9,i,1:size(x_path,2))=sqrt(sum(abs(x*ones(1,size(x_path,2))-x_path).^2));
ivan@154 164 total_time(9, i) = time;
ivan@154 165
ivan@154 166 if (numiter > max_its)
ivan@154 167 max_its = numiter;
ivan@154 168 end;
ivan@154 169
ivan@154 170 % 2-ALPS(5)
ivan@154 171 disp('=====================================================================');
ivan@154 172 [x_hat, numiter, time, x_path] = AlgebraicPursuit(y, Phi, K, 'memory', 2, 'mode', 5, 'tolerance', Ttol, 'ALPSiterations', its, 'verbose', verbose, 'tau', tau, 'useCG', useCG, 'mu', mu);
ivan@154 173 str = sprintf('Error norm: %f ', norm(x-x_hat));
ivan@154 174 disp(str);
ivan@154 175 num_iter(10, i) = numiter;
ivan@154 176 error(10,i,1:size(x_path,2))=sqrt(sum(abs(x*ones(1,size(x_path,2))-x_path).^2));
ivan@154 177 total_time(10, i) = time;
ivan@154 178
ivan@154 179 if (numiter > max_its)
ivan@154 180 max_its = numiter;
ivan@154 181 end;
ivan@154 182
ivan@154 183 % 3-ALPS(2)
ivan@154 184 disp('=====================================================================');
ivan@154 185 [x_hat, numiter, time, x_path] = AlgebraicPursuit(y, Phi, K, 'memory', 3, 'mode', 2, 'tolerance', Ttol, 'ALPSiterations', its, 'verbose', verbose, 'tau', tau, 'useCG', useCG, 'mu', mu);
ivan@154 186 str = sprintf('Error norm: %f ', norm(x-x_hat));
ivan@154 187 disp(str);
ivan@154 188 num_iter(11, i) = numiter;
ivan@154 189 error(11,i,1:size(x_path,2))=sqrt(sum(abs(x*ones(1,size(x_path,2))-x_path).^2));
ivan@154 190 total_time(11, i) = time;
ivan@154 191
ivan@154 192 if (numiter > max_its)
ivan@154 193 max_its = numiter;
ivan@154 194 end;
ivan@154 195 end;
ivan@154 196
ivan@154 197 set(0,'DefaultAxesFontSize',12);
ivan@154 198
ivan@154 199 figure;
ivan@154 200 semilogy(squeeze(median(error(1,:,:),2)),'LineWidth',3); hold on
ivan@154 201 semilogy(squeeze(median(error(2,:,:),2)),'--','LineWidth',3);
ivan@154 202 semilogy(squeeze(median(error(3,:,:),2)),':','LineWidth',3);
ivan@154 203 semilogy(squeeze(median(error(4,:,:),2)),'k','LineWidth',4);
ivan@154 204 semilogy(squeeze(median(error(5,:,:),2)),'k--','LineWidth',4);
ivan@154 205 semilogy(squeeze(median(error(6,:,:),2)),'k:','LineWidth',4);
ivan@154 206 semilogy(squeeze(median(error(7,:,:),2)),'k-.','LineWidth',4);
ivan@154 207 semilogy(squeeze(median(error(8,:,:),2)),'r--','LineWidth',3);
ivan@154 208 semilogy(squeeze(median(error(9,:,:),2)),'m--','LineWidth',3);
ivan@154 209 semilogy(squeeze(median(error(10,:,:),2)),'k--','LineWidth',3);
ivan@154 210 semilogy(squeeze(median(error(11,:,:),2)),'g--','LineWidth',3);
ivan@154 211
ivan@154 212 xlabel('[i]:iteration number')
ivan@154 213 ylabel('||x-x_i||')
ivan@154 214 axis([1 max_its + 10 10^-5 2*10^0])
ivan@154 215
ivan@154 216 legend(strcat('0-IHT(0) [', num2str(mean(num_iter(1,:))),'][', num2str(mean(total_time(1,:))),']'), ...
ivan@154 217 strcat('0-IHT(2) [', num2str(mean(num_iter(2,:))),'][', num2str(mean(total_time(2,:))),']'), ...
ivan@154 218 strcat('0-IHT(4) [', num2str(mean(num_iter(3,:))),'][', num2str(mean(total_time(3,:))),']'), ...
ivan@154 219 strcat('0-IHT(5) [', num2str(mean(num_iter(4,:))),'][', num2str(mean(total_time(4,:))),']'), ...
ivan@154 220 strcat('1-IHT(2) [', num2str(mean(num_iter(5,:))),'][', num2str(mean(total_time(5,:))),']'), ...
ivan@154 221 strcat('1-IHT(4) [', num2str(mean(num_iter(6,:))),'][', num2str(mean(total_time(6,:))),']'), ...
ivan@154 222 strcat('1-IHT(5) [', num2str(mean(num_iter(7,:))),'][', num2str(mean(total_time(7,:))),']'), ...
ivan@154 223 strcat('2-ALPS(2) [', num2str(mean(num_iter(8,:))),'][', num2str(mean(total_time(8,:))),']'), ...
ivan@154 224 strcat('2-ALPS(4) [', num2str(mean(num_iter(9,:))),'][', num2str(mean(total_time(9,:))),']'), ...
ivan@154 225 strcat('2-ALPS(5) [', num2str(mean(num_iter(10,:))),'][', num2str(mean(total_time(10,:))),']'), ...
ivan@154 226 strcat('3-ALPS(2) [', num2str(mean(num_iter(11,:))),'][', num2str(mean(total_time(11,:))),']'));
ivan@154 227 title( strcat('N= ', num2str(N), ', M= ', num2str(M), ', K= ', num2str(K)) );
ivan@154 228 shg;