【问题标题】:Getting gradient descent to work in octave. (Andrew ng's machine learn course, excersise 1)让梯度下降以八度音程工作。 (Andrew ng 的机器学习课程,练习 1)
【发布时间】:2019-09-02 04:36:51
【问题描述】:

我正在尝试实施/解决 Andrew ng 在 coursera 上的机器学习课程中的第一个编程练习。 我无法以八度音阶实现线性梯度下降(对于一个变量)。我没有像在解决方案中那样得到相同的参数值,但我的参数朝着相同的方向发展(至少我是这么认为的)。所以我的代码中可能有一个错误。或许比我更有经验的人可以启发我。

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

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

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

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

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

87.587

对于 theta2

979.93

【问题讨论】:

  • 在内部 for 循环中,您将替换 temp0temp1 m 次,然后只使用最后一个值
  • 谢谢,我认为这可能是错误。我完全没有看到我这么愚蠢。非常感谢。

标签: machine-learning octave gradient-descent


【解决方案1】:

我现在知道我的问题是什么了。我将为可能对此感兴趣的人快速描述一下。所以我不小心在我的循环之外计算了 avriable 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

【讨论】:

  • is sum() 命令在上面的例子中是多余的,因为 theta 的值显示了预测误差的总和??
  • @AbhishekChoudhery 我不明白你的意思。
猜你喜欢
  • 2020-08-13
  • 1970-01-01
  • 2013-11-22
  • 2023-02-25
  • 2018-03-26
  • 2019-08-01
  • 1970-01-01
  • 2016-10-27
  • 1970-01-01
相关资源
最近更新 更多