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
我对两个theta都得到相同的答案。有人可以告诉我我的代码有什么问题吗
答案 0 :(得分: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