Machine Learning - Gradient Descent
Gradient Descent (Octave)
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
% Linear Regression
% Parameters:
% - X: Training Set of d(m*n)
% - y: Target Set of d(m*1)
% - theta: para, d(n*1)
% - alpha: learning ratio
% - num_iters: numer of iterations
m = length(y);
J_history = zeros(num_iters, 1);
fitures = size(X, 2);
for iter = 1:num_iters
theta=theta-alpha/m*(X'*(X*theta-y))
J_history(iter) = computeCost(X, y, theta);
end
end
------------------------------------------------------------------------------
function J = computeCost(X, y, theta)
% Compute cost for linear regression
m = length(y);
J = 0;
delta_y=X*theta-y;
J=sum((X*theta-y).^2)/2/m
return;
end