[regression] finished exercise
This commit is contained in:
@@ -13,7 +13,7 @@ function [p, ps, mses] = gradientDescent(x, y, func, p0, epsilon, threshold)
|
||||
% mses: vector with the corresponding mean squared errors
|
||||
|
||||
p = p0;
|
||||
gradient = ones(1, length(p0)) * 1000.0;
|
||||
gradient = ones(size(p0)) * 1000.0;
|
||||
ps = [];
|
||||
mses = [];
|
||||
while norm(gradient) > threshold
|
||||
@@ -29,13 +29,12 @@ function mse = meanSquaredError(x, y, func, p)
|
||||
end
|
||||
|
||||
function gradmse = meanSquaredGradient(x, y, func, p)
|
||||
gradmse = zeros(size(p, 1), size(p, 2));
|
||||
gradmse = zeros(size(p));
|
||||
h = 1e-7; % stepsize for derivatives
|
||||
ph = eye(length(p))*h; % ... for each parameter
|
||||
mse = meanSquaredError(x, y, func, p);
|
||||
for i = 1:length(p) % for each coordinate ...
|
||||
pi = p;
|
||||
pi(i) = pi(i) + h; % displace i-th parameter
|
||||
msepi = meanSquaredError(x, y, func, pi);
|
||||
msepi = meanSquaredError(x, y, func, p + ph(:,i));
|
||||
gradmse(i) = (msepi - mse)/h;
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user