/* Output from p2c, the Pascal-to-C translator */ /* From input file "bpexp2.p" */ #include "p2c.h" /*************************************************************** *************** * * * GD.PAS * * * * Written by Larrie Hutton. Last revision: 8/28/90. * * Revised to allow old binary file use MCO 5/91 * * Revised to allow long form reals in output MCO 5/91 * * Revised to quicken input count and combine w bin write MCO 6/91 * * cf Get NV2 * * Revised to write wt.mat output file routinely MCO 10/30/91 Revised to save output for multiple outputs MCO 5/2/95 **************************************************************** **************/ /*$N+*/ #define Ver 8.2 #define MaxIn 2500 #define MaxHid 25 #define MaxOut 70 #define MaxData LONG_MAX #define MaxInDat 15000 #define MaxTstDat 15000 #define Neither 0 #define Screen 1 #define Files 2 #define Both 3 #define Train 1 #define Test 2 typedef double IVT[MaxIn + 1]; typedef double HVT[MaxHid + 1]; typedef double OVT[MaxOut + 1]; typedef double WM1[MaxOut + 1][MaxHid]; typedef double WM2[MaxHid + 1][MaxIn]; typedef double MID[MaxOut][MaxInDat]; typedef double MTD[MaxOut][MaxTstDat]; Static IVT InpuV; /* current input */ Static OVT Target; /* current output */ Static HVT HidAcv; /* hidden activations */ Static HVT HidThr; /* hidden thresholds */ Static HVT HidDelta; /* hidden errors */ Static OVT OutAcv; /* output activations */ Static MID OATrn; /*output for training data*/ Static MTD OATst; /*output for test data*/ Static OVT OutThr; /* output thresholds */ Static OVT OutDelta; /* output errors */ Static OVT OutErr; /* output errors */ Static WM1 OutWS; /* output weights saved */ Static WM2 HidW; /* hidden to input weights */ Static WM2 LastHidWC; /* last hidden to input weight changes */ Static WM1 OutW; /* output to hidden weights */ Static WM1 LastOutWC; /* last output to hidden weight changes */ Static Char ans; Static double Tot, eta, alpha, decay, thsh, tmtr, magn; Static long NTotal, NData, NTest, StTest, Iter, Person, DoMore, StCycle,Cycle, NI, NH, NO, T, W, D, NL, LastCycle, LoadMat,OType, Cj,IType, PHid, POut, PTar, PData, PStats, PWts, PBoth,PIt; Static boolean PM; Static Char Mark[3]; Static FILE *scrapin, *scrapout, *scrapmat, *scrapwt; double drand48(); long seed; void srand48(); /*************************************************************** *************** * * * GET DATA FROM ONE CASE * * **************************************************************** **************/ Static Void GetPerson(DataPoint) long DataPoint; { long i; double Sum; long FORLIM; Sum = 0.0; switch (IType) { case 1: FORLIM = NI; for (i = 1; i <= FORLIM; i++){ fscanf(scrapin,"%lg",&InpuV[i]); /* InpuV[i]=InpuV[i] + '0' ; */ /* printf("%lg\n",InpuV[i]);*/} FORLIM = NO; for (i = 1; i <= FORLIM; i++){ fscanf(scrapin,"%lg",&Target[i]); /* printf("%lg\n",Target[i]) ;*/ /* Target[i]=Target[i] + '0';*/} break; case 2: FORLIM = NI; for (i = 1; i <= FORLIM; i++){ fread(&InpuV[i], sizeof(double), 1, scrapin); Sum += InpuV[i]; } if (Sum != 0) { FORLIM = NI; for (i = 1; i <= FORLIM; i++) InpuV[i] /= Sum; } FORLIM = NO; for (i = 1; i <= FORLIM; i++) fread(&Target[i], sizeof(double), 1, scrapin); break; case 3: FORLIM = NI; for (i = 1; i <= FORLIM; i++) { fread(&InpuV[i], sizeof(double), 1, scrapin); Sum += InpuV[i] * InpuV[i]; } Sum = sqrt(Sum); if (Sum != 0) { FORLIM = NI; for (i = 1; i <= FORLIM; i++) InpuV[i] /= Sum; } FORLIM = NO; for (i = 1; i <= FORLIM; i++) fread(&Target[i], sizeof(double), 1, scrapin); break; case 4: FORLIM = NI; for (i = 1; i <= FORLIM; i++) { fread(&InpuV[i], sizeof(double), 1, scrapin); InpuV[i] = 1 / (1 + exp(-InpuV[i])); } FORLIM = NO; for (i = 1; i <= FORLIM; i++) fread(&Target[i], sizeof(double), 1, scrapin); break; }/*case*/ } /* GetPerson */ #define OverFlow 88 /*************************************************************** *************** * * * SQUASHING FUNCTION * * **************************************************************** **************/ Static double Squash(Val) double Val; { double Result; Val /= tmtr; if (Val > OverFlow) Val = OverFlow; else if (Val < -OverFlow) Val = -OverFlow; switch (OType) { case 1: /*because hidden still uses logistic*/ Result = 1.0 / (1.0 + exp(-Val)); break; case 2: Result = 1.0 / (1.0 + exp(-Val)); break; case 3: if (1 / (1 + exp(-Val)) > drand48()) Result = 1.0; else Result = 0.0; break; } return Result; } /* Squash */ #undef OverFlow /*************************************************************** *************** * * * FEED FORWARD * * **************************************************************** **************/ Static Void FeedForward() { long i, j; double Sum; long FORLIM, FORLIM1; FORLIM = NH; for (i = 1; i <= FORLIM; i++) { Sum = 0.0; FORLIM1 = NI; for (j = 1; j <= FORLIM1; j++) Sum += HidW[i][j - 1] * InpuV[j]; HidAcv[i] = Squash(Sum + HidThr[i]); } FORLIM = NO; for (i = 1; i <= FORLIM; i++) { Sum = 0.0; FORLIM1 = NH; for (j = 1; j <= FORLIM1; j++) Sum += OutW[i][j - 1] * HidAcv[j]; if (OType > 1) OutAcv[i] = Squash(Sum + OutThr[i]); else OutAcv[i] = Sum + OutThr[i]; } } /* FeedForward */ /*************************************************************** *************** * * * BACK PROPAGATION FOR OUTPUT TO HIDDEN * * **************************************************************** **************/ Static Void BackPropOut() { long i, j, FORLIM, FORLIM1; FORLIM = NO; for (i = 1; i <= FORLIM; i++) { if (OType > 1) OutErr[i] = (Target[i] - OutAcv[i]) * OutAcv[i] * (1.0 -OutAcv[i]); else OutErr[i] = Target[i] - OutAcv[i]; FORLIM1 = NH; for (j = 0; j < FORLIM1; j++) { OutWS[i][j] = OutW[i][j]; LastOutWC[i][j] = eta * OutErr[i] * HidAcv[j + 1] + alpha *LastOutWC[i][j]; OutW[i][j] = (1 - decay) * (OutW[i][j] + LastOutWC[i][j]); } OutDelta[i] = eta * OutErr[i] + alpha * OutDelta[i]; OutThr[i] = (1 - decay) * (OutThr[i] + OutDelta[i]); } } /* BackPropOut */ /*************************************************************** *************** * * * BACK PROPAGATION FOR HIDDEN TO INPUT * * **************************************************************** **************/ Static Void BackPropHid() { long i, j; double Error; long FORLIM, FORLIM1; FORLIM = NH; for (i = 1; i <= FORLIM; i++) { Error = 0.0; FORLIM1 = NO; for (j = 1; j <= FORLIM1; j++) Error += OutErr[j] * OutWS[j][i - 1]; Error *= HidAcv[i] * (1.0 - HidAcv[i]); FORLIM1 = NI; for (j = 0; j < FORLIM1; j++) { LastHidWC[i][j] = eta * Error * InpuV[j + 1] + alpha *LastHidWC[i][j]; HidW[i][j] = (1 - decay) * (HidW[i][j] + LastHidWC[i][j]); } HidDelta[i] = eta * Error + alpha * HidDelta[i]; HidThr[i] = (1 - decay) * (HidThr[i] + HidDelta[i]); } } /* BackPropHid */ /*************************************************************** *************** * * * GENERALIZED DELTA RULE * * **************************************************************** **************/ Static Void GDRule() { FeedForward(); BackPropOut(); BackPropHid(); } /* GDRule */ /*************************************************************** *************** * * * GET N,V INFO * * **************************************************************** **************/ Static Void GetNV(Tot, N, V) double *Tot; long *N, *V; { double Num; boolean Quit, More; *Tot = 0.0; *N = 0; *V = 0; rewind(scrapin); do { Quit = false; if (P_eof(scrapin)) Quit = true; if (!Quit) { if (!P_eoln(scrapin)) Quit = true; } if (!Quit) { fscanf(scrapin, "%*[^\n]"); getc(scrapin); } } while (!Quit); while (!P_eof(scrapin)) { fscanf(scrapin, "%lg", &Num); (*Tot)++; More = false; if (P_eof(scrapin)) (*N)++; else { do { Quit = false; if (P_eof(scrapin)) Quit = true; if (!Quit) { if (!P_eoln(scrapin)) Quit = true; } if (!Quit) { fscanf(scrapin, "%*[^\n]"); getc(scrapin); } if (!Quit) More = true; } while (!Quit); } if (More) (*N)++; if (*N == 1000) printf("1K\n"); } if (*Tot == 0) *N = 0; if (*Tot != 0) *V = (long)(*Tot / *N); } /* GetNV */ /*************************************************************** *************** * * * SETUP * * **************************************************************** **************/ Static Void SetUp() { long VTotal; thsh = 0.0; eta = 0.60; alpha = 0.3; tmtr = 1.0; decay = 0.0; magn = 0.1; seed = 0; IType = 1; OType = 2; PHid = 0; POut = 1; PTar = 0; PData = 0; PWts = 0; PStats = 1; PBoth = 1; Iter = 10; PIt = 1; T = 80; W = 8; D = 3; NL = T / W; PM = false; strcpy(Mark, " "); GetNV(&Tot, &NTotal, &VTotal); NTotal = (long)floor(Tot / VTotal + 0.5); if (scrapout != NULL) rewind(scrapout); else scrapout = tmpfile(); if (scrapout == NULL) _EscIO(FileNotFound); LoadMat = 0; NO = 33; NH = 10; NI = VTotal - NO; NTest = NTotal / 2; NData = NTotal - NTest; StTest = NData + 1; printf("Back prop, ver. %1.1f. Written by Larrie Hutton.\n",Ver); printf("Modification #24 after conversion to C by M.C.O'Neill\n"); printf("The current maximum values are:\n"); printf(" Max data:%ld Max input:%ld Max hidden:%ld Max output:%ld\n\n", MaxData, (long)MaxIn, (long)MaxHid, (long)MaxOut); } /* SetUp */ /*************************************************************** **************** * * * WRITELIST * * **************************************************************** ***************/ Static Void WriteList() { long i; if (((unsigned long)PData < 32 && ((1L << PData) & ((1L << Screen) | (1L << Both))) != 0) || ((unsigned long)PWts < 32 && ((1L << PWts) & ((1L << Screen) | (1L << Both))) != 0) || ((unsigned long)PStats < 32 && ((1L << PStats) & ((1L << Screen) | (1L << Both))) != 0)) { for (i = 1; i <= 79; i++) putchar('-'); printf("\n1:NDt 2:NTe 3:NI 4:NH 5:NO 6:Sho7:NIt8:PIt 9:G 10:Sd 11:R12:LRt13:Mm\n"); printf( "----- ----- ---- ---- ---- ----- ----- ----- --- -----\n"); printf("%5ld%6ld%5ld%5ld%5ld%6ld%6ld%6ld%4ld%6ld%5.2f%7.4f%6.3f%7.4f\n", NData, NTest, NI, NH, NO, PHid * 10000 + PBoth * 1000 + PData * 100 + PWts * 10 +PStats, Iter, PIt, LoadMat, seed, magn, eta, alpha, 100.0 *decay); for (i = 1; i <= 79; i++) putchar('-'); putchar('\n'); } if (((unsigned long)PData >= 32 || ((1L << PData) & ((1L << Files) | (1L << Both))) == 0) && ((unsigned long)PWts >= 32 || ((1L << PWts) & ((1L << Files) | (1L << Both))) == 0) && ((unsigned long)PStats >= 32 || ((1L << PStats) & ((1L << Files) | (1L << Both))) == 0)) return; for (i = 1; i <= 79; i++) putc('-', scrapout); fprintf(scrapout, "\n1:NDt 2:NTe 3:NI 4:NH 5:NO 6:Sho7:NIt8:PIt 9:G 10:Sd 11:R 12:LRt 13:Mm\n"); fprintf(scrapout, "----- ----- ---- ---- ---- ----- ----- ----- --- ----- ----\n"); fprintf(scrapout, "%5ld%6ld%5ld%5ld%5ld%6ld%6ld%6ld%4ld%6ld%5.2f%7.4f%6.3f%7.4f\n", NData, NTest, NI, NH, NO, PHid * 10000 + PBoth * 1000 + PData * 100 + PWts * 10 +PStats, Iter, PIt, LoadMat, seed, magn, eta, alpha, 100.0 *decay); for (i = 1; i <= 79; i++) putc('-', scrapout); putc('\n', scrapout); } /* WriteList */ /*************************************************************** *************** * * * SELECT OUTPUT LOCATIONS * * **************************************************************** **************/ Static Void PrintChoices() { printf("For all choices, 0 is none, 1 is screen, 2 is file, 3is both.\n"); do { printf(" Choice for output from statistics: "); scanf("%ld%*[^\n]", &PStats); getchar(); } while ((unsigned long)PStats >= 32 || ((1L << PStats) & 0xf)== 0); do { printf(" Choice for output from weight matrix: "); scanf("%ld%*[^\n]", &PWts); getchar(); } while ((unsigned long)PWts >= 32 || ((1L << PWts) & 0xf) ==0); do { printf(" Choice for output from hidden, output, or target nodes: "); scanf("%ld%*[^\n]", &PData); getchar(); } while ((unsigned long)PData >= 32 || ((1L << PData) & 0xf)== 0); if (PData > 0) { do { printf(" Choice for output from hidden nodes (0 or 1):"); scanf("%ld%*[^\n]", &PHid); getchar(); } while ((unsigned long)PHid >= 32 || ((1L << PHid) & 0x3)== 0); do { printf(" Choice for output from output nodes (0 or 1):"); scanf("%ld%*[^\n]", &POut); getchar(); } while ((unsigned long)POut >= 32 || ((1L << POut) & 0x3)== 0); do { printf(" Choice for output from target nodes (0 or 1):"); scanf("%ld%*[^\n]", &PTar); getchar(); } while ((unsigned long)PTar >= 32 || ((1L << PTar) & 0x3)== 0); } if (DoMore == 3) return; do { printf(" Choice for output from test data (0 or 1): "); scanf("%ld%*[^\n]", &PBoth); getchar(); } while ((unsigned long)PBoth >= 32 || ((1L << PBoth) & 0x3)== 0); /* PrintChoices */ } /*************************************************************** *************** * * * GETPARAMS * * **************************************************************** **************/ Static Void GetParams() { long Choice; WriteList(); do { printf("Enter number of parameter to change (or 0 tocontinue): "); scanf("%ld%*[^\n]", &Choice); getchar(); switch (Choice) { case 0: /* blank case */ break; case 1: printf("Enter new number of data points: "); scanf("%ld%*[^\n]", &NData); getchar(); break; case 2: printf("Enter number of test points (last n data points):"); scanf("%ld%*[^\n]", &NTest); getchar(); StTest = NTotal - NTest + 1; break; case 3: printf("Enter new number of inputs: "); scanf("%ld%*[^\n]", &NI); getchar(); NTotal = (long)floor(Tot / (NI + NO) + 0.5); break; case 4: printf("Enter new number of hidden-layer neurons: "); scanf("%ld%*[^\n]", &NH); getchar(); break; case 5: printf("Enter new number of output variables: "); scanf("%ld%*[^\n]", &NO); getchar(); NTotal = (long)floor(Tot / (NI + NO) + 0.5); break; case 6: PrintChoices(); break; case 7: printf("Enter new number of iterations: "); scanf("%ld%*[^\n]", &Iter); getchar(); break; case 8: printf("Enter multiple for printing weight matrix: "); scanf("%ld%*[^\n]", &PIt); getchar(); break; case 9: printf("Enter 1 for old \"scrapmat\" from disk or 0 to cancel: "); scanf("%ld%*[^\n]", &LoadMat); getchar(); break; case 10: printf("Enter seed for random weights: "); scanf("%ld%*[^\n]", &seed); getchar(); srand48(seed); break; case 11: printf("Enter new magnitude for random weights: "); scanf("%lg%*[^\n]", &magn); getchar(); break; case 12: printf("Enter new learning rate: "); scanf("%lg%*[^\n]", &eta); getchar(); break; case 13: printf("Enter new momentum: "); scanf("%lg%*[^\n]", &alpha); getchar(); break; case 14: printf("Enter new decay rate: "); scanf("%lg%*[^\n]", &decay); getchar(); break; case 15: printf("Enter new temperature: "); scanf("%lg%*[^\n]", &tmtr); getchar(); break; case 16: printf("Enter new threshold: "); scanf("%lg%*[^\n]", &thsh); getchar(); break; case 17: printf("Current values are input: %ld; output: %ld.\n",IType, OType); printf("Input: 1-No change 2-Normalize 3-Norm Unit 4-Norm Sig\n"); printf(" Enter input transform: "); scanf("%ld%*[^\n]", &IType); getchar(); printf("Output: 1-Linear 2-Sigmoid 3-Stochastic\n"); printf(" Enter output transform: "); scanf("%ld%*[^\n]", &OType); getchar(); break; case 18: printf("These options permit changes in numeric format.\n"); printf(" Enter terminal width: "); scanf("%ld%*[^\n]", &T); getchar(); printf(" Enter numeric width: "); scanf("%ld%*[^\n]", &W); getchar(); printf(" Enter decimal places: "); scanf("%ld%*[^\n]", &D); getchar(); NL = T / W; break; }/*case*/ } while (Choice != 0); NTotal = (long)floor(Tot / (NI + NO) + 0.5); } /* GetParams */ /*************************************************************** *************** * * * INITIALIZE WEIGHTS * * **************************************************************** **************/ Static Void GetWeights() { long i, j; double Scrap; long FORLIM, FORLIM1; FORLIM = seed; for (i = 0; i <= FORLIM; i++) Scrap = drand48(); if (LoadMat == 0) { LastCycle = 0; FORLIM = NO; for (i = 1; i <= FORLIM; i++) { FORLIM1 = NH; for (j = 0; j < FORLIM1; j++) { OutW[i][j] = magn * (drand48() * 2 - 1); LastOutWC[i][j] = 0.0; } OutThr[i] = magn * (drand48() * 2 - 1); OutAcv[i] = 0.0; OutDelta[i] = 0.0; } FORLIM = NH; for (i = 1; i <= FORLIM; i++) { FORLIM1 = NI; for (j = 0; j < FORLIM1; j++) { HidW[i][j] = magn * (drand48() * 2 - 1); LastHidWC[i][j] = 0.0; } HidThr[i] = magn * (drand48() * 2 - 1); HidAcv[i] = 0.0; HidDelta[i] = 0.0; } return; } rewind(scrapmat); fscanf(scrapmat, "%ld", &LastCycle); FORLIM = NO; for (i = 1; i <= FORLIM; i++) { FORLIM1 = NH; for (j = 0; j < FORLIM1; j++) { fscanf(scrapmat, "%lg", &OutW[i][j]); LastOutWC[i][j] = 0.0; } fscanf(scrapmat, "%lg", &OutThr[i]); OutAcv[i] = 0.0; OutDelta[i] = 0.0; } FORLIM = NH; for (i = 1; i <= FORLIM; i++) { HidAcv[i] = 0.0; HidDelta[i] = 0.0; FORLIM1 = NI; for (j = 0; j < FORLIM1; j++) { fscanf(scrapmat, "%lg", &HidW[i][j]); LastHidWC[i][j] = 0.0; } fscanf(scrapmat, "%lg", &HidThr[i]); } } /* GetWeights */ /*************************************************************** *************** * * * READDATA * * **************************************************************** **************/ Static Void ReadData(ActvSet) long ActvSet; { long i, j; double scrap; long FORLIM, FORLIM1; rewind(scrapin); if (ActvSet != Test) return; FORLIM = NTotal - NTest; for (i = 1; i <= FORLIM; i++) { FORLIM1 = NO + NI; for (j = 1; j <= FORLIM1; j++) /* fread(&scrap, sizeof(double), 1, scrapin);*/ fscanf(scrapin,"%lg",&scrap); } } /* ReadData */ /*************************************************************** *************** * * * PRINT HEADER * * **************************************************************** **************/ Static Void PrintHeader() { long i; if (((unsigned long)PData < 32 && ((1L << PData) & ((1L << Screen) | (1L << Both))) != 0) || ((unsigned long)PWts < 32 && ((1L << PWts) & ((1L << Screen) | (1L << Both))) != 0) || ((unsigned long)PStats < 32 && ((1L << PStats) & ((1L << Screen) | (1L << Both))) != 0)) { for (i = 1; i <= 79; i++) putchar('='); putchar('\n'); } if (((unsigned long)PData >= 32 || ((1L << PData) & ((1L << Files) | (1L << Both))) == 0) && ((unsigned long)PWts >= 32 || ((1L << PWts) & ((1L << Files) | (1L << Both))) == 0) && ((unsigned long)PStats >= 32 || ((1L << PStats) & ((1L << Files) | (1L << Both))) == 0)) return; for (i = 1; i <= 79; i++) putc('=', scrapout); putc('\n', scrapout); } /* PrintHeader */ /*************************************************************** *************** * * * PRINT NUMBER TO FILE OR SCREEN * * **************************************************************** **************/ Static Void PrintNum(ToF, N, Last, Num) long ToF, N, Last; double Num; { if (ToF == Screen) { if (!PM) printf("%*.*f", (int)W, (int)D, Num); else printf("%*.*f%1s", (int)(W - 1), (int)D, Num, Mark); if (N % NL == 0 || N == Last) putchar('\n'); return; } if (!PM) fprintf(scrapout, "%*.*f", (int)W, (int)D, Num); else fprintf(scrapout, "%*.*f%1s", (int)(W - 1), (int)D, Num,Mark); if (N % NL == 0 || N == Last) putc('\n', scrapout); } /* PrintNum */ /*************************************************************** **************/ Static Void PrintNumWt(ToF, N, Last, Num) long ToF, N, Last; double Num; { if (!PM) fprintf(scrapwt, "% .5E", Num); else fprintf(scrapwt, "% .5E%1s", Num, Mark); } /* PrintNumWt */ /*************************************************************** *************** * * * Long Form PRINT NUMBER TO FILE OR SCREEN * * **************************************************************** **************/ Static Void PrintNum2(ToF, N, Last, Num) long ToF, N, Last; double Num; { if (ToF == Screen) { if (!PM) printf("% .5E", Num); else printf("% .5E%1s", Num, Mark); if (N % NL == 0 || N == Last) putchar('\n'); return; } if (!PM) fprintf(scrapout, "% .5E", Num); else fprintf(scrapout, "% .5E%1s", Num, Mark); if (N % NL == 0 || N == Last) putc('\n', scrapout); } /* PrintNum */ /*************************************************************** *************** * * SHOW OUTPUT * * **************************************************************** **************/ Static Void ShowOutput(TSet, NPoints, Iter) long TSet, NPoints, Iter; { long i, j, DoCR, FORLIM, FORLIM1; PM = true; DoCR = PHid * NH + POut * NO + PTar * NO + 1; if ((unsigned long)PData < 32 && ((1L << PData) & ((1L << Screen) | (1L << Both))) != 0) { printf("Output* for set %0c on epoch %ld:\n", (Char)(TSet +96), Iter); ReadData(TSet); for (i = 1; i <= NPoints; i++) { GetPerson(i); printf("%c%*ld", (Char)(TSet + 96), (int)(W - 1), i); strcpy(Mark, " "); FORLIM1 = NH * PHid; for (j = 1; j <= FORLIM1; j++) PrintNum((long)Screen, j + 1, DoCR, HidAcv[j]); FORLIM1 = POut * NO; for (j = 1; j <= FORLIM1; j++) { if ( TSet == Train) OutAcv[j] = OATrn[j][i]; else OutAcv[j] = OATst[j][i]; if ((OutAcv[j] - 0.9) * (Target[j] - 0.9) >= 0) strcpy(Mark, " "); else strcpy(Mark, " *"); PrintNum2((long)Screen, j + PHid * NH + 1, DoCR, OutAcv[j]); } FORLIM1 = PTar * NO; for (j = 1; j <= FORLIM1; j++) { if ((OutAcv[j] - 0.9) * (Target[j] - 0.9) >= 0) strcpy(Mark, " "); else strcpy(Mark, " *"); PrintNum((long)Screen, j + PHid * NH + POut * NO + 1, DoCR,Target[j]); } } for (i = 1; i <= 79; i++) putchar('-'); putchar('\n'); } if ( (TSet == Test) || (unsigned long)PData < 32 && ((1L << PData) & ((1L << Files) | (1L << Both))) != 0) { fprintf(scrapout, "Output* for set %0c on epoch %ld:\n", (Char)(TSet + 96), Iter); ReadData((long)TSet); for (i = 1; i <= NPoints; i++) { GetPerson(i); fprintf(scrapout, "%c%*ld", (Char)(TSet + 96), (int)(W -1), i); strcpy(Mark, " "); FORLIM = NH * PHid; for (j = 1; j <= FORLIM; j++) PrintNum((long)Files, j + 1, DoCR, HidAcv[j]); FORLIM = POut * NO; for (j = 1; j <= FORLIM; j++) { if ( TSet == Train) OutAcv[j] = OATrn[j][i]; else OutAcv[j] = OATst[j][i]; if ((OutAcv[j] - 0.9) * (Target[j] - 0.9) >= 0) strcpy(Mark, " "); else strcpy(Mark, " *"); PrintNum2((long)Files, j + PHid * NH + 1, DoCR, OutAcv[j]); } FORLIM = PTar * NO; for (j = 1; j <= FORLIM; j++) { if ((OutAcv[j] - 0.9) * (Target[j] - 0.9) >= 0) strcpy(Mark, " "); else strcpy(Mark, " *"); PrintNum((long)Files, j + PHid * NH + POut * NO + 1, DoCR,Target[j]); } } for (i = 1; i<=79; i++) putc('-',scrapout); putc('\n',scrapout); } PM = false; } /* ShowOutput */ /*************************************************************** *************** * * * WRITE WEIGHT MATRIX * * **************************************************************** **************/ Static Void WriteWeights(TSet, N) long TSet, N; { long i, j, FORLIM, FORLIM1; if ((unsigned long)PWts < 32 && ((1L << PWts) & ((1L << Screen) | (1L << Both))) != 0) { printf("Output and hidden weights for set %ld. Biases inlast column.\n", TSet); printf("%5ld\n\n", N); FORLIM = NO; for (i = 1; i <= FORLIM; i++) { FORLIM1 = NH; for (j = 1; j <= FORLIM1; j++) PrintNum((long)Screen, j, NH + 1, OutW[i][j - 1]); PrintNum((long)Screen, NH + 1, NH + 1, OutThr[i]); } putchar('\n'); FORLIM = NH; for (i = 1; i <= FORLIM; i++) { FORLIM1 = NI; for (j = 1; j <= FORLIM1; j++) PrintNum((long)Screen, j, NI + 1, HidW[i][j - 1]); PrintNum((long)Screen, NI + 1, NI + 1, HidThr[i]); } for (i = 1; i <= 79; i++) putchar('-'); putchar('\n'); } if ((unsigned long)PWts >= 32 || ((1L << PWts) & ((1L << Files) | (1L << Both))) == 0) return; fprintf(scrapout, "Output and hidden weights for set %ld. Biases in lastcolumn.\n", TSet); fprintf(scrapout, "%5ld\n\n", N); FORLIM = NO; for (i = 1; i <= FORLIM; i++) { FORLIM1 = NH; for (j = 1; j <= FORLIM1; j++) PrintNum((long)Files, j, NH + 1, OutW[i][j - 1]); PrintNum((long)Files, NH + 1, NH + 1, OutThr[i]); } putc('\n', scrapout); FORLIM = NH; for (i = 1; i <= FORLIM; i++) { FORLIM1 = NI; for (j = 1; j <= FORLIM1; j++) PrintNum((long)Files, j, NI + 1, HidW[i][j - 1]); PrintNum((long)Files, NI + 1, NI + 1, HidThr[i]); } for (i = 1; i <= 79; i++) putc('-', scrapout); putc('\n', scrapout); } /* WriteWeights */ /************************************************************ WriteWTMat ************************************************************/ Static Void WriteWtMat(TSet, N) long TSet, N; { long i, j, FORLIM, FORLIM1; fprintf(scrapwt, "%5ld\n", N); FORLIM = NO; for (i = 1; i <= FORLIM; i++) { FORLIM1 = NH; for (j = 1; j <= FORLIM1; j++) { if (OutW[i][j - 1] < 0) putc(' ', scrapwt); PrintNumWt((long)Files, j, NH + 1, OutW[i][j - 1]); } if (OutThr[i] < 0) putc(' ', scrapwt); PrintNumWt((long)Files, NH + 1, NH + 1, OutThr[i]); } FORLIM = NH; for (i = 1; i <= FORLIM; i++) { FORLIM1 = NI; for (j = 1; j <= FORLIM1; j++) { if (HidW[i][j - 1] < 0) putc(' ', scrapwt); PrintNumWt((long)Files, j, NI + 1, HidW[i][j - 1]); } if (HidThr[i] < 0) putc(' ', scrapwt); PrintNumWt((long)Files, NI + 1, NI + 1, HidThr[i]); } } /* WriteWtMat */ typedef double TVT[MaxOut + 1]; /*************************************************************** *************** * * * TESTDATA * * **************************************************************** **************/ Static Void TestData(TSet, DTest, Cycle) long TSet, DTest, Cycle; { long i, j, St, DP1, DP2; double X, Y, N, Temp; TVT Correct, SumX, SumY, SumErr, SumErrSq, SumXSq, SumYSq, MeanX, MeanY,MeanErr, ErRMS, SDX, SDY, SumXY, R, Cov, Slope, Intrcpt, PVar, IsYes, IsNo, PredYes, PredNo, Sens, Spfty; long FORLIM, FORLIM1; double TEMP1, TEMP2, TEMP3; N = DTest; if ((unsigned long)PStats < 32 && ((1L << PStats) & ((1L << Screen) | (1L << Files) | (1L < 0.5 && Y > 0.5 || X <= 0.5 && Y <= 0.5) { Correct[j]++; Correct[0]++; } if (X > 0.5) { IsYes[j]++; IsYes[0]++; if (Y > 0.5) { PredYes[j]++; PredYes[0]++; } } if (X <= 0.5) { IsNo[j]++; IsNo[0]++; if (Y <= 0.5) { PredNo[j]++; PredNo[0]++; } } SumX[j] += X; SumY[j] += Y; SumXSq[j] += X * X; SumYSq[j] += Y * Y; SumErr[j] += fabs(X - Y); TEMP1 = X - Y; SumErrSq[j] += TEMP1 * TEMP1; SumXY[j] += X * Y; SumX[0] += X; SumY[0] += Y; SumXSq[0] += X * X; SumYSq[0] += Y * Y; SumErr[0] += fabs(X - Y); TEMP1 = X - Y; SumErrSq[0] += TEMP1 * TEMP1; SumXY[0] += X * Y; } } FORLIM = NO; for (i = 1; i <= FORLIM; i++) { Correct[i] /= N; Sens[i] = (PredYes[i] + 0.000001) / (IsYes[i] + 0.000001); Spfty[i] = (PredNo[i] + 0.000001) / (IsNo[i] + 0.000001); MeanX[i] = SumX[i] / N; MeanY[i] = SumY[i] / N; MeanErr[i] = SumErr[i] / N; ErRMS[i] = sqrt(fabs(SumErrSq[i] / N)); TEMP1 = SumX[i]; SDX[i] = sqrt(fabs((SumXSq[i] - TEMP1 * TEMP1 / N) / N)); TEMP1 = SumY[i]; SDY[i] = sqrt(fabs((SumYSq[i] - TEMP1 * TEMP1 / N) / N)); Cov[i] = (SumXY[i] - SumX[i] * SumY[i] / N) / N; if (SDX[i] == 0 || SDY[i] == 0) { R[i] = 0.0; Slope[i] = 0.0; Intrcpt[i] = 0.0; } else { R[i] = Cov[i] / (SDX[i] * SDY[i]); TEMP1 = SDY[i]; Slope[i] = Cov[i] / (TEMP1 * TEMP1); TEMP1 = SDX[i]; TEMP2 = ErRMS[i]; TEMP3 = SDX[i]; PVar[i] = (TEMP1 * TEMP1 - TEMP2 * TEMP2) / (TEMP3 * TEMP3); Intrcpt[i] = MeanX[i] - Slope[i] * MeanY[i]; } } Correct[0] /= N * NO; Sens[0] = (PredYes[0] + 0.000001) / (IsYes[0] + 0.000001); Spfty[0] = (PredNo[0] + 0.000001) / (IsNo[0] + 0.000001); MeanX[0] = SumX[0] / (N * NO); MeanY[0] = SumY[0] / (N * NO); MeanErr[0] = SumErr[0] / (N * NO); ErRMS[0] = sqrt(fabs(SumErrSq[0] / (N * NO))); TEMP1 = SumX[0]; SDX[0] = sqrt(fabs((SumXSq[0] - TEMP1 * TEMP1 / (N * NO)) /(N * NO))); TEMP1 = SumY[0]; SDY[0] = sqrt(fabs((SumYSq[0] - TEMP1 * TEMP1 / (N * NO)) /(N * NO))); Cov[0] = (SumXY[0] - SumX[0] * SumY[0] / (N * NO)) / (N *NO); if (SDX[0] == 0 || SDY[0] == 0) { R[0] = 0.0; Slope[0] = 0.0; Intrcpt[0] = 0.0; } else { R[0] = Cov[0] / (SDX[0] * SDY[0]); TEMP1 = SDY[0]; Slope[0] = Cov[0] / (TEMP1 * TEMP1); TEMP1 = SDX[0]; TEMP2 = ErRMS[0]; TEMP3 = SDX[0]; PVar[0] = (TEMP1 * TEMP1 - TEMP2 * TEMP2) / (TEMP3 *TEMP3); Intrcpt[0] = MeanX[0] - Slope[0] * MeanY[0]; } } Temp = fabs(MeanX[0] + 3.0 * SDX[0]); if (Temp <= 1) Temp = 1.0; DP1 = 3 - (long)(log(Temp) / log(10.0)); if (DP1 < 0) DP1 = 0; if (DP1 <= 1) DP2 = DP1 + 1; else DP2 = DP1; if (NO == 1) St = 1; else St = 0; if ((unsigned long)PStats < 32 && ((1L << PStats) & ((1L << Screen) | (1L << Both))) != 0) { if (TSet == 1 || (unsigned long)DoMore < 32 && ((1L << DoMore) & 0x18) != 0) printf( "S Iters ON MeanA MeanP SDAct SDPrd PrCrc Sensv Spcfc ErAbs ErRMS PrpVar PrVar2\n"); FORLIM = NO; for (i = St; i <= FORLIM; i++) printf("%0c%6ld%3ld%6.*f%6.*f%6.*f%6.*f%6.3f%6.3f%6.3f%6.*f%6.*f%7.4f%7.4f\n", (Char)(TSet + 64), Cycle, i, (int)DP1, MeanX[i],(int)DP1, MeanY[i], (int)DP2, SDX[i], (int)DP2, SDY[i],Correct[i], Sens[i], Spfty[i], (int)DP2, MeanErr[i], (int)DP2,ErRMS[i], R[i] * R[i], PVar[i]); for (i = 1; i <= 79; i++) putchar('-'); putchar('\n'); } if ((unsigned long)PStats >= 32 || ((1L << PStats) & ((1L << Files) | (1L << Both))) == 0) return; if (TSet == 1 || (unsigned long)DoMore < 32 && ((1L << DoMore)& 0x18) != 0) fprintf(scrapout, "S Iters ON MeanA MeanP SDAct SDPrd PrCrc Sensv Spcfc ErAbs ErRMS PrpVar PrVar2\n"); FORLIM = NO; for (i = St; i <= FORLIM; i++) fprintf(scrapout, "%0c%6ld%3ld%6.*f%6.*f%6.*f%6.*f%6.3f%6.3f%6.3f%6.*f%6.*f%7.4f%7.4f\n", (Char)(TSet + 64), Cycle, i, (int)DP1, MeanX[i], (int)DP1,MeanY[i], (int)DP2, SDX[i], (int)DP2, SDY[i], Correct[i], Sens[i],Spfty[i], (int)DP2, MeanErr[i], (int)DP2, ErRMS[i], R[i] * R[i],PVar[i]); for (i = 1; i <= 79; i++) putc('-', scrapout); putc('\n', scrapout); } /* TestData */ /*************************************************************** *************** * * * SHOW RESULTS * * **************************************************************** **************/ Static Void ShowResults(ActvSet, NumCycles, DataPoints) long ActvSet, NumCycles, DataPoints; { PrintHeader(); ShowOutput(ActvSet, DataPoints, NumCycles); if (PBoth == 1 && ActvSet == Train) ShowOutput((long)Test, NTest, NumCycles); WriteWeights(ActvSet, NumCycles); TestData(ActvSet, DataPoints, NumCycles); if (PBoth == 1 && ActvSet == Train) TestData((long)Test, NTest, NumCycles); } /* ShowResults */ /*************************************************************** *************** * * * MAIN PROGRAM * * **************************************************************** **************/ main(argc, argv) int argc; Char *argv[]; { Char STR1[256]; long FORLIM, FORLIM1; if (argc < 3) { printf("Command line not complete.\n"); printf("Requires input and output file names + optionalweight matrix.\n"); return(10); } PASCAL_MAIN(argc, argv); scrapwt = NULL; scrapmat = NULL; scrapout = NULL; scrapin = NULL; scrapin = fopen(strcpy(STR1, P_argv[1]), "r"); if (scrapin == NULL) _EscIO(FileNotFound); scrapout = fopen(strcpy(STR1, P_argv[2]), "w"); if (scrapout == NULL) _EscIO(FileNotFound); if (argc==4){ scrapmat = fopen(strcpy(STR1, P_argv[3]), "r"); if (scrapmat == NULL) _EscIO(FileNotFound); } /* scrapin1 = fopen("scrapin1", "wrb"); if (scrapin1 == NULL) _EscIO(FileNotFound)*/ scrapwt = fopen("wt.mat", "w"); if (scrapwt == NULL) _EscIO(FileNotFound); if (scrapwt != NULL) { rewind(scrapwt); } else scrapwt = tmpfile(); if (scrapwt == NULL) _EscIO(FileNotFound); SetUp(); DoMore = 1; do { GetParams(); WriteList(); if (DoMore < 2) GetWeights(); StCycle = LastCycle + 1; FORLIM = Iter; for (Cycle = StCycle; Cycle <= FORLIM; Cycle++) { ReadData((long)Train); FORLIM1 = NData; for (Person = 1; Person <= FORLIM1; Person++) { GetPerson(Person); GDRule(); for (Cj = 1; Cj<= NO; Cj++) { OATrn[Cj][Person]=OutAcv[Cj]; } } ReadData((long)Test); FORLIM1 = NTest; for (Person = 1; Person <=FORLIM1 ; Person++) { GetPerson(Person); FeedForward(); for (Cj = 1; Cj<=NO; Cj++){ OATst[Cj][Person]=OutAcv[Cj]; } } LastCycle = Cycle; if (Cycle % PIt == 0 || Cycle == Iter) { ShowResults((long)Train, Cycle, NData); } } printf("1-More, 2-More and keep weights, 3-End, 4-End andkeep Sho: "); scanf("%ld%*[^\n]", &DoMore); getchar(); WriteWtMat((long)Train, Cycle); } while ((unsigned long)DoMore >= 32 || ((1L << DoMore) &0x18) == 0); if (DoMore == 3) { PrintChoices(); WriteList(); ShowResults((long)Test, Iter, NTest); } if (scrapin != NULL) fclose(scrapin); scrapin = NULL; if (scrapout != NULL) fclose(scrapout); scrapout = NULL; if (scrapmat != NULL) fclose(scrapmat); scrapmat = NULL; /*if (scrapin1 != NULL) fclose(scrapin1); scrapin1 = NULL*/ if (scrapwt != NULL) fclose(scrapwt); scrapwt = NULL; /*(scrapin != NULL) fclose(scrapin); if (scrapout != NULL) fclose(scrapout); if (scrapmat != NULL) fclose(scrapmat); if (scrapwt != NULL) fclose(scrapwt); if (scrapin1 != NULL) fclose(scrapin1)*/ exit(0); } /* Main */ /* End. */