我参加机器学习课程并尝试在matlab中实现梯度下降算法。函数computeCost工作正常,因为我已单独测试它。我用它来查看每次迭代的成本,而且它似乎并没有减少。它只是随机波动。 alpha的值被赋予0.01,所以我知道它不是学习率太大的问题。我得到的theta的答案与预期的输出非常不同。我哪里错了?提前谢谢!
function theta = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% Initialize some useful values
m = length(y); % number of training examples
temp1=0;
temp2=0;
for iter = 1:num_iters
for k = 1:m
temp1 = temp1 + (theta(1) + theta(2)*X(k, 2) - y(k));
temp2 = temp2 + ((theta(1) + theta(2)*X(k, 2) - y(k))*X(k, 2));
end
theta(1) = theta(1)-(1/m)*alpha*temp1;
theta(2) = theta(2)-(1/m)*alpha*temp2;
computeCost(X, y, theta)
end
end
编辑:这里也是computeCost
function J = computeCost(X, y, theta)
m = length(y); % number of training examples
J = 0;
temp = 0;
for index = 1:m
temp = temp + (theta(1) + theta(2)*X(index, 2)-y(index))^2;
end
J = temp/(2*m);
end
答案 0 :(得分:3)
尝试更改:
temp1=0;
temp2=0;
for iter = 1:num_iters
到
for iter = 1:num_iters
temp1=0;
temp2=0;
需要为每次迭代计算新的渐变(或者您在动量项中有效构建)。