C++ 线性回归的梯度下降不起作用
我正在尝试为线性回归实现一个简单的梯度下降算法。我使用的是犰狳C++线性代数库,我也是新的犰狳。这就是我想做的:C++ 线性回归的梯度下降不起作用,c++,machine-learning,linear-regression,data-analysis,armadillo,C++,Machine Learning,Linear Regression,Data Analysis,Armadillo,我正在尝试为线性回归实现一个简单的梯度下降算法。我使用的是犰狳C++线性代数库,我也是新的犰狳。这就是我想做的: void linRegression(mat &features, mat &targets, double alpha,double error){ mat theta = ones(features.n_cols+1); mat temp = zeros(features.n_cols+1); mat features_new = join
void linRegression(mat &features, mat &targets, double alpha,double error){
mat theta = ones(features.n_cols+1);
mat temp = zeros(features.n_cols+1);
mat features_new = join_horiz(ones(features.n_rows),features);
mat predictions;
double con = alpha*(1.0/features.n_rows);
int j = 0;
while(j<1000){
mat step_error = (features_new*theta - targets);
for(unsigned int i=0;i<theta.n_rows;i++){
temp(i) = con*sum(step_error%features_new.col(i));
}
theta = theta-temp;
mat pred = predict(theta,features_new);
cout<<theta<<endl;
j++;
}
}
void-lin回归(材料与特征、材料与目标、双阿尔法、双错误){
matθ=一(特征n_cols+1);
材料温度=零(特性n_cols+1);
mat features_new=连接水平面(一个(features.n_行),特征);
mat预测;
双con=alpha*(1.0/features.n_行);
int j=0;
while(j我认为while循环中的计算是不正确的。至少你可以在没有for循环的情况下做得更出色。下面是一个关于1个功能问题的简短代码:
#include <iostream>
#include <armadillo>
using namespace std;
using namespace arma;
int main(int argc, char** argv)
{
mat features(10, 1);
features << 6.110100 << endr
<< 5.527700 << endr
<< 8.518600 << endr
<< 7.003200 << endr
<< 5.859800 << endr
<< 8.382900 << endr
<< 7.476400 << endr
<< 8.578100 << endr
<< 6.486200 << endr
<< 5.054600 << endr;
mat targets(10, 1);
targets << 17.59200 << endr
<< 9.130200 << endr
<< 13.66200 << endr
<< 11.85400 << endr
<< 6.823300 << endr
<< 11.88600 << endr
<< 4.348300 << endr
<< 12.00000 << endr
<< 6.598700 << endr
<< 3.816600 << endr;
mat theta = ones(features.n_cols + 1);
mat features_new = join_horiz(ones(features.n_rows), features);
double alpha = 0.01;
double con = alpha*(1.0 / features.n_rows);
int j = 0;
while (j < 20000){
mat step_error = (features_new*theta - targets);
theta = theta - con * (features_new.t() * step_error);
j++;
}
theta.print("theta:");
system("pause");
return 0;
}
通过正态方程法得出的结果为:
theta:
0.5071
1.3427
编辑
你的代码确实正确!
问题可能在于特征规范化。我将我的示例扩展为2个特征回归并添加了规范化。如果没有规范化,它对我也不起作用
#include <iostream>
#include <armadillo>
using namespace std;
using namespace arma;
int main(int argc, char** argv)
{
mat features(10, 2);
features << 2104 << 3 << endr
<< 1600 << 3 << endr
<< 2400 << 3 << endr
<< 1416 << 2 << endr
<< 3000 << 4 << endr
<< 1985 << 4 << endr
<< 1534 << 3 << endr
<< 1427 << 3 << endr
<< 1380 << 3 << endr
<< 1494 << 3 << endr;
mat m = mean(features, 0);
mat s = stddev(features, 0, 0);
int i, j;
//normalization
for (i = 0; i < features.n_rows; i++)
{
for (j = 0; j < features.n_cols; j++)
{
features(i, j) = (features(i, j) - m(j))/s(j);
}
}
mat targets(10, 1);
targets << 399900 << endr
<< 329900 << endr
<< 369000 << endr
<< 232000 << endr
<< 539900 << endr
<< 299900 << endr
<< 314900 << endr
<< 198999 << endr
<< 212000 << endr
<< 242500 << endr;
mat theta = ones(features.n_cols + 1);
mat features_new = join_horiz(ones(features.n_rows), features);
double alpha = 0.01;
double con = alpha*(1.0 / features.n_rows);
while (j < 20000){
mat step_error = (features_new*theta - targets);
theta = theta - con * (features_new.t() * step_error);
j++;
}
cout << theta << endl;
system("pause");
return 0;
}
我认为while循环中的计算是不正确的。至少在没有for循环的情况下,您可以做得更出色。下面是一个关于1个功能问题的简短代码:
#include <iostream>
#include <armadillo>
using namespace std;
using namespace arma;
int main(int argc, char** argv)
{
mat features(10, 1);
features << 6.110100 << endr
<< 5.527700 << endr
<< 8.518600 << endr
<< 7.003200 << endr
<< 5.859800 << endr
<< 8.382900 << endr
<< 7.476400 << endr
<< 8.578100 << endr
<< 6.486200 << endr
<< 5.054600 << endr;
mat targets(10, 1);
targets << 17.59200 << endr
<< 9.130200 << endr
<< 13.66200 << endr
<< 11.85400 << endr
<< 6.823300 << endr
<< 11.88600 << endr
<< 4.348300 << endr
<< 12.00000 << endr
<< 6.598700 << endr
<< 3.816600 << endr;
mat theta = ones(features.n_cols + 1);
mat features_new = join_horiz(ones(features.n_rows), features);
double alpha = 0.01;
double con = alpha*(1.0 / features.n_rows);
int j = 0;
while (j < 20000){
mat step_error = (features_new*theta - targets);
theta = theta - con * (features_new.t() * step_error);
j++;
}
theta.print("theta:");
system("pause");
return 0;
}
通过正态方程法得出的结果为:
theta:
0.5071
1.3427
编辑
你的代码确实正确!
问题可能在于特征规范化。我将我的示例扩展为2个特征回归并添加了规范化。如果没有规范化,它对我也不起作用
#include <iostream>
#include <armadillo>
using namespace std;
using namespace arma;
int main(int argc, char** argv)
{
mat features(10, 2);
features << 2104 << 3 << endr
<< 1600 << 3 << endr
<< 2400 << 3 << endr
<< 1416 << 2 << endr
<< 3000 << 4 << endr
<< 1985 << 4 << endr
<< 1534 << 3 << endr
<< 1427 << 3 << endr
<< 1380 << 3 << endr
<< 1494 << 3 << endr;
mat m = mean(features, 0);
mat s = stddev(features, 0, 0);
int i, j;
//normalization
for (i = 0; i < features.n_rows; i++)
{
for (j = 0; j < features.n_cols; j++)
{
features(i, j) = (features(i, j) - m(j))/s(j);
}
}
mat targets(10, 1);
targets << 399900 << endr
<< 329900 << endr
<< 369000 << endr
<< 232000 << endr
<< 539900 << endr
<< 299900 << endr
<< 314900 << endr
<< 198999 << endr
<< 212000 << endr
<< 242500 << endr;
mat theta = ones(features.n_cols + 1);
mat features_new = join_horiz(ones(features.n_rows), features);
double alpha = 0.01;
double con = alpha*(1.0 / features.n_rows);
while (j < 20000){
mat step_error = (features_new*theta - targets);
theta = theta - con * (features_new.t() * step_error);
j++;
}
cout << theta << endl;
system("pause");
return 0;
}
你尝试过更小的学习率吗?你缩放过你的功能吗?fit函数根据迭代次数是什么样的?是的,我尝试过更小的学习率。我没有缩放数据,但数据是小而线性的。我对成本函数使用了平方误差,并在每次迭代中增加。你尝试过更小的学习率吗?是吗你缩放了你的功能?拟合函数是什么样子取决于迭代次数?是的,我尝试了更小的学习速率。我没有缩放数据,但数据很小且呈线性。我使用了成本函数的平方误差,并在每次迭代中增加。对于我使用的数据,正态方程将是一个很好的选择。但我正在尝试了解gradient Delegate算法。你能告诉我我在wile Loop里面做错了什么吗对不起,我的浏览器没有加载全部代码。但是你提供的代码产生的结果和我的一样。我认为这是一个特征规范化的问题。可能是梯度步数过多,从来没有达到最小值。对不起,你的代码是c正确。请看一下我更新答案中的数据规范化。对于我使用的数据来说,正态方程是一个很好的选择。但我正在尝试理解梯度下降算法。你能告诉我我在wile Loop中做错了什么吗?对不起,我的浏览器没有加载全部代码。但是你提供的代码产生的结果与m相同我认为这是一个特征规范化的问题。可能是梯度步长过大,从未达到最小值。对不起,您的代码是正确的。请查看我更新的答案中的数据规范化。