⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mybp_vise.cpp

📁 C版BP(逆向传播神经网络算法)的实现,感兴趣的可以看下!
💻 CPP
字号:
/*文件输入输出目录为:c:\

训练样本文件名:traningdata.txt

值为:

1
1
-1
1
-1
1
0
1
0
1

输出文件名为:阈值.txt    权值.txt

=========================
*/
#include "stdlib.h"
#include "math.h"
#include "conio.h"
#include "stdio.h"
#define N 4 /*/学习样本个数*/
#define IN 2 /*/输入层神经元数目*/
#define HN 2 /*/隐层神经元数目*/
#define ON 1 /*/输出层神经元数目*/
#define Z 20 /*/旧权值保存-》每次study的权值都保存下来*/
#define n_N_n 1 /*测试用的样本数*/
double P[IN]; /*/单个样本输入数据*/
double T[ON]; /*/单个样本输出数据*/
double P_test[IN];/*单个测试用的输入数据*/
double T_test[ON];/*单个测试用的输出数据*/
double W[HN][IN]; /*/输入层至隐层权值*/
double V[ON][HN]; /*/隐层至输出层权值*/
double X[HN]; /*/隐层的输入*/
double Y[ON]; /*/输出层的输入*/
double H[HN]; /*/隐层的输出*/
double O[ON]; /*/输出层的输出*/
double YU_HN[HN]; /*/隐层的阈值*/
double YU_ON[ON]; /*/输出层的阈值*/
double err_m[N]; /*/第m个样本的总误差*/
double a; /*/输出层至隐层的学习效率*/
double b; /*/隐层至输入层学习效率*/
double alpha;  /*/动量因子,改进型bp算法使用*/
double d_err[ON];/*输出层校正误差*/

FILE *fp;
/*定义一个放学习样本的结构*/
struct {
double input[IN];
double teach[ON];
}Study_Data[N];

/*用于存放测试的样本*/
struct{
	double input_test[IN];
	double teach_test[ON];
}Test_Data[n_N_n];

/*改进型bp算法用来保存每次计算的权值*/
struct {
double old_W[HN][IN];
double old_V[ON][HN];
}Old_WV[Z];


int Start_Show()
{
//	clrscr();
printf("\n                       ***********************\n");
printf("                       *    Welcome to use   *\n");
printf("                       *  this program of    *\n");
printf("                       *  calculating the BP *\n");
printf("                       *      model!         *\n");
printf("                       *   Happy every day!  *\n");
printf("                       ***********************\n");
printf("\n\nBefore starting,please read the follows carefully:\n\n");
printf("    1.Please ensure the Path of the 'traningdata.txt'(xunlianyangben.txt) is \n correct,like 'c:\\traningdata.txt'!\n");
printf("    2.The calculating results will be saved in the Path of 'c:\\'!\n");
printf("    3.The program will load 12 datas when running from 'c:\\traningdata.txt'!\n");
printf("    4.The program of BP can study itself for no more than 30000 times.\nAnd surpassing the number,the program will be ended by itself in\npreventing running infinitely because of error!\n");
printf("\n\n\n");
printf("Now press any key to start...\n");
getch();
getch();
//clrscr();
return 1;
}

int End_Show()
{
printf("\n\n---------------------------------------------------\n");
printf("The program has reached the end successfully!\n\nPress any key to exit!\n\n");
printf("\n                       ***********************\n");
printf("                       *    This is the end  *\n");
printf("                       * of the program which*\n");
printf("                       * can calculate the BP*\n");
printf("                       *      model!         *\n");
printf("                       ***********************\n");
printf("                       *  Thanks for using!  *\n");
printf("                       *   Happy every day!  *\n");
printf("                       ***********************\n");
getch();
exit(0);
}

GetTrainingData()      /*OK*/
{ 
   int m,i,j;
//  float datr;

if((fp=fopen("c:\\traningdata.txt","r"))==NULL)         /*读取训练样本*/
 {
  printf("Cannot open file strike any key exit!");
  getch();
  exit(1);
 }

int times=0;
for( m=0;m<N;m++)
{
	for(int i=0;i<IN;i++)
	{
		fscanf(fp,"%lf",&Study_Data[m].input[i]);
		times++;
	}
	for(int j=0;j<ON;j++)
	{
		fscanf(fp,"%lf",&Study_Data[m].teach[j]);
		times++;
	}
}

fclose(fp);
printf("\nThere are [%d] datats that have been loaded successfully!\n",times);


/*show the data which has been loaded!*/
printf("\nShow the data which has been loaded as follows:\n");
for(m=0;m<N;m++) 
{
	for(i=0;i<IN;i++)   
	{
		printf("\nStudy_Data[%d].input[%d]=%lf",m,i,Study_Data[m].input[i]);
	}
  for(j=0;j<ON;j++)
  {printf("\nStudy_Data[%d].teach[%d]=%lf",m,j,Study_Data[m].teach[j]);
   }
 }
printf("\n\nPress any key to start calculating...");
getch();
 return 1;
}

