Search code examples
machine-learningoctavegradient-descent

Trouble Implementing Gradient Descent in Octave


I've been trying to implement gradient descent in Octave. This is the code I have so far:

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
  %GRADIENTDESCENT Performs gradient descent to learn theta
  %   theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by
  %   taking num_iters gradient steps with learning rate alpha

  % Initialize some useful values
  m = length(y); % number of training examples
  J_history = zeros(num_iters, 1);

  for iter = 1:num_iters

% ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
%               theta.
%
% Hint: While debugging, it can be useful to print out the values
%       of the cost function (computeCost) and gradient here.
%
theta
X
y
theta' .* X
for inner = 1:length(theta)
  hypothesis = (X * theta - y)';        

  % Updating the parameters

  temp0 = theta(1) - (alpha * (1/m) * hypothesis * X(:, 1));
  temp1 = theta(2) - (alpha * (1/m) * hypothesis * X(:, 2)); 
  theta(1) = temp0;
  theta(2) = temp1;

  J_history(iter) = computeCost(X, y, theta);

  end

  end

I can't really tell what's going wrong with this code, it compiles and runs but it's being auto-graded and it fails every time.

EDIT: Sorry, wasn't specific. I was supposed to implement a single step of GD, not the whole loop

EDIT 2: Here's the full thing. Only the stuff inside the for loop is relevant imo.

EDIT 3: Both test cases fail, so there's something wrong with my calculations.


Solution

  • I think my problem is that I had an extra for loop in there for some reason.