lrCostFunction.m 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. function [J, grad] = lrCostFunction(theta, X, y, lambda)
  2. %LRCOSTFUNCTION Compute cost and gradient for logistic regression with
  3. %regularization
  4. % J = LRCOSTFUNCTION(theta, X, y, lambda) computes the cost of using
  5. % theta as the parameter for regularized logistic regression and the
  6. % gradient of the cost w.r.t. to the parameters.
  7. % Initialize some useful values
  8. m = length(y); % number of training examples
  9. % You need to return the following variables correctly
  10. J = 0;
  11. grad = zeros(size(theta));
  12. % ====================== YOUR CODE HERE ======================
  13. % Instructions: Compute the cost of a particular choice of theta.
  14. % You should set J to the cost.
  15. % Compute the partial derivatives and set grad to the partial
  16. % derivatives of the cost w.r.t. each parameter in theta
  17. %
  18. % Hint: The computation of the cost function and gradients can be
  19. % efficiently vectorized. For example, consider the computation
  20. %
  21. % sigmoid(X * theta)
  22. %
  23. % Each row of the resulting matrix will contain the value of the
  24. % prediction for that example. You can make use of this to vectorize
  25. % the cost function and gradient computations.
  26. %
  27. % Hint: When computing the gradient of the regularized cost function,
  28. % there're many possible vectorized solutions, but one solution
  29. % looks like:
  30. % grad = (unregularized gradient for logistic regression)
  31. % temp = theta;
  32. % temp(1) = 0; % because we don't add anything for j = 0
  33. % grad = grad + YOUR_CODE_HERE (using the temp variable)
  34. %
  35. % exclude the first term
  36. rt = lambda / (2 * m) * sum((theta(2:end) .^ 2));
  37. J = 1/m * sum(-y .* log(sigmoid(X * theta)) - (1 .- y) .* log(1 .- sigmoid(X * theta))) ...
  38. + rt;
  39. % regularized mask
  40. rm = [zeros(1,1); theta(2:end)];
  41. grad = 1/m * X' * (sigmoid(X * theta) .- y) + lambda/m .* rm;
  42. % =============================================================
  43. grad = grad(:);
  44. end