home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: OtherApp / OtherApp.zip / mousepen.zip / BPNN.CPP < prev    next >
Text File  |  1994-11-18  |  5KB  |  188 lines

  1. // Backpropogation Neural Network for Numerical character recognition
  2. // By ROBERT ALDRIDGE - 17/09/94
  3.  
  4. #define INCL_VIO
  5.  
  6. #include <os2.h>
  7. #include <iostream.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <math.h>
  11. #include "num.h"
  12.  
  13. #define HI 0.8
  14. #define LOW 0.2
  15.  
  16.  
  17. typedef struct Neuron {
  18.    float output;
  19.    float weight[nn];
  20.    float s;
  21. } neuron;
  22.  
  23. neuron layer1[nn],layer2[nn];              // 2 Layers of Neurons
  24.  
  25. short ncnt, wcnt, tcnt;                    // A couple of counters
  26.  
  27. float L1delta[nn], L2delta[nn], temp, t, d;
  28.  
  29. FILE *fptr;
  30.  
  31. void weightinit();
  32. void calcoutput();
  33. void adjustweights();
  34. void clrscr();
  35. void saveweights();
  36. void testnet();
  37. void loadweights();
  38. void roundup(float *dnum);
  39.  
  40. int main()
  41. {
  42.    short cnt2, iter;
  43.    weightinit();
  44.    clrscr();
  45.    cout << "Enter No. of Passes : ";
  46.    cin >> iter;
  47.    cout << endl << "Enter Training Constant : ";
  48.    cin >> d;
  49.    cout << endl << "Enter Threshold Value : ";
  50.    cin >> t;
  51.    // Main Loop
  52.    if(iter>=1) {
  53.       for(cnt2=0; cnt2<iter; cnt2++) {
  54.          tcnt=0;
  55.          while(tcnt<inps) {
  56.             VioSetCurPos(6,0,0);
  57.             cout << "Pass : " << cnt2 << endl;
  58.             calcoutput();
  59.             adjustweights();
  60.             tcnt++;
  61.          }
  62.       }
  63.    saveweights();
  64.    }
  65.    loadweights();
  66.    testnet();
  67.    return 0;
  68. }
  69.  
  70. // Initialize weights with random values
  71. void weightinit()
  72. {
  73.    for(ncnt=0; ncnt<nn; ncnt++) {                             // Neuron Loop
  74.       for(wcnt=0; wcnt<nn; wcnt++) {                          // Weight Loop
  75.          layer1[ncnt].weight[wcnt]=0.000001*rand();
  76.          layer2[ncnt].weight[wcnt]=0.000001*rand();
  77.       }
  78.    }
  79. }
  80.  
  81.  
  82. // Calculate outputs
  83. void calcoutput()
  84. {
  85.    // Input Layer - layer1
  86.    for(ncnt=0; ncnt<nn; ncnt++) {                              // Neuron Loop
  87.       temp=0;
  88.       for(wcnt=0; wcnt<nn; wcnt++)                             // Weight Loop
  89.          temp+=(input[tcnt][wcnt]*layer1[ncnt].weight[wcnt])-t;
  90.       layer1[ncnt].s=temp;
  91.       layer1[ncnt].output=(1/(1+exp(-layer1[ncnt].s)));
  92.    }
  93.  
  94.    // First hidden layer - layer2
  95.    for(ncnt=0; ncnt<nn; ncnt++) {                              // Neuron Loop
  96.       temp=0;
  97.       for(wcnt=0; wcnt<nn; wcnt++)                             // Weight Loop
  98.          temp+=(layer1[wcnt].output*layer2[ncnt].weight[wcnt])-t;
  99.       layer2[ncnt].s=temp;
  100.       layer2[ncnt].output=(1/(1+exp(-layer2[ncnt].s)));
  101.    }
  102.  
  103. }
  104.  
  105. void adjustweights()
  106. {
  107.    float sum;
  108.    int scnt;
  109.    // Layer 2
  110.    for(ncnt=0; ncnt<nn; ncnt++) {             // Find Delta for each Neuron
  111.       L2delta[ncnt]=layer2[ncnt].output*(1-layer2[ncnt].output)
  112.                     *(correctout[tcnt][ncnt]-layer2[ncnt].output);
  113.          for(wcnt=0; wcnt<nn; wcnt++)         // Adjust each weight
  114.             layer2[ncnt].weight[wcnt]+=d*L2delta[ncnt]*layer1[wcnt].output;
  115.    }
  116.  
  117.  
  118.    // Layer 1
  119.    for(ncnt=0; ncnt<nn; ncnt++) {             // Find Error of each Neuron
  120.       sum=0;
  121.       for(scnt=0; scnt<nn; scnt++)
  122.          sum+=L2delta[scnt]*layer2[scnt].weight[ncnt];
  123.       L1delta[ncnt]=layer1[ncnt].output*(1-layer1[ncnt].output)*sum;
  124.       for(wcnt=0; wcnt<nn; wcnt++)
  125.          layer1[ncnt].weight[wcnt]+=d*L1delta[ncnt]*input[tcnt][wcnt];
  126.    }
  127. }
  128.  
  129. // OS/2 API Calls to Clear the screen
  130. void clrscr()
  131. {
  132.    BYTE bCell[2];
  133.    bCell[0] = 0x20;
  134.    bCell[1] = 0x00;
  135.    VioScrollDn(0,0,0xFFFF,0xFFFF,0xFFFF,bCell,0);
  136.    VioSetCurPos(0,0,0);
  137. }
  138.  
  139. void saveweights()
  140. {
  141.    char fname[14];
  142.    cout << "Enter Weight filename : ";
  143.    cin >> fname;
  144.    fptr=fopen(fname,"wb");
  145.    for(ncnt=0; ncnt<nn; ncnt++) {
  146.       fwrite(layer1[ncnt].weight, sizeof(layer1[ncnt].weight),1,fptr);
  147.       fwrite(layer2[ncnt].weight, sizeof(layer2[ncnt].weight),1,fptr);
  148.    }
  149.     fclose(fptr);
  150. }
  151.  
  152. void loadweights()
  153. {
  154.    char fname[14];
  155.    cout << "Enter Weight filename : ";
  156.    cin >> fname;
  157.    fptr=fopen(fname,"rb");
  158.    for(ncnt=0; ncnt<nn; ncnt++) {
  159.       fread(layer1[ncnt].weight,sizeof(layer1[ncnt].weight),1,fptr);
  160.       fread(layer2[ncnt].weight,sizeof(layer2[ncnt].weight),1,fptr);
  161.    }
  162.     fclose(fptr);
  163. }
  164.  
  165.  
  166. void testnet()
  167. {
  168.    short cnt, cnt2;
  169.    for(cnt2=0; cnt2<inps; cnt2++) {
  170.       cout << endl << "Correct  Real" << endl;
  171.       tcnt=cnt2;
  172.       calcoutput();
  173.       for(cnt=0; cnt<64; cnt++) {
  174.          // roundup(&layer2[cnt].output);
  175.          cout << correctout[cnt2][cnt] << "        " << layer2[cnt].output << endl;
  176.       }
  177.    }
  178. }
  179.  
  180. void roundup(float* dnum)
  181. {
  182.    if(*dnum<LOW)
  183.       *dnum=0;
  184.    else if(*dnum>HI)
  185.       *dnum=1;
  186. }
  187.  
  188.