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

📄 backprop.c

📁 This is code tutorial for image processing include:histogram,sketon....
💻 C
📖 第 1 页 / 共 4 页
字号:
      output_layer(h, wout, y, p, n, o, type);   

printf("\nThe input was");
display_2d_weights(x, 1, m);      
printf("\n\nResults: (output)");
display_2d_weights(y, 1, o);      
   
      fclose(in_file);
      fclose(out_file);
      fclose(win_file);
      fclose(whid_file);
      fclose(wout_file);
   
   }  /* ends if working */

  
      /*********************************
      *
      *   Free each of the arrays
      *
      **********************************/

   free(x);
   free(y);
   free(delta);
   free(h_delta);
   free(target);
   free(h);
   free(whid);
   free(win);
   free(wout);   
   free(tmp_x);
   free(tmp_target);
   return(1);
}  /* ends main */

/***************************************************/
/*PAGE input_layer

   void input_layer(...

   This function performs the calculations
   between the input array and the input weights
   to produce the first layer of neurons.
   
   x[1][m] win[m][p] = first column of h[p][n]
   
   Use a tmp array in the call to matrix_multiply
   then copy the answer back to the first column
   of h.
*/

void input_layer(ELEMENT *h, 
                 ELEMENT *x, 
                 ELEMENT *win, 
                 int     m,
                 int     p,
                 int     n,
                 char    type[])
{
   ELEMENT *tmp;
   tmp = (ELEMENT  *) 
         malloc((int)(p) * sizeof(ELEMENT));

   zero_array(tmp, p);      
   matrix_multiply(x, 1, m, win, p, tmp);
   apply_function(tmp, p, type);
   copy_1d_to_2d(tmp, h, p, n, 0, "col");
   free(tmp);      
   
}  /* ends input_layer */

/***************************************************/
/*PAGE output_layer

   void output_layer(...

   This function performs the calculations
   between the last layer of neurons and the output
   weights to produce the outputs of the network.
   
   last column of h[p][n] wout[p][o] = y[o][1]
   
*/

void output_layer(ELEMENT *h,
                  ELEMENT *wout, 
                  ELEMENT *y, 
                  int     p,
                  int     n,
                  int     o,
                  char    type[])
{
   ELEMENT *tmp;
   
   tmp = (ELEMENT  *) 
         malloc((int)(p) * sizeof(ELEMENT));
   copy_2d_to_1d(tmp, h, p, n, (n-1), "col");   
   zero_array(y, o);      
   matrix_multiply(tmp, 1, p, wout, o, y);
   apply_function(y, o, type);
   free(tmp);      
   
}  /* ends output_layer */

/***************************************************/
/*PAGE inner_layers

   void inner_layers(...
   
   This function performs the forward calculations
   for the inner layers.
   h[p][n]  whid[(n-1)][p][p]
   
   Perform the multiplications by using the matrix
   multiplication function and temporary arrays.
   
   for i=0; i<n-1; i++
      Copy the ith column of h to a tmp1 array
      Copy the ith pXp array of whid to a tmp2 array
      matrix_multiply tmp1 X tmp2 = tmp3
      Copy tmp3 to the i+1 column of h
   end loop over i   
*/

void inner_layers(ELEMENT *h,
                  ELEMENT *whid,
                  int     p,
                  int     n,
                  char    type[])
{        
   ELEMENT *tmp1, *tmp2, *tmp3;
   int i;
   
   tmp1 = (ELEMENT  *) 
          malloc((int)(p) * sizeof(ELEMENT));
   tmp2 = (ELEMENT  *) 
          malloc((int)(p*p) * sizeof(ELEMENT));
   tmp3 = (ELEMENT  *) 
          malloc((int)(p) * sizeof(ELEMENT));
     
   for(i=0; i<(n-1); i++){
      zero_array(tmp3, p);
      copy_2d_to_1d(tmp1, h, p, n, i, "col");
      copy_3d_to_2d(whid, tmp2, (n-1), p, p, i);
      matrix_multiply(tmp1, 1, p, tmp2, p, tmp3);
      apply_function(tmp3, p, type);
      copy_1d_to_2d(tmp3, h, p, n, (i+1), "col");
   }  /* ends loop over n-1 inner layers */
   
   free(tmp1);
   free(tmp2);
   free(tmp3);
}  /* ends inner_layers */                     

