| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5692 人关注过本帖
标题:该程序一运行就显示exe停止工作。各位帮忙解答一下。。
只看楼主 加入收藏
matt111
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-4-24
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
该程序一运行就显示exe停止工作。各位帮忙解答一下。。
求帮忙看看程序。作用是输入两个变量x1,x2的值,得出y值。
机器学习入门。线性回归方程和梯度下降函数。
问题是:该程序一运行就显示exe停止工作。是什么原因?没报错


#include<iostream.h>
#include<iomanip.h>


double hypothesis()     //定义假设函数h()
{   
    int x3[35],x4[35];
    double theta[2];
    double hypothesis;
    for(int i=0;i<35;i++)
    {   hypothesis=theta[0]+theta[1]*x3[i]+theta[2]*x4[i];
        return 0 ;
    }
};

void main()
{
    int y[]={399900,329900,369000,232000,539900,299900,314900,198999,212000,242500,
               239999,347000,329999,699900,259900,449900,299900,199900,499998,599000,
               252900,255000,242900,259900,573900,249900,464500,469000,475000,299900,
               349900,169900,314900,579900,285900};           // 定义y数组

    int x1[]={2104,1600,2400,1416,3000,1985,1534,1427,1380,1494,1940,2000,
              1890,4478,1268,2300,1320,1236,2609,3031,1767,1888,1604,1962,
              3890,1100,1458,2526,2200,2637,1839,1000,2040,3137,1811};     // 定义第一列x1变量

    int x2[]={3,3,3,2,4,4,3,3,3,3,4,3,3,5,3,4,2,3,4,4,3,2,3,4,3,3,3,3,3,3,2,1,4,3,4};   // 定义第二列x2变量
   
    int w1=0,w2=0,w3=0; //w1代表x1的平均值,w2代表x2的平均值,w3代表y的平均值
    for(int i=1;i<=35;i++)
    {
        w1=w1+x1[i];
        w2=w2+x2[i];
        w3=w3+y[i];
    }
    w1=w1/35;
    w2=w2/35;
    w3=w3/35;

    int f1=0,f2=0,f3=0; //f1代表x1的标准差,f2代表x2的标准差,f3代表x3的标准差
    for(i=1;i<=35;i++)
    {
        f1=f1+(x1[i]-w1)*(x1[i]-w1);
        f2=f2+(x2[i]-w2)*(x2[i]-w2);
        f3=f3+(y[i]-w3)*(y[i]-w3);
    }
   
    int x3[35];
    int x4[35];
    int y1[35];

    x3[i]=(x1[i]-w1)/f1;
    x4[i]=(x2[i]-w2)/f2;
    y1[i]=(y[i]-w3)/f3;
   
   
    double theta[2];
    theta[0]=theta[1]=theta[2]=3;

    double sum,sum1,sum2;                 // 中间变量,用于迭代求和  
    sum=sum1=sum2=0;
   
    double t,t1,t2;                            // i代表第几个训练集;
    double J;                             // 参数 J 用来放代价函数值
    double a;                             // 学习率,开始时先假设为0.3


    cout<<"请输入学习率a=";
    cin>>a;

    for(i=1;i<=35;i++)                     
    {
        hypothesis( );
        t=hypothesis( )-y1[i];
        sum=sum+t;                     // 求和
    }
   
    J=sum/(2*35);                      // 求代价函数值
   
    do{
        for(i=1;i<=35;i++)                 //不断改变theta值直到代价函数值为0
        {
            hypothesis(  );
            t=hypothesis( )-y1[i];
            
            t1=t*x3[i];
            sum1=sum1+t1;
            t2=t*x4[i];
            sum2=sum2+t2;
            sum=sum+t;
        }   
            theta[0]=theta[0]-sum/(a*35);
            theta[1]=theta[1]-sum1/(a*35);
            theta[2]=theta[2]-sum2/(a*35);
            
            for(i=1;i<=35;i++)                     
            {
                hypothesis( );
                t=hypothesis( )-y1[i];
                sum=sum+t;                     // 求和
            }            // Question1:返回J函数
            
            J=sum/(2*35);  
    }
    while(J==0);
   
        cout<<"theta[0]="<<setprecision(6)<<theta[0]<<endl;
        cout<<"theta[1]="<<setprecision(6)<<theta[1]<<endl;
        cout<<"theta[2]="<<setprecision(6)<<theta[2]<<endl;
   
        
   
    int m,n;
    double h;
    cout<<"请输入x1,x2的值";
    cin>>m;
    cin>>n;
    h=theta[0]+theta[1]*m+theta[2]*n;
    cout<<"得出的y值为:"<<h<<endl;
}
搜索更多相关主题的帖子: hypothesis include double return 
2015-04-24 19:42
zcdjt
Rank: 3Rank: 3
等 级:论坛游侠
威 望:4
帖 子:99
专家分:181
注 册:2014-9-9
收藏
得分:20 
using namespace std;你为什么不写?

今朝醉
2015-04-24 20:19
matt111
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-4-24
收藏
得分:0 
回复 楼主 matt111
VC一般都不用写。而且我这个程序之前还是可以运行的,只是算出来的数据不对。修改之后就直接不能运行了
2015-04-24 20:26
matt111
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-4-24
收藏
得分:0 
回复 楼主 matt111
dsdsds d
2015-04-24 20:50
快速回复:该程序一运行就显示exe停止工作。各位帮忙解答一下。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017776 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved