Я пытаюсь реализовать алгоритм пакетного градиентного спуска в Octave. Это не работает, когда есть много итераций. Проверка показывает, что алгоритм не работает, потому что после каждой итерации у него есть колебания в значениях.
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
%GRADIENTDESCENTMULTI Performs gradient descent to learn theta
% theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
% ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
% theta.
%
% Hint: While debugging, it can be useful to print out the values
% of the cost function (computeCostMulti) and gradient here.
%
x0=X(:,1)';
x1=X(:,2)';
x2=X(:,3)';
Z=(X*theta)-y;
L=x0*Z;
M=x1*Z;
N=x2*Z;
theta=[theta(1,1)-alpha*((1/m)*L);theta(2,1)-alpha*((1/m)*M);theta(3,1)-alpha*((1/m)*N)];
% ============================================================
% Save the cost J in every iteration
J_history(iter) = computeCostMulti(X, y, theta);
end
end
Это код и данные из курса, но размерность матрицы X
равен 47×2
, y
равен 47×1
и, наконец, размерность вектора theta
равна 3×1
![command window](https://i.stack.imgur.com/zRBuB.png)
Может кто-нибудь, помогите мне, пожалуйста?