Xem mẫu

  1. EPS=1.0; do { EPS/=2.0; tol1=1.0+EPS; } while(tol1>1.0); EPS=(float)sqrt((double)EPS); df=(float *)malloc(N*sizeof(float)); dfp=(float *)malloc(N*sizeof(float)); S=(float *)malloc(N*sizeof(float)); xt=(float *)malloc(N*sizeof(float)); dfun(x,df,N); for(i=0;i
  2. if( df[i]*S[i]>0.0){ test=-1.0; break; } } if(test
  3. float f( float alpha, float (*fun)(float *),float *x, float *xt, float *S, int N) { int i; float q; for(i=0;i=y0) { dx=-dx; x1=x0+dx; y1=f(x1,fun,x,xt,s,N); } dx=2.0*dx; x2=x1+dx; y2=f(x2,fun,x,xt,s,N); iter=0 ; while(y2
  4. iter++; dx=2.0*dx; x0=x1; y0=y1; x1=x2; y1=y2; x2=x1+dx; y2=f(x2,fun,x,xt,s,N); } *a=x0; *b=x2; } /* Brent's algorithm for obtaining the minimum of a single variable function. */ #define CGOLD 0.381966 float Brent(float ax, float bx , float (*fun) (float *) , float TOL, float *x,float *xt, float *S, int N) { float a,b,u,v,w,xx,e,fx,fv,fu,fw,xm,tol1,tol2,c,r,q,p; int iter; a=ax; b=bx; v=a+CGOLD*(b-a); w=v; xx=v; e=0.0; fx=f(xx,fun,x,xt,S,N); fv=fx; fw=fx; c=0.0; iter=0; while(iter
  5. xm=0.5*(a+b); tol1=EPS*(float)fabs((double)xx)+TOL/3.0; tol2=2.0*tol1; if((float)fabs((double)(xx-xm))tol1) { r=(xx-w)*(fx-fv); q=(xx-v)*(fx-fw); p=(xx-v)*q-(xx-w)*r; q=2.0*(q-r); if(q>0.0) p=p; q=(float)fabs((float)q); r=e; e=c; /* is parabola acceptable.*/ if(((float)fabs((double)p) q*(a-xx))|| (p < q*(b-xx))) {/* fit parabola.*/ if(q==0.0) q=1.e-10; c=p/q; u=xx+c; /* f must not be evaluated too close to a or b. */ if( (((u-a)=xm) e=a-xx; else e=b-xx; c=CGOLD*e; } 314
  6. /* update a,b,v,w, and x. */ l2: if(fabs((double)c)>=tol1) u=xx+c; else u=xx+((c>0.0)?tol1:-tol1); fu=f(u,fun,x,xt,S,N); if(fu=xx) a=xx; else b=xx; v=w; fv=fw; w=xx; fw=fx; xx=u ; fx=fu; continue; } else { if(u
  7. liÖu cã thÓ biÓu diÔn trªn s¬ ®å mµu nh­ h×nh 12.13. Ch­¬ng tr×nh cã thÓ bÞ ng¾t t¹i bÊt kú lóc nµo b»ng c¸ch Ên phÝm ESC. KÕt qu¶ cña hÖ thèng sÏ ®­îc l­u mét c¸ch tù ®éng trong mét file ®Æc biÖt cã tªn ban ®Çu do ng­êi dïng ®Æt. NÕu sau ®ã b¹n muèn tiÕp tôc víi "®µo t¹o", b¹n cÇn quay trë vÒ ch­¬ng tr×nh chÝnh, nh­ng lÇn nµy tr¶ lêi "y" khi ch­¬ng tr×nh hái b¹n: B¹n cã muèn dïng c¸c träng sè ®­îc ®µo t¹o tr­íc kh«ng? Lý do ph¶i cã phÝm ESC lµ ®µo t¹o ®ßi hái mét thêi gian dµi vµ b¹n muèn ng¾t ch­¬ng tr×nh, dïng m¸y tÝnh vµo c¸c viÖc kh¸c. §Ó ¸p dông, chóng ta dïng mét mét perceptron ba líp (còng cã thÓ dïng perceptron hai líp), víi líp che khuÊt ®Çu tiªn cã t¸m nót, líp che khuÊt thø hai cã bèn nót, vµ líp ra chØ cã mét nót. Líp vµo, tÊt nhiªn lµ chØ cã hai nót, mét cho x vµ mét cho y - c¸c biÕn cña biÓu ®å mµu. TÝn hiÖu ra cña hÖ thèng sÏ lµ 1 nÕu d÷ liÖu biÓu diÔn cho s¾c mµu skin, vµ 0 cho c¸c tr­êng hîp cßn l¹i. Ch­¬ng tr×nh b¾t ®Çu b»ng mét sè tÝnh ngÉu nhiªn gi÷a 0 vµ 1, vµ ®ßi hái gÇn 17,000 phÐp lÆp vµ h¬n 5 giê tÝnh to¸n trªn m¸y 486-25 MHz. Sai lÖch gi¶m xuèng tõ 32 xuèng 0.0057. File chøa hÖ thèng nµy, ch¼ng h¹n nh­, sè c¸c líp, sè c¸c ®iÓm trong mçi líp, vµ träng sè cã s½n trªn ®Üa d­íi tªn " WTSST.DAT". H×nh 12.13 "TINT2.DAT" dïng thö m¹ng thÇn kinh. B©y giê chóng ta cÇn ph¸t triÓn mét ch­¬ng tr×nh ®Ó kiÓm tra c¸ch lµm viÖc thùc sù cña hÖ thèng. LiÖt kª cho tÊt c¶ c¸c thuËt to¸n nµy trong mét ch­¬ng tr×nh ®­îc cho ë d­íi ®©y. Ch­¬ng tr×nh 12.4 TESNLYE.C . KiÓm tra. 316
  8. /* Program 12.4 "TESNLYE.C". Testing a multilayer network.*/ /************************************ * Developed by M.A.Sid-Ahmed. * * ver. 1.0, 1992. * * @ 1994 * *************************************/ /* Program for testing a multi-layer perceptron. */ void float fun(float *); #include #include #include #include int M,*NL,*NS,L; int *d; float *xp,*y,*net,*delta,theta; void main() { float *w,q,xt; int i,j,N,xd,ind,Nt; char file_name[14],file_name2[14],ch; FILE *fptr,*fptr2; clrscr(); printf("\nEnter file_name for weights-->"); scanf("%s",file_name); fptr=fopen(file_name,"r"); if(fptr==NULL) { printf("file %s does not exist. ",file_name); exit(1); } fscanf(fptr,"%d ",&L); NL=(int *)malloc(L*sizeof(int)); NS=(int *)malloc((L-2)*sizeof(int)); 317
  9. for(i=0;i
  10. if((fscanf(fptr2,"%f ",&xt))==EOF) /* input data. */ { ind=0; break; } } if(ind==0) break; for(i=0;i
  11. void fun(float *w) { int i,j,k,m,n,Nt1,Nt2; float error, E; for(k=0;k
nguon tai.lieu . vn