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

📄 dh_bp.c

📁 有学习神经网络程序及例子 应用效果良好 实际操作简单 用于多方面研究
💻 C
📖 第 1 页 / 共 2 页
字号:
/*this is ann of bp counter program*/ 
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <malloc.h> 
#include <string.h>
/* define constant used throughout function */
#define NMXUNIT  20/*max no of units in a layer*/
#define NMXHLR  5/* max no. of hidden layer*/
#define NMXOATTR 20/*max no. of output feature*/
#define NMXINP 5000/*max no. of input samples*/
#define NMXIATTR 20/*max no. of input features*/
#define SEXIT  3/*exit successfully*/
#define RESTRT 2/*restart */
#define FEXIT 1/*exit in failure*/
#define CONTNE 0/*continue calculation*/
/* Data base:declarations of variables */
float eta;  /** learning rate **/
float alpha; /**momentum rate **/
float err_curr; /** normalized error **/
float maxe; /**max allowed system error **/
float maxep; /**max allowed pattern error**/
float *wtptr[NMXHLR+1];
float *outptr[NMXHLR+2];
float *errptr[NMXHLR+2];
float *delw[NMXHLR+1];
float target[NMXINP][NMXOATTR];
float input[NMXINP][NMXIATTR],ep[NMXINP];
float outpt[NMXINP][NMXOATTR];
int nunit[NMXHLR+2],nhlayer,ninput,ninattr,noutattr;
int result,cnt,cnt_num;
int nsnew,nsold;
char task_name[20];
FILE *fp1,*fp2,*fp3,*fp10,*fp11,*fp12,*fopen();
int fplot10;
  /*random number generator(computer independent)*/
long randseed=568731L;

int random()
{
  randseed=15625l*randseed+22221L;
  return((randseed>>16)& 0x7FFF);
}
	      /*allocate dynamic storage for the net*/
init()
{
  int len1,len2,i,k;
  float *p1,*p2,*p3,*p4;
  len1=len2=0;
  nunit[nhlayer+2]=0;
  for (i=0;i<(nhlayer+2);i++){
    len1 +=(nunit[i]+1)*nunit[i+1];
    len2 +=nunit[i]+1;
    }

    p1=(float *) calloc(len1+1,sizeof(float));
    p2=(float *) calloc(len2+1,sizeof(float));
    p3=(float *) calloc(len2+1,sizeof(float));
    p4=(float *) calloc(len1+1,sizeof(float));
/* set up initial pointers */
    wtptr[0]=p1;
    outptr[0]=p2;
    errptr[0]=p3;
    delw[0]=p4;
/* set up the rest of pointers */
    for (i=1;i<(nhlayer+1);i++){
      wtptr[i]=wtptr[i-1]+nunit[i]*(nunit[i-1]+1);
      delw[i]=delw[i-1]+nunit[i]*(nunit[i-1]+1);
      }
      for (i=1;i<(nhlayer+2);i++){
      outptr[i]=outptr[i-1]+nunit[i-1]+1;
      errptr[i]=errptr[i-1]+nunit[i-1]+1;
      }
/* set up threshold outputs */
      for (i=0;i<nhlayer+1;i++){
	*(outptr[i]+nunit[i])=1.0;
      }
    }


/* initialize weights with random number between
	       -.5 and +.5 */
initwt()
{
  int i,j;
  for(j=0;j<nhlayer+1;j++)
     for(i=0;i<(nunit[j]+1)*nunit[j+1];i++){
     *(wtptr[j]+i)=random()/pow(2.,15.)-.5;
     *(delw[j]+i)=0.;
 }
 }


 set_up()
 {
 int i;
 
 fp10=fopen("bp.ini","r");
 nhlayer=0;

 /* eta=.9;
 printf("\nMomentum rate eta(default=.9)?:");*/
 fscanf(fp10,"%f",&eta);
 /*alpha=.7;
 printf("\nLeaning rate alpha (default=.7)?:");*/
 fscanf(fp10,"%f",&alpha);
 /*maxe=.01,maxep=.001;
 printf("\nMax total error(default=.01)?:");*/
 fscanf(fp10,"%f",&maxe);
 /*printf("\nMax individual error(default=.001)?:");*/
 /*fscanf(fp10,"%f",&maxep);  */
 /*cnt_num=1000;
 printf("\nMax number of iteration(default=1000)?:");*/
 fscanf(fp10,"%d",&cnt_num);
 /*printf("\nNumber of hidden layers?:");*/
 for(i=0;i<5;i++){
  /* printf("\n\nNumber of units for hidden layer %d?:",i+1);*/
    fscanf(fp10,"%d",&nunit[i+1]);
    if(nunit[i+1]!=0) nhlayer=nhlayer+1;
    }
 /*printf("\nCreate error file ?if so type 1,or type 0:");
 scanf("%d",&fplot10);*/
  fplot10=1;
 printf("\nExecution starts请稍候。。。");
 nunit[nhlayer+1]=noutattr;
 nunit[0]=ninattr;
 fclose(fp10);
}


/* read file for net architecture and learning
	  parameters.file name has suffix _v.dat */

  dread (taskname)
  char *taskname;
  {
   int i,j,c;
   char var_file_name[20];
   strcpy(var_file_name,taskname);
   strcat(var_file_name,"_v.dat");
   if((fp1=fopen(var_file_name,"r"))==NULL)
   {
   perror("\n Cannot open data file");
   exit(0);
   }
   fscanf(fp1,"%d %d %d %f %f %d %d\n",&ninput,&noutattr,
	    &ninattr,&eta,&alpha,&nhlayer,&cnt_num);
   for (i=0;i<nhlayer+2;i++)
   {
       fscanf(fp1," %d ",&nunit[i]);
   }
   if((c=fclose(fp1)) != 0)
      printf("\n File cannot be closed %d",c);
  }


