Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
  2. %GRADIENTDESCENTMULTI Performs gradient descent to learn theta
  3. % theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by
  4. % taking num_iters gradient steps with learning rate alpha
  5.  
  6. % Initialize some useful values
  7. m = length(y); % number of training examples
  8. J_history = zeros(num_iters, 1);
  9.  
  10. for iter = 1:num_iters
  11.  
  12. % ====================== YOUR CODE HERE ======================
  13. % Instructions: Perform a single gradient step on the parameter vector
  14. % theta.
  15. %
  16. % Hint: While debugging, it can be useful to print out the values
  17. % of the cost function (computeCostMulti) and gradient here.
  18. %
  19. theta_old = theta;
  20. hipoteza = X * theta_old;
  21. diff = hipoteza - y;
  22.  
  23. for j = 1:length(theta)
  24. predvsota(j) = diff(j) .* X(j);
  25. vsota = sum(predvsota);
  26. theta(j) = theta_old(j) - (alpha * (1 / m)) * vsota;
  27. end;
  28.  
  29. % ============================================================
  30.  
  31. % Save the cost J in every iteration
  32. J_history(iter) = computeCostMulti(X, y, theta);
  33.  
  34. end
  35.  
  36. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement