machine-learning - 我的梯度下降没有给出确切的值
问题描述
我已经在Octave中编写了梯度下降 算法,但它并没有给我确切的答案。答案从一位数到两位数不等。
这是我的代码:
function theta = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y); % number of training examples
s = 0;
temp = theta;
for iter = 1:num_iters
for j = 1:size(theta, 1)
for i = 1:m
h = theta' * X(i, :)';
s = s + (h - y(i))*X(i, j);
end
s = s/m;
temp(j) = temp(j) - alpha * s;
end
theta = temp;
end
end
为了:
theta = gradientDescent([1 5; 1 2; 1 4; 1 5],[1 6 4 2]',[0 0]',0.01,1000);
我的梯度下降给出了这个:
4.93708
-0.50549
但预计会给出这个:
5.2148
-0.5733
解决方案
小修复:
- 您的变量
s
可能增量初始化不正确。 - 所以它的
temp
变量可能是new theta
- 错误地计算增量
尝试以下更改。
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
temp = theta;
for iter = 1:num_iters
temp = zeros(length(theta), 1);
for j = 1:size(theta)
s = 0
for i = 1:m
s = s + (X(i, :)*theta - y(i)) * X(i, j);
end
end
s = s/m;
temp(j) = temp(j) - alpha * s;
end
theta = temp;
J_history(iter) = computeCost(X, y, theta);
end
end