extra code
This commit is contained in:
parent
4086da2f7a
commit
c2485055c8
31
regression/code/errorGradient.m
Normal file
31
regression/code/errorGradient.m
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
clear
|
||||||
|
load('lin_regression.mat')
|
||||||
|
|
||||||
|
ms = -1:0.5:5;
|
||||||
|
ns = -10:1:10;
|
||||||
|
|
||||||
|
error_surf = zeros(length(ms), length(ns));
|
||||||
|
gradient_m = zeros(size(error_surf));
|
||||||
|
gradient_n = zeros(size(error_surf));
|
||||||
|
|
||||||
|
for i = 1:length(ms)
|
||||||
|
for j = 1:length(ns)
|
||||||
|
error_surf(i,j) = lsqError([ms(i), ns(j)], x, y);
|
||||||
|
grad = lsqGradient([ms(i), ns(j)], x, y);
|
||||||
|
gradient_m(i,j) = grad(1);
|
||||||
|
gradient_n(i,j) = grad(2);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
figure()
|
||||||
|
hold on
|
||||||
|
[N, M] = meshgrid(ns, ms);
|
||||||
|
%surface(M,N, error_surf, 'FaceAlpha', 0.5);
|
||||||
|
contour(M,N, error_surf, 50);
|
||||||
|
quiver(M,N, gradient_m, gradient_n)
|
||||||
|
xlabel('slope')
|
||||||
|
ylabel('intercept')
|
||||||
|
zlabel('error')
|
||||||
|
set(gcf, 'paperunits', 'centimeters', 'papersize', [15, 12.5], ...
|
||||||
|
'paperposition', [0., 0., 15, 12.5])
|
||||||
|
saveas(gcf, 'error_gradient', 'pdf')
|
31
regression/code/gradientDescent.m
Normal file
31
regression/code/gradientDescent.m
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
clear
|
||||||
|
close all
|
||||||
|
load('lin_regression.mat')
|
||||||
|
|
||||||
|
position = [-2. 10.];
|
||||||
|
gradient = [];
|
||||||
|
errors = [];
|
||||||
|
count = 1;
|
||||||
|
eps = 0.01;
|
||||||
|
|
||||||
|
while isempty(gradient) || norm(gradient) > 0.1
|
||||||
|
gradient = lsqGradient(position, x,y);
|
||||||
|
errors(count) = lsqError(position, x, y);
|
||||||
|
position = position - eps .* gradient;
|
||||||
|
count = count + 1;
|
||||||
|
end
|
||||||
|
figure()
|
||||||
|
subplot(2,1,1)
|
||||||
|
hold on
|
||||||
|
scatter(x,y, 'displayname', 'data')
|
||||||
|
xaxis = min(x):0.01:max(x);
|
||||||
|
f_x = position(1).*xaxis + position(2);
|
||||||
|
plot(xaxis, f_x, 'displayname', 'fit')
|
||||||
|
xlabel('Input')
|
||||||
|
ylabel('Output')
|
||||||
|
grid on
|
||||||
|
legend show
|
||||||
|
subplot(2,1,2)
|
||||||
|
plot(errors)
|
||||||
|
xlabel('optimization steps')
|
||||||
|
ylabel('error')
|
43
regression/code/gradientDescentPlot.m
Normal file
43
regression/code/gradientDescentPlot.m
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
clear
|
||||||
|
close all
|
||||||
|
|
||||||
|
load('lin_regression.mat')
|
||||||
|
|
||||||
|
ms = -1:0.5:5;
|
||||||
|
ns = -10:1:10;
|
||||||
|
|
||||||
|
position = [-2. 10.];
|
||||||
|
gradient = [];
|
||||||
|
error = [];
|
||||||
|
eps = 0.01;
|
||||||
|
|
||||||
|
% claculate error surface
|
||||||
|
error_surf = zeros(length(ms), length(ns));
|
||||||
|
for i = 1:length(ms)
|
||||||
|
for j = 1:length(ns)
|
||||||
|
error_surf(i,j) = lsqError([ms(i), ns(j)], x, y);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
figure()
|
||||||
|
hold on
|
||||||
|
[N, M] = meshgrid(ns, ms);
|
||||||
|
surface(M,N, error_surf, 'FaceAlpha', 0.5);
|
||||||
|
view(3)
|
||||||
|
xlabel('slope')
|
||||||
|
ylabel('intersection')
|
||||||
|
zlabel('error')
|
||||||
|
|
||||||
|
% do the descent
|
||||||
|
|
||||||
|
while isempty(gradient) || norm(gradient) > 0.1
|
||||||
|
gradient = lsqGradient(position, x,y);
|
||||||
|
error = lsqError(position, x, y);
|
||||||
|
plot3(position(1), position(2), error, '.', 'color', 'red', 'markersize', 20)
|
||||||
|
position = position - eps .* gradient;
|
||||||
|
pause(0.05)
|
||||||
|
end
|
||||||
|
|
||||||
|
grid on
|
||||||
|
set(gcf, 'paperunits', 'centimeters', 'papersize', [15, 15], ...
|
||||||
|
'paperposition', [0., 0., 15, 15])
|
||||||
|
saveas(gcf, 'gradient_descent', 'pdf')
|
Reference in New Issue
Block a user