#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "mlp_inter.h" #include "../ntuple/str.h" extern void ku_alfa( void ); extern char * ku_getc( void ); extern char * ku_getf( void ); extern int ku_geti( void ); extern char * ku_gets( void ); extern char * ku_getl( void ); extern int ku_npar( void ); extern char * ku_path( void ); extern double ku_getr( void ); extern char * ku_proc( const char* prompt, const char* dfault ); extern int ku_exec( const char* cmd ); /* global variables */ #define NLMAX 4 int nepoch = 0; int NepochTot = 0; int lprint = 0; int Npat[2]={0,0},Ninput[2],Noutput[2],Pond[2]; int netdefined = 0; int learndefined = 0; int examplesdefined = 0; char chinputs[1024] = {'\0'}; int Nweights = 0; int Nlayer = 0, Nneur[NLMAX]; int LearnReset = 50, LearnMethod = 6; float LearnPar = 0.1, LearnDecay = 0.992, LearnMomentum = 0.1; float LearnFlatSpot = 0, LearnTau = 1.5; float LearnLambda = 1; /* "subroutines" */ void MLP_Error(char *s1, char *s2); void MLP_PatSet(int ifile, char *ch, int *idn); void MLP_prpawf(char *chinput, char *filename, int idn); void MLP_WritePat(int ifile, char *filename); /****************************************************************/ /* mlppawc */ /* */ /* main routine for MLP commands within PAW */ /* */ /* J.Schwindling 18-MAY-99 */ /* commands implemented are: MLP/CREATE */ /* MLP/RESET */ /* MLP/LEARN */ /* MLP/LMET */ /* MLP/WEIGHTS/WRITE */ /* MLP/WEIGHTS/READ */ /* MLP/L(T)PAT/SET */ /* MLP/L(T)PAT/READ */ /* MLP/L(T)PAT/WRITE */ /* */ /* Modified by J.Schwindling 09-JUN-99 MLP/STATUS */ /* J.Schwindling 22-NOV-99 0 hidden layer */ /* */ /****************************************************************/ void mlppawc() { char *cmd_path; char *cmd_string; char *cmd_char; char *filename; char s[1024]; int i, j, nr; static int idn; char *cc[NLMAX]; float rmax, rmin, rmaxdep, redfact=0.98; float err, rmax1, rmax2, rmin1, rmin2, x[2], y[2], par1, par2, par3; float *error, *error2, *xx; int idummy, idummy1, idummy2, idummy3, ifile, iepoch, iiepoch, lastupd; int lplot; int one = 1; int zero = 0; cmd_path = str_new( ku_path() ); cmd_string = strrchr( cmd_path, '/' ); if ( cmd_string != 0 ) { cmd_string += 1; } else { cmd_string = cmd_path; } if(strcasecmp(cmd_string,"CREATE") == 0) /* MLP/CREATE */ { Nlayer = 0; Nweights = 0; Nneur[0] = ku_geti(); Nneur[1] = ku_geti(); Nneur[2] = ku_geti(); Nneur[3] = ku_geti(); Nlayer = 4; if(Nneur[2] == 0) { Nlayer = 3; Nneur[2] = Nneur[3]; } if(Nneur[1] == 0) { Nlayer = 2; Nneur[1] = Nneur[3]; } Nweights = 0; for(i=1;i0) { ifile = 0; err = sqrt (mlptest_(&ifile)/(float) Npat[0]); rmax1 = 1.02*err; rmin1 = 0.98*err; rmax = rmax1; rmin = rmin1; if(Npat[1]!=0) { err = error2[iepoch]; rmax2 = 1.02*err; rmin2 = 0.98*err; if(rmax2 > rmax) rmax = rmax2; if(rmin2 < rmin) rmin = rmin2; } HMAXIM(2000000,rmax); HMINIM(2000000,rmin); HPLOT(2000000," "," ",0); rmaxdep = rmax; } /* change scales of plot */ if(error[iepoch]0) { if(lastupd==iepoch-1) { redfact = redfact*redfact; } lastupd = iepoch; rmin = error[iepoch]*redfact; HMINIM(2000000,rmin); HPLOT(2000000," "," ",0); for(j=1;j<=iepoch+1;j++) { xx[j-1] = (float) j; } IPL(iepoch,xx,error); if(Npat[1]!=0) { ISLN(2); IPL(iepoch,xx,error2); ISLN(1); } IUWK(0,2); } if(Npat[1]!=0 && lplot>0) { if(error2[iepoch]0 && lplot>0) { x[0] = (float) (iepoch); x[1] = (float) (iepoch+1); y[0] = error[iepoch-1]; y[1] = error[iepoch]; IPL(2,x,y); if(Npat[1]!=0) { y[0] = error2[iepoch-1]; y[1] = error2[iepoch]; ISLN(2); IPL(2,x,y); ISLN(1); } IUWK(0,2); } if(lprint != 0 || iepoch==nepoch-1) /* print error on screen */ { if(Npat[1]==0) { printf("Iepoch %d \t Error %f\n", iepoch+1,error[iepoch]); } else { printf("Iepoch %d \t Error %f \t test %f\n", iepoch+1,error[iepoch], error2[iepoch]); } } } MLP_prpawf(chinputs,filename,idn); HMAXIM(2000000,rmaxdep); HPAK(2000000,error); if(Npat[1]!=0) { HPAK(2000001,error2); } free(error); free(error2); free(xx); } else if(strcasecmp(cmd_path,"/MLP/LMET") == 0) { LearnMethod = ku_geti(); par1 = (float) ku_getr(); par2 = (float) ku_getr(); par3 = (float) ku_getr(); LearnFlatSpot = 0.; if(LearnMethod == 1 || LearnMethod == 2) /* stochastic or fixed step steepest descent */ { if(par1<0) { LearnPar = 0.1; } else { LearnPar = par1; } if(par2<0) { LearnMomentum = 0; } else { LearnMomentum = par2; } if(par3<0) { LearnDecay = 1; } else { LearnDecay = par3; } LearnReset = 1000; LearnTau = 3; LearnLambda = 1; } else { if(par1<0) { LearnReset =1000; } else { LearnReset = (int) par1; } if(par2<0) { LearnTau = 1.5; } else { LearnTau = par2; } if(par3<0) { LearnLambda = 1; } else { LearnLambda = par3; } LearnPar = 0.1; LearnDecay = 1; LearnMomentum = 0; } mlp_setlearn(LearnMethod,LearnPar,LearnDecay,LearnMomentum, LearnFlatSpot,LearnReset,LearnTau,LearnLambda); learndefined = 1; } else if(strcasecmp(cmd_path,"/MLP/WEIGHTS/WRITE") == 0) { cmd_char = str_tolower(strdup( ku_getc() )); SaveWeights(cmd_char,NepochTot); } else if(strcasecmp(cmd_path,"/MLP/WEIGHTS/READ") == 0) { cmd_char = str_tolower(strdup( ku_getc() )); LoadWeights(cmd_char,&NepochTot); } else if(strcasecmp(cmd_path,"/MLP/LPAT/SET") == 0) { MLP_PatSet(0,chinputs,&idn); } else if(strcasecmp(cmd_path,"/MLP/TPAT/SET") == 0) { MLP_PatSet(1,chinputs,&idn); } else if(strcasecmp(cmd_path,"/MLP/LPAT/READ") == 0) { cmd_char = str_tolower(strdup( ku_getc() )); ReadPatterns(cmd_char,0,&netdefined,&learndefined, &examplesdefined); ifile = 0; mlpgetnpat_(&ifile,&(Npat[ifile]),&(Ninput[ifile]), &Noutput[ifile],&Pond[ifile]); } else if(strcasecmp(cmd_path,"/MLP/LPAT/WRITE") == 0) { cmd_char = str_tolower(strdup( ku_getc() )); MLP_WritePat(0,cmd_char); } else if(strcasecmp(cmd_path,"/MLP/TPAT/WRITE") == 0) { cmd_char = str_tolower(strdup( ku_getc() )); MLP_WritePat(1,cmd_char); } else if(strcasecmp(cmd_path,"/MLP/TPAT/READ") == 0) { cmd_char = str_tolower(strdup( ku_getc() )); ReadPatterns(cmd_char,1,&idummy1,&idummy2,&idummy3); ifile = 1; mlpgetnpat_(&ifile,&(Npat[ifile]),&(Ninput[ifile]), &Noutput[ifile],&Pond[ifile]); } else /* should not happen */ { printf("Command not existing or not implemented\n"); } } /****************************************************************/ /* MLP_Error */ /* */ /* prints an error message */ /* */ /* J.Schwindling 18-MAY-99 */ /****************************************************************/ void MLP_Error(char *s1, char *s2) { printf(" *** %s: %s\n",s1,s2); } /****************************************************************/ /* MLP_Set1dPat */ /* */ /* sets the examples from a 1d histogram */ /* */ /* inputs: int ifile = 0: learning examples */ /* = 1: test examples */ /* int idn: histogram number */ /* int ipond: example weighting (1) or not (0) */ /* */ /* return value (int) = number of examples */ /* */ /* J.Schwindling 28-MAY-99 */ /* Modified: 08-JUN-99 example weighting */ /****************************************************************/ int MLP_Set1dPat(int ifile, int idn, int ipond) { char chtitl[80]; int nx, ny, loc, nwt, ier; float xmi, xma, ymi, yma, dbin; float x; float *y, *ey, pond; int idummy, i; float rdummy; HGIVE(idn,chtitl,nx,xmi,xma,ny,ymi,yma,nwt,loc); y = (float *) malloc(nx*sizeof(float)); ey = (float *) malloc(nx*sizeof(float)); ier = mlp_setnpat(ifile,nx,ipond); if(ier!=0) { MLP_Error("MLP/LPAT/SET", "Error while setting number of patterns"); free(ey); free(y); return(-1); } dbin = (xma - xmi) / (float) nx; HUNPAK(idn,y," ",1); HUNPKE(idn,ey," ",1); for(i=0;i0.0000001) pond = 1/(ey[i]*ey[i]); else pond = 1; } ier = mlp_setpat(ifile,idummy,&x,&(y[i]),pond); } free(ey); free(y); return nx; } /****************************************************************/ /* MLP_Set2dPat */ /* */ /* sets the examples from a 2d histogram */ /* */ /* inputs: int ifile = 0: learning examples */ /* = 1: test examples */ /* int idn: histogram number */ /* int ipond: example weighting (1) or not (0) */ /* */ /* return value (int) = number of examples */ /* */ /* J.Schwindling 28-MAY-99 */ /* Modified: 08-JUN-99 example weighting */ /****************************************************************/ int MLP_Set2dPat(int ifile, int idn, int ipond) { char chtitl[80]; int nx, ny, loc, nwt, ier; float xmi, xma, ymi, yma, dbinx, dbiny; float x[2], y, ey, pond; int idummy, idummy2, i, j; float rdummy; HGIVE(idn,chtitl,nx,xmi,xma,ny,ymi,yma,nwt,loc); idummy = 0; idummy2 = nx*ny; ier = mlp_setnpat(ifile,idummy2,ipond); if(ier!=0) { MLP_Error("MLP/LPAT/SET", "Error while setting number of patterns"); return(-1); } dbinx = (xma - xmi) / (float) nx; dbiny = (yma - ymi) / (float) ny; for(i=1;i<=nx;i++) { x[0] = xmi + ((float) i - 0.5) * dbinx; for(j=1;j<=ny;j++) { x[1] = ymi + ((float) j - 0.5) * dbiny; y = HIJ(idn,i,j); ey = HIJE(idn,i,j); if(ipond==0) { pond = 1; } else { if(ey>0.0000001) pond = 1/(ey*ey); else pond = 0; } idummy = (i-1)*ny+j; rdummy = 1; ier = mlp_setpat(ifile,idummy,x,&y,pond); } } return(nx*ny); } /****************************************************************/ /* MLP_count */ /* */ /* counts the number of items separated by % */ /* */ /* input: char *s = input string */ /* return value (int) = number of items */ /* */ /* J.Schwindling 31-MAY-99 */ /****************************************************************/ int MLP_count(char *s) { char tmp[1024]; int i=0; strcpy(tmp,s); if (strtok(tmp,"%")) { i=1; while (strtok(NULL,"%")) i++; } return i; } /****************************************************************/ /* MLP_getnItems */ /* */ /* gets n items separated by % */ /* */ /* input: int n = number of items */ /* char *s = input string */ /* output: char **ss = returned items */ /* */ /* J.Schwindling 31-MAY-99 */ /****************************************************************/ void MLP_getnItems(int n, char *s, char **ss) { char tmp[1024]; int i; strcpy(tmp,s); if (n>0) { strcpy(ss[0],strtok(tmp,"%")); for (i=1;i +(%le) * OUT%d\n", (double) weights[jn],jn); } fprintf(W,"C\n"); for(in=0; in +(%le) * OUT%d\n", (double) weights[jn],jn); fprintf(W,"C\n"); fprintf(W," END\n"); fprintf(W," REAL FUNCTION SIGMOID(X)\n"); fprintf(W," IF(X.GT.37.) THEN\n"); fprintf(W," SIGMOID = 1.\n"); fprintf(W," ELSE IF(X.LT.-37.) THEN\n"); fprintf(W," SIGMOID = 0.\n"); fprintf(W," ELSE\n"); fprintf(W," SIGMOID = 1./(1.+EXP(-X))\n"); fprintf(W," ENDIF\n"); fprintf(W," END\n"); fclose(W); }