home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the 3D Game Programming Gurus / gurus.iso / Articles / AIandBeyond / neuralnet.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-14  |  2.9 KB  |  151 lines

  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <fstream.h>
  4.               
  5. struct NEURALNET
  6. {
  7.     float Input[512];    // the input data
  8.       float Hidden[64];    // the hidden layer neurons
  9.      float Output[8];    // the output data
  10.     float Weight1[512][64];    // the first set of weights
  11.       float Weight2[64][8];    // the second weight set
  12.    float Target[8];
  13.  
  14.    float dk[8];
  15.      float dj[64];
  16. };
  17.  
  18.  
  19. void Net_Recognize(NEURALNET Net)
  20. {
  21.    int i, j, k;
  22.    for(j=0; j<64; j++)
  23.    {
  24.        float sum=0;
  25.       for(i=0; i<512; i++)
  26.       {
  27.           sum=+(Net.Input[i])*(Net.Weight1[i][j]);
  28.       }
  29.       Net.Hidden[j]=1/(1+exp(-sum));
  30.    }
  31.    for(k=0; k<8; k++)
  32.    {
  33.        float sum=0;
  34.       for(j=0; j<64; j++)
  35.       {
  36.           sum=+(Net.Hidden[j])*(Net.Weight2[j][k]);
  37.       }
  38.       Net.Output[k]=1/(1+exp(-sum));
  39.    }
  40. }
  41.  
  42. void Net_Learn(NEURALNET Net)
  43. {
  44.    int i, j, k;
  45.     Net_Recognize(Net);
  46.     for(k=0; k<8; k++)
  47.    {
  48.        Net.dk[k]=(Net.Target[k]-Net.Output[k])*(Net.Output[k])*(1-Net.Output[k]);
  49.    }
  50.    for(j=0; j<64; j++)
  51.    {
  52.        for(k=0; k<8; k++)
  53.       {
  54.           Net.Weight2[j][k]=+(Net.dk[k])*(Net.Hidden[k]);
  55.       }
  56.    }
  57.    for(j=0; j<64; j++)
  58.    {
  59.        float sum=0;
  60.       for(k=0; k<8; k++)
  61.       {
  62.           sum=+(Net.dk[k])*(Net.Weight2[j][k]);
  63.       }
  64.       Net.dj[j]=(Net.Hidden[j])*(1-Net.Hidden[j])*sum;
  65.    }
  66.    for(i=0; i<512; i++)
  67.    {
  68.        for(j=0; j<64; j++)
  69.       {
  70.           Net.Weight1[i][j]=+(Net.dj[j])*(Net.Input[i]);
  71.       }
  72.    }
  73. }
  74.  
  75. void Net_Init(NEURALNET Net)
  76. {
  77.    int i, j, k;
  78.    fstream file;
  79.    file.open("VOICE.NET", ios::binary | ios::out);
  80.    for(i=0; i<512; i++)
  81.    {
  82.        for(j=0; j<64; j++)
  83.       {
  84.           Net.Weight1[i][j]=random(1000000)*0.000001;
  85.          file<<Net.Weight1[i][j]<<"\n";
  86.       }
  87.    }
  88.    for(j=0; j<64; j++)
  89.    {
  90.        for(k=0; k<8; k++)
  91.       {
  92.           Net.Weight2[j][k]=random(1000000)*0.000001;
  93.          file<<Net.Weight2[j][k]<<"\n";
  94.       }
  95.    }
  96.    file.close();
  97. }
  98.  
  99. void Net_Load(NEURALNET Net)
  100. {
  101.    int i, j, k;
  102.    fstream file;
  103.    file.open("VOICE.NET", ios::binary | ios::in);
  104.    if(file==0)
  105.    {
  106.        Net_Init(Net);
  107.       return;
  108.    }
  109.    for(i=0; i<512; i++)
  110.    {
  111.        for(j=0; j<64; j++)
  112.         {
  113.          file>>Net.Weight1[i][j];
  114.          cout<<Net.Weight1[i][j]<<"\n";
  115.       }
  116.    }
  117.    for(j=0; j<64; j++)
  118.    {
  119.        for(k=0; k<8; k++)
  120.       {
  121.          file>>Net.Weight2[j][k];
  122.          cout<<Net.Weight2[j][k]<<"\n";
  123.       }
  124.    }
  125.    file.close();
  126. }
  127.  
  128. void Net_Save(NEURALNET Net)
  129. {
  130.    int i, j, k;
  131.    fstream file;
  132.    file.open("VOICE.NET", ios::binary | ios::out);
  133.    for(i=0; i<512; i++)
  134.    {
  135.        for(j=0; j<64; j++)
  136.       {
  137.          file<<Net.Weight1[i][j]<<"\n";
  138.          cout<<Net.Weight1[i][j]<<"\n";
  139.       }
  140.    }
  141.    for(j=0; j<64; j++)
  142.    {
  143.        for(k=0; k<8; k++)
  144.       {
  145.          file<<Net.Weight2[j][k]<<"\n";
  146.          cout<<Net.Weight2[j][k]<<"\n";
  147.       }
  148.    }
  149.    file.close();
  150. }
  151.