首页 > 解决方案 > 我的线性回归梯度下降不会收敛

问题描述

我被困了好几个小时,试图弄清楚为什么我的线性回归梯度下降代码不会收敛。尝试了非常小的 alpha 和非常大的迭代,仍然无法正常工作。

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent
% t to learn theta
%   theta = GRADIENTDESCENT(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

ST0=0;
ST1=0;

for iter = 1:num_iters
   for i=1:m
       ST0=ST0+((theta(1)+theta(2)*X(i))-y(i));
       ST1=ST1+(((theta(1)+theta(2)*X(i))-y(i)))*X(i,2);
   end

  ST0=ST0/m;
  ST0=ST0*alpha;
  ST1=ST1/m;
  ST1=ST1*alpha;  
  theta(1)=theta(1)-ST0;
  theta(2)=theta(2)-ST1;
    J= computeCost(X, y, theta);

    J_history(iter) = J;


end

end

标签: machine-learninglinear-regressiongradient-descent

解决方案


推荐阅读