home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SNNSV32.ZIP / SNNSv3.2 / tools / sources / netlearn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-25  |  5.3 KB  |  170 lines

  1. /*****************************************************************************
  2.   FILE           : netlearn.c
  3.   SHORTNAME      : 
  4.   SNNS VERSION   : 3.2
  5.  
  6.   PURPOSE        : SNNS-Kernel Backpropagation Test Program
  7.   NOTES          :
  8.  
  9.   AUTHOR         : Niels Mache 
  10.   DATE           : 19.10.90
  11.  
  12.   CHANGED BY     : 
  13.   IDENTIFICATION : @(#)netlearn.c    1.8 4/12/94
  14.   SCCS VERSION   : 1.8
  15.   LAST CHANGE    : 4/12/94
  16.  
  17.              Copyright (c) 1990-1994  SNNS Group, IPVR, Univ. Stuttgart, FRG
  18.  
  19. ******************************************************************************/
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25.  
  26. /*  SNNS-Kernel constants and data type definitions  */
  27. #include "glob_typ.h"
  28. /*  SNNS-Kernel User-Interface Function Prototypes   */
  29. #include "kr_ui.h"
  30.  
  31.  
  32. static void  errChk( err_code )
  33. int  err_code;
  34. {
  35.   if (err_code != KRERR_NO_ERROR)  {
  36.     printf( "%s\n", krui_error( err_code ));
  37.     exit( 1 );
  38.   }
  39. }
  40.  
  41. int main()
  42. {
  43.   int   ret_code, N, i, j, no_of_sites, no_of_links, no_of_units,
  44.     no_of_patterns, dummy, NoOfReturnVals, no_of_input_params,
  45.         no_of_output_params, step, set_no;
  46.   char    *netname, file_name[80];
  47.   bool    shuffle;
  48.   float learn_parameters[5], updateParameterArray[5],
  49.     parameterInArray[5], sum_error;
  50.   float *return_values;
  51.  
  52.  
  53.   printf( "\n%s\n", krui_getVersion() );
  54.   printf( "----  Network Learning -----\n\n" );
  55.  
  56.   printf("Filename of the network file: ");
  57.   scanf("%s", file_name);
  58.  
  59.   printf( "Loading the network ...\n\n" );
  60.   ret_code = krui_loadNet( file_name, &netname );
  61.   errChk( ret_code );
  62.   krui_getNetInfo( &no_of_sites, &no_of_links, &dummy, &dummy );
  63.   no_of_units = krui_getNoOfUnits();
  64.   printf( "Network name: %s\n", netname );
  65.   printf( "No. of units       : %d\n", no_of_units );
  66.   printf( "No. of input units : %d\n", krui_getNoOfTTypeUnits( INPUT ) );
  67.   printf( "No. of output units: %d\n", krui_getNoOfTTypeUnits( OUTPUT ) );
  68.   printf( "No. of sites: %d\n", no_of_sites );
  69.   printf( "No. of links: %d\n\n", no_of_links );
  70.   printf( "Learning function: %s\n", krui_getLearnFunc() );
  71.   printf( "Update function  : %s\n", krui_getUpdateFunc() );
  72.  
  73.   printf( "\nFilename of the pattern file: " );
  74.   scanf( "%s", file_name );
  75.   printf( "Loading the patterns ...\n\n" );
  76.   ret_code = krui_loadNewPatterns( file_name, &set_no );
  77.   errChk( ret_code );
  78.   no_of_patterns = krui_getNoOfPatterns();
  79.   printf( "No. of patterns: %d\n", no_of_patterns );
  80.  
  81.   /*  determine the no. of parameters of the current learning function
  82.   */
  83.   (void) krui_getFuncParamInfo( krui_getLearnFunc(), LEARN_FUNC,
  84.                 &no_of_input_params, &no_of_output_params );
  85.   printf( "\nThe learning function '%s' needs %d input parameters:\n",
  86.       krui_getLearnFunc(), no_of_input_params );
  87.   for (i = 0; i < no_of_input_params; i++)  {
  88.      printf( "Parameter [%d]: ", i + 1 );
  89.      scanf( "%f", &learn_parameters[ i ] );
  90.   }
  91.  
  92.   printf( "\nChoose no. of cycles: " );
  93.   scanf("%d", &N);
  94.   if (N <= 0)  {
  95.      printf( "\nInvalid no. of cycles !\n" );
  96.      exit( 1 );
  97.   }
  98.  
  99.   printf( "\nShuffle patterns ? (Y/N): " );
  100.   scanf( "%s", file_name );
  101.   shuffle = toupper( file_name[0] ) == 'Y';
  102.   if (shuffle)
  103.     printf( "\nShuffleing of patterns enabled\n" );
  104.   else
  105.     printf( "\nShuffleing of patterns disabled\n" );
  106.  
  107.   printf( "\nBegin learning ...\n" );
  108.  
  109.   step = ((N - 1) / 20) + 1;
  110.   for (i = 0; i < N; i++)  {
  111.     if (shuffle)
  112.       {  /*  shuffle patterns every cycle  */
  113.       ret_code = krui_shufflePatterns( TRUE );
  114.       errChk( ret_code );
  115.     }
  116.  
  117. /*  REMEMBER:  return_values[ 0 ] returns the current net error
  118.                learn_parameters[ 0 ] contains the learning parameter
  119.                learn_parameters[ 1 ] contains the max. devitation to learn
  120. */
  121.     ret_code = krui_learnAllPatterns( learn_parameters, 1, &return_values, &NoOfReturnVals );
  122.     errChk( ret_code );
  123.  
  124.     /*    print the return values of the learning function  */
  125.     if ((i % step == 0) || i == (N - 1))  {
  126.       printf( "\nCycle: %d\nLearning function value(s): ", i + 1);
  127.       for (j = 0; j < NoOfReturnVals; j++ )
  128.         printf( "[%d]: %f  ", j + 1, return_values[ j ] );
  129.       printf( "\n" );
  130.     }
  131.   }
  132.  
  133.   printf( "\nEnd learning ...\n" );
  134.   printf( "\nCalculating the last error of the network ...\n" );
  135.  
  136.   sum_error = 0.0;
  137.   parameterInArray[0] = 0.0;  /*  set the max. devitation to 0.0  */
  138.  
  139. /*   ====================== Attention: ==================================
  140.    Formerly the network propagated the patterns here. With the new pattern 
  141.    handling of version 3.2 this can not longer be done without major rewriting 
  142.    of the function krui_testNet. Since this function is not used within SNNS,
  143.    we decided, that it is not worth the effort and dropped the function.
  144.    ==> Anybody who needs it here has to rewrite it himself. Sorry.
  145. */
  146. /*
  147.   for (i = 1; i <= no_of_patterns; i++)  {
  148.     ret_code = krui_testNet( i,
  149.                              updateParameterArray, 0,
  150.                  parameterInArray, 1,
  151.                  &return_values, &NoOfReturnVals );
  152.     errChk( ret_code );
  153.     sum_error += return_values[0];
  154.   }
  155.   printf( "The error is: %g\n", sum_error );
  156. */
  157.  
  158.   /*  save the network    */
  159.   printf( "\n\nFilename of the network to save: " );
  160.   scanf( "%s", file_name );
  161.   printf( "Saving the network ...\n" );
  162.   ret_code = krui_saveNet( file_name, netname );
  163.   errChk( ret_code );
  164.  
  165.   /*  before exiting: delete network  */
  166.   krui_deleteNet();
  167.  
  168.   return( 0 );
  169. }
  170.