/exercises/mlclass-ex1/gradientDescent.m

http://github.com/jneira/machine-learning-course · Objective C · 35 lines · 28 code · 7 blank · 0 comment · 2 complexity · 24abfce24b6f861c6bbb94bf1ceb41a2 MD5 · raw file

  1. function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
  2. %GRADIENTDESCENT Performs gradient descent to learn theta
  3. % theta = GRADIENTDESENT(X, y, theta, alpha, num_iters) updates theta by
  4. % taking num_iters gradient steps with learning rate alpha
  5. % Initialize some useful values
  6. m = length(y); % number of training examples
  7. J_history = zeros(num_iters, 1);
  8. num_vars=size(X,2);
  9. for iter = 1:num_iters
  10. % ====================== YOUR CODE HERE ======================
  11. % Instructions: Perform a single gradient step on the parameter vector
  12. % theta.
  13. %
  14. % Hint: While debugging, it can be useful to print out the values
  15. % of the cost function (computeCost) and gradient here.
  16. %
  17. theta_tmp=zeros(num_vars,1);
  18. for j=1:num_vars
  19. theta_tmp(j)= theta(j) - (alpha/m) * sum(((X*theta)-y).*X(:,j));
  20. end
  21. theta=theta_tmp;
  22. % by @dnene
  23. % theta = theta - (X' * (X * theta - y) * alpha / m);
  24. % ============================================================
  25. % Save the cost J in every iteration
  26. J_history(iter) = computeCost(X, y, theta);
  27. end
  28. end