Skip to content

Instantly share code, notes, and snippets.

@oskar-j
Created March 26, 2015 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oskar-j/b6d675363d4e4d7d0387 to your computer and use it in GitHub Desktop.
Save oskar-j/b6d675363d4e4d7d0387 to your computer and use it in GitHub Desktop.
Gradient descent algorithm [Machine Learning]
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
t = theta.';
theta_p1 = theta(1,1);
theta_p2 = theta(2,1);
sump1 = 0;
sump2 = 0;
for n = 1:m
sump1 = sump1 + ( ( (t(1) * X(n,1) + t(2) * X(n,2) ) - y(n,1) )*X(n,1) );
end
for n = 1:m
sump2 = sump2 + ( ( (t(1) * X(n,1) + t(2) * X(n,2) ) - y(n,1) )*X(n,2) );
end
theta_p1 = theta_p1 - (alpha * (1/m) * sump1);
theta_p2 = theta_p2 - (alpha * (1/m) * sump2);
theta(1,1) = theta_p1;
theta(2,1) = theta_p2;
% ============================================================
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment