[C] 神经元模型 →→→→→进入此内容的聊天室

来自 , 2019-11-03, 写在 C, 查看 161 次.
URL http://www.code666.cn/view/ca43108d
  1. /*神经元模型*/
  2.  
  3. #include < math.h>
  4. #include < stdio.h>
  5. #include < time.h>
  6. #include < stdlib.h>
  7. #include < conio.h>
  8.  
  9. #define AND1 .571388
  10. #define AND2 .560999
  11. #define OR1 1.149045
  12. #define OR2 1.131896
  13. #define SITA 1
  14. #define NIU .1
  15. #define OMIGA 50
  16.  
  17. float w[2];
  18. float terg[4][3];
  19.  
  20. main() {
  21.         int m_result = 1, s;
  22.         float w1[2];
  23.         while ( m_result != 5 ) {
  24.                 m_result = menu ( m_result );
  25.                 date();
  26.                 switch ( m_result ) {
  27.                 case ( 1 ) :         /*    学习*/
  28.                 case ( 3 ) :
  29.                         randomize();  /*重新学习*/
  30.                         w[0] = random ( 80 ) +10;
  31.                         w[0] = w[0]/100.0;
  32.                         w[1] = random ( 80 ) +10;
  33.                         w[1] = w[1]/100.0;
  34.                         study();
  35.                         break;
  36.                 case ( 2 ) :
  37.                         practic ( w );   /*实践新模型*/
  38.                         break;
  39.                 case ( 4 ) :
  40.                         printf ( " what do you want to see, OR or AND?(1/2) " );
  41.                         scanf ( " %d" , & s );
  42.                         if ( s==1 ) {w1[0] = OR1;  w1[1] = OR2;  } else if ( s==2 ) {w1[0] = AND1; w1[1] = AND2; } else break;
  43.                         practic ( w1 );
  44.                         break;        /*演示已有模型*/
  45.                 default: {}
  46.                 }       /*End Switch*/
  47.         }               /*End While */
  48.         clrscr();
  49.         printf ( " Good Bye!" );
  50. }
  51. menu ( int r ) {
  52.         int r1;
  53.         clrscr();
  54.         printf ( " \n\n\n" );
  55.         printf ( " \t\t1\tStudy a Model\n" );   /*    学习    */
  56.         printf ( " \t\t2\tPratice the model\n" ); /*重新学习    */
  57.         printf ( " \t\t3\tDefail the MOdel\n" );  /*实践新模型  */
  58.         printf ( " \t\t4\tPrepared Model\n" );  /*演示已有模型*/
  59.         printf ( " \t\t5\tEnd\n" );             /*结束        */
  60.         printf ( " \n\n\t\tWhat do you want to choice?(%d)" , r );
  61.         r1 = getche();
  62.         if ( r1!=13 ) r = r1-48;
  63.         return ( r );
  64. }
  65.  
  66. date() {  /*显示模型*/
  67.         int i, j;
  68.         clrscr();
  69.         for ( i = 0; i < 4; i++ )
  70.                 for ( j = 0; j< 3; j++ ) {
  71.                         gotoxy ( 20+j*4, i+5 );
  72.                         printf ( "%.0f", terg[i][j] );
  73.                 }
  74.         printf ( "\n\n\tThe quarities:%f, %f \n", w[0], w[1] );
  75. }
  76.  
  77. study() { /*学习子函数*/
  78.         float x, y, width;
  79.         int wrong = 1, i, total = 0;
  80.         input();  /*andinput(); */ /*orinput();  */
  81.         while ( wrong ) {
  82.                 wrong = 0;
  83.                 for ( i = 0; i< 4; i++ ) {
  84.                         x = OMIGA* ( w[0]*terg[i][0]+w[1]*terg[i][1]-SITA );
  85.                         y = 1.0/ ( 1.0+exp ( -x ) );
  86.                         width = terg[i][2]-y;  /*误差 = "教师信号-Y" */
  87.                         if ( fabs ( width ) >=.01 ) {
  88.                                 wrong = 1;
  89.                                 total++;
  90.                                 w[0] = w[0]* ( 1+width*NIU );  /*改变权值0*/
  91.                                 w[1] = w[1]* ( 1+width*NIU );  /*改变权值1*/
  92.                                 if ( total> =200 )    /*防止程序进入死循环*/
  93.                                         {error(); return 1; }
  94.                         }
  95.                 }
  96.         }/*若i的四次循环均满足" fabs(width)> =.01" 则wrong=0, 结束循环*/
  97. }
  98. practic ( quarity ) /*实践新模型或演示已有模型, 由quarity[]值决定*/
  99. float quarity[2];
  100. {
  101.         int k, y;
  102.         float x, width, x1, x2;
  103.         for ( k = 0; k < 4; k++ ) {
  104.                 gotoxy ( 0, 4+k*3 );
  105.                 printf ( "\nPlease input :x1, x2 " );
  106.                 scanf ( "%f, %f", &x1, &x2 );
  107.                 x = OMIGA* ( quarity[0]*x1+quarity[1]*x2-SITA );
  108.                 y = 1.0/ ( 1.0+exp ( -x ) ) +.01;  /*精度为0.01*/
  109.                 printf ( "\t\t\ty = "%d" , y);
  110.                          getch();
  111.                  }
  112.                  }
  113.  
  114.                          input() /*当选择"学习"功能时输入事件*/
  115.                  {int i;
  116.         printf("Please input the 4 groups data:\n\n");
  117.                          for(i = 0; i< 4; i++){
  118.                          printf("\tThe %d group x1, x2, teacher's point: ", i+1);
  119.                          scanf("%f, %f, %f", &terg[i][0], &terg[i][1], &terg[i][2]);
  120.                  }
  121.                          clrscr();
  122.                  }
  123.  
  124.                          andinput()
  125.                  {terg[0][0] = 1; terg[0][1] = 1; terg[0][2] = 1;
  126.                          terg[1][0] = 1;
  127.                          terg[1][1] = 0;
  128.                          terg[1][2] = 0;
  129.                          terg[2][0] = 0;
  130.                          terg[2][1] = 1;
  131.                          terg[2][2] = 0;
  132.                          terg[3][0] = 0;
  133.                          terg[3][1] = 0;
  134.                          terg[3][2] = 0;
  135.                  }
  136.  
  137.                          orinput()
  138.                  {terg[0][0] = 1;
  139.                          terg[0][1] = 1;
  140.                          terg[0][2] = 1;
  141.                          terg[1][0] = 1;
  142.                          terg[1][1] = 0;
  143.                          terg[1][2] = 1;
  144.                          terg[2][0] = 0;
  145.                          terg[2][1] = 1;
  146.                          terg[2][2] = 1;
  147.                          terg[3][0] = 0;
  148.                          terg[3][1] = 0;
  149.                          terg[3][2] = 0;
  150.                  }
  151.  
  152.                          error() /*初始权值选择不当或模型无法实现(如异或模型)时退出*/
  153.                  {clrscr();
  154.                          printf("\n\n\n\tBecause the firse QUARITIES's choise is unavailble, \n");
  155.                          printf("\tIt can't get the right result, forveve!");
  156.                  }
  157.  

回复 "神经元模型"

这儿你可以回复上面这条便签

captcha