home *** CD-ROM | disk | FTP | other *** search
/ Collection of Education / collectionofeducationcarat1997.iso / COMPUSCI / NNUTL101.ZIP / NNSIM2 / NNSTRUCT.C < prev   
C/C++ Source or Header  |  1993-07-13  |  7KB  |  149 lines

  1. /*-----------------------------------------------------------------------*
  2.  * Greg Stevens                                                  6/24/93 *
  3.  *                               nnstruct.c                              *
  4.  *                                             [file 3 in a series of 6] *
  5.  *                                                                       *
  6.  *  This file contains the data structure declarations for a neural net, *
  7.  * as well as the declaration for the initialization procedure for it,   *
  8.  * as well as defining the global variable NUMNODES, which is initialized*
  9.  * in the net initialization procedure.                                  *
  10.  *                                                                       *
  11.  *                                                                       *
  12.  *  USE OF InitNet:                                                      *
  13.  *                                                                       *
  14.  * At the beginning of your program should appear a variable declaration *
  15.  *                                                                       *
  16.  *  NNETtype N;   /* any variable name here *\                           *
  17.  *                /* NUMNODES is already declared *\                     *
  18.  *  N = InitNet( NUMNODES );                                             *
  19.  *                                                                       *
  20.  *-----------------------------------------------------------------------*/
  21. #include "nninputs.c"                 /* includes constant definitions   */
  22. #include "stdlib.h"                   /* for randomizing functions       */
  23.  
  24. typedef struct
  25.           {
  26.              float state;               /* activation state value of node*/
  27.              float weights[ MAXNODES ]; /* input connection weights      */
  28.              float thresh;              /* threshhold input value        */
  29.              int actfn;                 /* activation functn: 0=linear,  */
  30.                                         /*                    1=logistic */
  31.            } node;
  32.  
  33. /* NOTE: for any connections that do not exist, the value of that weight */
  34. /*       is zero.                                                        */
  35.  
  36. typedef struct
  37.           {
  38.              node unit[ NUMLAYERS ][ MAXNODES ];
  39.           } NNETtype;
  40.  
  41. /* NOTE: a net need only declared as type NNETtype, and all information  */
  42. /*       can be accessed through the single field unit, thus, a call to  */
  43. /*       the activation state of the second unit in the third layer is:  */
  44. /*                                                                       */
  45. /*       nnet.unit[3][2].state                                           */
  46. /*                                                                       */
  47.  
  48. /* global variable declaration */
  49. int NUMNODES[ NUMLAYERS ];
  50.  
  51. /* prototype for InitNet */
  52. NNETtype InitNet( int NMND[ NUMLAYERS ] );
  53.  
  54. /* definition for InitNet */
  55. NNETtype InitNet( int NMND[ NUMLAYERS ] )
  56. {
  57.    NNETtype N;                /* hold the initialized net value to return*/
  58.    int c,d,f;                 /* counters for looping through the array  */
  59.  
  60.    srand(1);                  /* initializes the random number generator */
  61.  
  62.  /* Initialize NUMNODES */
  63.  
  64.    NMND[0] = INPUT_LAYER_SIZE;
  65.  
  66.    for ( c=1; (c<(NUMLAYERS-1)); ++c )
  67.       {
  68.          if (c==1) NMND[c] = HL_SIZE_1;
  69.          if (c==2) NMND[c] = HL_SIZE_2;
  70.          if (c==3) NMND[c] = HL_SIZE_3;
  71.          if (c==4) NMND[c] = HL_SIZE_4;
  72.          if (c==5) NMND[c] = HL_SIZE_5;
  73.          if (c==6) NMND[c] = HL_SIZE_6;
  74.          if (c==7) NMND[c] = HL_SIZE_7;
  75.          if (c==8) NMND[c] = HL_SIZE_8;
  76.          if (c==9) NMND[c] = HL_SIZE_9;
  77.          if (c==10) NMND[c] = HL_SIZE_10;
  78.       }
  79.  
  80.    NMND[ NUMLAYERS-1 ] = OUTPUT_LAYER_SIZE;
  81.  
  82.  /* Initialize N */
  83.  
  84.    /* WEIGHTS: random weights between connected nodes, all others 0       */
  85.    /* STATES:  set to 1 if an actual node, set to -5 if not               */
  86.    /* THRESH:  set initially to a random value                            */
  87.    /* ACTFN:   all hidden units set to logistic (1), outputs to linear (0)*/
  88.    /*          and input units and non-units set to (-1)                  */
  89.  
  90.    for (c=0; (c<NUMLAYERS); ++c)
  91.     for (d=0; (d<MAXNODES); ++d)         /* for each node in each layer...*/
  92.       if (d<NMND[c])                     /* IF it's an actual node...     */
  93.         {
  94.            /* ACTIVATION STATES */
  95.   
  96.            N.unit[c][d].state = 1;              /* set initial state = 1  */
  97.  
  98.            if (c==0)                            /* if its input layer...  */
  99.              {
  100.                N.unit[c][d].actfn = -1;         /* no activation function */
  101.  
  102.                for (f=0; (f<MAXNODES); ++f)
  103.                 {
  104.                   N.unit[c][d].weights[f] = 0;  /*no weights to 1st layer */
  105.                   N.unit[c][d].thresh     = -5; /*no threshhold for 1st   */
  106.                 }
  107.              }
  108.             else
  109.              {
  110.                /* ACTIVATION FUNCTION */
  111.  
  112.                if (c==NUMLAYERS-1)              /* if  the output layer   */
  113.                  {                              /*    its a linear unit   */
  114.                    N.unit[c][d].actfn = 0;
  115.                  }
  116.                 else
  117.                  {                              /* otherwise, logistic    */
  118.                    N.unit[c][d].actfn = 1;
  119.                  }
  120.  
  121.                /* THRESHHOLD (randomize)*/
  122.  
  123.                N.unit[c][d].thresh = (rand() % 100) / 100.0;
  124.  
  125.                /* WEIGHTS */
  126.  
  127.                for (f=0; (f<MAXNODES); ++f)     /* for each node in the   */
  128.                                                 /* weight list            */
  129.                if (f<NMND[c-1])                 /* if from actual node,   */
  130.                   N.unit[c][d].weights[f] = (rand() % 100) / 100.0;
  131.                                                         /* set to ran num */
  132.                                                         /* 0-1, to 0.##   */
  133.                 else                                    /* if not actual  */
  134.                   N.unit[c][d].weights[f] = 0;          /* node, turn off */
  135.              }   /* end if input layer */
  136.         }
  137.        else                              /* If it isn't an actual node... */
  138.         {
  139.            N.unit[c][d].state = -5;                /* set act state to -5 */
  140.        N.unit[c][d].thresh= -5;                /* no threshhold       */
  141.            N.unit[c][d].actfn = -1;                /* no activation func. */
  142.  
  143.            for (f=0; (f<MAXNODES); ++f)            /*turn off connex to it*/
  144.              N.unit[c][d].weights[f] = 0;
  145.        }
  146.  
  147.    return( N );
  148. }
  149.