/* TO EXERCISE & DISPLAY THE SIGMOID FUNCTION TO BE USED IN NEURAL NETWORKS Author: Robert John Morton YE572246C December 1993 */ #include #include #include #include #include long PlotErr[9]; // THE FOLLOWING TWO FUNCTIONS ARE TO BE EXERCISED short SigTab[1025]; #define R 32767 #define RR 65556 // 65534 + 22 void SigGen(void) { int i; for (i = 0; i < 1024; i++) SigTab[i] = (double)(RR / (1 + exp(-((double)(((long)(i)) << 8))/R)) - R); SigTab[1024] = R; } int Sigmoid(int x) { int s, y, j; if ((s = x) < 0) x = -x; y = *(SigTab + (j = x >> 5)); y += ((*(SigTab + j + 1) - y) * (x & 0x1F)) >> 5; if (s < 0) y = -y; return( y ); } // BELOW IS THE EXERCISER FOR THE TWO FUNCTIONS ABOVE #define XYscale 8 // right shift scaling factor #define Yorg 174 // origin displacement from top left #define Xorg 171 // origin displacement from top left void ShowScale( short x1, short y1, short x2, short y2 ) { _moveto((x1 >> XYscale) + Xorg, (y1 >> XYscale) + Yorg); _lineto((x2 >> XYscale) + Xorg, (y2 >> XYscale) + Yorg); } double GetTime() { struct timeb TimeData; ftime(&TimeData); return((double)(TimeData.millitm) / 1000 + TimeData.time); } main() { int x, y, X, Y; char far *s; double LookTime, ExpTime; _setvideomode(_VRES16COLOR); _settextposition( 3,19); printf("+32767"); _settextposition(20,19); printf("-32767"); _settextposition(11, 3); printf("-32767"); _settextposition(12,35); printf("+32767"); _settextposition(1, 1); _setcolor(7); ShowScale(0, -32767, 0, +32767); ShowScale(-32767, 0, +32767, 0); printf("SIGMOID FUNCTION FOR USE IN NEURAL NETWORKS"); _settextposition( 4,46); printf("The SIGMOID formula is:"); _settextposition( 6,46); printf("f(x) = (2R+d)/(1+exp(-kx)) - R"); _settextposition( 8,46); printf("Where R = 32767 and"); _settextposition( 9,46); printf("-32767 <= x <= +32767"); _settextposition(11,46); printf("`k' determines how step-like the"); _settextposition(12,46); printf("function becomes. In this test"); _settextposition(13,46); printf("k = 8. `k' can conveniently be a"); _settextposition(14,46); printf("multiple of 2: 2,4,8,16,32,64..."); _settextposition(16,46); printf("`d' is a small adjusting factor"); _settextposition(17,46); printf("to make f(x) hit 32767 at exactly"); _settextposition(18,46); printf("the point at which x hits 32767."); SigGen(); _setcolor(9); x = -32767; // Draw the sigmoid curve A: y = Sigmoid(x); _setpixel(Xorg + (x >> XYscale), Yorg - (y >> XYscale)); if (x < 32767) { x++; goto A; } _settextposition(24,1); printf("CHECKING ACCURACY OF LOOKUP & INTERPOLATION..."); _settextposition(6,8); printf("Error"); _setcolor(10); x = -32767; Y = 0; B: if (x < 0) X = -x; else X = x; y = (double)(RR / (1 + exp(-((double)(((long)(X)) << 3))/R)) - R); if (x < 0) y = -y; _setpixel(Xorg + (x >> XYscale), Yorg - (y >> XYscale)); y -= Sigmoid(x); (PlotErr[y+2])++; if(y > Y) Y = y; _settextposition(6,14); printf("%2d", y); if (x < 32767) { x++; goto B; } _settextposition(7, 8); printf("MaxErr%2d",Y); _settextposition(20,43); printf("RESULTS:"); _settextposition(20,53); printf("Error = +2: %6ld",PlotErr[4]); _settextposition(21,53); printf("Error = +1: %6ld",PlotErr[3]); _settextposition(22,53); printf("Error = 0: %6ld",PlotErr[2]); _settextposition(23,53); printf("Error = -1: %6ld",PlotErr[1]); _settextposition(24,53); printf("Error = -2: %6ld",PlotErr[0]); _settextposition(24,1); printf("CHECKING SPEED ADVANTAGE... "); x = -32767; ExpTime = GetTime(); C: if (x < 0) X = -x; else X = x; y = (double)(RR / (1 + exp(-((double)(((long)(X)) << 3))/R)) - R); if (x < 0) y = -y; if (x < 32767) { x++; goto C; } ExpTime = GetTime() - ExpTime; x = -32767; LookTime = GetTime(); D: y = Sigmoid(x); if (x < 32767) { x++; goto D; } LookTime = GetTime() - LookTime; _settextposition(26,53); printf("Exp(x) Time: %4.2f", ExpTime); _settextposition(27,53); printf("Lookup Time: %4.2f", LookTime); _settextposition(28,53); printf("Speed Ratio: %4.2f", ExpTime/LookTime); _settextposition(24,1); printf("FINISHED. HIT `RETURN' TO EXIT.\07"); _settextposition(26,1); getchar(); _setvideomode(_DEFAULTMODE); }