home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / Classes / Neural-Network / hoptest.m < prev    next >
Encoding:
Text File  |  1992-07-29  |  2.1 KB  |  92 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 "Random.h"
  8. #import "Neuron.h"
  9. #import <stdio.h>
  10.  
  11. #define L 100        // number of nodes
  12. #define N 5        // number of patterns
  13. #define M 2            // test pattern
  14.  
  15. void main()
  16. {
  17.     id rand = [[Random alloc] init];
  18.     id nodes = [[List alloc] init];
  19.     
  20.     double T = 2.0;                            // initial temperature
  21.  
  22.     double patterns[N][L], temp, error;
  23.     int    i,j,k;
  24.     
  25.     //
  26.     // create the nodes
  27.     //
  28.     for(i=0; i<L; i++) {
  29.         [nodes addObject:[[Neuron alloc] init]];
  30.         [[nodes lastObject] setType:Sign];
  31.     }
  32.         
  33.     //
  34.     // make the connections
  35.     //
  36.     for(j=0; j<L; j++)
  37.         for(i=0; i<L; i++)
  38.             if(i!=j)
  39.                 [[nodes objectAt:j] connect:[nodes objectAt:i]];
  40.  
  41.     // set seed variables
  42.     [rand setSeeds:5335:7777:32197];
  43.     
  44.     // generate patterns
  45.     printf("generating patterns...\n");
  46.     for(i=0; i<N; i++) 
  47.         for(j=0; j<L; j++)
  48.             patterns[i][j] = [rand percent] > 0.5 ? 1.0 : -1.0;
  49.                         
  50.     // set initial temperature
  51.     printf("setting initial temperature to: %f\n",T);
  52.     for(i=0; i<L; i++)
  53.         [[nodes objectAt:i] setTemp:T];
  54.  
  55.     // set weights
  56.     printf("setting weights...\n");
  57.     for(j=0; j<L; j++)
  58.         for(k=0; k<L; k++) {
  59.             temp=0.0;
  60.             if (j!=k) {
  61.                 for(i=0; i<N; i++)
  62.                     temp += patterns[i][j]*patterns[i][k];
  63.                 [[nodes objectAt:j] setWeightFor:[nodes objectAt:k] to:temp];
  64.             }
  65.         }
  66.         
  67.     // apply trial input (one of the patterns with noise added)
  68.     printf("applying input...\n");
  69.     for(i=0; i<L; i++)
  70.         [[nodes objectAt:i]
  71.             setOutput:( [rand percent] > 0.6 ? -patterns[M][i] : patterns[M][i])];
  72.         
  73.     // step and report error
  74.     for(i=0; i<1000; i++) {
  75.         error = 0.0;
  76.         for(j=0; j<L; j++)
  77.             [[nodes objectAt:[rand randMax:(L-1)]] step];
  78.         for(k=0; k<L; k++)
  79.             error += ([[nodes objectAt:k] lastOutput] - patterns[M][k])*
  80.                 ([[nodes objectAt:k] lastOutput] - patterns[M][k])/4;
  81.         printf("[%u] error: %lf  temp: %lf\n",i,error,T);
  82.         // decay the temperature
  83.         if(i > 50)
  84.             T = 0.99*T;
  85.         for(j=0; j<L; j++)
  86.             [[nodes objectAt:j] setTemp:T];
  87.     }
  88.         
  89.         
  90.     
  91. }
  92.