Machine learning 我的梯度下降没有给出准确的值

Machine learning 我的梯度下降没有给出准确的值,machine-learning,neural-network,octave,gradient-descent,Machine Learning,Neural Network,Octave,Gradient Descent,我已经用八度音阶写了梯度下降算法,但它并没有给我确切的答案。答案从一位数到两位数不等 这是我的密码: function theta = gradientDescent(X, y, theta, alpha, num_iters) m = length(y); % number of training examples s = 0; temp = theta; for iter = 1:num_iters for j = 1:size(theta, 1) for i = 1:m

我已经用八度音阶写了梯度下降算法,但它并没有给我确切的答案。答案从一位数到两位数不等

这是我的密码:

function theta = gradientDescent(X, y, theta, alpha, num_iters)

m = length(y); % number of training examples
s = 0;
temp = theta;
for iter = 1:num_iters
  for j = 1:size(theta, 1)
    for i = 1:m
      h = theta' * X(i, :)';
      s = s + (h - y(i))*X(i, j);
    end
    s = s/m;
    temp(j) = temp(j) - alpha * s;
  end 
  theta = temp; 
end

end
用于:

我的梯度下降给出:

 4.93708
-0.50549
但预计它会给出:

 5.2148
-0.5733
小补丁:

您的变量s可能未正确初始化增量。 所以它是温度变量,可能是新的θ 错误地计算增量 尝试以下更改

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)

m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
temp = theta;
for iter = 1:num_iters
    temp = zeros(length(theta), 1);
    for j = 1:size(theta)
        s = 0
        for i = 1:m
            s = s + (X(i, :)*theta - y(i)) * X(i, j);
        end
    end
    s = s/m;
    temp(j) = temp(j) - alpha * s;
end 
    theta = temp; 
    J_history(iter) = computeCost(X, y, theta);
end
end

为什么结果是错误的?例如:您是如何确定预期输出的?为什么您确信这些结果是正确的?因为这些结果是由机器学习课程提供商给出的。那么您想从我们这里得到什么?你实现的是一个梯度下降。在得到准确的值之前,我们不会为您修改参数或更改算法。你必须找出这两种算法在哪里开始出现分歧,并从那里调查问题所在。如果可以,请获取老师提供的代码并比较不同的代码。我只是想知道我的算法是否正确,对于无意中造成的不便,我深表歉意。鉴于您的结果与老师的结果不同,您已经确定它是不正确的。问题是,我们不能说为什么它们不同,也不能帮助你得到相同的结果,因为我们不知道老师是如何得到这些结果的。我们需要整本书/课程/网络研讨会/无论你在哪里学习,都可以看到老师做了什么。因此,我们根本无法帮助您。很抱歉
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)

m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
temp = theta;
for iter = 1:num_iters
    temp = zeros(length(theta), 1);
    for j = 1:size(theta)
        s = 0
        for i = 1:m
            s = s + (X(i, :)*theta - y(i)) * X(i, j);
        end
    end
    s = s/m;
    temp(j) = temp(j) - alpha * s;
end 
    theta = temp; 
    J_history(iter) = computeCost(X, y, theta);
end
end