/* TO EXERCISE & DISPLAY THE SIGMOID FUNCTION TO BE USED IN NEURAL NETWORKS Author Robert John Morton YE572246C December 1997 tab = 3 spaces */ #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 ); } int Gaussian(int x) { #define D 9000 double yy, xx = x; xx /= D; yy = exp( -(xx * xx) ); return(R * yy); } // 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); } main() { #define k 8 int x, y, X, Y, pe = 1000; long e, o; char far *s; float slope; _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("THE SIGMOID FUNCTION'S FIRST DERIVATIVE FOR USE IN THE MLP LEARNING ALGORITHM\n"); _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."); _settextposition(20,46); printf("The blue shadow curve behind the"); _settextposition(21,46); printf("green is the pure Gaussian curve"); _settextposition(22,46); printf("y = exp(-(x*x)) commonly used in"); _settextposition(23,46); printf("radial basis function networks. "); _settextposition(23,3); printf(" x ="); _settextposition(24,3); printf(" f(x) = (blue)"); _settextposition(25,3); printf("f'(x) = +0.000 (green) f'(x) = (k/2)*(R + f(x))*(R - f(x))/(R*R)"); _settextposition(27,3); printf("f'(x) is plotted above as 32767 * (2 / k) times its actual value."); SigGen(); x = -32767; // Draw the sigmoid curve A: y = Sigmoid(x); _settextposition(23,11); printf("%+6d",x); _settextposition(24,11); printf("%+6d",y); _setcolor(9); _setpixel(Xorg + (x >> XYscale), Yorg - (y >> XYscale)); o = y; y = Gaussian(x); _setpixel(Xorg + (x >> XYscale), Yorg - (y >> XYscale)); e = (((R + o) * (R - o)) >> 15); y = e; _setcolor(10); _setpixel(Xorg + (x >> XYscale), Yorg - (y >> XYscale)); e = (e * pe) >> 13; slope = e; slope /= 1000; _settextposition(25,11); printf("%+5.3f",slope); if (x < 32751 && !kbhit()) { x += 16; goto A; } _settextposition(29,3); printf("FINISHED. HIT `RETURN' TO EXIT.\07"); _settextposition(29,1); getchar(); _setvideomode(_DEFAULTMODE); }