home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / Classes / Neural-Network / boltz.m < prev    next >
Encoding:
Text File  |  1992-07-29  |  2.8 KB  |  129 lines

  1. /* =======================================================
  2.     Neural Network Classes for the NeXT Computer
  3.     Written by: Ralph Zazula
  4.                     University of Arizona - Fall 1991
  5.                     zazula@pri.com (NeXT Mail)
  6. ==========================================================*/
  7. #import "Neuron.h"
  8. #import "Random.h"
  9. #import <math.h>
  10.  
  11. #define N   4                // number of neurons
  12. #define I   3                // input neurons
  13. #define O   1               // output neurons
  14. #define PREFACT ETA
  15.  
  16. void main()
  17. {
  18.     id    random = [[Random alloc] init];
  19.     int    i,j,k,n;
  20.     id        neuron;
  21.     id        nList = [[List alloc] init];
  22.     
  23.     int    update_list[N];
  24.     double    inputs[I];
  25.     double    target[O];
  26.     
  27.     long        count;
  28.     int        K = 10;
  29.     double    T = 10.0, Tf = 0.01, ETA = 0.1, Gamma = 0.995, E = 0.0;
  30.     
  31.     //
  32.     // create the neurons
  33.     //
  34.     for(i=0; i<N; i++) {
  35.         [nList addObject:[[[[[[Neuron alloc] init] 
  36.                                 setRandom:random] 
  37.                                 setType:Tanh]
  38.                                 setTemp:T]
  39.                                 setSymmetric:YES]];
  40.     }
  41.     //
  42.     // make the connections (fully-connected)
  43.     //
  44.     for(i=0; i<N; i++)
  45.         for(j=0; j<N; j++)
  46.             if(j!=i)
  47.                 [[nList objectAt:i] 
  48.                     connect:[nList objectAt:j]
  49.                     withWeight:(1.0 - 2.0*[random percent])];    
  50.     while( T > Tf ) {
  51.      for(k=0;k<K;k++) {
  52.         //
  53.         // apply inputs
  54.         // 
  55.         for(i=0; i<I; i++) {
  56.             inputs[i] = [random percent] > 0.5 ? -1 : 1;
  57.         }
  58.         inputs[2] = 1.0;
  59.         target[0] = (inputs[0] == inputs[1] ? 1.0 : -1.0);
  60.         
  61.         for(i=0; i<I; i++)
  62.             [[nList objectAt:i] setOutput:inputs[i]];
  63.         //
  64.         // clamp outputs
  65.         //
  66.         for(i=I; i<I+O; i++)
  67.             [[nList objectAt:i] setOutput:target[i-I]];
  68.         
  69.         // 
  70.         // do "clamped" training
  71.         //    
  72.         for(i=0; i<N; i++) {                    // loop over hidden nodes
  73.             update_list[i] = n = [random randMin:I max:(N-1)];
  74.             neuron = [nList objectAt:n];
  75.             if(n > I+O)                                // don't unclamp outputs
  76.                 [neuron step];    
  77.             for(j=0; j<N; j++) {
  78.                 if(j!=n) {
  79.                     [neuron changeWeightFor:[nList objectAt:j] 
  80.                         by:(PREFACT)*[neuron lastOutput]*
  81.                             [[nList objectAt:j] lastOutput]];
  82.                  }
  83.             }
  84.         }
  85.         //
  86.         // do "free" (un)learning
  87.         //
  88.         for(i=I; i<N; i++)
  89.             [[nList objectAt:i] step];                    // unclamp outputs
  90.         for(i=0; i<N; i++) {                            // loop over all nodes
  91.             n = update_list[i];
  92.             neuron = [nList objectAt:n];
  93.             [neuron step];
  94.             for(j=0; j<N; j++) {
  95.                if(j!=n) {
  96.                     [neuron changeWeightFor:[nList objectAt:j] 
  97.                         by:-(PREFACT)*[neuron lastOutput]*
  98.                             [[nList objectAt:j] lastOutput]];
  99.                 }
  100.             }
  101.         }
  102.         if((count %1) == 0) {
  103.             for(i=0; i<O; i++)
  104.              E += (0.5/O)*fabs(target[i] - [[nList objectAt:I+i] lastOutput]);
  105.             if(E == 0.0) E = MINFLOAT;
  106.         }
  107.     }
  108.         printf("%e %e\n",T,E);
  109.         E=0.0;
  110.         
  111.         T = Gamma*T;
  112.         for(i=0; i<N; i++)
  113.             [[nList objectAt:i] setTemp:T];
  114.     }
  115.      
  116.     //
  117.     // dump the weights
  118.     //
  119.     for(i=0; i<N; i++) {
  120.         neuron = [nList objectAt:i];
  121.         for(j=0; j<N; j++)
  122.             if(j!=i)
  123.                 printf("%e ",[neuron getWeightFor:[nList objectAt:j]]);
  124.         printf("\n");
  125.     }
  126.     printf("ETA: %e K: %u Gamma: %e\n",ETA,K,Gamma);
  127.     printf("N: %u I: %u O: %u\n",N,I,O);
  128. }
  129.