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

📄 backprop.c

📁 This is code tutorial for image processing include:histogram,sketon....
💻 C
📖 第 1 页 / 共 4 页
字号:
                   int     b,
                   int     this,
                   char    option[])
{
   int i; 
   
   if(strncmp(option, "row", 3) == 0){
      for(i=0; i<b; i++){
         /** one]i] = two[this][i] **/
         one[i] = two[this*b + i];
      }
   }  /* ends if row */

   if(strncmp(option, "col", 3) == 0){
      for(i=0; i<a; i++){
         /** one[i] = two[i][this] **/
         one[i] = two[i*b + this];
      }
   }  /* ends if col */

}  /* ends copy_2d_to_1d */                   
/***************************************************/
/*PAGE lower_case_string

   void lower_case_string(string)
   
   This function converts the characters in the
   string to lower case.
   
*/

void lower_case_string(char string[])
{
   int i, c;
   for(i=0; i<(int)(strlen(string)); i++){
      c         = string[i];
      string[i] = tolower(c);
   }
}  /* ends lower_case_string */   
/***************************************************/
/*PAGE get_program_parameters

   void get_program_parameters(...
   
   This function gets all the parameters for 
   the program.  You can either enter the parameters
   interactively or have the program read them
   from an ASCII file.
*/

void get_program_parameters(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 string[LENGTH];
   int  choice;

   printf("\nEntering the program parameters");
   printf("\nEnter input from");
   printf("\n   1. keyboard");
   printf("\n   2. file");
   printf("\n   :");
   gets(string);
   choice = atoi(string);
   
   if(choice == 1){  /* keyboard input */
   
      printf("\nEnter the mode ");
      printf("(input, training, working)");
      gets(mode);
      lower_case_string(mode);
   
      printf("\nEnter m (# of inputs): ");
      gets(string);
      *m = atoi(string);
   
      printf("\nEnter n (# of hidden layers): ");
      gets(string);
      *n = atoi(string);
   
      printf("\nEnter o (# of outputs): ");
      gets(string);
      *o = atoi(string);
   
      printf("\nEnter p (# of neurons per ");
      printf("hidden layer): ");
      gets(string);
      *p = atoi(string);

      printf("\nEnter data_sets (# of data sets): ");
      gets(string);
      *data_sets = atoi(string);
      
      printf("\nEnter the input file name:");
      gets(in_file_name);
      
      printf("\nEnter the output file name:");
      gets(out_file_name);
      
      printf("\nEnter the target file name:");
      gets(target_file_name);
      
      printf("\nEnter the input weights file name:");
      gets(win_file_name);
      
      printf("\nEnter the hidden weights file name:");
      gets(whid_file_name);
      
      printf("\nEnter the output weights file name:");
      gets(wout_file_name);
      
   }  /* ends if choice == 1, keyboard */
   
   else{  /* file input */
      printf("\nEnter the name of the file");
      printf("\ncontaining the program parameters");
      printf("\n   :");
      gets(string);
      file_input_parameters(string, 
                            mode, 
                            m, 
                            n, 
                            o, 
                            p,
                            data_sets,
                            type,
                            in_file_name,
                            out_file_name,
                            target_file_name,
                            whid_file_name,
                            win_file_name,
                            wout_file_name);
      
   }  /* ends else file input */

}  /* ends get_program_parameters */          

/***************************************************/
/*PAGE get_array_from_user

   void get_array_from_user(...
   
   This function interacts with the user so the 
   user can type in the elements of an array. 
   
   The user can choose to have the program read
   the array from an ASCII text file.  This makes it
   easier to enter and change the numbers using an
   editor instead of interacting with the program.
   The numbers in the ASCII file are kept one
   number per line.
*/

void get_array_from_user(ELEMENT *array, int size)
{
   char string[LENGTH];
   FILE *array_file;
   int  choice, i;  
   
   printf("\nEnter input from");
   printf("\n   1. keyboard");
   printf("\n   2. file");
   printf("\n   :");
   gets(string);
   choice = atoi(string);
   
   if(choice == 1){  /* keyboard input */   
   
      for(i=0; i<size; i++){
         printf("\nEnter element %d: ", i);
         gets(string);
         array[i] = (ELEMENT)(atof(string));
      }  /* ends loop over i */
   }  /* ends keyboard input */
   
   else{  /* read array from an ASCII file */ 
   
      printf("\nEnter the name of the file");
      printf("\ncontaining the array elements");
      printf("\n   :");
      gets(string);
      array_file = open_file(string, "r");
      i = 0;
      while(fgets(string, LENGTH, array_file)){
         array[i] = (ELEMENT)(atof(string));       
         i++;
      }  /* ends while fgets */
      fclose(array_file);

   }  /* ends else read from an ASCII file */
   
}  /* ends get_array_from_user */   
/***************************************************/
/*PAGE open_file

   void open_file(...
   
   This function opens a file using the attributes
   passed to it for the file.
*/

FILE *open_file(file_name, attributes)
   char *file_name, *attributes;
{  
   FILE *file_pointer;
   if((file_pointer = 
          fopen(file_name, attributes)) == '\0'){
      printf(
         "\n\nERROR - cannot open file %s\n",
         file_name);
      exit(0);
   }
   
   return(file_pointer);

}  /* ends open_file */               
/***************************************************/
/*PAGE write_to_file

   void write_to_file(...
   
   This function writes an array of ELEMENT 
   to a file.
*/

void write_to_file(ELEMENT *array,
                   int     size,
                   FILE    *file_pointer)
{
   int written = -1;
   written = fwrite(array,
                    size*sizeof(ELEMENT), 
                    1, 
                    file_pointer); 

}  /* ends write_to_file */                      
/***************************************************/
/*PAGE read_from_file

   void read_from_file(...
   
   This function reads an array of ELEMENT 
   from a file.
*/

