梯度下降代码错误 - 获得相同的答案

问题描述 投票:0回答:1
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)

    m = length(y); % number of training examples
    J_history = zeros(num_iters, 1);
    h = X * theta; 

    for iter = 1:num_iters
        temp0 = theta(1) - alpha * (1/m) * sum(h - y);
        temp1 = theta(2) - alpha * (1/m) * sum(h - y).*X(:,2);
        theta(1) = temp0;
        theta(2) = temp1;
        J_history(iter) = computeCost(X, y, theta);
    end

我得到了两个相同的答案。有人能告诉我我的代码有什么问题

matlab
1个回答
1
投票

您的预测h需要在循环内进行更改。目前,您正在调整theta,但不会使用新的theta值重新计算预测。所以你的theta值不能收敛。此外,循环内的总和超过了整个乘法运算:

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

for iter = 1:num_iters
    h = X * theta
    temp0 = theta(1) - alpha * (1/m) * sum(h - y);
    temp1 = theta(2) - alpha * (1/m) * sum((h - y).*X(:,2));
    theta(1) = temp0;
    theta(2) = temp1;
    J_history(iter) = computeCost(X, y, theta);
end
© www.soinside.com 2019 - 2024. All rights reserved.