0
我已經使用矢量化實現了以下梯度下降代碼,但似乎成本函數不能正確遞減。相反,每次迭代都會增加成本函數。使用矢量化的梯度下降的八度碼不正確地更新成本函數
假設THETA成爲n + 1個矢量,Y是上午向量,X是設計矩陣M *(N + 1)
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y); % number of training examples
n = length(theta); % number of features
J_history = zeros(num_iters, 1);
error = ((theta' * X')' - y)*(alpha/m);
descent = zeros(size(theta),1);
for iter = 1:num_iters
for i = 1:n
descent(i) = descent(i) + sum(error.* X(:,i));
i = i + 1;
end
theta = theta - descent;
J_history(iter) = computeCost(X, y, theta);
disp("the value of cost function is : "), disp(J_history(iter));
iter = iter + 1;
end
的計算成本函數是:
function J = computeCost(X, y, theta)
m = length(y);
J = 0;
for i = 1:m,
H = theta' * X(i,:)';
E = H - y(i);
SQE = E^2;
J = (J + SQE);
i = i+1;
end;
J = J/(2*m);
不應該'對於i = 1:N'增量'i'適合你?你也在循環中進行。 (很長時間以來,我做了任何八度...) – 2014-10-30 15:15:52
是啊,那是真的 – Dcoder 2014-10-30 15:23:04