home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / Classes / Neural-Network / bptest.m < prev    next >
Encoding:
Text File  |  1992-07-29  |  2.0 KB  |  79 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. //
  8. // This feed-forward, back-prop network is setup to learn
  9. // the identity function (output=input).  The number N is the
  10. // number of inputs (outputs) and H is the number of hidden
  11. // nodes.  The case (H < N) is most interesting - this is a form
  12. // of data compression.
  13. // The number P determines the number of random patterns to learn.
  14. //
  15.  
  16. #import "BackPropEngine.h"
  17. #import <stdio.h>
  18. #define N 4
  19. #define H 8
  20. #define P 10
  21.  
  22. void main()
  23. {
  24.     //
  25.     // create a Back-Prop network
  26.     //
  27.    id    bp = [[BackPropEngine alloc]  initWithInputs:N
  28.                                        hidden:H
  29.                                        outputs:N];
  30.  
  31.    id        random = [[Random alloc] init];    // allocate a Random instance
  32.  
  33.     
  34.    double inputs[P][N];
  35.    double target[P][N];
  36.    
  37.    int   i,j;
  38.    
  39.    [bp setEta:0.9];                // set the learning-rate
  40.  
  41.    
  42.     //
  43.     // create the random patterns
  44.     //
  45.     for(j=0; j<P; j++)
  46.         for(i=0; i<N; i++)
  47.             inputs[j][i] = target[j][i] = [random percent]*0.8;
  48.             
  49.     //
  50.     // start the learning loop
  51.     //
  52.    for(j=0; j<100000; j++) {
  53.         for(i=0; i<20; i++) {
  54.            [bp applyInput:inputs[j%P]];
  55.           [bp correctWithTarget:target[j%P]];
  56.         }
  57.       if(!(j % P))                                    // BPE doesn't average the errors
  58.          printf("%u %e\n",j,[bp getError]);  // should do that here...
  59.    }
  60.    
  61.  
  62.     //
  63.     // dump the weights
  64.     //
  65.    for(j=0; j<[[bp inputs] count]; j++) {
  66.       for(i=0; i<[[bp hidden] count]; i++)
  67.          printf("%10e  ",[[[bp hidden] objectAt:i]
  68.             getWeightFor:[[bp inputs] objectAt:j]]);
  69.       printf("\n");
  70.    }
  71.     printf("-----\n");
  72.    for(j=0; j<[[bp hidden] count]; j++) {
  73.       for(i=0; i<[[bp outputs] count]; i++)
  74.          printf("%10e  ",[[[bp outputs] objectAt:i]
  75.             getWeightFor:[[bp hidden] objectAt:j]]);
  76.       printf("\n");
  77.    }
  78. }
  79.