我使用了随机梯度下降算法去假设了一个带有theta0和theta1的函数去预测一个已知的函数
我假设一个函数为h(x)=theta0+theta1*x去通过已知函数得到的数据带入但是theta1已经收敛的很接近了但是theta0基本没变,我已经验算了好几遍我的算式应该没有写错我现在也不知道问题在哪所以求助各位大佬,对了我是用c++写的,如果知道我的错误的请指出来谢谢了,祝各位大佬过个好年!
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
double f1(double x)
{
double y;
y = 10 + 1.8 * x;
return y;
}
double htheta(double theta0, double theta1, double x)
{
double y;
y = theta0 + theta1 * x;
return y;
}
double juedui(double x, double y)
{
double jueduizhi=0, ju;
ju = x - y;
if (ju < 0)
jueduizhi = -ju;
else if (ju > 0)
jueduizhi = ju;
return jueduizhi;
}
int main()
{
double theta0, theta1, alph;
double jtheta0, jtheta1;
theta0=20,theta1 = 20;
alph = 1e-7;
for (int i = 0;; i++)
{
jtheta0 = (htheta( theta0, theta1,i) - f1(i));
jtheta1 = (htheta( theta0, theta1, i) - f1(i)) * i;
theta0 = theta0 - alph*jtheta0;
theta1 = theta1 - alph*jtheta1;
cout << theta0 <<" "<< theta1 <<" "<<i<< endl;
if (juedui(htheta(theta0,theta1,i),f1(i))< 1e-4)
{
break;
}
}
}
https://blog.csdn.net/qq_41800366/article/details/86600893
虽然不怎么理解你的意思,w0的应该是接近于你写的式子 y = 10 + 1.8 * x;,那么w0趋近于10,w1趋近于1.8,不知道对不对,没学c++没理解你的意思