-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gradient_Visualize.m
124 lines (94 loc) · 3.38 KB
/
Gradient_Visualize.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
%% VISUALIZATION OF THE STEEPEST GRADIENT DESCENT METHOD FOR SOLVING LINEAR SYSTEMS
% * @file Gradient_Visualize.m
% * @brief
%
% GRADIENT DESCENT - STEEPEST DESCENT DEMO SCRIPT
%
% WE TRY TO SOLVE THE MINIMIZATION || y - Hx ||^2 USING THE
% STEEPEST GRADIENT DESCENT METHOD
%
% H is mxn, x is nx1 and y is mx1 and m > n
%
% * @date 1/27/2019
% * @author srijeshs
%% STEP 1: INITIALIZATION
clear all; clc;
% SETUP DIMENSIONS OF THE DEMO
DIM_1 = 2;
DIM_2 = 1;
% INITIALIZE X Y and H
H = rand(DIM_1);
cond_H = cond(H);
% FOR A VISUAL DEMONSTRATION, WE CHOOSE A RELATIVELY WELL-CONDITIONED H
while(cond_H > 5)
H = rand(DIM_1);
cond_H = cond(H);
end
X_true = rand(2,1);
Y = H*X_true;
% INITIALIZE THE RESIDUAL FUNCTION SPACE FOR A RANGE OF (X1,X2) PAIRS
residual_x = @(H,X1,X2,Y) [(Y(1) - X1.*H(1,1) + X2.*H(1,2)).^2 + (Y(2) - X1.*H(2,1) + X2.*H(2,2)).^2];
% x1, x2 DISCRETE SPACE DEFINITION
x1 = linspace(-10,10,1000);
x2 = linspace(-10,10,1000);
[X2,X1] = meshgrid(x2,x1);
% FOR EACH (X1,X2) PAIR IN OUR SURFACE,
% COMPUTE RESIDUAL Y - HX, WITH X = [X1 ; X2]
residual_space = residual_x(H,X1,X2,Y);
% PLOT THE RESIDUAL SURFACE GENERATED W.R.T THE TWO DIMENSIONS
figure(1);
imagesc(x1,x2,residual_space');
axis equal tight;
title('Residual surface to minimize');
xlabel('Dimension 1');
ylabel('Dimension 2');
hold on;
% PLOT THE TRUE X VECTOR THAT OUR SOLUTION MUST CONVERGE TO
plot(X_true(1),X_true(2),'-*g');
% INITIALIZE ESTIMATE OF X
X_est = ones(2,1).*3;
% PLOT THE INITAL ESTIMATE OF THE X VECTOR
h0 = plot(X_est(1),X_est(2),'-*r');
% INITIALIZE THE LEARNING RATE AND STOPPING THRESHOLD
lrate = 1e-1; haltThresh = 1e-5; nItermax = 1e+5;
% FUNCTION HANDLES FOR ERROR NORM, GRADIENT ALONG A DIMENSION
Err_Norm = @(Hmat,x_est,y_true) norm((y_true-Hmat*x_est),2);
% WE COMPUTE THE DERIVATIVE BY THE CENTRAL DIFFERENCE METHOD
Dim_Grad = @(Hmat,x_plus,x_minus,y_true,lrate) ...
(Err_Norm(Hmat,x_plus,y_true) - Err_Norm(Hmat,x_minus,y_true))./2*(lrate);
%% STEP 2: BEGIN ITERATIONS
nIter = 1;
I_n = eye(DIM_1);
delete(h0);
% INITIALIZE GRADIENT VECTOR
Grad_x = zeros(DIM_1,1);
while(norm(Y - H*X_est)>=haltThresh && nIter <= nItermax)
% PLOT CURRENT POSITION OF OUR ESTIMATE
h1 = plot(X_est(1),X_est(2),'*r');
pause(0.01); % Pause for visualization
delete(h1); % Delete this iteration's vector
% UPDATE THE GRADIENT VECTOR BY COMPUTING GRAD ALONG EACH DIMENSION
for iDim = 1:DIM_1
% FORWARD INCREMENT AND BACKWARD DECREMENT VECTORS FOR THIS DIM
xPlus = X_est + (I_n(:,iDim)).*lrate;
xMinus = X_est - (I_n(:,iDim)).*lrate;
% GRADIENT ALONG THIS DIMENSION
Grad_x(iDim) = Dim_Grad(H,xPlus,xMinus,Y,lrate);
end
% UPDATE ESTIMATE OF X USING THE GRADIENT VECTOR CALCULATED
X_est = X_est - Grad_x;
% INCREMENT ITERATION COUNT
nIter = nIter+1;
end
% PLOT THE FINAL CONVERGED SOLUTION
plot(X_est(1),X_est(2),'*r');
hold off;
%% PRINT RESULTS
% ITERATION COUNT AND 2-NORM ERROR UPON TERMINATION
fprintf('No. of iterations = %d\nResidual norm = %f\n\n',nIter,norm(X_est-X_true,2));
fprintf('X_true:\n');
disp(X_true);
fprintf('X_est:\n');
disp(X_est);
% VERIFICATION WITH PSEUDO-INVERSE SOLUTION
fprintf('2-norm of error w.r.t pseudo inverse closed-form solution = %f\n',norm(X_est - (pinv(H)*Y),2));