GetTestingData()
{
	int n,i,j;
	if((fp=fopen("c:\\testingdata.txt","r"))==NULL)         /*读取训练样本*/
 {
  printf("Cannot open file strike any key exit!");
  getch();
  exit(1);
 }

int times=0;
for( n=0;n<n_N_n;n++)
{
	for(int i=0;i<IN;i++)
	{
		fscanf(fp,"%lf",&Test_Data[n].input_test[i]);
		times++;
	}
	for(int j=0;j<ON;j++)
	{
		fscanf(fp,"%lf",&Test_Data[n].teach_test[j]);
		times++;
	}
}
fclose(fp);
printf("\nThere are [%d] datats that have been loaded successfully!\n",times);


/*show the data which has been loaded!*/
printf("\nShow the data which has been loaded as follows:\n");
for(n=0;n<n_N_n;n++) 
{
	for(i=0;i<IN;i++)   
	{
		printf("\nTest_Data[%d].input_test[%d]=%lf",n,i,Test_Data[n].input_test[i]);
	}
  for(j=0;j<ON;j++)
  {printf("\nTest_Data[%d].teach_test[%d]=%lf",n,j,Test_Data[n].teach_test[j]);
   }
 }
printf("\n\nPress any key to start calculating...");
getch();
 return 1;


}

/*///////////////////////////////////*/
/*初始化权、阈值子程序*/
/*///////////////////////////////////*/
initial()
{int i;
 int ii;
 int j;
 int jj;
 int k;
 int kk;
/*隐层权、阈值初始化*/

 for(i=0;i<HN;i++) 
 {
  for(j=0;j<IN;j++)
  {
	  W[i][j]=(double)((rand()/32767.0)*2-1); 
	  /*初始化输入层到隐层的权值,随机模拟0 和 1 -1 */
    printf("w[%d][%d]=%lf\n",i,j,W[i][j]);
   }
  }
 for(ii=0;ii<HN;ii++) 
 {
  for(jj=0;jj<ON;jj++)
  {
	  V[ii][jj]= (double)((rand()/32767.0)*2-1); 
	  /*初始化隐层到输出层的权值,随机模拟0 和 1 -1*/
    printf("V[%d][%d]=%lf\n",ii,jj,V[ii][jj]);
   }
  }
 for(k=0;k<HN;k++)
 {
  YU_HN[k] = (double)((rand()/32767.0)*2-1);  /*隐层阈值初始化 ,-0.01 ~ 0.01 之间*/
  printf("YU_HN[%d]=%lf\n",k,YU_HN[k]);
  }
 for(kk=0;kk<ON;kk++) 
 {
  YU_ON[kk] = (double)((rand()/32767.0)*2-1); /*输出层阈值初始化 ,-0.01 ~ 0.01 之间*/
  }
  return 1;
}/*子程序initial()结束*/


/*//////////////////////////////////////////*/
/*第m个学习样本输入子程序*/
/*/////////////////////////////////////////*/
input_P(int m)
{ int i;

  for(i=0;i<IN;i++)
  {
	  P[i]=Study_Data[m].input[i];
	  printf("P[%d]=%lf\n",i,P[i]);
  }
/*获得第m个样本的数据*/
return 1;
}/*子程序input_P(m)结束*/

/*获取第n个测试数据*/
input_P_test(int n)
{
	int i;
	for (i=0;i<n;i++)
	{
		P_test[i]=Test_Data[n].input_test[i];
		printf("P_test[%d]=%lf\n",i,P_test[i]);
	}
	return 1;
}
input_T_test(int n)
{
	int i;
	for(i=0;i<n;i++)
	{
		T_test[i]=Test_Data[n].teach_test[i];
		printf("T_test[%d]=%lf\n",T_test[i]);
	}
	return 1;
}

/*/////////////////////////////////////////*/
/*第m个样本输出信号子程序*/
/*/////////////////////////////////////////*/
input_T(int m)
{int k;

 for(k=0;k<ON;k++)
	 T[k]=Study_Data[m].teach[k];
return 1;
}/*子程序input_T(m)结束*/


H_I_O()
{
 double sigma;
 int i,j;
 for(j=0;j<HN;j++)
 {
   sigma=0;
   for(i=0;i<IN;i++)
   {
	   sigma+=W[j][i]*P[i];/*求隐层内积*/
    }

   X[j]=sigma-YU_HN[i];/*求隐层净输入,X[j]=ΣW[j][i]*P[i]-θ[j],其中θ[j]是阈值*/
   H[j]=1.0/(1.0+exp(-X[j]));/*求隐层输出 采用S型激活函数*/
   }
return 1;
}/*子程序H_I_O()结束*/

