BP算法的C语言实现.docx

上传人:b****1 文档编号:13254327 上传时间:2023-06-12 格式:DOCX 页数:19 大小:19.96KB
下载 相关 举报
BP算法的C语言实现.docx_第1页
第1页 / 共19页
BP算法的C语言实现.docx_第2页
第2页 / 共19页
BP算法的C语言实现.docx_第3页
第3页 / 共19页
BP算法的C语言实现.docx_第4页
第4页 / 共19页
BP算法的C语言实现.docx_第5页
第5页 / 共19页
BP算法的C语言实现.docx_第6页
第6页 / 共19页
BP算法的C语言实现.docx_第7页
第7页 / 共19页
BP算法的C语言实现.docx_第8页
第8页 / 共19页
BP算法的C语言实现.docx_第9页
第9页 / 共19页
BP算法的C语言实现.docx_第10页
第10页 / 共19页
BP算法的C语言实现.docx_第11页
第11页 / 共19页
BP算法的C语言实现.docx_第12页
第12页 / 共19页
BP算法的C语言实现.docx_第13页
第13页 / 共19页
BP算法的C语言实现.docx_第14页
第14页 / 共19页
BP算法的C语言实现.docx_第15页
第15页 / 共19页
BP算法的C语言实现.docx_第16页
第16页 / 共19页
BP算法的C语言实现.docx_第17页
第17页 / 共19页
BP算法的C语言实现.docx_第18页
第18页 / 共19页
BP算法的C语言实现.docx_第19页
第19页 / 共19页
亲,该文档总共19页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

BP算法的C语言实现.docx

《BP算法的C语言实现.docx》由会员分享,可在线阅读,更多相关《BP算法的C语言实现.docx(19页珍藏版)》请在冰点文库上搜索。

BP算法的C语言实现.docx

BP算法的C语言实现

终于搞定了,输出结果完全正确,运行训练次数竟然有几万次!

汗!

(适合普通BP算法和改进型BP[带缓冲项]算法)

//训练样本大概是这些(每组前三个为in样本数据后面为out结果):

//0000.9

//0010.1

//0100.1

//0110.9

//1000.1

//1010.9

//1100.9

//1110.1

测试样本可以自己试试这些数据

//00.10.9(期望:

0.1)

//0.90.90.1(期望0.9)

-----------------------------------------分割线-----------------------------------------

#include"stdlib.h"

#include"math.h"

#include"conio.h"

#include"stdio.h"

#include"time.h"

#defineN8/*学习样本个数(测试样本个数)*/

#defineIN3/*输入层神经元数目*/

#defineHN2/*隐层神经元数目*/

#defineON1/*输出层神经元数目*/

floatP[IN];/*单个样本输入数据*/

floatT[ON];/*单个样本教师数据*/

floatW[HN][IN];/*输入层至隐层权值*/

floatV[ON][HN];/*隐层至输出层权值*/

floatX[HN];/*隐层的输入*/

floatY[ON];/*输出层的输入*/

floatH[HN];/*隐层的输出*/

floatO[ON];/*输出层的输出*/

floatYU_HN[HN];/*隐层的阈值*/

floatYU_ON[ON];/*输出层的阈值*/

floaterr_m[N];/*第m个样本的总误差*/

floata;/*输出层至隐层学习效率*/

floatb;/*隐层至输入层学习效率*/

floatalpha;  /*/动量因子,改进型bp算法使用*/

floatd_err[ON];/*δk*/

floate_err[HN];/*δj*/

FILE*fp;

/*定义一个放学习样本的结构*/

struct{

floatinput[IN];

floatteach[ON];

       }Study_Data[N];

/*定义一个放测试样本的结构*/

struct{

floatinput[IN];

floatexpect[ON];

       }Test_Data[N];

/*改进型bp算法用来保存每次计算的权值*/

floatold_W[HN][IN];

floatold_V[ON][HN];

intStart_Show()