/* read file containing weights and thresholds.
      file name has suffix_w.dat */

 wtread (taskname)
 char *taskname;
 {
 int i,j,c;
 char wt_file_name[20];
 strcpy(wt_file_name,taskname);
 strcat(wt_file_name,"_w.dat");
 if((fp2=fopen(wt_file_name,"r"))==NULL)
 {
 perror("\n Cannot open data file");
 exit(0);
 }
 for(i=0;i<nhlayer+1;i++){
  for(j=0;j<(nunit[i]+1)*nunit[i+1];j++){
   fscanf(fp2," %f ",(wtptr[i]+j));
   }
   fscanf(fp2,"\n");
   }
   if((c=fclose(fp2)) != 0)
     printf("\nFile cannot be closed %d",c);
 }


/* create file for net architecture and learning
      paramters .File name has suffix _v.dat */

 dwrite(taskname)
 char *taskname;
 {
  int i,j,c,m;
  char var_file_name[20];
  float max0,min0,max1[10],min1[10];
  strcpy(var_file_name,taskname);
  strcat(var_file_name,"_v.dat");

  fp1=fopen("bpmax","r");
     for(m=0;m<ninattr;m++)  fscanf(fp1,"%f %f",&max0,&min0);
   for(m=0;m<noutattr;m++) fscanf(fp1,"%f %f",&max1[m],&min1[m]);
 fclose(fp1);

 if((fp1=fopen(var_file_name,"w+"))==NULL)
  {
  perror("Cannot open data file");
  exit(0);
  }
  fprintf(fp1,"%d %d %d %f %f %d %d\n",ninput,noutattr,
	      ninattr,eta,alpha,nhlayer,cnt_num);
  for(i=0;i<nhlayer+2;i++){
   fprintf(fp1," %d ",nunit[i]);
  }
  fprintf(fp1,"\n%d %f",cnt,err_curr);

  fprintf(fp1,"\n");
  for(i=0;i<ninput;i++){
   for (j=0;j<noutattr;j++) { outpt[i][j]=min1[j]+outpt[i][j]*(max1[j]-min1[j]);
   fprintf(fp1,"%f  ",outpt[i][j]); }
  fprintf(fp1,"\n");
  }
  if((c=fclose(fp1)) !=0)
    printf("\nFile cannot be closed %d",c);
    }


/* create file for saving weights and thresholds
    lerning from training. file name has suffix
					  _w.dat */
 wtwrite(taskname)
 char *taskname;
 {
 int i,j,c,k;
 char wt_file_name[20];
 strcpy(wt_file_name,taskname);
 strcat(wt_file_name,"_w.dat");
 if((fp2=fopen(wt_file_name,"w+"))==NULL)
 {
 perror("Cannat open data file");
 exit(0);
 }
 k=0;
 for(i=0;i<nhlayer+1;i++)
 {
   for(j=0;j<(nunit[i]+1)*nunit[i+1];j++)
   {
    if(k==8)
     {
     k=0;
     fprintf(fp2,"\n");
     }
    fprintf(fp2," %f ",*(wtptr[i]+j));
    k++;
   }
  }
   if((c=fclose(fp2)) != 0)
    printf("\nFile cannot be closed %d",c);
 }


/* bottom_up calculation of net for input pattern i */

 void forward(i)
 {
   int m,n,p,offset;
   float net;
/* input level output calculation */
   for(m=0;m<ninattr;m++)
    *(outptr[0]+m)=input[i][m];
/* hidden & output layer output calcultion */
  for(m=1;m<nhlayer+2;m++){
   for(n=0;n<nunit[m];n++){
    net=0.0;
    for(p=0;p<nunit[m-1]+1;p++){
     offset=(nunit[m-1]+1)*n+p;
      net += *(wtptr[m-1]+offset)
       *(*(outptr[m-1]+p));
  }
	*(outptr[m]+n)=1/(1+exp(-net));
  }
  }
  for(n=0;n<nunit[nhlayer+1];n++)
     outpt[i][n]=*(outptr[nhlayer+1]+n);
  }


/* several condition are checked to see whether
	leraning should terminate */

 int introspective(nfrom,nto)
 int nfrom;
 int nto;
 {
  int i,flag;
/* reached max iteration ?*/
 if(cnt>=cnt_num)return(FEXIT);
/* error for each pattern small enough?*/
/* nsnew=0;
 flag=1;
 for(i=nfrom;(i<nto) && (flag==1);i++){
   if(ep[i]<=maxep)nsnew++;
    else flag=0;
 }
 if(flag==1) return(SEXIT);    */
/* system total error small enough? */

if(err_curr <=maxe)return(SEXIT);
return(CONTNE);
}


/* threshold is treated as weight of link from
    a virtual node whose output value is unity */

int rumelhart(from_snum,to_snum)
int from_snum;
int to_snum;
{
int i,j,k,m,n,p,offset,index;
float out;
char *err_file="criter.dat";
nsold=0;
cnt=0;
result=CONTNE;
if(fplot10==1)
   if((fp3=fopen(err_file,"w"))==NULL)
   {
   perror("Cannot open error file");
   exit(0);

⌨️ 快捷键说明

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