/***************************************************/
/*PAGE matrix_multiply

   void matrix_multiply(...
   
   This function performs basic matrix multiplication.
   A[m][n] * B[n][p] = C[m][p]
*/
void matrix_multiply(ELEMENT *A, int m, int n, 
                     ELEMENT *B, int p, 
                     ELEMENT *C)
{
   int i, j, k;
   for(i=0; i<m; i++){
      for(j=0; j<p; j++){
         for(k=0; k<n; k++){
            /** C[i][j] = 
                C[i][j] + A[i][k] * B[k][j] **/
            C[i*p + j] = 
               C[i*p + j] + A[i*n + k]*B[k*p + j];
         }  /* ends loop over k */
      }  /* ends loop over j */
   }  /* ends loop over i */
}  /* ends matrix_multiply */                 
/***************************************************/
/*PAGE activation_function

   ELEMENT activation_function(...
   
   This function applies the sigmoid
   to an input ELEMENT.  It returns the
   result of the function.

*/

ELEMENT activation_function(input, type)
   char    type[];
   ELEMENT input;
{          
   double  a, b, c;
   ELEMENT result = (ELEMENT)(0.0);

   if( strcmp(type, "sigmoid") == 0){
      a      = (double)(input);
      b      = exp(-1.0*a);
      c      = 1.0/(1.0 + b);
      result = (ELEMENT)(c);
   }
   else{
      printf("\nERROR: must use sigmoid function");
      exit(2);
   }

   return(result);
   
}  /* ends activation_function */
/***************************************************/
/*PAGE apply_function

   void apply_function(...
   
   This function applies the selected activation 
   function to each element in an array.
*/

void apply_function(ELEMENT *array, 
                    int     size, 
                    char    type[])   
{
   ELEMENT tmp;
   int i;
   for(i=0; i<size; i++){
      tmp = activation_function(array[i], type);
      array[i] = tmp;
   }
}  /* ends apply_function */                    
/***************************************************/
/*PAGE initialize_2d_weights

   void initialize_2d_weights(...

   This function initializes the weights in th
   2-dimensional array.  They are set
   to 0 2 4 6 0 2 4 6 ...
*/

void initialize_2d_weights(ELEMENT *array, 
                           int a, 
                           int b)
{
   ELEMENT count = (ELEMENT)(0.0);
   int i, j, odd = 0;;
   for(i=0; i<a; i++){
      for(j=0; j<b; j++){
         odd++;
         count = (ELEMENT)(rand());
         while(count > 1.0) 
            count = count/(ELEMENT)(10.0);
         if( (odd % 2) == 1) 
            count = count*(ELEMENT)(-1.0);
         /****array[(i*a) + j] = 
              (ELEMENT)((i*a) + j);****/
         array[(i*b) + j] = count;
      }  /* ends loop over j */
   }  /* ends loop over i */
}  /* ends initialize_2d_weights */

/***************************************************/
/*PAGE initialize_3d_weights

   void initialize_3d_weights(...

   This function initializes the weights in the
   2-dimensional array.  They are set
   to 0 2 4 6 0 2 4 6 ...
*/

void initialize_3d_weights(ELEMENT *array, 
                           int a, 
                           int b, 
                           int c)
{
   ELEMENT count = (ELEMENT)(0.0);
   int i, j, k, odd = 0;;
   for(i=0; i<a; i++){
      for(j=0; j<b; j++){
         for(k=0; k<c; k++){
            odd++;  
            count = (ELEMENT)(rand());
            while(count > 1.0) 
               count = count/(ELEMENT)(10.0);
            if( (odd % 2) == 1) 
               count = count*(ELEMENT)(-1.0);
            array[(i*b*c) + (j*c) + k] = count;
         }  /* ends loop over k */
      }  /* ends loop over j */
   }  /* ends loop over i */
            
}  /* ends initialize_3d_weights */
/***************************************************/
/*PAGE display_2d_weights

   void display_2d_weights(...

   This function displays the 2-dimemsional
   weights to the screen.  array[a][b]
*/

