|
|
@ -27,6 +27,10 @@ grad = zeros(size(theta));
|
|
|
|
% prediction for that example. You can make use of this to vectorize
|
|
|
|
% prediction for that example. You can make use of this to vectorize
|
|
|
|
% the cost function and gradient computations.
|
|
|
|
% the cost function and gradient computations.
|
|
|
|
%
|
|
|
|
%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
J = 1/m * (-y'*log(sigmoid(X*theta)) - (1-y)'*log(1-sigmoid(X*theta))) ...
|
|
|
|
|
|
|
|
+ lambda/(2*m) * theta(2:end)' * theta(2:end);
|
|
|
|
|
|
|
|
|
|
|
|
% Hint: When computing the gradient of the regularized cost function,
|
|
|
|
% Hint: When computing the gradient of the regularized cost function,
|
|
|
|
% there're many possible vectorized solutions, but one solution
|
|
|
|
% there're many possible vectorized solutions, but one solution
|
|
|
|
% looks like:
|
|
|
|
% looks like:
|
|
|
@ -36,14 +40,8 @@ grad = zeros(size(theta));
|
|
|
|
% grad = grad + YOUR_CODE_HERE (using the temp variable)
|
|
|
|
% grad = grad + YOUR_CODE_HERE (using the temp variable)
|
|
|
|
%
|
|
|
|
%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
regularization_term = lambda/m * vertcat([0], theta(2:end));
|
|
|
|
|
|
|
|
grad = 1/m * X' * (sigmoid(X*theta) - y) + regularization_term;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
% =============================================================
|
|
|
|
% =============================================================
|
|
|
|
|
|
|
|
|
|
|
|