/*测试子程序*/
H_I_O_test()
{
	double sigma;
 int i,j;
 for(j=0;j<HN;j++)
 {
   sigma=0;
   for(i=0;i<IN;i++)
   {
	   sigma+=W[j][i]*P_test[i];/*求隐层内积*/
    }

   X[j]=sigma-YU_HN[i];/*求隐层净输入,X[j]=ΣW[j][i]*P[i]-θ[j],其中θ[j]是阈值*/
   H[j]=1.0/(1.0+exp(-X[j]));/*求隐层输出 采用S型激活函数*/
   }
return 1;

}


O_I_O()
{int k;
 int j;
 double sigma;
 for(k=0;k<ON;k++)
 {
  sigma=0.0;
  for(j=0;j<HN;j++)
  {
   sigma+=V[k][j]*H[k];
  }
 Y[k]=sigma-YU_ON[k];/*输出层的净输入*/
 O[k]=1.0/(1.0+exp(-Y[k]));/*求输出层的输出,S型激活函数*/
 }
return 1;
}

/*测试用的O_I_O*/
O_I_O_test()
{
	int k;
 int j;
 double sigma;
 for(k=0;k<ON;k++)
 {
  sigma=0.0;
  for(j=0;j<HN;j++)
  {
   sigma+=V[k][j]*H[k];
  }
 Y[k]=sigma-YU_ON[k];/*输出层的净输入*/
 O[k]=1.0/(1.0+exp(-Y[k]));/*求输出层的输出,S型激活函数*/
 }
return 1;

}


int Err_O_H(int m)
{int k;
double abs_err[ON];
double sqr_err=0;
for (k=0;k<ON;k++)
  {
  abs_err[k]=T[k]-O[k];
  sqr_err+=(abs_err[k])*(abs_err[k]);
  
  d_err[k]=abs_err[k]*O[k]*(1.0-O[k]);
  /*dt=(yt-ct)*ct*(1-ct),利用网络误差计算各输出层单元的校正误差*/

  }
err_m[m]=sqr_err/2;
  //第m个样本的总误差,均方差
return 1;
}

/*测试样本的误差计算*/
int Err_O_H_test(int n)
{
	int k;
double abs_err[ON];
double sqr_err=0;
for (k=0;k<ON;k++)
  {
  abs_err[k]=T_test[k]-O[k];
  sqr_err+=(abs_err[k])*(abs_err[k]);
  }
err_m[n]=sqr_err/2;
  //第n个样本的总误差,均方差
return 1;
}

double e_err[HN];
/*中间层的校正误差*/
int Err_H_I()
{
 int j,k;
 double sigma;
 for(j=0;j<HN;j++) 
 {
  sigma=0.0;
  for(k=0;k<ON;k++)
  {
   sigma+=d_err[k]*V[k][j];
   }
 e_err[j]=sigma*H[j]*(1-H[j]);
 }
return 1;
}


saveWV(int m)
{int i;
 int ii;
 int j;
 int jj;
 for(i=0;i<HN;i++)
 {
   for(j=0;j<IN;j++)
   {
     Old_WV[m].old_W[i][j] = W[i][j];
    }
  }
 for(ii=0;ii<ON;ii++)
 {
   for(jj=0;jj<HN;jj++)    {
     Old_WV[m].old_V[ii][jj] = V[ii][jj];
    }
  }
return 1;
}

/*学习规则*/
int Delta_O_H(int n)                 /*(int m,int n)*/
{/*关于BP和改进的BP算法,还需要了解*/
	int k,j;
 if(n<1)  /*n<=1*/
  {
   for (k=0;k<ON;k++)
   {
     for (j=0;j<HN;j++)
	 {
       V[k][j]=V[k][j]+a*d_err[k]*H[j];/*a,b学习系数*/
      }
     YU_ON[k]+=a*d_err[k];
    }
  }
 else if(n>1)
  {
   for (k=0;k<ON;k++)
   {
     for (j=0;j<HN;j++)
	 {
       V[k][j]=V[k][j]+a*d_err[k]*H[j]+alpha*(V[k][j]-Old_WV[(n-1)].old_V[k][j]);
      }
     YU_ON[k]+=a*d_err[k];
    }
  }
return 1;
}

