Realization of C language in BP neural network

Source: Internet
Author: User
Tags rand
#include "stdio.h"
#include "Stdlib.h"
#include "time.h"
#include "math.h"
/*********************************************
Inpoints is the number of input neurons that can be changed
Outpoints is the number of output neurons
Defaultpoints is the number of neurons in the hidden layer
Datagrough is the number of sample data
**********************************************


The following data definitions can be modified *****/
#define A 0
#define a 1
#define B 1
#define C 1
#define ALFA 0.85
#define BETA 0.2//Learning rate 0~1
#define TOTAL 20000
#define Inpoints 9
#define OUTPOINTS 5
#define Defaultpoints 28
#define Datagrough 44
#define FORECASTDATA 4


/********** define the desired variable ********/
Double inpointdata[datagrough][inpoints],outpointdata[datagrough][outpoints]; /* Input and OUTPUT data * *
Double inpointdata_max[inpoints],inpointdata_min[inpoints]; /* Maximum data per factor * *
Double outpointdata_max[outpoints],outpointdata_min[outpoints]; /* Minimum data per factor * *
Double w[defaultpoints][inpoints],limen[defaultpoints],v[outpoints][defaultpoints]; /* Connection weight, threshold value * *
Double dlta_w[defaultpoints][inpoints],dlta_limen[defaultpoints],dlta_v[outpoints][defaultpoints]; /* Join Right, threshold correction value * *
Double Defaultoutpoint[defaultpoints],outpoint_dp[outpoints],outpoint_ep[datagrough];
/************************** Read Data file ******************************/
void ReadData ()
{
FILE *FP1,*FP2;
int i,j;
if ((Fp1=fopen ("d:\\data\\ training input. txt", "R")) ==null)
{
printf ("1can not open the file\n");
Exit (0);
}
for (i=0;i<datagrough;i++)
for (j=0;j<inpoints;j++)
FSCANF (FP1, "%lf", &inpointdata[i][j]);
Fclose (FP1);


if ((Fp2=fopen ("d:\\data\\ training output. txt", "R")) ==null)
{
printf ("2can not open the file\n");
Exit (0);
}
for (i=0;i<datagrough;i++)
for (j=0;j<outpoints;j++)
FSCANF (FP2, "%lf", &outpointdata[i][j]);
Fclose (FP2);
}
/*****************************************************/


Normalized ******************************************************/of/*****************************************
void Unitary ()
{
int i,j;
int k=0;
for (j=0;j<inpoints;j++)//Find the maximum and minimum values for each column stored in the array inpointdata_max[j], Inpointdata_min[j]
{
INPOINTDATA_MAX[J]=INPOINTDATA[0][J];
INPOINTDATA_MIN[J]=INPOINTDATA[0][J];
for (i=0;i<datagrough;i++)
if (Inpointdata_max[j]<inpointdata[i][j])
INPOINTDATA_MAX[J]=INPOINTDATA[I][J];
else if (Inpointdata_min[j]>inpointdata[i][j])
INPOINTDATA_MIN[J]=INPOINTDATA[I][J];
}


for (j=0;j<outpoints;j++)//Find the maximum and minimum values for each column stored in the array outpointdata_max[j], Outpointdata_min[j]
{
OUTPOINTDATA_MAX[J]=OUTPOINTDATA[0][J];
OUTPOINTDATA_MIN[J]=OUTPOINTDATA[0][J];
for (i=0;i<datagrough;i++)
if (Outpointdata_max[j]<outpointdata[i][j])
OUTPOINTDATA_MAX[J]=OUTPOINTDATA[I][J];
else if (Outpointdata_min[j]>outpointdata[i][j])
OUTPOINTDATA_MIN[J]=OUTPOINTDATA[I][J];
}
/*************** data is processed, and the data after processing is all within [0,1] *************************/
for (j=0;j<inpoints;j++)
for (i=0;i<datagrough;i++)
if (inpointdata_max[j]==0)
inpointdata[i][j]=0;
Else
inpointdata[i][j]= (inpointdata[i][j]-inpointdata_min[j]+a)/(Inpointdata_max[j]-inpointdata_min[j]+a);


