BP(Back Propagation)网络是1986年由Rumelhart和McCelland为首的科学家小组提出,是一种按误差逆传播算法训练的多层前馈网络,是目前应用最广泛的神经网络模型之一。BP网络能学习和存贮大量的输入输出模式映射关系,而无需事前揭示描述这种映射关系的数学方程。它的学习规则是使用最速下降法(梯度法),通过反向传播来不断调整网络的权值和阈值,使网络的误差平方和最小。BP神经网络模型拓扑结构包括输入层(input layer)、隐层(hide layer)和输出层(output layer)。
以下是我收集的一些关于神经网络的文章:
神经网络介绍——利用反向传播算法的模式学习
http://www.ibm.com/developerworks/cn/Linux/other/l-neural/index.html
人工智能 Java 坦克机器人系列: 神经网络,上部
http://www.ibm.com/developerworks/cn/java/j-lo-robocode3/index.html
人工智能 Java 坦克机器人系列: 神经网络,下部
http://www.ibm.com/developerworks/cn/java/j-lo-robocode4/
使用 Python 构造神经网络--Hopfield 网络可以重构失真的图案并消除噪声
http://www.ibm.com/developerworks/cn/linux/l-neurnet/
提供一个Matlab的BP神经网络的基础资料
http://www.cnblogs.com/galaxyprince/archive/2010/12/20/1911157.html
http://www.codeproject.com/KB/recipes/aforge_neuro.aspx
作者已经给出好几种形式的应用例子
以下C++代码实现了BP网络,通过8个3位二进制样本对应一个期望输出,训练BP网络,最后训练好的网络可以将输入的三位二进制数对应输出一位十进制数。
-
- #include<iostream>
- #include<cmath>
- using namespace std;
-
- #define innode 3 //输入结点数
- #define hidenode 10//隐含结点数
- #define outnode 1 //输出结点数
- #define trainsample 8//BP训练样本数
- class BpNet
- {
- public:
- void train(double p[trainsample][innode ],double t[trainsample][outnode]);
- double p[trainsample][innode];
- double t[trainsample][outnode];
-
- double *recognize(double *p);
-
- void writetrain();
- void readtrain();
-
- BpNet();
- virtual ~BpNet();
-
- public:
- void init();
- double w[innode][hidenode];
- double w1[hidenode][outnode];
- double b1[hidenode];
- double b2[outnode];
-
- double rate_w;
- double rate_w1;
- double rate_b1;
- double rate_b2;
-
- double e;
- double error;
- double result[outnode];
- };
-
- BpNet::BpNet()
- {
- error=1.0;
- e=0.0;
-
- rate_w=0.9;
- rate_w1=0.9;
- rate_b1=0.9;
- rate_b2=0.9;
- }
-
- BpNet::~BpNet()
- {
-
- }
-
- void winit(double w[],int n)
- {
- for(int i=0;i<n;i++)
- w[i]=(2.0*(double)rand()/RAND_MAX)-1;
- }
-
- void BpNet::init()
- {
- winit((double*)w,innode*hidenode);
- winit((double*)w1,hidenode*outnode);
- winit(b1,hidenode);
- winit(b2,outnode);
- }
-
- void BpNet::train(double p[trainsample][innode],double t[trainsample][outnode])
- {
- double pp[hidenode];
- double qq[outnode];
- double yd[outnode];
-
- double x[innode];
- double x1[hidenode];
- double x2[outnode];
- double o1[hidenode];
- double o2[hidenode];
-
- for(int isamp=0;isamp<trainsample;isamp++)
- {
- for(int i=0;i<innode;i++)
- x[i]=p[isamp][i];
- for(int i=0;i<outnode;i++)
- yd[i]=t[isamp][i];
-
-
- for(int j=0;j<hidenode;j++)
- {
- o1[j]=0.0;
- for(int i=0;i<innode;i++)
- o1[j]=o1[j]+w[i][j]*x[i];
- x1[j]=1.0/(1+exp(-o1[j]-b1[j]));
-
-
- }
-
- for(int k=0;k<outnode;k++)
- {
- o2[k]=0.0;
- for(int j=0;j<hidenode;j++)
- o2[k]=o2[k]+w1[j][k]*x1[j];
- x2[k]=1.0/(1.0+exp(-o2[k]-b2[k]));
-
-
- }
-
- for(int k=0;k<outnode;k++)
- {
- qq[k]=(yd[k]-x2[k])*x2[k]*(1-x2[k]);
- for(int j=0;j<hidenode;j++)
- w1[j][k]+=rate_w1*qq[k]*x1[j];
- }
-
- for(int j=0;j<hidenode;j++)
- {
- pp[j]=0.0;
- for(int k=0;k<outnode;k++)
- pp[j]=pp[j]+qq[k]*w1[j][k];
- pp[j]=pp[j]*x1[j]*(1-x1[j]);
-
- for(int i=0;i<innode;i++)
- w[i][j]+=rate_w*pp[j]*x[i];
- }
-
- for(int k=0;k<outnode;k++)
- {
- e+=fabs(yd[k]-x2[k])*fabs(yd[k]-x2[k]);
- }
- error=e/2.0;
-
- for(int k=0;k<outnode;k++)
- b2[k]=b2[k]+rate_b2*qq[k];
- for(int j=0;j<hidenode;j++)
- b1[j]=b1[j]+rate_b1*pp[j];
- }
- }
-
- double *BpNet::recognize(double *p)
- {
- double x[innode];
- double x1[hidenode];
- double x2[outnode];
- double o1[hidenode];
- double o2[hidenode];
-
- for(int i=0;i<innode;i++)
- x[i]=p[i];
-
- for(int j=0;j<hidenode;j++)
- {
- o1[j]=0.0;
- for(int i=0;i<innode;i++)
- o1[j]=o1[j]+w[i][j]*x[i];
- x1[j]=1.0/(1.0+exp(-o1[j]-b1[j]));
-
-
- }
-
- for(int k=0;k<outnode;k++)
- {
- o2[k]=0.0;
- for(int j=0;j<hidenode;j++)
- o2[k]=o2[k]+w1[j][k]*x1[j];
- x2[k]=1.0/(1.0+exp(-o2[k]-b2[k]));
-
-
- }
-
- for(int k=0;k<outnode;k++)
- {
- result[k]=x2[k];
- }
- return result;
- }
-
- void BpNet::writetrain()
- {
- FILE *stream0;
- FILE *stream1;
- FILE *stream2;
- FILE *stream3;
- int i,j;
-
- if(( stream0 = fopen("w.txt", "w+" ))==NULL)
- {
- cout<<"创建文件失败!";
- exit(1);
- }
- for(i=0;i<innode;i++)
- {
- for(j=0;j<hidenode;j++)
- {
- fprintf(stream0, "%f\n", w[i][j]);
- }
- }
- fclose(stream0);
-
-
- if(( stream1 = fopen("w1.txt", "w+" ))==NULL)
- {
- cout<<"创建文件失败!";
- exit(1);
- }
- for(i=0;i<hidenode;i++)
- {
- for(j=0;j<outnode;j++)
- {
- fprintf(stream1, "%f\n",w1[i][j]);
- }
- }
- fclose(stream1);
-
-
- if(( stream2 = fopen("b1.txt", "w+" ))==NULL)
- {
- cout<<"创建文件失败!";
- exit(1);
- }
- for(i=0;i<hidenode;i++)
- fprintf(stream2, "%f\n",b1[i]);
- fclose(stream2);
-
-
- if(( stream3 = fopen("b2.txt", "w+" ))==NULL)
- {
- cout<<"创建文件失败!";
- exit(1);
- }
- for(i=0;i<outnode;i++)
- fprintf(stream3, "%f\n",b2[i]);
- fclose(stream3);
-
- }
-
- void BpNet::readtrain()
- {
- FILE *stream0;
- FILE *stream1;
- FILE *stream2;
- FILE *stream3;
- int i,j;
-
-
- if(( stream0 = fopen("w.txt", "r" ))==NULL)
- {
- cout<<"打开文件失败!";
- exit(1);
- }
- float wx[innode][hidenode];
- for(i=0;i<innode;i++)
- {
- for(j=0;j<hidenode;j++)
- {
- fscanf(stream0, "%f", &wx[i][j]);
- w[i][j]=wx[i][j];
- }
- }
- fclose(stream0);
-
-
- if(( stream1 = fopen("w1.txt", "r" ))==NULL)
- {
- cout<<"打开文件失败!";
- exit(1);
- }
- float wx1[hidenode][outnode];
- for(i=0;i<hidenode;i++)
- {
- for(j=0;j<outnode;j++)
- {
- fscanf(stream1, "%f", &wx1[i][j]);
- w1[i][j]=wx1[i][j];
- }
- }
- fclose(stream1);
-
-
- if(( stream2 = fopen("b1.txt", "r" ))==NULL)
- {
- cout<<"打开文件失败!";
- exit(1);
- }
- float xb1[hidenode];
- for(i=0;i<hidenode;i++)
- {
- fscanf(stream2, "%f",&xb1[i]);
- b1[i]=xb1[i];
- }
- fclose(stream2);
-
-
- if(( stream3 = fopen("b2.txt", "r" ))==NULL)
- {
- cout<<"打开文件失败!";
- exit(1);
- }
- float xb2[outnode];
- for(i=0;i<outnode;i++)
- {
- fscanf(stream3, "%f",&xb2[i]);
- b2[i]=xb2[i];
- }
- fclose(stream3);
- }
-
-
- double X[trainsample][innode]= {
- {0,0,0},{0,0,1},{0,1,0},{0,1,1},{1,0,0},{1,0,1},{1,1,0},{1,1,1}
- };
- double Y[trainsample][outnode]={
- {0},{0.1429},{0.2857},{0.4286},{0.5714},{0.7143},{0.8571},{1.0000}
- };
-
- int main()
- {
- BpNet bp;
- bp.init();
- int times=0;
- while(bp.error>0.0001)
- {
- bp.e=0.0;
- times++;
- bp.train(X,Y);
- cout<<"Times="<<times<<" error="<<bp.error<<endl;
- }
- cout<<"trainning complete..."<<endl;
- double m[innode]={1,1,1};
- double *r=bp.recognize(m);
- for(int i=0;i<outnode;++i)
- cout<<bp.result[i]<<" ";
- double cha[trainsample][outnode];
- double mi=100;
- double index;
- for(int i=0;i<trainsample;i++)
- {
- for(int j=0;j<outnode;j++)
- {
-
- cha[i][j]=(double)(fabs(Y[i][j]-bp.result[j]));
- if(cha[i][j]<mi)
- {
- mi=cha[i][j];
- index=i;
- }
- }
- }
- for(int i=0;i<innode;++i)
- cout<<m[i];
- cout<<" is "<<index<<endl;
- cout<<endl;
- return 0;
- }