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