home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 306_01 / synapsys.h < prev    next >
Text File  |  1991-02-21  |  4KB  |  94 lines

  1. /******************************************************************************
  2.  
  3.                                   SYNAPSYS.H
  4.  
  5.                           NEURAL NETWORK SIMULATION
  6.  
  7.       COPYRIGHT 1988, 1989, 1990, 1991 GREGORY COLVIN, ALL RIGHTS RESERVED
  8.  
  9. ******************************************************************************/
  10.  
  11. /* define WORD to be signed sixteen bit integer on most machines */
  12. #define WORD short
  13.  
  14. /*
  15.     A neuron is characterized by two functions: a transfer function and an
  16.     error function.
  17.  
  18.     Each neuron can take inputs from many neurons and transfer the sum of the
  19.     inputs to an output activation.  The activation of a neuron is given in
  20.     units of 1/128, and must be constrained to vary within the range of
  21.     -127/128 through 127/128.  The transfer function is linear from -102/128
  22.     through 102/128.  Above and below those values a logarithmic function is
  23.     used to constrain the resulting activation within limits.
  24.  
  25.     Each neuron can also sum the error signals from many output neurons and
  26.     compute an error correction factor.  The error correction factor is
  27.     proportional to the learning rate, which is given in units of 1/128,
  28.     and to the gain of the synaptic layer.
  29.  
  30. */
  31. typedef struct
  32. { WORD activation;            /* activation of neuron for feedforward        */
  33.   WORD errors;                /* sum of errors from feedback                 */
  34. } NEURON;
  35.  
  36. #define ACTIVATION_SHIFT 7    /* units of 1/128: 2**-7 */
  37. #define RATE_SHIFT       7    /* units of 1/128: 2**-7 */
  38.  
  39. /*
  40.     Each synapse connects an input neuron to an output neuron, and consists of
  41.     a modifiable weight.  The value at each synapse is constrained to vary from
  42.     -32767 to 32767.  This value is in units of 1/1024.  Thus the effective
  43.     weight varies from -31.999 to 31.999.
  44. */
  45. typedef WORD SYNAPSE;
  46. #define MAX_SYNAPSE 32767
  47. #define SYNAPSE_SHIFT 10      /* units of 1/1024: 2**-10 */
  48.  
  49. /*
  50.     A layer of synapses is modeled as an array of input neurons, a matrix of
  51.     synapses, and an array of output neurons.  Each input neuron has a weighted
  52.     synapse on every output neuron.
  53.  
  54.     In feedforward mode the activations of the input neurons are fed through
  55.     the synapses to the output neurons.
  56.  
  57.     In feedback mode the error signals applied to the output neurons are used
  58.     to modify the synaptic weights with a correction factor, which is also fed
  59.     back as an error signal to the input neurons.
  60.  
  61.     In multi-layer networks the outputs of one layer serve as the inputs to the
  62.     next layer, so that feedforward and feedback can be carried through the
  63.     entire network one layer at a time.
  64. */
  65. typedef struct layer
  66. { struct layer *prev_layer;   /* pointer to previous layer, if any           */
  67.   int n_inputs;               /* number of input neurons                     */
  68.   NEURON *inputs;             /* same address as outputs of previous layer   */
  69.   SYNAPSE *synapses;          /* synapses[n_inputs][n_outputs]               */
  70.   SYNAPSE *history;           /* previous values for use in learning         */
  71.   char rate;                  /* learning rate, larger values learn faster   */
  72.   char momentum;              /* learning momentum, true or false            */
  73.   int n_outputs;              /* number of output neurons                    */
  74.   NEURON *outputs;            /* same address as inputs of next layer        */
  75.   struct layer *next_layer;   /* pointer to next layer, if any               */
  76. } LAYER;
  77.  
  78. typedef struct
  79. { LAYER *first_layer;
  80.   LAYER *last_layer;
  81. } NETWORK;
  82.  
  83. NETWORK *new_network();
  84.  
  85. /*
  86.         Good random number generator with period 4286449341.
  87.         Result is unsigned WORD with uniform distribution in range 0 to 65535.
  88.         Seeds must be unsigned WORD, with no side effects.
  89.         Quite fast if seeds are in registers.
  90. */
  91. #define U2RAND(seed1,seed2) \
  92. (((seed1)*=65421,++(seed1))+((seed2)*=65521,++(seed2)))
  93.  
  94.