我已经在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