-
Notifications
You must be signed in to change notification settings - Fork 1
/
pg_benchmarking.m
118 lines (100 loc) · 3.33 KB
/
pg_benchmarking.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
%Use pg_benchmarking with custom problem data (modify the data) or randomly
%generated problem data (default) to compare the LASSO function values
%under different machine representation. The default function implements PG
%under 'double precision', 'single precision', '12 bits fixed-point' and
%'16 bits fixed-point' representations. Edit 'mytypes.m' to add custom data
%types. type 'help mytypes.m' for furhter information.
function pg_benchmarking
clear;
clc;
close all;
addpath 'C:\Users\ah225\Documents\MATLAB\RPFOSolver Benchmark\PG'
%% Problem data
% s = RandStream.create('mt19937ar','seed',0);
% RandStream.setDefaultStream(s);
m = 100; % number of examples
n = 500; % number of features
x0 = randn(n,1);
A = randn(m,n);
A = A*spdiags(1./sqrt(sum(A.^2))',0,n,n); % normalize columns
v = sqrt(0.001)*randn(m,1);
b = A*x0 + v;
fprintf('solving instance with %d examples, %d variables\n', m, n);
fprintf('nnz(x0) = %d; signal-to-noise ratio: %.2f\n', nnz(x0), norm(A*x0)^2/norm(v)^2);
gamma_max = norm(A'*b,'inf');
gamma = 0.1*gamma_max;
% cached computations for all methods
AtA = A'*A;
Atb = A'*b;
%% Global constants and defaults
MAX_ITER = 100;
ABSTOL = 1e-4;
RELTOL = 1e-2;
%% Extra parameters for ADMM
lambda = 1;
rho = 1/lambda;
% % Test inputs
% m = 200; % number of examples
% n = 100; % number of features
% %x0 = sprandn(n,1,0.05);
% x0 = randn(n,1);
% A = randn(m,n);
% %A = A*spdiags(1./sqrt(sum(A.^2))',0,n,n); % normalize columns
% v = sqrt(0.1)*randn(m,1);
% b = A*x0 + v;
% fprintf('solving instance with %d examples, %d variables\n', m, n);
% fprintf('nnz(x0) = %d; signal-to-noise ratio: %.2f\n', nnz(x0), norm(A*x0)^2/norm(v)^2);
% gamma_max = norm(A'*b,'inf');
% gamma = 0.01*gamma_max;
% %Cached computations for all methods
% AtA = A'*A;
% Atb = A'*b;
L = max(eigs(AtA));
lambda = 1/L;
beta = 0.5;
% %Global constants and defaults
% MAX_ITER = 200;
% ABSTOL = eps;
%
f = @(u) 0.5*sum_square(double(A*u-b));
% Run
y0 = entrypoint('double',f,x0,A,b,AtA,Atb,lambda,gamma,beta,MAX_ITER,ABSTOL);
y01 = entrypoint('single',f,x0,A,b,AtA,Atb,lambda,gamma,beta,MAX_ITER,ABSTOL);
y8 = entrypoint('fixed8',f,x0,A,b,AtA,Atb,lambda,gamma,beta,MAX_ITER,ABSTOL);
y16 = entrypoint('fixed16',f,x0,A,b,AtA,Atb,lambda,gamma,beta,MAX_ITER,ABSTOL);
y12 = entrypoint('fixed12',f,x0,A,b,AtA,Atb,lambda,gamma,beta,MAX_ITER,ABSTOL);
% Plot
subplot(4,1,1);plot(y0,'k');
legend('Ground truth objective')
title('Ground truth')
subplot(4,2,3);plot(y01,'k');
title('Single precision floating-point output')
subplot(4,2,4);
try
semilogy(abs(y0(1:numel(y01))-double(y01)),'r');
catch
semilogy(abs(y0-double(y01(1:numel(y0)))),'r');
end
title('Single precision floating-point error')
subplot(4,2,5);plot(y12,'k');
title('12-bit fixed-point output')
subplot(4,2,6);
try
semilogy(abs(y0(1:numel(y12))-double(y12)),'r');
catch
semilogy(abs(y0-double(y12(1:numel(y0)))),'r');
end
title('12-bit fixed-point error')
subplot(4,2,7);plot(y16,'k');
title('16-bit fixed-point output')
xlabel('Iterations, k')
subplot(4,2,8);
try
semilogy(abs(y0(1:numel(y16))-double(y16)),'r');
catch
semilogy(abs(y0-double(y16(1:numel(y0)))),'r');
end
title('16-bit fixed-point error')
xlabel('Iterations, k')
pg_solv_mex
end