⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 backprop.c

📁 This is code tutorial for image processing include:histogram,sketon....
💻 C
📖 第 1 页 / 共 4 页
字号:
                        x[i]*h_delta[j*n + 0];
      }   /* ends loop over j */
   }  /* ends loop over i */
}  /* ends change_input_weights */
/***************************************************/
/*PAGE error_is_acceptable
       
   int error_is_acceptable(...
   
   This function looks at the error array.  If all
   elements are smaller than the MIN_ERROR, return
   1, else return 0.
*/
   
int error_is_acceptable(ELEMENT *errors, int size)
{
   int i, result = 1;
   for(i=0; i<size; i++){   
      if(el_abs(errors[i]) > (ELEMENT)(MIN_ERROR))
         result = 0;
   }  /* ends loop over i */
   return(result);
}  /* ends error_is_acceptable */
/***************************************************/
/*PAGE el_abs

   ELEMENT el_abs
   
   This function returns the absolute value of
   an ELEMENT number.
*/

ELEMENT el_abs(number)
   ELEMENT number;
{
   ELEMENT result;
   if(number < (ELEMENT)(0.0))
      result = (ELEMENT)(-1.0) * (ELEMENT)(number);
   else
      result = (ELEMENT)(number);
   return(result);      
}  /* ends el_abs */         
/***************************************************/
/*PAGE file_input_parameters

   void file_input_parameters(...
   
   This function gets all the parameters for 
   the program by reading an input text file.
   This allows the user to type these parameters
   one time in an ASCII file and use them over
   and over quickly.
   
   You store the program information in the ASCII
   file in the following format.  The parameters
   are kept on separate lines.  Each line begins
   with a keyword, then a space, then the value.
   You can put the lines in any order.  You
   YOU MUST USE THE CORRECT KEYWORD OR THIS ROUTINE
   WILL GET LOST!
   
   mode xxxx
   m xxxx
   n xxxx
   o xxxx 
   p xxxx
   data-sets xxxx
   type xxxx
   in-file xxxx
   out-file xxxx
   target-file xxxx
   whid-file xxxx
   win-file xxxx
   wout-file xxxx
*/

void file_input_parameters(char file_name[],
          char mode[], 
          int *m, 
          int *n, 
          int *o, 
          int *p,
          int *data_sets,
          char type[],
          char in_file_name[],
          char out_file_name[],
          char target_file_name[],
          char whid_file_name[],
          char win_file_name[],
          char wout_file_name[])
          
{
   char keyword[LENGTH], 
        string[LENGTH], 
        value[LENGTH];
        
   FILE *parameters_file;
   
   parameters_file = open_file(file_name, "r");
   
   while(fgets(string, LENGTH, parameters_file)){
      sscanf(string, "%s %s", keyword, value);  
      
      if(strcmp(keyword, "mode") == 0){
         strcpy(mode, value);
      }  /* ends if */

      if(strcmp(keyword, "m") == 0){
         *m = atoi(value);
      }  /* ends if */

      if(strcmp(keyword, "n") == 0){
         *n = atoi(value);
      }  /* ends if */

      if(strcmp(keyword, "o") == 0){  
         *o = atoi(value);
      }  /* ends if */

      if(strcmp(keyword, "p") == 0){
         *p = atoi(value);
      }  /* ends if */

      if(strcmp(keyword, "data-sets") == 0){
         *data_sets = atoi(value);
      }  /* ends if */

      if(strcmp(keyword, "type") == 0){
         strcpy(type, value);
      }  /* ends if */

      if(strcmp(keyword, "in-file") == 0){
         strcpy(in_file_name, value);
      }  /* ends if */

      if(strcmp(keyword, "out-file") == 0){
         strcpy(out_file_name, value);
      }  /* ends if */

      if(strcmp(keyword, "target-file") == 0){
         strcpy(target_file_name, value);
      }  /* ends if */

      if(strcmp(keyword, "whid-file") == 0){
         strcpy(whid_file_name, value);
      }  /* ends if */

      if(strcmp(keyword, "win-file") == 0){
         strcpy(win_file_name, value);
      }  /* ends if */

      if(strcmp(keyword, "wout-file") == 0){
         strcpy(wout_file_name, value);
      }  /* ends if */
      
   }  /* ends while fgets */
   
   fclose(parameters_file);
   
}  /* ends file_input_parameters */
/***************************************************/
/***************************************************/
/***************************************************/
/***************************************************/
/***************************************************/
/***************************************************/
/***************************************************/
/***************************************************/
/***************************************************/
/*PAGE ifdef test cases
*/




#ifdef MAT_MUL_TEST
   /* try h = x * y 
      x[1][3] y[3][2] = h[1][2] */

   x    = (ELEMENT  *) malloc((int)(3) * sizeof(ELEMENT));
   y    = (ELEMENT  *) malloc((int)(6) * sizeof(ELEMENT));
   h    = (ELEMENT  *) malloc((int)(2) * sizeof(ELEMENT));
   x[0] = 2; x[1] = 3; x[2] = 4;
   h[0] = 0; h[1] = 0;
   initialize_2d_weights(y, 3, 2);
   matrix_multiply(x, 1, 3, y, 2, h);
   display_2d_weights(x, 1, 3);
   display_2d_weights(y, 3, 2);
   display_2d_weights(h, 1, 2);
   exit(0);      
#endif


#ifdef NEVER
   initialize_3d_weights(h, n, p, p);
   initialize_3d_weights(wh, (n-1), n, m);
   display_3d_weights(h, n, p, p);
   display_3d_weights(wh, (n-1), n, m);
#endif                      


#ifdef TEST_3D_COPY
   n = 4;
   p = 3;
   m = 2;
   whid = (ELEMENT  *) malloc((int)((n-1)*p*m) * sizeof(ELEMENT));
   h    = (ELEMENT  *) malloc((int)(p*m) * sizeof(ELEMENT));
   initialize_3d_weights(whid, (n-1), p, m);
   copy_3d_to_2d(whid, h, (n-1), p, m, 2);
   display_2d_weights(h, p, m);
   display_3d_weights(whid, (n-1), p, m);
   free(whid);
   free(h);
#endif

#ifdef TEST_1D_COPY
   n = 4;
   p = 3;
   m = 2;
   whid = (ELEMENT  *) malloc((int)(p*m) * sizeof(ELEMENT));
   h    = (ELEMENT  *) malloc((int)(p) * sizeof(ELEMENT));
   initialize_2d_weights(whid, p, m);
   fill_array(h, m);
   display_2_weights(h, 1, p);
   copy_1d_to_2d(h, whid, p, m, 0, "row");
   display_2d_weights(whid, p, m);
   free(whid);
   free(h);
   exit(0);
#endif           
 

#ifdef TEST_2D_COPY
   n = 4;
   p = 3;
   m = 2;
   whid = (ELEMENT  *) malloc((int)(p*m) * sizeof(ELEMENT));
   h    = (ELEMENT  *) malloc((int)(p) * sizeof(ELEMENT));
   initialize_2d_weights(whid, p, m);
   fill_array(h, p);
   display_2d_weights(h, 1, p);
   copy_2d_to_1d(h, whid, p, m, 1, "col");
   display_2d_weights(whid, p, m);
   display_2d_weights(h, 1, p);
   free(whid);
   free(h);
   exit(0);
#endif           

#ifdef INNER_TEST
   p = 4;
   n = 3;
   h    = (ELEMENT  *) malloc((int)(p*n) * sizeof(ELEMENT));
   whid = (ELEMENT  *) malloc((int)((n-1)*p*p) * sizeof(ELEMENT));
   initialize_2d_weights(h, p, n);
   initialize_3d_weights(whid, (n-1), p, p);
   display_2d_weights(h, p, n);
   display_3d_weights(whid, (n-1), p, p);
   inner_layers(h, whid, p, n);
   display_2d_weights(h, p, n);
   display_3d_weights(whid, (n-1), p, p);      
   exit(0);
#endif


#ifdef INPUT_LAYER_TEST
   n = 3;
   m = 3;
   p = 4;
   x    = (ELEMENT  *) malloc((int)(m) * sizeof(ELEMENT));
   h    = (ELEMENT  *) malloc((int)(p*n) * sizeof(ELEMENT));
   win  = (ELEMENT  *) malloc((int)(m*p) * sizeof(ELEMENT));
   
   initialize_2d_weights(win, m, p);
   x[0] = 2; x[1] = 3; x[2] = 4;
   fill_array(h, (p*n));
   display_2d_weights(h, p, n);   
   input_layer(h, x, win, m, p, n);
   display_2d_weights(x, 1, m);
   display_2d_weights(win, m, p);
   display_2d_weights(h, p, n);
   exit(0);
#endif


#ifdef OUTPUT_LAYER_TEST
   n = 3;
   o = 5;
   p = 4;
   y    = (ELEMENT  *) malloc((int)(o) * sizeof(ELEMENT));
   h    = (ELEMENT  *) malloc((int)(p*n) * sizeof(ELEMENT));
   wout = (ELEMENT  *) malloc((int)(p*o) * sizeof(ELEMENT));
   initialize_2d_weights(wout, p, o);
   initialize_2d_weights(h, p, n);
   output_layer(h, wout, y, p, n, o);   
   display_2d_weights(h, p, n);   
   display_2d_weights(wout, p, o);
   display_2d_weights(y, 1, o);
   exit(0);
#endif