Delta_H_I(int n)               /*(int m,int n)*/
{ int i,j;

if(n<=1)   /*n<=1*/
 {
  for (j=0;j<HN;j++)
  {
    for (i=0;i<IN;i++)
	{
      W[j][i]=W[j][i]+b*e_err[j]*P[i];
     }
    YU_HN[j]+=b*e_err[j];
   }
 }
else if(n>1)
 {
  for(j=0;j<HN;j++)
  {
    for(i=0;i<IN;i++)
	{
      W[j][i]=W[j][i]+b*e_err[j]*P[i]+alpha*(W[j][i]-Old_WV[(n-1)].old_W[j][i]);//改进型bp
     }
    YU_HN[j]+=b*e_err[j];
   }
 }
return 1;
}


double Err_Sum()
{int m;
double total_err=0;
for(m=0;m<N;m++)
 {
  total_err+=err_m[m];
 }
return total_err;
}

/*测试误差*/
double Err_Sum_test()
{int n;
double total_err=0;
for(n=0;n<n_N_n;n++)
 {
  total_err+=err_m[n];
 }
return total_err;
}

void savequan()
{ int i,j,k;
  int ii,jj,kk;

if((fp=fopen("c:\\权值cnlj.txt","a"))==NULL)         /*save the result at c:\*.txt*/
 {
  printf("Cannot open file strike any key exit!");
  getch();
  exit(1);
 }

fprintf(fp,"Save the result of “权值”(quanzhi) as follows:\n");
for(i=0;i<HN;i++)
 {
  for(j=0;j<IN;j++)  fprintf(fp,"W[%d][%d]=%f\n",i,j,W[i][j]);
 }
fprintf(fp,"\n");
for(ii=0;ii<ON;ii++) {
  for(jj=0;jj<HN;jj++)  fprintf(fp,"V[%d][%d]=%f\n",ii,jj,V[ii][jj]);
  }
fclose(fp);
printf("\nThe result of “权值.txt”(quanzhi) has been saved successfully!\nPress any key to continue...");
getch();


if((fp=fopen("c:\\阈值cnlj.txt","a"))==NULL)         /*save the result at c:\*/
 {
  printf("Cannot open file strike any key exit!");
  getch();
  exit(1);
 }
fprintf(fp,"Save the result of “输出层的阈值”(huozhi) as follows:\n");
 for(k=0;k<ON;k++)   fprintf(fp,"YU_ON[%d]=%f\n",k,YU_ON[k]);

fprintf(fp,"\nSave the result of “隐层的阈值为”(huozhi) as follows:\n");
 for(kk=0;kk<HN;kk++)  fprintf(fp,"YU_HN[%d]=%f\n",kk,YU_HN[kk]);

fclose(fp);
printf("\nThe result of “阈值.txt”(huozhi) has been saved successfully!\nPress any key to continue...");
getch();
}

/**********************/
/**程序入口,即主程序**/
/**********************/

void main()
{double Pre_error;
double sum_err;
int study;
int flag;
flag=8000;
a=0.9;
b=0.9;
alpha=0.9;
study=0;
Pre_error=0.0001;
/*实际值为Pre_error=0.0001;*/

Start_Show();
GetTrainingData();
initial();

do
 {int m;
  ++study;
  for(m=0;m<N;m++)
  {
    input_P(m);
    input_T(m);
    H_I_O();
    O_I_O();
    Err_O_H(m);
    Err_H_I();
    saveWV(m);          
	/****************/
    Delta_O_H(m);                             /*(m,study)*/
    Delta_H_I(m);                              /*(m,study)*/
   }
  sum_err=Err_Sum();
  printf("sum_err=%lf\n",sum_err);
  printf("Pre_error=%lf\n\n",Pre_error);

  if(study>flag)
   {
    printf("\n*******************************\n");
    printf("The program is ended by itself because of error!\nThe learning times is surpassed!\n");
    printf("*****************************\n");
    getch();
    break;
   }

 }while (sum_err>Pre_error);

printf("\n****************\n");
printf("\nThe program have studyed for [%d] times!\n",study);
printf("\n****************\n");
savequan();        /*save the results*/

/*处理测试数据*/
printf("\n******************\n");
printf("\nThe program will do testing, press any key to start .......\n");
getch();

GetTestingData();
for(int n=0;n<n_N_n;n++)
{
input_P_test(n);
input_T_test(n);
H_I_O_test();
O_I_O_test();
Err_O_H_test(n);
}
double sum_err_test=Err_Sum_test();
printf("\n*********************\n");
for(int c=0;c<n_N_n;c++)
for(int d=0;d<ON;d++)
printf("\nTest_Data[%d].teach_test[%d]=%lf,O[%d]=%lf\n",c,d,T_test[d],d,O[d]);
printf("\nAnd the total wucha of the testing is:%lf\n",sum_err_test);
printf("\nThe efficiency of study is a=%lf,b=%lf.\n",a,b); 
printf("\nThe testing ended successfully!press any key to continue.......\n");
getch();
/*测试结束*/

End_Show();
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -