-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathgradient_step.m
More file actions
26 lines (20 loc) 路 827 Bytes
/
Copy pathgradient_step.m
File metadata and controls
26 lines (20 loc) 路 827 Bytes
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
% GRADIENT STEP function.
% It performs one step of gradient descent for theta parameters.
function [gradients] = gradient_step(X, y, theta, lambda)
% X - training set.
% y - training output values.
% theta - model parameters.
% lambda - regularization parameter.
% Initialize number of training examples.
m = length(y);
% Initialize variables we need to return.
gradients = zeros(size(theta));
% Calculate hypothesis.
predictions = hypothesis(X, theta);
% Calculate regularization parameter.
regularization_param = (lambda / m) * theta;
% Calculate gradient steps.
gradients = (1 / m) * (X' * (predictions - y)) + regularization_param;
% We should NOT regularize the parameter theta_zero.
gradients(1) = (1 / m) * (X(:, 1)' * (predictions - y));
end