Matlab中梯度下降的不正确结果

时间:2013-10-16 02:48:50

标签: matlab gradient-descent

我正在参加Matlab的课程,我已经完成了梯度下降实施,但结果不正确。

代码:

for iter = 1:num_iters

sumTheta1 = 0;
sumTheta2 = 0;
for s = 1:m
    sumTheta1 = theta(1) + theta(2) .* X(s,2) - y(s);
    sumTheta2 = theta(1) + theta(2) .* X(s,2) - y(s) .* X(s,2);
end

theta(1) = theta(1) - alpha .* (1/m) .* sumTheta1;
theta(2) = theta(2) - alpha .* (1/m) .* sumTheta2;

J_history(iter) = computeCost(X, y, theta);

end

这是重要的部分。我认为公式的实现是正确的,即使它没有被优化。公式是:

theta1 = theta1 - (alpha)(1/m)(summation_i^m(theta1 + theta2*x(i)-y(i)))
theta2 = theta2 - (alpha)(1/m)(summation_i^m(theta1 + theta2*x(i)-y(i)))(x(i))

问题出在哪里?

编辑:CODE已更新

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

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


for iter = 1:num_iters

for s = 1:m

sumTheta1 = ((theta(1) .* X(s,1)) + (theta(2) .* X(s,2))) - (y(s));
sumTheta2 = ((theta(1) .* X(s,1)) + (theta(2) .* X(s,2))) - (y(s)) .* X(s,2);
end

temp1 = theta(1) - alpha .* (1/m) .* sumTheta1;
temp2 = theta(2) - alpha .* (1/m) .* sumTheta2;

theta(1) = temp1;
theta(2) = temp2;

J_history(iter) = computeCost(X, y, theta);

end

end

编辑(2):修正了它,工作代码。

知道了,正是+ Dan暗示这样做了我会接受他的答案,仍然把代码放在任何被困的人:),干杯。

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

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


for iter = 1:num_iters

sumTheta1 = 0;
sumTheta2 = 0;

for s = 1:m

sumTheta1 = sumTheta1 + ((theta(1) .* X(s,1)) + (theta(2) .* X(s,2))) - (y(s));
sumTheta2 = sumTheta2 + (((theta(1) .* X(s,1)) + (theta(2) .* X(s,2))) - (y(s))) .* X(s,2);
end

temp1 = theta(1) - alpha .* (1/m) .* sumTheta1;
temp2 = theta(2) - alpha .* (1/m) .* sumTheta2;

theta(1) = temp1;
theta(2) = temp2;

% Save the cost J in every iteration    
J_history(iter) = computeCost(X, y, theta);

end

end 

3 个答案:

答案 0 :(得分:1)

乍一看,我注意到你的sumTheta1实际上并没有求和,而是每次迭代都会替换它自己。我想你的意思是:

sumTheta1 = sumTheta1 + theta(1) + theta(2) .* X(s,2) - y(s);

sumTheta2

也一样

但是为了将来参考,你可以替换这个(纠正的)循环:

for s = 1:m
    sumTheta1 = theta(1) + theta(2) .* X(s,2) - y(s);
    sumTheta2 = theta(1) + theta(2) .* X(s,2) - y(s) .* X(s,2);
end

使用此向量化公式

sumTheta1 = sum(theta(1) + theta(2)*X(:,2) - y);
sumTheta2 = sum(theta(1) + theta(2)*X(:,2) - y.*X(:,2))

答案 1 :(得分:1)

如果我看到这个公式

theta1 = theta1 - (alpha)(1/m)(summation_i^m(theta1 + theta2*x(i)-y(i)))

我猜matlab的等价物是:

theta1 = theta1 - alpha/m*(theta1 + theta2)*sum(x-y)

您可以按如下方式确定m

m =length(x);

然而,你的两个公式让我想知道你是想要按顺序还是同时计算它们。

在第二种情况下,创建一个临时变量并在计算中使用它。

myFactor = alpha/m*(theta1_previous + theta2_previous)

theta1 = theta1_previous - myFactor*sum(x-y)
theta2 = theta2_previous - myFactor*sum((x-y).*x)

答案 2 :(得分:1)

矢量化版本:

for iter = 1:num_iters
    theta = theta - (alpha .* X'*(X * theta - y) ./m);
    J_history(iter) = computeCost(X, y, theta);
end
相关问题