Bp.rar

  • PUDN用户
    了解作者
  • Visual C++
    开发工具
  • 4KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 2
    下载次数
  • 2011-09-15 11:36
    上传日期
bp神经网络的代码,经过试用可行。感兴趣的童鞋可以下载,欢迎试用。。。
Bp.rar
  • Bp.txt
    16.4KB
内容介绍
// high.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <stdlib.h> #include <math.h> #define HN 3 //隐含层神经元个数, 请多次修改其值来进行比较 #define InputN 7 #define OutN 2 #define datanum 40 //样本数 #define g(x) 1/(1+exp(-x)) //sigmod函数 using namespace std; int main() { double x_out[InputN]; //输入层的输出值 double hn_out[HN]; //隐含层的输出值 double y_out[OutN]; //输出层的输出值 double y[OutN]; //输出层的期望输出值 double w[InputN][HN]; //输入层到隐含层的权值 double v[HN][OutN]; //隐含层到输出层的权值 double deltaw[InputN][HN]; double deltav[HN][OutN]; double hn_delta[HN]; //隐含层的delta double y_delta[OutN]; //输出层的delta double error; //全局误差 double errlimit=0.001; //预设的全局误差极限 double alpha=0.1, beta=0.1; //预设的学习速率 int loop=0; //训练次数 int times=5000; //预设的训练次数上限 int i, j, m; double max, min; double sumtemp; double errtemp; struct //训练样本对. 对XOR问题, 共有4对 { double input[InputN]; double teach[OutN]; }data[datanum] = {{4.200587,8.757649,14.978449,15.480242,30.709634,19.859563,35.354203,0,1}, {4.912205,10.224217,17.249002,18.363114,36.177741,23.484644,38.206684,0,1}, {5.309812,11.092133,18.154547,19.772628,38.794567,25.370193,39.839171,0,1}, {5.615145,11.764081,19.022791,21.290701,41.742122,27.374956,41.851938,0,1}, {5.843736,12.290715,19.703589,22.435027,44.146490,28.945351,43.666399,0,1}, {6.015878,12.696909,20.125752,22.851541,44.958188,29.549662,44.511815,0,1}, {6.153213,13.020128,20.476103,22.934779,45.036376,29.690620,44.941687,0,1}, {6.270115,13.304488,20.821552,22.960920,45.075201,29.766627,45.363155,0,1}, {6.359281,13.521615,21.137008,22.970687,45.166506,29.841441,45.723331,0,1}, {6.430120,13.698384,21.445734,23.023830,45.351677,29.958037,46.145080,0,1}, {4.124488,8.582926,16.963662,19.873017,39.233231,24.810660,38.373806,1,0}, {4.789069,9.907390,18.554956,21.352603,41.494259,26.482576,41.886792,1,0}, {5.183136,10.697777,21.239630,23.197596,45.985331,28.626130,45.609321,1,0}, {5.410237,11.175709,22.575798,23.690074,46.930698,29.288304,47.643572,1,0}, {5.606571,11.612154,23.631866,23.860633,47.608452,29.667776,50.489644,1,0}, {5.786944,12.027482,22.254399,26.196148,51.791571,32.743371,50.454785,1,0}, {5.928602,12.388281,21.652601,24.308387,48.183356,30.932533,47.380323,1,0}, {6.050356,12.724998,21.493515,24.292141,48.676177,31.959651,47.210969,1,0}, {6.154450,13.043803,21.356899,24.063475,47.789482,32.226907,46.843936,1,0}, {6.242909,13.330388,21.323999,23.931372,47.630165,32.160825,46.621496,1,0}, {4.235358,8.763290,18.473495,16.792820,34.791179,21.197701,34.754591,1,1}, {4.853780,9.996412,19.847666,18.700436,38.101760,23.723281,38.720912,1,1}, {5.231157,10.760096,20.462854,19.686574,39.804636,25.092985,41.005477,1,1}, {5.472075,11.264183,21.058809,20.348120,41.074616,26.006408,42.601942,1,1}, {5.696392,11.751526,22.456451,21.896793,44.208653,27.893742,44.793297,1,1}, {5.881703,12.173240,22.654148,23.043217,46.094555,29.602790,46.441358,1,1}, {6.052079,12.585853,22.626990,23.555203,46.837821,30.751759,47.218810,1,1}, {6.178310,12.927438,22.406138,24.011062,47.261654,33.612870,48.479065,1,1}, {6.284794,13.227863,22.358022,24.357752,47.718723,32.302620,50.261308,1,1}, {6.383178,13.508680,22.424404,24.779228,48.500702,32.140609,49.154664,1,1}, {4.299308,9.152762,15.421466,17.675226,34.517496,23.291179,34.628997,0,0}, {4.936995,10.502877,16.963223,20.539346,39.485078,26.326950,39.856932,0,0}, {5.293174,11.276321,17.799186,21.822592,41.644759,27.956030,43.535013,0,0}, {5.535797,11.819616,18.351642,21.598274,42.315036,27.941591,41.701853,0,0}, {5.731628,12.238882,18.925173,21.540031,41.898882,27.750075,42.522606,0,0}, {5.880877,12.545825,19.455761,21.649135,42.254586,27.970551,43.349977,0,0}, {6.002338,12.786770,19.977228,21.879609,42.823541,28.296087,44.552184,0,0}, {6.092610,12.978257,20.472328,22.229696,43.583015,28.731769,46.271359,0,0}, {6.168544,13.153884,20.952942,22.700636,44.528047,29.283625,47.871948,0,0}, {6.235362,13.334141,21.354733,23.183381,45.456116,29.855198,47.910412,0,0}}; //{{6.4479, 17.875, 24.925, 26.324, 52.825, 35.417, 52.013, 1,0}, // {6.4121, 14.065, 26.306, 24.966, 51.032, 31.998, 51.087, 0,1}, // {6.7335, 17.653, 26.721, 26.545, 54.826, 36.172, 53.261, 0,1}, // {6.5748, 17.99, 28.637, 26.831, 54.77, 36.756, 54.592, 1,0}, // {6.3806, 14.07, 24.463, 24.929, 49.74, 31.972, 51.98, 0,1}, // {6.3533, 15.964, 24.787, 24.793, 49.924, 33.025, 49.597, 1,0}, // {6.3806, 14.07, 24.463, 24.929, 49.74, 31.972, 51.98, 0,1}, // {6.7335, 17.653, 26.721, 26.545, 54.826, 36.172, 53.261, 0,1}, // {6.4121, 14.065, 26.306, 24.966, 51.032, 31.998, 51.087, 0,1}}; //注意这里的值需要改变 // //----------------------------------------------------------- // //初始化. 将连接权值、偏置系数置为(-1,1)之间的随机数 cout<<"initializing............"<<endl; for(i=0; i<InputN; i++) //w的随机输入 for(j=0; j<HN; j++) { w[i][j] = ((double)rand()/32767.0)*2-1; deltaw[i][j] = 0; // cout<<"w["<<i+1<<"]["<<j+1<<"]="<<w[i][j]<<endl; } for(i=0; i<HN; i++) //v的随机输入 for(j=0;j<OutN;j++) { v[i][j] = ((double)rand()/32767.0)*2-1; deltav[i][j] = 0; // cout<<"v["<<i+1<<"]["<<j+1<<"]="<<v[i][j]<<endl; } //训练. cout<<endl<<"training beginning............"<<endl; while(loop < 10000) { loop++; error = 0.0; for(m=0; m<datanum; m++) //对datanum个样本进行增量训练,m为样本个数 { //输入正向传播 max=0.0; min=0.0; for(i=0; i<InputN; i++) //设置输入层的输出 { x_out[i] = data[m].input[i]; if (max < x_out[i]) //比较得出输入的最大最小值 max = x_out[i]; if (min > x_out[i]) min = x_out[i]; // cout<<"x_out["<<i+1<<"]="<<x_out[i]<<endl; } for(i=0; i<InputN; i++) //归一化后的输入值 { x_out[i] = (x_out[i] - min) / (max-min); // cout<<"x_out["<<i+1<<"]="<<x_out[i]<<endl; } for(i=0; i<OutN; i++) //输出层的期望输出 { y[i] = data[m].teach[i]; // cout<<"y["<<i+1<<"]="<<y[i]<<endl; } for(i=0; i<HN; i++) //计算隐含层的输出 { sumtemp = 0.0; for(j=0; j<InputN; j++) sumtemp += w[j][i] * x_out[j]; hn_out[i] = tanh(sumtemp); //隐含层作用函数为y=tanh(x) } for(i=0; i<OutN; i++) //计算输出层的输出 { sumtemp = 0.0;
评论
    相关推荐
    • 数据库课程设计
      一个数据库课程设计,access管理工具实现,用的是窗体!
    • 数据库课程设计
      数据库课程设计十分完整有需要的请下载啊谢谢
    • 数据库课程设计
      广东工业大学数据库课程设计,可视化界面连接数据库,delphi7
    • 数据库课程设计
      数据库课程设计实验及其描述 数据库课程设计实验及其描述 数据库课程设计实验及其描述 数据库课程设计实验及其描述 数据库课程设计实验及其描述 数据库课程设计实验及其描述
    • 数据库课程设计
      数据库课程设计》由周爱武、汪海威、肖云编著,遵循数据库课程设计的具体要求,独立于具体的数据库教材,从实际应用系统的需求着手,引导读者逐步完成数据库设计全过程,重点讲解数据库系统的需求分析、概念设计、...
    • 数据库课程设计
      数据库课程设计人事管理系统 数据库课程设计人事管理系统数据库课程设计人事管理系统数据库课程设计人事管理系统数据库课程设计人事管理系统数据库课程设计人事管理系统数据库课程设计人事管理系统数据库课程设计...
    • 数据库课程设计
      数据库课程设计,基于visual basic自助银行管理系统,界面很清爽,实用。同学都说好,所以就上传了!!!
    • 数据库课程设计
      数据库课程设计 里面有详细的文档资料 包含数据库一切的图 以及生成的数据库表文件 期末得分为优秀
    • 数据库课程设计
      可以作为数据库课程设计,也可以作为Java的课程设计,内容全面。本资源转载的,非本人原创。用于交流学习,特此申明!
    • 数据库课程设计
      数据库课程设计蓝天大学学生管理系统 2.商店信息管理系统 3.实验室机房收费管理系统 4.图书馆资料检索系统 5.企业库存管理系统 6.仓库管理系统 7.工程项目管理系统 8.教材管理系统 9.企业人事管理系统 10.企业财务...