Machine learning 我的线性回归梯度下降赢得';t收敛
我花了好几个小时试图弄明白为什么我的线性回归梯度下降代码不会收敛。 尝试使用非常小的alpha和非常大的迭代,仍然不起作用Machine learning 我的线性回归梯度下降赢得';t收敛,machine-learning,linear-regression,gradient-descent,Machine Learning,Linear Regression,Gradient Descent,我花了好几个小时试图弄明白为什么我的线性回归梯度下降代码不会收敛。 尝试使用非常小的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, n
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
我建议用一个非常简单的数据集测试一个非常简单的直线方程“y=ax+b”,比如“x=[1.0,2.0,3.0,4.0,5.0]”和“y=[2.2,3.3,4.4,5.5,6.6]”,因为这将使代码的故障排除变得更加容易。用这些数据设置初始参数值“[1.0,1.0]”也将有助于代码的故障排除。我是用x做的=[1,2,3]y=[2,4,6]它没有收敛到全局最小值,我建议用一个非常简单的数据集“x=[1.0,2.0,3.0,4.0,5.0]”和“y=[2.2,3.3,4.4,5.5,6]”测试一个非常简单的直线方程“y=ax+b”因为这将使代码的故障排除变得更容易。使用这些数据生成初始参数值“[1.0,1.0]”也将有助于代码的故障排除。我使用了x=[1,2,3]y=[2,4,6],但它没有收敛到全局最小值,无论我降低了多少alpha