{

clrscr();

printf("\n                       ***********************\n");

printf("                       *    Welcometouse   *\n");

printf("                       *  thisprogramof    *\n");

printf("                       *  calculatingtheBP*\n");

printf("                       *      model!

         *\n");

printf("                       *   Happyeveryday!

  *\n");

printf("                       ***********************\n");

printf("\n\nBeforestarting,pleasereadthefollowscarefully:

\n\n");

printf("    TheprogramofBPcanstudyitselffornomorethan200000times.\nAndsurpassingthenumber,theprogramwillbeendedbyitselfin\npreventingrunninginfinitelybecauseoferror!

\n");

printf("\n\n\n");

printf("Nowpressanykeytostart...\n");

getch();

clrscr();

}

intEnd_Show()

{

printf("\n\n---------------------------------------------------\n");

printf("Theprogramhasreachedtheendsuccessfully!

\n\nPressanykeytoexit!

\n\n");

printf("\n                       ***********************\n");

printf("                       *    Thisistheend  *\n");

printf("                       *oftheprogramwhich*\n");

printf("                       *cancalculatetheBP*\n");

printf("                       *      model!

         *\n");

printf("                       ***********************\n");

printf("                       *  Thanksforusing!

  *\n");

printf("                       *   Happyeveryday!

  *\n");

printf("                       ***********************\n");

getch();

exit(0);

}

/*读取训练样本*/

GetTrainingData()

{inti,j,m;

floatdatr;

if((fp=fopen("sample.txt","r"))==NULL)

     {

      printf("Cannotopenfilestrikeanykeyexit!

");

      getch();

      exit

(1);

     }

for(i=0;i

     {j=0;

      while(j!

=(IN+ON)&&fscanf(fp,"%f",&datr)!

=EOF)

       {if(j>IN-1)Study_Data[i].teach[j-IN]=datr;

        else       Study_Data[i].input[j]=datr;

/*printf("\ntheStudy_Data[%d].input[%d]=%f\n%f",i,j,Study_Data[i].input[j],datr);getch();*/

/*usetochecktheloadedtrainingdatas*/

        j++;

       }

     }

fclose(fp);

printf("\nThereare[%d]sampledatasthathavebeenloadedsuccessfully!

\n",N*(IN+ON));

printf("\nShowthedatawhichhasbeenloadedasfollows:

\n");

for(m=0;m

   {for(i=0;i

     {printf("Study_Data[%d].input[%d]=%f         ",m,i,Study_Data[m].input[i]);}

    for(j=0;j

     {printf("Study_Data[%d].teach[%d]=%f         ",m,j,Study_Data[m].teach[j]);}

   }

printf("\n\n\nPressanykeytobeginStudy...");

getch();

clrscr();

return1;

}

/*初始化权、阈值子程序*/

initial()

{inti;

intii;

intj;

intjj;

intk;

intkk;

printf("\nRandsomWeightvalueandBiasvalueasfollow:

\n");

srand(time(NULL));/*随机函数种子*/

printf("\nWeightValue:

\n");

for(i=0;i

  for(j=0;j

/*初始化输入层到隐层的权值,随机模拟0.5~-0.5*/

    printf("\nw[%d][%d]=%f",i,j,W[i][j]);

    }

  }

for(ii=0;ii

  for(jj=0;jj

/*初始化隐层到输出层的权值,随机模拟0.5~-0.5*/

    printf("\nV[%d][%d]=%f",ii,jj,V[ii][jj]);

    }

  }

printf("\n\nBiasValue:

\n");

for(k=0;k

  YU_HN[k]=1.0;

/*隐层阈值初始化,-0.01~0.01之间*/

    printf("\nYU_HN[%d]=%f",k,YU_HN[k]);

    }

for(kk=0;kk

  YU_ON[kk]=1.0;

/*输出层阈值初始化,-0.01~0.01之间*/

    printf("\nYU_ON[%d]=%f\n",kk,YU_ON[kk]);

   }

printf("\n\n\n\n\nPressanykeytostartculculating...:

\n");

getch();

clrscr();

printf("Pleasewait...");

return1;

}

/*第m个学习样本输入子程序*/

input_P(intm)

{inti,j;

  for(i=0;i

  return1;

}

/*第m个样本教师信号子程序*/

input_T(intm)

{intk;

for(k=0;k

return1;

}

/*求净输入,输出*/

IN_OUT()

{

floatsigma1,sigma2;

inti,j,ii,jj;

for(i=0;i

   sigma1=0.0;

   for(j=0;j

   {sigma1+=W[i][j]*P[j];}/*求隐层内积*/

    X[i]=sigma1+YU_HN[i];

    H[i]=1.0/(1.0+exp(-X[i]));

   }

for(ii=0;ii

   sigma2=0.0;

   for(jj=0;jj

   {sigma2+=V[ii][jj]*H[jj];}

    Y[ii]=sigma2+YU_ON[ii];

    O[ii]=1.0/(1.0+exp(-Y[ii]));

   }

return1;

}

/*误差分析*/

/*δk*/

intErr_O_H(intm)

{intk;

floatabs_err[ON];

floatsqr_err=0.0;

for(k=0;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]);

   err_m[m]=sqr_err/2;

  }

return1;

}

/*δj*/

intErr_H_I()

{

intj,k;

floatsigma;

for(j=0;j

  sigma=0.0;

  for(k=0;k

   {sigma+=d_err[k]*V[k][j];}

    e_err[j]=sigma*H[j]*(1-H[j]);

  }

return1;

}

/*总误差*/

floatErr_Sum()

{intm;

floattotal_err=0.0;

for(m=0;m

   {total_err+=err_m[m];}

returntotal_err;

}

/*新旧权值更新量交替--改进型Bp算法*/

saveWV()

{inti;

intii;

intj;

intjj;

for(i=0;i

   for(j=0;j

     {old_W[i][j]=b*e_err[i]*P[j];}

   }

for(ii=0;ii

   for(jj=0;jj

     {old_V[ii][jj]=a*d_err[ii]*H[jj];}

   }

return1;

}

/*更新权值,阈值*/

/*输出层*/

intDelta_O_H(intn,intstudy)

{intk,j;

if((n<=1)||(study=1))

  {

   for(k=0;k

     for(j=0;j

       {V[k][j]=V[k][j]+a*d_err[k]*H[j];}

     YU_ON[k]+=a*d_err[k];

    }

  }

else

  {

   for(k=0;k

     for(j=0;j

       {V[k][j]=V[k][j]+(1.0-alpha)*a*d_err[k]*H[j]+alpha*old_V[k][j];}

     YU_ON[k]+=a*d_err[k];

    }

  }

return1;

}

/*隐层*/

Delta_H_I(intn,intstudy)

{inti,j;

if((n<=1)||(study=1))

{

  for(j=0;j

     for(i=0;i

       {W[j][i]=W[j][i]+b*e_err[j]*P[i];}

     YU_HN[j]+=b*e_err[j];

    }

  }

else

{

  for(j=0;j

    for(i=0;i

      {W[j][i]=W[j][i]+(1.0-alpha)*b*e_err[j]*P[i]+alpha*old_W[j][i];}

    YU_HN[j]+=b*e_err[j];

   }

}

return1;

}

/*保存更新*/

voidsavequan()

{inti,j,k;

  intii,jj,kk;

  /*saveweight*/

  if((fp=fopen("weight.txt","a"))==NULL)

   {

     printf("Cannotopenfilestrikeanykeyexit!

");

     getch();

     exit

(1);

   }

  for(i=0;i

    {for(j=0;j

  fprintf(fp,"\n");

  for(ii=0;ii

    {for(jj=0;jj

  fclose(fp);

  printf("\nTheresultofweight.txt(quanzhi)hasbeensavedsuccessfully!

");

  /*savelimit*/

  if((fp=fopen("limit.txt","a"))==NULL)

   {

     printf("Cannotopenfilestrikeanykeyexit!

");

     getch();

     exit

(1);

   }

  for(k=0;k

  for(kk=0;kk

  fclose(fp);

  printf("\nTheresultoflimit.txt(yuzhi)hasbeensavedsuccessfully!

\n\n\n\n\nPressanykeytoTest...");

  getch();

  clrscr();

}

/*读取测试样本*/

GetTestData()

{inti,j,m;

floatdatr;

if((fp=fopen("test.txt","r"))==NULL)

     {

      printf("Cannotopenfilestrikeanykeyexit!

");

      getch();

      exit

(1);

     }

for(i=0;i

     {j=0;

      while(j!

=(IN+ON)&&fscanf(fp,"%f",&datr)!

=EOF)

       {if(j>IN-1)Test_Data[i].expect[j-IN]=datr;

        else       Test_Data[i].input[j]=datr;

        j++;

       }

     }

fclose(fp);

printf("\nThereare[%d]testdatasthathavebeenloadedsuccessfully!

\n",N*(IN+ON));

printf("\nShowthedatawhichhasbeenloadedasfollows:

\n");

for(m=0;m

   {for(i=0;i

     {printf("Test__Data[%d].input[%d]=%f         ",m,i,Test_Data[m].input[i]);}

    for(j=0;j

     {printf("Test__Data[%d].expec[%d]=%f         ",m,j,Test_Data[m].expect[j]);}

   }

printf("\n\n\n\nPressanykeytoculculating...");

getch();

clrscr();

return1;

}

/*样本测试及结果*/

Test()

{inti,j,k,m;

floatnet1[HN],net2[ON],H_net[HN],O_net[ON];

for(m=0;m

    {for(i=0;i

       {net1[i]=0.0;

        for(j=0;j

           {net1[i]+=Test_Data[m].input[j]*W[i][j];

           }

        net1[i]=net1[i]+YU_HN[i];

        H_net[i]=1.0/(1.0+exp(-net1[i]));

       }

     for(k=0;k

       {net2[k]=0.0;

        for(j=0;j

           {net2[k]+=H_net[j]*V[k][j];

           }

        net2[k]=net2[k]+YU_ON[k];

        O_net[k]=1.0/(1.0+exp(-net2[k]));

        printf("\nTestresult[%d]=%f\n",m,O_net[k]);

       }

    }

}

/*直接输入测试*/

OnlineSet()

{chars;

printf("\n\n\n\nIfyouwanttotestthedatayouinputtednow,pressY(orotherbuttonexceptQ);\nOrpressQtoquitTest!

\n");

while

(1){

      s=getch();

      switch(s)

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2