void read_from_file(ELEMENT *array,
                   int     size,
                   FILE    *file_pointer)
{
   int read = -1;
   read = fread(array,
                size*sizeof(ELEMENT), 
                1, 
                file_pointer); 

}  /* ends read_from_file */                      
/***************************************************/
/*PAGE output_layer_error

   void output_layer_error(...
   
   This function computes the error found in the 
   output layer and places the error in the delta
   array.
*/
   
void output_layer_error(ELEMENT *y, 
                        ELEMENT *target, 
                        ELEMENT *delta, 
                        int     o)
{                        
   ELEMENT one = (ELEMENT)(1.0);
   int i;
   
   for(i=0; i<o; i++){
      delta[i] = y[i]*(one - y[i])*(target[i] - y[i]);
   }
}  /* ends output_layer_error */
/***************************************************/ 
/*PAGE neuron_deltas

   void neuron_deltas(...
   
   This function calculates the deltas for the 
   neurons in the network.  It starts are the
   output stage of the network and works its
   way back through the network to the input
   layer.
*/   

void neuron_deltas(ELEMENT *h, 
                   ELEMENT *h_delta, 
                   ELEMENT *delta, 
                   ELEMENT *whid,
                   ELEMENT *wout, 
                   int     n, 
                   int     o,
                   int     p)
{                   
   ELEMENT *tmp, *tmph, *tmpw;
   ELEMENT one = (ELEMENT)(1.0);
   int     i, j;
   
   
   tmp  = (ELEMENT  *) 
          malloc((int)(p) * sizeof(ELEMENT));
   tmph = (ELEMENT  *) 
          malloc((int)(p) * sizeof(ELEMENT));
   tmpw = (ELEMENT  *) 
          malloc((int)(p*p) * sizeof(ELEMENT));

      /**********************************
      *
      *   First, find the deltas for the
      *   last layer of neurons using the
      *   delta and wout arrays.
      *
      *************************************/   
      
   zero_array(tmp, p);
   matrix_multiply(wout, p, o, delta, 1, tmp);

   for(i=0; i<p; i++){
      /**h_delta[i][n-1] = 
         h[i][n-1] * (one - h[i][n-1]) * tmp[i];**/
      h_delta[i*n + n-1] = 
         h[i*n + n-1] * (one - h[i*n + n-1]) * tmp[i];
      
   }                                            

      /************************************
      *
      *   Now, find the deltas for all the
      *   other layers of neurons.
      *
      *************************************/   

   for(i=(n-1); i>0; i--){

      copy_3d_to_2d(whid, tmpw, (n-1), p, p, (i-1));
      copy_2d_to_1d(tmph, h_delta, p, n, i, "col");

      zero_array(tmp, p);
      matrix_multiply(tmpw, p, p, tmph, 1, tmp);
      
      for(j=0; j<p; j++){
         /**h_delta[j][i-1] = 
            h[j][i-1]*(one - h[j][i-1])*tmp[j];**/
         h_delta[j*n + i-1] = 
            h[j*n + i-1]*(one - h[j*n + i-1])*tmp[j];
      }  /* ends loop over j */
   }  /* ends loop over i */
      
   free(tmp);
   free(tmph);
   free(tmpw);
}  /* ends neuron_deltas */
/***************************************************/
/*PAGE change_output_weights

   void change_output_weights(...
   
   This function modifies the weights in the wout
   matrix.
*/
   
void change_output_weights(ELEMENT *wout, 
                           ELEMENT *delta, 
                           ELEMENT *h, 
                           int     n,
                           int     o, 
                           int     p)
{
   int i, j;
   
   for(i=0; i<p; i++){
      for(j=0; j<o; j++){
         /**wout[i][j] = 
            wout[i][j] + TWOMU*h[i][n-1]*delta[j];**/
         wout[i*o + j] = wout[i*o + j] +
           (ELEMENT)(TWOMU) * h[i*n + n-1] * delta[j];
      }  /* ends loop over j */
   }  /* ends loop over i */
}  /* ends change_output_weights */
/***************************************************/
/*PAGE change_hidden_weights

   void change_hidden_weights(...
   
   This function modifies the weights in the whid
   matrix.
*/
   
void change_hidden_weights(ELEMENT *whid, 
                           ELEMENT *h, 
                           ELEMENT *h_delta, 
                           int     n,
                           int     p)
{
   int i, j, k;
   
   for(i=(n-1); i>0; i--){
      for(j=0; j<p; j++){
         for(k=0; k<p; k++){
            /**whid[i-1][j][k] = wout[i-1][j][k]
                  + TWOMU*h[j][i-1]*h_delta[k][i];**/
                  
            whid[(i-1)*p*p + j*p + k] = 
               whid[(i-1)*p*p + j*p + k] +
               (ELEMENT)(TWOMU) *
               h[j*n + i-1] *
               h_delta[k*n + i];
               
         }  /* ends loop over k */
      }  /* ends loop over j */
   }  /* ends loop over i */
}  /* ends change_hidden_weights */
/***************************************************/
/*PAGE change_input_weights

   void change_input_weights(...
   
   This function modifies the weights in the 
   win matrix.
*/

void change_input_weights(ELEMENT *x, 
                          ELEMENT *win, 
                          ELEMENT *h_delta, 
                          int     m, 
                          int     n, 
                          int     p)
{                          
   int i, j;
   
   for(i=0; i<m; i++){
      for(j=0; j<p; j++){
         /**win[i][j] = win[i][j] + 
                        TWOMU*x[i]*h_delta[j][0];**/
         win[i*p + j] = win[i*p + j] +              
                        (ELEMENT)(TWOMU) *

⌨️ 快捷键说明

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