/******************************************************************************* Title : Linear Network (Classification) Inputs : 1. Input File Containing Training Data (Infile) 2. No. of Inputs (N) 3. Possible No. of Classes (Nc) Output : classification error % Notations Used: Nv --> No. of patterns N --> No. of inputs Nout --> No. of outputs X --> Input Vector Xa --> Augmented input vector Wo --> Weight vector connecting input to the output to --> Output threshold ty --> Desired output y --> Actual output R --> Auto correlation Matrix C --> Cross correlation matrix dstd --> desired mean dstd --> desired standard deviation ******************************************************************************** ! IMPORTANT: Significant changes have been made to the program. Have a thorough look at it. *******************************************************************************/ #include #include #include #include #define PI 3.1415926 #define dmean 0.0 #define dstd 0.5 int IX=3; int IY=4009; int IZ=234; char Infile[14]; void main(void) { int i, j; int N, Nc, Nv; float *X, *Xa, *ty; float **Wo,*to, **W, **R, **C; int ClassId; FILE *ifs; void CGrad(int ,float * ,float **,float *); void error(float **, int, int); void classification_error(float **, int, int); float slete(float, float); printf("Enter Input data file : "); gets(Infile); printf("Enter No. of Inputs, N : "); scanf("%d",&N); printf("Enter Possible no. of classes, Nc : "); scanf("%d",&Nc); ifs=fopen(Infile,"r"); if (ifs == NULL ) { perror(Infile); exit(1); } /* Allocating memory dynamically for the variables used */ X= (float *)malloc(sizeof(float)*(N) ); Xa= (float *)malloc(sizeof(float)*(N+1) ); ty= (float *)malloc(sizeof(float)*Nc ); W= (float **)malloc(sizeof(float*)*Nc); Wo= (float **)malloc(sizeof(float*)*Nc); to= (float *)malloc(sizeof(float)*Nc ); R= (float **)malloc(sizeof(float*)*(N+1)); C= (float **)malloc(sizeof(float*)*Nc); for (i=0;i Weight vector containing Wo and to */ for (i=0;i numerator of B2 */ Num += p[l]*g[l]/-2.0; /* Den --> denominator of B2 */ for(m=0;mMax_y) { Max_y= y[i]; Class_obtained= i+1; } } /* Checking whether the obtained class is equal to the desired class or not. if they are not equal it means that an error has occurred. so we increment the classification error count by 1 */ if ( ClassId != Class_obtained ) { Class_err += 1; } } /* calculating classification error% */ percentage_error=((float)Class_err/(float)Nv)*100.0; printf("\nclassification error = %d\n",Class_err); printf("\nclassification error percentage = %f \n",percentage_error); fclose(ifsc); } // end of classification error subroutine /****************************************************************************** Subroutine : Random no. Generator -the random nos. generated are uniformly distributed between 0 and 1. -IX, IY, IZ are the SEEDS *******************************************************************************/ float rand1(int *ix, int *iy, int *iz) { int ixx, iyy, izz; float itemp; float temp; ixx=(*ix)/177; *ix=171*(*ix%177)-2*ixx; if(*ix < 0) *ix+=30269; iyy=(*iy)/176; *iy=176*(*iy%176)-2*iyy; if(*iy < 0) *iy+=30307; izz=(*iz)/178; *iz=170*(*iz%178)-2*izz; if(*iz < 0) *iz+=30323; temp=(float)(*ix)/30629.0+(float)(*iy)/30307.0+(float)(*iz)/30323.0; itemp=floor(temp); return (temp-itemp); } /* Gaussian Random No. generator */ float slete(float std, float xmean) { float rand1(int *, int *, int *); return (xmean+std*cos(2*PI*rand1(&IX, &IY, &IZ))*sqrt(-2.0*log(rand1(&IX, &IY, &IZ)))); }