bp neural network algorithm #include <stdio.h> #include <math.h> #include <conio.h> #include <stdlib.h> # Define NH 3/* input layer/#define NI 4/* hidden Layer/#define NJ 1/* Output layer/#define NK 100/* Sample capacity/#define TEST 70/* Test SET capacity */#define NR 0.7/* Learning efficiency * * #define EPS 0.00001 float X[NK][NH],D[NK][NJ],WHI[NH][NI],WIJ[NI][NJ],THI[NI],THJ[NJ]; int h,i,j,k,ff; float XMIN[NH],XMAX[NH],DMIN[NJ],DMAX[NJ]; FILE *FP1,*FP2,*FP3,*FP4; void init (void); void startleaning (void); void testsample (void); void Readw (void); void Readt (void); void Writew (void); Float sigmoid (float a); Double Ranu (void); Char filename1[]={"Samplefile.txt"}; void init (void) {int min,max; if (fp1==0) {System ("CLS"); printf ("Can not find the Learning sample file!/n"); exit (0);} for (k=0;k<nk;k++) {for (h=0;h<nh;h++) fscanf (FP1, "%f,", &x[k][h]);//neural network input for (j=0;j<nj;j++) fscanf (FP1 , "%f,", &d[k][j]); Neural Network output} for (h=0;h<nh;h++) {min=1; Max=1 for (k=0;k<nk;k++) {if (x[k][h]<x[min][h)) min=k; if (x[k][h]>x[MAX][H]) max=k; } Xmin[h]=x[min][h]; XMAX[H]=X[MAX][H]; for (k=0;k<nk;k++)/* Neural network input normalized */x[k][h]= (X[K][H]-XMIN[H))/(XMAX[H]-XMIN[H); /*x[k][h]=x[k][h]/xmax[h];*/} for (j=0;j<nj;j++) {min=1;max=1, for (k=0;k<nk;k++) {if (d[k][j]<d[min][j)) min =k; if (D[k][j]>d[max][j]) max=k; } Dmin[j]=d[min][j]; DMAX[J]=D[MAX][J]; for (k=0;k<nk;k++)/* Neural network output normalized */d[k][j]= (D[K][J]-DMIN[J))/(DMAX[J]-DMIN[J); /*d[k][j]=d[k][j]/dmax[j];*/}}/*----------------------------------------------------/void startlearning (void) { long int nt,n; float T,ERROR[NK],GERROR,XJ[NJ],XI[NI],YJ[NJ],YI[NI],PXI[NI],PXJ[NJ]; float u0=0,u1=0,u2=0,u3=0; float V0,v1,v2,v3; for (i=0;i<ni;i++) {for (h=0;h<nh;h++) Whi[h][i]=-0.8+1.6*ranu (), for (j=0;j<nj;j++) Wij[i][j]=-0.8+1.6*ranu (); Thi[i]=-0.5+ranu (); for (j=0;j<nj;j++) Thj[j]=-0.5+ranu (); Fp2=fopen ("W.txt", "w+"); /* weights, threshold initialization/////* Learning started * * printf ("/t/nplease Enter the Learning times:/n"); scanf ("%ld", &nt); for (n=0;n<nt;n++)/* Learning times * * gerror=0; for (k=0;k<nk;k++)/* Single sample loop/{for (i=0;i<ni;i++) {t=0, for (h=0;h<nh;h++) t+=whi[h][i]*x[k][h]; Xi[i]=t+thi[i] ; Yi[i]=sigmoid (Xi[i]); /* Hidden layer Output/} for (j=0;j<nj;j++) {t=0; for (i=0;i<ni;i++) t+=wij[i][j]*yi[i]; xj[j]=t+thj[j]; Yj[j]=sigmoid (xj[j)); /* Output layer Output/for (j=0;j<nj;j++)/* Output layer Single sample point error rate * * * pxj[j]=yj[j]* (1-YJ[J)) * (Yj[j]-d[k][j]); for (i=0;i<ni;i++)/* Hidden layer Single sample point error change rate/{t=0; for (j=0;j<nj;j++) t+=pxj[j]*wij[i][j]; pxi[i]=yi[i]* (1-yi[i)) *t; j=0;j<nj;j++) {//THJ[J]=THJ[J]-NR*PXJ[J]; v0=thj[j]; thj[j]=thj[j]-0.7*nr*pxj[j]+0.3*u0;//THJ[J]=THJ[J]-NR*PXJ [j]; U0=thj[j]-v0; for (i=0;i<ni;i++) {v1=wij[i][j]; wij[i][j]=wij[i][j]-0.7*nr*pxj[j]*yi[i]+0.3*u1; u1=wij[i][j]-v1;//wij[i][j]= Wij[i][j]-nr*pxj[j]*yi[i]; /* Hidden layer to the output layer weight correction, which nr for the Step/}} for (i=0;i<ni;i++) {v2=thi[i]; thi[i]=thi[i]-0.7*nr*pxi[i]+0.3*u2; u2=thi[i]-v2;//Thi[i ]=thi[i]-nr*pxi[i]; for (h=0;h<nh;h++) {v3=whi[h][i]; whi[h][i]=whi[h][i]-0.7*nr*pxi[i]*x[k][h]+0.3*u3 u3=whi[h][i]-v3;//whi[h][i]=WHI[H][I]-NR*PXI[I]*X[K][H]; /* Input layer to the hidden layer weight correction, which nr for the Step/}} t=0; for (j=0;j<nj;j++) t+= (Yj[j]-d[k][j]) * (Yj[j]-d[k][j])/2.0; error[k]=t; GERROR+=ERROR[K]; /* Global error G (Lobal) error*/}/* Single sample Loop end/if (gerror<eps) break; /* Learning cycle End/printf ("%f,%f,%f,%f,%f,%f", thi[0],thj[0],wij[0][0],wij[0][1],whi[1][0],whi[0][1]); Writew (); printf ("/t/nglobal error=%f/n", gerror); printf ("/t/nare You satisfied with the global error?/n"); printf ("Press any key to choose a next task!/n"); Getch (); }/*-------------------------------------------------/void testsample (void) {float TX[NH],T,XJ[NJ],XI[NI],YJ[NJ], Yi[ni]; if (fp2==0) {//CLRSCR (); printf ("/t/ncan not find the weight file:w.txt/n"); exit (0);} readw (); for (ff=0;ff<test;ff++)/* Test sample forecast, result save/{for (h=0;h<nh;h++) fscanf (FP3, "%f,", &tx[h]); for (h=0;h<nh;h++) Tx[h]= (Tx[h]-xmin[h])/(Xmax[h]-xmin[h]); /* Normalized process */for (i=0;i<ni;i++) {t=0; for (h=0;h<nh;h++) t+=whi[h][i]*tx[h]; xi[i]=t+thi[i]; Yi[i]=sigmoid (xi[i)); for (j=0;j<nj;j++) {t=0; for (i=0;i<ni;i++) t+=wij[i][j]*yi[i]; XJ[J]=T+THJ[J]; Yj[j]=sigmoid (Xj[j]); }//printf ("/t/nnetwork output:/n"); /* Forward Forecast */for (j=0;j<nj;j++)/* Save result/{yj[j]=yj[j]* (Dmax[j]-dmin[j]) +dmin[j]; fprintf (FP4, "%f/n", Yj[j]);}//tx[ NH-1]=YJ[0]; Rewind (FP4); for (h=0;h<nh;h++)/* Test sample input///FSCANF (FP3, "%f,", &tx[h]); printf ("/t/nare you satisfied with the output?/n"); printf ("Press any key to choose a next task!/n"); Getch (); }/*----------------------------------------------/void Writew (void) {rewind (FP2); for (h=0;h<nh;h++) {for (i=0;i <ni;i++) fprintf (FP2, "%8.3f", Whi[h][i]); fprintf (FP2, "n"); } fprintf (FP2, "n"); for (i=0;i<ni;i++) fprintf (FP2, "%8.3f", Thi[i)); fprintf (FP2, "/n/n"); for (j=0;j<nj;j++) {for (i=0;i<ni;i++) fprintf (FP2, "%8.3f", Wij[i][j]); fprintf (FP2, "n"); } fprintf (FP2, "n"); for (j=0;j<nj;j++) fprintf (FP2, "%8.3f", Thj[j]); /*------------------------------------------------/void Readw (void) {for (h=0;h<nh;h++) for (i=0;i<ni;i++) fscanf (FP2, "%f", &whi[h][i]); for (i=0;i<ni;i++) fscanf (FP2, "%f", &thi[i)); for (j=0;j<nj;j++) for (i=0;i<ni;i++) fscanf (FP2, "%f", &wij[i][j]); for (j=0;j<nj;j++) fscanf (FP2, "%f", &thj[j)); }/*--------------------------------/float sigmoid (float a) {return (1.0/1+exp (a));}/*------------------------- -------*/Double Ranu (void) {static double xrand=3.0; double m=8589934592.0, a=30517578125.0; Lp:xrand=fmod (xrand*a,m); /* Divide to take surplus/if (xrand>1.0) return (xrand/m); else {xrand=1.0; goto LP;}} /*----------------------------------/Void Main () {Fp1=fopen ("Sample.txt", "R"); Fp2=fopen ("W.txt", "r+"); Fp3=fopen ("Test.txt", "r+"); Fp4=fopen ("Testre.txt", "r+"); Init (); while (1) {System ("CLS"), printf ("/t/n Please choose a next task.../n/n"), printf ("/t/n (S) to start learning./n"); printf ("/t/n (t) to test samples./n"); printf ("/t/n (R) to resume learning./n"); printf ("/t/n (Q) quit./n"); Switch (GetChar ()) {case ' s ': startlearning (), break, Case ' t ': TestsamplE (); break; Case ' R ': startlearning (); Case ' Q ': Exit (0); } fclose (FP1); Fclose (FP2); Fclose (FP3); Fclose (FP4); Getch (); }/*----------------by ahzhming@163.com-------------------------*