for (j=0;j<outpoints;j++)
for (i=0;i<datagrough;i++)
if (outpointdata_max[j]==0)
outpointdata[i][j]=0;
Else
outpointdata[i][j]= (outpointdata[i][j]-outpointdata_min[j]+a)/(Outpointdata_max[j]-outpointdata_min[j]+a);


}
/*****************************************************/


/********************* initialization, random assignment of initial value **************************/
void initialization ()
{
int i,j;
Srand ((unsigned) time (NULL)); Header filename #include <time.h>


for (i=0;i<defaultpoints;i++)//The connection right to the input layer to the hidden layer is assigned a random value lianjie_w[i][j], these values are in [0,1]
for (j=0;j<inpoints;j++)
{
W[i][j]= (rand () *2.0/rand_max-1)/2;
dlta_w[i][j]=0;
}


for (i=0;i<defaultpoints;i++)
{
Limen[i]= (rand () *2.0/rand_max-1)/2;
dlta_limen[i]=0;
}


for (i=0;i<outpoints;i++)//To assign initial value to the connection right of the hidden layer to the output layer
for (j=0;j<defaultpoints;j++)
{
V[i][j]= (rand () *2.0/rand_max-1)/2;
dlta_v[i][j]=0;
}
}
/********************** calculation output error *******************************/of single sample
void out_sub1 (int t)
{
int i,j;
Double defaultinpoint[defaultpoints];
Double outpoint_y[outpoints];
outpoint_ep[t]=0;
for (i=0;i<defaultpoints;i++)
{
Double sum=0;
for (j=0;j<inpoints;j++)
SUM+=W[I][J]*INPOINTDATA[T][J];
Defaultinpoint[i]=sum+limen[i];
Defaultoutpoint[i]=1/(A+b*exp ( -1*c*defaultinpoint[i));/to O[i]
}


for (j=0;j<outpoints;j++)//Seek y[i]
{
outpoint_y[j]=0;
for (i=0;i<defaultpoints;i++)
Outpoint_y[j]+=v[j][i]*defaultoutpoint[i];
OUTPOINT_DP[J]=OUTPOINTDATA[T][J]-OUTPOINT_Y[J];
OUTPOINT_EP[T]+=OUTPOINT_DP[J]*OUTPOINT_DP[J]/2;
}
}
/***************************** Inverse calculation Weight value ******************************************/
void out_sub2 (int t)
{
int i,j,k;
Double S;
for (i=0;i<defaultpoints;i++)
{
s=0;
for (j=0;j<outpoints;j++)
{
Dlta_v[j][i]=alfa*dlta_v[j][i]+beta*outpoint_dp[j]*defaultoutpoint[i]; //
S+=V[J][I]*OUTPOINT_DP[J];
V[j][i]+=dlta_v[j][i];
}
dlta_limen[i]=alfa*dlta_limen[i]+beta*defaultoutpoint[i]* (1-defaultoutpoint[i]) *s;//
Limen[i]+=dlta_limen[i];
for (k=0;k<inpoints;k++)
{
dlta_w[i][k]=alfa*dlta_w[i][k]+beta*defaultoutpoint[i]* (1-defaultoutpoint[i]) *s*inpointdata[t][k];//
W[I][K]=W[I][K]+DLTA_W[I][K];
}
}
}
/*******************************************************/
void Forecast ()
{
int i,j,t,k=0;
Double e,e1[forecastdata]={0}; Training error
Double SSS;
Double inputdata_x[forecastdata][inpoints],tp[forecastdata][outpoints];
Double definpoint,defoutpoint[defaultpoints],y[forecastdata][outpoints];//y[forecastdata][outpoints] for network test output
FILE *FP1,*FP3;
if ((Fp1=fopen ("d:\\data\\ predictive input. txt", "R") ==null)//Check data entry
{
printf ("3can not open the file\n");
Exit (0);
}
for (i=0;i<forecastdata;i++)
for (j=0;j<inpoints;j++)
FSCANF (FP1, "%lf", &inputdata_x[i][j]);
Fclose (FP1);


if ((Fp3=fopen ("d:\\data\\ predictive output. txt", "R")) ==null)//actual test result output
{
printf ("31can not open the file\n");
Exit (0);
}
for (i=0;i<forecastdata;i++)
for (j=0;j<outpoints;j++)
FSCANF (FP3, "%lf", &tp[i][j]);
Fclose (FP3);


for (j=0;j<inpoints;j++)//Check Data normalization
for (i=0;i<forecastdata;i++)
if (inpointdata_max[j]==0)
inputdata_x[i][j]=0;
Else
inputdata_x[i][j]= (inputdata_x[i][j]-inpointdata_min[j]+a)/(Inpointdata_max[j]-inpointdata_min[j]+a);


for (j=0;j<outpoints;j++)
for (i=0;i<forecastdata;i++)
if (outpointdata_max[j]==0)
tp[i][j]=0;
Else
tp[i][j]= (tp[i][j]-outpointdata_min[j]+a)/(Outpointdata_max[j]-outpointdata_min[j]+a);


Todo
{
Initialization (); Initialize connection weights W[i][j],limen[i],v[k][i]
k=0;
Todo
{
e=0;
for (t=0;t<datagrough;t++)
{
Out_sub1 (t); Forward Computing Network output
OUT_SUB2 (t); Inverse calculation, correction weight value
E+=OUTPOINT_EP[T]; Calculate output error
}
k++;
}while ((k<total) && (e>0.1));
sss=0; Intermediate parameters
for (t=0;t<forecastdata;t++)
{
e1[t]=0;
for (i=0;i<defaultpoints;i++)
{
Double sum=0;
for (j=0;j<inpoints;j++)
SUM+=W[I][J]*INPUTDATA_X[T][J];
Definpoint=sum+limen[i];
Defoutpoint[i]=1/(A+b*exp ( -1*c*definpoint));
}
for (j=0;j<outpoints;j++)
{
y[t][j]=0;
for (i=0;i<defaultpoints;i++)
Y[t][j]+=v[j][i]*defoutpoint[i];
e1[t]+= (Y[t][j]-tp[t][j]) * (Y[t][j]-tp[t][j])/2;
y[t][j]=y[t][j]* (outpointdata_max[j]-outpointdata_min[j]+a) +outpointdata_min[j]-a;
}
SSS+=E1[T];
}
Sss=sss/forecastdata;
printf ("%lf%lf\n", e,sss);
}while (sss>0.12);
}
/********************************************************/




void Main ()
{
int i,j,k;
FILE *FP2;
ReadData (); Read training data: input and output
Unitary (); Normalized, the input output data is normalized, and the result is in [0,1]
Forecast (); Inspection error
if ((Fp2=fopen ("d:\\data\\. txt", "w")) ==null)//File output training good weights
{
printf ("6can not open the file\n");
Exit (0);
}
for (i=0;i<defaultpoints;i++)
{
for (k=0;k<inpoints;k++)
fprintf (FP2, "%lf", W[i][k]);
fprintf (FP2, "\ n");
}
fprintf (FP2, "\ n");
for (i=0;i<defaultpoints;i++)
fprintf (FP2, "%lf", Limen[i]);
fprintf (FP2, "\ n");
for (i=0;i<defaultpoints;i++)
{
for (j=0;j<outpoints;j++)
fprintf (FP2, "%lf", V[j][i]);
fprintf (FP2, "\ n");
}
Fclose (FP2);

}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.