梯度下降在线性回归中无法收敛

我在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));    endendJtheta

谢谢。


回答:

在Matlab在线版中,我在47次迭代后就收敛了:

data = load('ex1data2.txt');X = data(:,1:2);y = data(:,3);mu = mean(X); % 均值.s = max(X) - min(X); % 范围.X = X - mu;X = X ./ s; m = size(X, 1); % 行数.n = size(X, 2); % 列数.X = [ones(m, 1) X]; % 添加一列全为1的列以添加偏差.theta = zeros(n+1,1); % 初始化theta.J = costFunction(X,y,theta);alpha = 2.02;iterations = 100;          j_hist = zeros(iterations,1); % 初始化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);endfunction J = costFunction(X,y,theta)    prediction = X*theta;    m = size(X, 1);    sqError = (prediction - y).^2; % 忽略负值.    J = (1/(2*m))*sum(sqError); % 平方根的导数抵消.end%J%j_hist%theta

然后,运行后你可以通过取消注释来单独检查J、j_hist和theta的值。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注