Code:
#include<conio.h>
#include<graph.h>
#include<stdlib.h>
#include<float.h>
#include<math.h>
#include<stdio.h>
#define n0 4 /* input Layer */
#define n1 6 /* hidden Layer */
#define n2 1 /* output Layer */
struct vedioconfig vc;
float uk,uk_1,yk1,yk,yk_1; /*defined variable used */
float netin0[n0],netin1[n1],netin2[n2];
float bias1[n1],prd1bs1[n1];
float weight1[n1][n0],weight2[n2][n1];
float prd1wt1[n1][n0],prd1wt2[n2][n1];
float actv1[n1],netout;
float coef,motum,rms;
float A1,A2,A3,B1,B2,B3;
unsigned seed_int,seed_u;
int epochs;
int eph_pts=30,rc1_pts=100;
float inwtsc=10.0;
float xxo,yyo;
float timegrph,ukgrph,yk1grph,netoutgrph;
main()
{
char ch,ch1;
_setvideomode(_VRES16COLOR);
_getvideoconfig(&vc);
xxo=60;
yyo=vc.numpixels/2.0;
_setlogirg(xxo,yyo);
A1=1.752821;A2=(-0.818731);A3=0.0;
B1=0.011698; B2=0.010942; B3=0.0;
printf("\nnlearn? (y/n");
ch=getch();
if(ch=='y')
{
printf("\n\nlearning coefficient?");
scanf("%f",&coef);
printf("\nmomentum?");
scanf("%f",&motum);
printf("\nseed_int?");
scanf("%d",&seed_u);
printf("\nepochs of trainning?");
scanf("1u",&epochs);
initialise();
learn();
savewt();
}
printf("\nrecall now?(y/n)");
ch=getche();
if(ch=='y'){
readwt();
recall();
}
printf("\nexit?(y/n)\n");
ch1=getche();
if(ch1='y')
{
_clearscreen(_GCLEARSCREEN);
_setvideomode(_DEFAULTMODE);
}
}
initialise() /*initialize the weights*/
{
int i,j;
srand(seed_int);
for(j=0;j<n1;j++)
{
bias1[j]=(2.0*(float)rand()/RAND_MAX-1.0)/inwtsc;
prd1bs1[j]=0.0;
for(i=0;i<n0;i++)
{
weight1[j][i]=(2.0*(float)rand()/RAND_MAX-1.0)/inwtsc;
prd1wt1[j][i]=0.0;
}
}
for(j=0;j<n2;j++)
{
for(i=0;i<n1;i++)
{
weight2[j][i]=(2.0*(float)rand()/RAND_MAX-1.00)/inwtsc;
prd1wt2[j][i]=0.0;
}
}
}
compout()
{
register int i,j;
float ea,eb;
for(j=0;j<n1;j++)
{
netin1[j]=bias1[j];
for(i=0;i<n0;i++)
netin1[j]+=weight1[j][i]*netin0[i];
ea=(float)exp((double)netin1[j]);
eb=(float)exp((double)((-1.0)*netin1[j]));
actv1[j]=(ea-eb)/(ea+eb);
}
for(j=0;j<n2;j++)
{
netin2[j]=0.0;
for(i=0;i<n1;i++)
netin2[j]+=weight2[j][i]*actv1[i];
netout=netin2[j];
}
}
comptwt()
{
register int i,j;
float error1[n1],error2[n2],sum1[n1];
float de1wt1[n1][n0],de1wt2[n2][n1];
float de1bs1[n1];
for(j=0;j<n2;j++)
{
error2[j]=yk1 - netout;
for(i=0;i<n1;i++)
de1wt2[j][i]=coef*actv1[i]+motum*prd1wt2[j][i];
}
for(j=0;j<n1;j++)
{
sum1[j]=0.0;
for(i=0;i<n2;i++)
sum1[j]+=error2[i]*weight2[i][j];
error1[j]=(1.0+actv1[j])*(1.0 - actv1[j])*sum1[j];
de1bs1[j]=coef*error1[j]+motum*prd1bs1[j];
for(i=0;i<n0;i++)
de1wt1[j][i]=coef*error1[j]*netin0[i]+motum*prd1wt1[j][i];
}
for(j=0;j<n1;j++)
{
bias1[j]+=de1bs1[j];
prd1bs1[j]=de1bs1[j];
for(i=0;i<n0;i++)
{
weight1[j][i]+=de1wt1[j][i];
prd1wt1[j][i]=de1wt1[j][i];
}
}
for(j=0;j<n2;j++)
{
for(i=0;i<n1;i++)
{
weight2[j][i]+=de1wt2[j][i];
prd1wt2[j][i]=de1wt2[j][i];
}
}
}
learn()
{
FILE *fptr;
register int k;
int i;
srand(seed_u);
for(k=0;k<epochs;k++)
{
_clearscreen(_GCLEARSCREEN);
printf("%d",k);
_setcolor(4);
_rectangle(_GBORDER,0,200,eph_pts,200);
printf("\nrms=%f",(float)sqrt((double)rms/eph_pts));
yk=yk_1=0.0;uk_1=0.0;
timegrph=0.0;rms=0.0;
srand(seed_u);
for(i=0;i<eph_pts;i++)
{
uk=(2.0*(float)rand()/RAND_MAX-1.0)*2.911162;
netin0[0]=yk;
netin0[1]=yk_1;
netin0[2]=uk;
netin0[3]=uk_1;
comptout();
yk1=A1*yk+A2*yk_1+A3*yk_1*yk_1+B1*uk+B2*uk_1+B3*uk*uk;
yk1grph=(-1.0)*yk1*100.0;
_setcolor(2);
_setpixel(timegrph,yk1grph);
netoutgrph=(-1.0)*netout*100.0;
_setcolor(7);
_setpixel(timegrph,netoutgrph);
comptwt();
rms+=(yk1 - netout)*(yk1 - netout);
yk_1=yk;yk=yk1;
uk-1=uk;
timegrph+=1.0;
}
}
}
recall() /*test net*/
{
FILE *fptr;
int i,k;
float basegrph,scalegrph;
_clearscreen(_GCLEARSCREEN);
_sector(4);
_rectangle(_GBORDER,0,200,eph_pts,-200);
srand(seed_u);
yk=yk_1=0.0;uk_1=0.0;
timegrph=0.0;rms=0.0;
for(k=0;k<rc1_pts;k++)
{
uk=2.911162*sin(2.0*3.14159*k/15.0);
netin0[0]=yk;
netin0[1]=yk_1;
netin0[2]=uk;
netin0[3]=uk_1;
comptout();
timegrph=k*(float)eph_pts/rc1_pts;
yk1=A1*yk+A2*yk_1+A3*yk_1+B1*uk+B2*uk_1+B3*uk*uk;
rms+=(yk1-netout)*(yk1 - netout);
yk1grph=(-1.0)*yk1*100.0;
_setcolor(2);
_setpixel(timegrph,yk1grph);
basegrph=0.0;
_setcolor(14);
_setpixel(timegrph+2.0,basegrph);
netoutgrph=(-1.0)*netout*100.0;
_setcolor(7);
_setpixel(timegrph,netoutgrph);
yk-1=yk;
yk=yk1;
uk_1=uk;
}
rms=(float)sqrt((double)rms/rc1_pts);
printf("rms_recall=%f\n",rms);
}
savewt() /*save weight*/
{
int i,j;
FILE *fptr;
fptr=fopen("wt.fil","w");
for(i=0;i<n1;i++)
{
for(j=0;j<n1;j++)
{
for(i=0;i<n0;i++)
fprintf(fptr,"%f",weight1[j][i]);
fprintf(fptr,"\n");
}
for(j=0;j<n2;j++)
{
for(i=0;i<n1;i++)
fprintf(fptr,"%f",weight2[j][i]);
fprintf(fptr,"\n");
}
fclose(fptr);
}
readwt()
{
int i,j;
FILE *fptr;
fptr=fopen("wt.fil","r");
for(i=0;i<n1;i++)
fscanf(fptr,"%f",&bias1[i]);
for(j=0;j<n1;j++)
for(i=0;i<n0;i++)
fscanf(fptr,&weight2[j][i]);
for(j=0;j<n2;j++)
for(i=0;i<n1;i++)
fscanf(fptr,"%f",&weight2[j][i]);
fclose(fptr);
}
Few compilation errors with number is shown here:::