void display_2d_weights(ELEMENT *array, int a, int b)
{  
   int i, j;
   printf("\n  %d X %d", a, b);
   for(i=0; i<a; i++){
      printf("\n2D - %d>>", i);
      for(j=0; j<b; j++){
         printf("%.4f ", array[(i*b) + j]);
      }
   }
}  /* ends display_2d_weights */


/***************************************************/
/*PAGE display_3d_weights

   void display_3d_weights(...

   This function displays the 3-dimemsional
   weights to the screen.  array[a][b][c]
*/

void display_3d_weights(ELEMENT *array, 
                        int a, 
                        int b, 
                        int c)
{
   int i, j, k; 
   printf("\n\n%d X %d X %d", a, b, c);
   for(i=0; i<a; i++){
      printf("\n\n3D>>");
      for(j=0; j<b; j++){
         printf("\n%d>>", i);
         for(k=0; k<c; k++){            
            printf("%.4f ", 
                   array[(i*b*c) + (j*c) + k]);
         }
      }
   }
}  /* ends display_3d_weights */

/***************************************************/
/*PAGE fill_array

   void fill_array(...
   
   This function sets all the elements of an array
   to the FILL value.
*/

void fill_array(ELEMENT *array, int size)
{
   int i;
   for(i=0; i<size; i++)
      array[i] = (ELEMENT)(FILL);
}  /* ends fill_array */
/***************************************************/
/*PAGE zero_array

   void zero_array(...
   
   This function sets all the elements of an array
   to zero.
*/

void zero_array(ELEMENT *array, int size)
{
   int i;
   for(i=0; i<size; i++)
      array[i] = (ELEMENT)(0.0);
}  /* ends zero_array */
/***************************************************/
/*PAGE copy_3d_to_2d

   void copy_3d_to_2d(...
   
   This function copies a 2D array from a big 3D
   array down into a simple 2D array.
   three[a][b][c] copies this 2D array down into
   two[b][c]
*/

void copy_3d_to_2d(ELEMENT *three,
                   ELEMENT *two,
                   int a,
                   int b,
                   int c,
                   int this)
{
   int i, j;
   for(i=0; i<b; i++){
      for(j=0; j<c; j++){
         /** two[i][j] = three[this][i][j] **/
         two[i*c + j] = three[this*b*c + i*c + j];
      }
   }
}  /* ends copy_3d_to_2d */                   
/***************************************************/
/*PAGE copy_1d_to_2d

   void copy_1d_to_2d(...
   
   This function copies a 1D array into a 2D array.
   You can copy the 1D array into either this row 
   of the 2D array (option = "row") or this column
   of the 2D array (option = "col").
   one[1][a] or one [1][b]
   two[a][b]
*/

void copy_1d_to_2d(ELEMENT *one,
                   ELEMENT *two,
                   int     a,
                   int     b,
                   int     this,
                   char    option[])
{
   int i; 
   
   if(strncmp(option, "row", 3) == 0){
      for(i=0; i<b; i++){
         /** two[this][i] = one[i] **/
         two[this*b + i] = one[i];
      }
   }  /* ends if row */

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

}  /* ends copy_1d_to_2d */                   
/***************************************************/ 
/*PAGE copy_2d_to_1d

   void copy_2d_to_1d(...
   
   This function copies this row or column from a 2D
   array into a 1D array.  The option parameter must
   equal either "row" or "col".
   one[1][a] or one [1][b]
   two[a][b]
*/

void copy_2d_to_1d(ELEMENT *one,
                   ELEMENT *two,
                   int     a,

⌨️ 快捷键说明

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