forked from joemehr/MachineLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradientDescentMulti.m
36 lines (25 loc) · 1.09 KB
/
gradientDescentMulti.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
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, numberOfIterations)
%GRADIENTDESCENTMULTI Performs gradient descent to learn theta
% theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, numberOfIterations) updates theta by
% taking numberOfIterations gradient steps with learning rate alpha
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(numberOfIterations, 1);
for iteration = 1:numberOfIterations
% Perform a single gradient step on the parameter vector theta.
% we minimize the value of J(theta) by changing the values of the
% vector theta NOT changing X or y
% alpha = learning rate as a single number
% hypothesis = mx1 column vector
% X = mxn matrix
% theta = nx1 column vector
hypothesis = X * theta;
% errors = mx1 column vector
% y = mx1 column vector
errors = hypothesis .- y;
newDecrement = (alpha * (1/m) * errors' * X);
theta = theta - newDecrement';
% Save the cost J in every iteration
J_history(iteration) = computeCostMulti(X, y, theta);
end
end