2017-08-02 70 views
1

我正在学习机器学习并尝试在matlab中实现梯度下降算法。 computeCost函数正常工作,因为我已经单独测试了它。我用它来查看每次迭代的成本,而且似乎并没有减少。它随机波动。阿尔法的价值是0.01,所以我知道这不是一个问题,学习率太大。我得到的theta的答案与预期的输出非常接近。我哪里错了?提前致谢!在matlab中给出不正确答案的渐变下降算法

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

看到computeCost会很有用 - 也就是说你的梯度完全不依赖于X(k,1)? –

+0

X是第一列中有一个的矩阵,作为theta1的系数。我不认为渐变取决于它。我还增加了computeCost。 – may

回答

2

尝试改变:

temp1=0; 
temp2=0; 
for iter = 1:num_iters 

for iter = 1:num_iters 
    temp1=0; 
    temp2=0; 

梯度需要计算新鲜对于每次迭代(或者你是有效的bui在动力学术语中)。

+0

修复它,非常感谢! – may