梯度下降不收敛于线性回归

时间:2020-06-20 08:47:53

标签: machine-learning octave linear-regression gradient-descent

我已经用Octave写了一个简单的线性回归算法,但是无论我选择什么学习速率和迭代次数,甚至在纸上画出矩阵,theta的值都不会收敛。谁能看到我的代码中的任何错误?

data = load('ex1data2.txt');
X = data(:,1:2);
y = data(:,3);          
m = rows(X);        
X = [ones(m,1), data(:,1:2)];   

alpha = 0.01;

iterations = 5000;          

n = columns(X);                 

theta = zeros(n,1);

for count = 1:iterations

    hypo = zeros(1,m);
    hypo = theta'*X';
    sqr_err = (hypo-y').*(hypo-y');
    sum_sqr_err = sum(sqr_err);
    J = 1/(2*m)*sum_sqr_err;

    for i = 1:n
        theta(i) = theta(i)-(alpha/m)*((hypo-y')*X(:,i));
    end

end

J
theta

谢谢。

2 个答案:

答案 0 :(得分:0)

sqr_err =(hypo-y')。*(hypo-y')

尝试删除“。”

答案 1 :(得分:0)

在Matlab在线中,这对我收敛了47次迭代:

data = load('ex1data2.txt');
X = data(:,1:2);
y = data(:,3);

mu = mean(X); % Mean.
s = max(X) - min(X); % Range.
X = X - mu;
X = X ./ s; 

m = size(X, 1); % Number of rows.
n = size(X, 2); % Number of columns.
X = [ones(m, 1) X]; % Add columns of ones to add biasing.
theta = zeros(n+1,1); % Initializing theta.

J = costFunction(X,y,theta);

alpha = 2.02;
iterations = 100;          

j_hist = zeros(iterations,1); % Initializing j_hist.
m = size(X, 1);
n = size(theta,1);

for i=1 : iterations
    hypo = X*theta;
    for j = 1 : n
        theta(j) = theta(j) - alpha*(1/m)*sum((hypo - y).*X(:,j));
    end
    j_hist(i) = costFunction(X,y,theta);
end

function J = costFunction(X,y,theta)
    prediction = X*theta;
    m = size(X, 1);
    sqError = (prediction - y).^2; % ignore negative vals.
    J = (1/(2*m))*sum(sqError); % derivative of sqroot cancels.
end

%J
%j_hist
%theta

然后,运行后,您可以通过取消注释来一次分别检查J,j_hist和theta。