使梯度下降以八度为单位进行工作(Andrew ng的机器学习课程,练习1)

时间:2019-04-11 13:55:32

标签: machine-learning octave linear gradient-descent

因此,我正在尝试实施/解决来自Coursera上的Andrew ng的机器学习课程的第一个编程练习。 我很难在八度中实现线性梯度下降(针对一个变量)。我没有像解决方案中一样返回相同的参数值,但是我的参数朝着相同的方向(至少我认为是这样)。所以我可能在代码中的某个地方有一个错误。也许比我有更多经验的人可以启发我。

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);

theta1 = theta(1);
theta2 = theta(2);

temp0 = 0;
temp1 = 0;

h = X * theta;
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.
    %
    temp0 = 0;
    temp1 = 0;
    for i=1:m
        error = (h(i) - y(i));
        temp0 = temp0 + error * X(i, 1));;
        temp1 = temp1 + error * X(i, 2));
    end
    theta1 = theta1 - ((alpha/m) * temp0);
    theta2 = theta2 - ((alpha/m) * temp1);
    theta = [theta1;theta2];

    % ============================================================

    % Save the cost J in every iteration    
    J_history(iter) = computeCost(X, y, theta);

end
end

我对练习1(以[0; 0]初始化的theta)的预期结果应该是theta1: -3.6303 和theta2: 1.1664

但是我变成输出theta1是 0.095420 而thetha2是 0.51890

here's a photo where you can see my function

这是我用于线性梯度下降的公式。

this is my formula for linear gradien descent

where  in the formula this is h(x)

EDIT1 :已编辑代码。现在我得到theta1了:

  

87.587

和theta2

  

979.93

1 个答案:

答案 0 :(得分:0)

我现在知道我的问题是什么。我将为可能对它感兴趣的人快速描述它。因此,我意外地在循环外计算了可用的 h 。因此,每次循环中都使用相同的值进行计算。

这是固定代码:

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);

theta1 = theta(1);
theta2 = theta(2);

temp0 = 0;
temp1 = 0;
error = 0;

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.
    %

    h = X * theta; %heres the variable i moved into the loop

    temp0 = 0;
    temp1 = 0;
    for i=1:m
        error = (h(i) - y(i));
        temp0 = temp0 + (error * X(i, 1));
        temp1 = temp1 + (error * X(i, 2));
        %disp(error);
    end
    theta1 = theta1 - ((alpha/m) * temp0);
    theta2 = theta2 - ((alpha/m) * temp1);
    theta = [theta1;theta2];

    % ============================================================

    % Save the cost J in every iteration    
    J_history(iter) = computeCost(X, y, theta);

end
end