#ifdef INIT_TEST
   p = 3;
   m = 4;
   o = 5;
   n = 6;
   win  = (ELEMENT  *) malloc((int)(m*p) * sizeof(ELEMENT));
   wout = (ELEMENT  *) malloc((int)(p*o) * sizeof(ELEMENT));
   h    = (ELEMENT  *) malloc((int)(p*n) * sizeof(ELEMENT));
   whid = (ELEMENT  *) malloc((int)((n-1)*p*p) * sizeof(ELEMENT));   
   initialize_2d_weights(win, p, m);
   initialize_2d_weights(wout, o, p);
   display_2d_weights(win, p, m);
   display_2d_weights(wout, o, p);
   initialize_2d_weights(h, n, p);
   initialize_3d_weights(whid, (n-1), p, p);
   display_2d_weights(h, p, n);
   display_3d_weights(whid, (n-1), p, p);
   exit(0);
#endif                      

#ifdef ACTIVATE_TEST
   ELEMENT aa, bb;
   char    type[LENGTH];
   float   aaa;
   
   /********
   printf("%.4f", logistic(3.0));
   printf("%.4f", logistic(-3.0));
   printf("%.4f", logistic(33.0));
   printf("%.4f", logistic(23.0));
   printf("%.4f", logistic(.33));
   printf("%.4f", logistic(0.0));
   **********/

      
   strcpy(type, "sigmoid");
   aa = (ELEMENT)(3.0);
   bb = activation_function(aa, type);
   printf("\n the %s of %.4f = %.4f", type, aa, bb);   

   strcpy(type, "sigmoid");
   aa = (ELEMENT)(-3.0);
   bb = activation_function(aa, type);
   printf("\n the %s of %.4f = %.4f", type, aa, bb);   
   
   strcpy(type, "sigmoid");
   aa = (ELEMENT)(.3);
   bb = activation_function(aa, type);
   printf("\n the %s of %.4f = %.4f", type, aa, bb);   

   strcpy(type, "sigmoid");
   aa = (ELEMENT)(-.3);
   bb = activation_function(aa, type);
   printf("\n the %s of %.4f = %.4f", type, aa, bb);   
   
   exit(0);   
#endif




#ifdef FORWARD_TEST
   strcpy(type, "sigmoid");
   m = 3;
   n = 5;
   o = 2;
   p = 4; 
   
   x    = (ELEMENT  *) malloc((int)(m) * sizeof(ELEMENT));
   y    = (ELEMENT  *) malloc((int)(o) * sizeof(ELEMENT)); 
   win  = (ELEMENT  *) malloc((int)(m*p) * sizeof(ELEMENT));
   wout = (ELEMENT  *) malloc((int)(p*o) * sizeof(ELEMENT));
   h    = (ELEMENT  *) malloc((int)(p*n) * sizeof(ELEMENT));
   whid = (ELEMENT  *) malloc((int)((n-1)*p*p) * sizeof(ELEMENT));
   
   x[0] = 3; 
   x[1] = -4; 
   x[2] = 5;
   initialize_2d_weights(win, m, p);
   initialize_2d_weights(wout, p, o);
   initialize_3d_weights(whid, (n-1), p, p);

   input_layer(h, x, win, m, p, n, type);
   inner_layers(h, whid, p, n, type);
   output_layer(h, wout, y, p, n, o, type);   
   
   printf("\nThis is the input array");
   display_2d_weights(x, 1, m);
   printf("\n\nThis is the win array");
   display_2d_weights(win, m, p);
   printf("\n\nThis is the h array");
   display_2d_weights(h, p, n);      
   printf("\n\nThis is the whid array");
   display_3d_weights(whid, (n-1), p, p);
   printf("\n\nThis is the output array");
   display_2d_weights(y, 1, o);
   
   exit(0);
   
#endif


#ifdef FILE_TEST 
   m = 3;
   n = 5;
   o = 2;
   p = 4; 

   strcpy(in_file_name, "input.xxx");
   strcpy(out_file_name, "output.xxx");
   strcpy(whid_file_name, "whid.xxx");
   strcpy(win_file_name, "win.xxx"); 
 
   if( (in_file = fopen(in_file_name, "w+b")) == '\0'){
      printf("\n\nERROR - cannot open input vector file\n");
      exit(0);
   }
   
   x    = (ELEMENT  *) malloc((int)(m) * sizeof(ELEMENT));

   x[0] = 3; 
   x[1] = -4; 
   x[2] = 5;
   
   fwrite(x, (m)*sizeof(ELEMENT), 1, in_file);
   
   fclose(in_file);  
   
   x[0] = FILL;
   x[1] = FILL;
   x[2] = FILL; 
   
   if( (in_file = fopen(in_file_name, "r+b")) == '\0'){
      printf("\n\nERROR - cannot open input vector file\n");
      exit(0);
   }
   
   fread(x, (m)*sizeof(ELEMENT), 1, in_file); 
   fclose(in_file);
   
   printf("\nThis is the input array");
   display_2d_weights(x, 1, m);
   
   
   free(x);
   exit(0);
   
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -