0

我正在学习 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))

那么问题可能出在哪里?

编辑:代码更新

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 
4

3 回答 3

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))
于 2013-10-16T06:25:46.610 回答
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)
于 2013-10-16T09:39:19.160 回答
1

矢量化版本:

for iter = 1:num_iters
    theta = theta - (alpha .* X'*(X * theta - y) ./m);
    J_history(iter) = computeCost(X, y, theta);
end
于 2013-11-12T20:16:05.810 回答