home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 306_01 / timetest.c < prev   
C/C++ Source or Header  |  1991-02-21  |  3KB  |  93 lines

  1. /******************************************************************************
  2.                                TIMETEST.C
  3.                    COPYRIGHT 1988, 1989 GREGORY COLVIN.
  4.                             ALL RIGHTS RESERVED.
  5.  
  6.     Benchmark single layer neural network in learning arbitary vector.
  7.  
  8.     A good example:
  9.  
  10.         enter number of iterations:
  11.         10
  12.  
  13.         enter number of inputs:
  14.         20
  15.  
  16.         enter number of outputs
  17.         20
  18.  
  19.         enter learning rate (0 to 128):
  20.         25
  21.  
  22.         enter momentum (0 or 1):
  23.         0
  24.  
  25. *******************************************************************************/
  26.  
  27. #include <stdio.h>
  28. #include <time.h>
  29. #include "synapsys.h"
  30.  
  31. main()
  32. {
  33.   char s[81];
  34.   int i, j, err, n_neuron[2], rate, momentum;
  35.   long n, n_inp, n_out, t1, t2;
  36.   float tt, te, sse;
  37.   NETWORK *network;
  38.   NEURON *inputs, *outputs;
  39.  
  40.   /* prompt for network to test */
  41.   printf ( "enter number of iterations:\n" );
  42.   gets(s), n = atoi( s );
  43.   printf( "enter number of inputs:\n" );
  44.   gets(s), n_inp = atoi( s );
  45.   n_neuron[0] = n_inp;
  46.   printf( "enter number of outputs:\n" );
  47.   gets(s), n_out = atoi( s );
  48.   n_neuron[1] = n_out;
  49.   printf( "enter learning rate (0 to 128):\n" );
  50.   gets(s), rate = atoi( s );
  51.   printf( "enter momentum (0 or 1):\n" );
  52.   gets(s), momentum = atoi( s );
  53.  
  54.   /* create one layer network, randomize synapses */
  55.   network = new_network( 1, n_neuron, &rate, &momentum  );
  56.   if (!network)
  57.     printf( "\nout of memory\n" ), exit(0);
  58.   inputs = network->first_layer->inputs;
  59.   outputs = network->last_layer->outputs;
  60.   randomize( network, 127, 1L );
  61.  
  62.   /* initialize input */
  63.   for (i=1; i<=n_inp; i++)
  64.     inputs[i].activation = i*100/n_inp;
  65.  
  66.   /* time network */
  67.   for (tt=te=i=0; i<n; i++)
  68.   {
  69.     /* calculate outputs */
  70.     t1 = clock();
  71.     feedforward( network );
  72.     t2 = clock();
  73.     tt += (float)(t2 - t1);
  74.  
  75.     /* calculate errors, report mean squared error */
  76.     for (sse=0,j=0; j<n_out; j++, sse += err*err)
  77.       err = outputs[j].errors = j*100/n_out - outputs[j].activation;
  78.     printf( "iteration %d  mse %f\n", i, sse/n_out );
  79.  
  80.     /* correct synapse weights */
  81.     t1 = clock();
  82.     feedback( network );
  83.     t2 = clock();
  84.     te += (float)(t2 - t1);
  85.   }
  86.  
  87.   /* report results */
  88.   printf( "feedforward: %9.2f transfer()/sec\n",
  89.           (n*n_inp*n_out)/(tt/CLOCKS_PER_SEC+.000001) );
  90.   printf( "feedback:    %9.2f error()/sec\n",
  91.           (n*n_inp*n_out)/(te/CLOCKS_PER_SEC+.000001) );
  92. }
  93.