Hexadroid

testing grounds

Mar 16th, 2021
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <random>
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <iostream>
  7. #include <windows.h>
  8.  
  9. static unsigned long x=123456789, y=362436069, z=521288629;
  10.  
  11. const double tempo = 1.00 / exp(1.00);
  12.  
  13. const int nr_neurons_A = 64;
  14. const int nr_neurons_B = 2;
  15.  
  16. int tota = nr_neurons_A + nr_neurons_B;
  17.  
  18.  
  19.  
  20. int Z;
  21. int A, B, C, D, E, F, G;
  22. int loops = 240000;
  23. double learn = 0.1;
  24. int showtime = 0.97*loops;
  25. int zrows = 4;
  26. int temp, i1, i2, rows, shuffled;
  27. int i, j;
  28. int showoutputs;
  29.  
  30.  
  31.  
  32.                                 //determine either cross or a circle, kruisje rondje
  33.   double train_input[4][64] = { {   1,0,0,0,0,0,0,1,
  34.                                     0,1,0,0,0,0,1,0,
  35.                                     0,0,1,0,0,1,0,0,
  36.                                     0,0,0,1,1,0,0,0,
  37.                                     0,0,0,1,1,0,0,0,
  38.                                     0,0,1,0,0,1,0,0,
  39.                                     0,1,0,0,0,0,1,0,
  40.                                     1,0,0,0,0,0,0,1},
  41.  
  42.                             {       1,0,0,0,0,0,0,1,
  43.                                     0,1,0,0,0,0,1,0,
  44.                                     0,0,1,0,0,1,0,0,
  45.                                     0,0,0,1,0,0,0,0,
  46.                                     0,0,0,0,1,0,0,0,
  47.                                     0,0,1,0,0,1,0,0,
  48.                                     0,1,0,0,0,0,1,0,
  49.                                     1,0,0,0,0,0,0,1},
  50.  
  51.                             {       0,0,1,1,1,1,0,0,
  52.                                     0,1,0,0,0,0,1,0,
  53.                                     1,0,0,0,0,0,0,1,
  54.                                     1,0,0,0,0,0,0,1,
  55.                                     1,0,0,0,0,0,0,1,
  56.                                     1,0,0,0,0,0,0,1,
  57.                                     0,1,0,0,0,0,1,0,
  58.                                     0,0,1,1,1,1,0,0},
  59.  
  60.  
  61.                             {       0,0,1,1,1,1,0,0,
  62.                                     0,1,0,0,0,0,1,0,
  63.                                     0,1,0,0,0,0,1,0,
  64.                                     0,1,0,0,0,0,1,0,
  65.                                     0,1,0,0,0,0,1,0,
  66.                                     0,1,0,0,0,0,1,0,
  67.                                     0,1,0,0,0,0,1,0,
  68.                                     0,0,1,1,1,1,0,0},
  69.  
  70.  
  71.                                          };
  72.  
  73.  
  74. double train_output[4][2] = {       {1,0},  //cross
  75.  
  76.                                     {1,0},  //cross
  77.  
  78.                                     {0,1},  //circle
  79.  
  80.                                     {0,1}    //circle
  81.                                          };
  82.  
  83.  
  84.  
  85.  
  86.    double requested[64] =       {   1,0,0,0,0,0,0,1,
  87.                                     0,1,0,0,0,0,1,0,
  88.                                     0,0,1,1,0,1,0,0,
  89.                                     0,0,0,1,0,0,0,0,
  90.                                     0,0,0,0,1,0,0,0,
  91.                                     0,0,1,0,0,1,0,0,
  92.                                     0,1,0,0,0,0,1,0,
  93.                                     1,0,0,0,0,0,0,1
  94.                              };
  95.  
  96.  
  97.  
  98.    //incase we are testing the "a.i." functionality
  99.    double output_for_requested_should_be[1][2] = {
  100.                                                     {1,0},  //cross
  101.                                                  };
  102.  
  103. double xDD, gDD, fDD, eDD, dDD, cDD, bDD, zDD;
  104. double bDD_xupd, bDD_xbupd;
  105. double cDD_wupd, cDD_bupd;
  106. double dDD_wupd, dDD_bupd;
  107. double eDD_wupd, eDD_bupd;
  108. double fDD_wupd, fDD_bupd;
  109. double gDD_wupd, gDD_bupd;
  110. double zDD_wupd, zDD_bupd;
  111.  
  112. double membDD_xupd[nr_neurons_A][nr_neurons_B];
  113.  
  114.  
  115.  
  116. double sqcalc, gemiddelde;
  117. double TOTAL, TOTALcCoD, TOTALao, TOTALbo, TOTALco, TOTALdo, TOTALeo, TOTALfo;
  118.  
  119. double xerr[nr_neurons_B], xerrtotal;
  120. double sqerrtot = 0;
  121.  
  122. double ato[5];
  123. double bto[5];
  124.  
  125.  
  126. double atoTOTAL[65];
  127. double btoTOTAL[3];
  128.  
  129.  
  130. bool training_mode = false;
  131.  
  132. double gDD_times, fDD_times, eDD_times, dDD_times, cDD_times, bDD_times;
  133. double avgtot, aw, bw, cw, dw, ew, fw, gw;
  134. double tussen, row_0, row_1, row_2, row_3;
  135.  
  136. struct MiddleNode {
  137.  
  138.  std::vector<double> w = std::vector<double>(256, 0.00);
  139.  std::vector<double> o = std::vector<double>(256, 0.00);
  140.  std::vector<double> bw = std::vector<double>(256, 0.00);
  141.  std::vector<double> b = std::vector<double>(256, 0.00);
  142.  
  143. };
  144.  
  145. struct InputNode {
  146.  
  147.  std::vector<double> w = std::vector<double>(256, 0.00);
  148.  std::vector<double> o = std::vector<double>(256, 0.00);
  149.  std::vector<double> bw = std::vector<double>(256, 0.00);
  150.  std::vector<double> b = std::vector<double>(256, 0.00);
  151.  
  152. };
  153.  
  154. struct OutputNode {
  155.  
  156.  std::vector<double> w = std::vector<double>(256, 0.00);
  157.  std::vector<double> o = std::vector<double>(256, 0.00);
  158.  std::vector<double> bw = std::vector<double>(256, 0.00);
  159.  std::vector<double> b = std::vector<double>(256, 0.00);
  160.  
  161. };
  162.  
  163. double choose(int x, int aa) {
  164.  
  165.   if (training_mode) return train_input[x][aa];
  166.   else return requested[aa];
  167.  
  168. }
  169.  
  170. unsigned long xorshf96(void) {          //semi random
  171. unsigned long t;
  172.     x ^= x << 16;
  173.     x ^= x >> 5;
  174.     x ^= x << 1;
  175.  
  176.    t = x;
  177.    x = y;
  178.    y = z;
  179.    z = t ^ x ^ y;
  180.  
  181.   return z;
  182. }
  183.  
  184.  
  185. double initialize() {
  186.  
  187.   return ((xorshf96()%10000)/10000.00);
  188.  
  189. }
  190.  
  191. double fd(double x) {
  192.  
  193.   return (x >= 0) ? (1) : (tempo);
  194.  
  195. }
  196.  
  197. double fx(double x) {
  198.  
  199.   return (x >= 0) ? (x) : (x*tempo);
  200.  
  201. }
  202.  
  203. int _tmain(int argc, _TCHAR * argv[]) {
  204.  
  205.   srand (time(NULL));
  206.  
  207.   std::random_device rd;
  208.   std::mt19937 gh(rd());
  209.  
  210.   learn = learn / tota;
  211.  
  212.  
  213.   InputNode a[nr_neurons_A];
  214.   MiddleNode b[nr_neurons_B];
  215.  
  216.  
  217.  
  218.  
  219.  
  220.   double input = 0;
  221.  
  222.   int i1, i2;
  223.  
  224.   for (i1 = 0; i1 < nr_neurons_A; i1++)
  225.     for (i2 = 0; i2 < nr_neurons_B; i2++) {
  226.       a[i1].w[i2] = initialize();
  227.       a[i1].bw[i2] = initialize();
  228.       a[i1].b[i2] = initialize();
  229.     }
  230.  
  231.   for (i1 = 0; i1 < nr_neurons_B; i1++) {
  232.       b[i1].w[0] = initialize();
  233.       b[i1].bw[0] = initialize();
  234.       b[i1].b[0] = initialize();
  235.     }
  236.  
  237.  
  238.  
  239.   sqcalc = 99999999.99;
  240.   for (temp = 0; temp < loops; temp++) {
  241.  
  242.  
  243.  
  244.     //for (rows = 0; rows < zrows+1; rows++)
  245.     rows = xorshf96() % (zrows + 1);
  246.  
  247.  
  248.  
  249.           if (rows == zrows) { training_mode = false; }
  250.          else { training_mode = true; }
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.       TOTALbo = 0; TOTALao = 0;
  264.  
  265.            //   std::fill(atoTOTAL, atoTOTAL+65, 0);
  266.             std::fill(btoTOTAL, btoTOTAL+3, 0);
  267.  
  268.             std::fill(ato, ato+65, 0);
  269.            // std::fill(bto, bto+3, 0);
  270.  
  271.  
  272.  
  273.         for (B = 0; B < nr_neurons_B; B++) {
  274.           TOTAL = 0;
  275.           for (A = 0; A < nr_neurons_A; A++) {
  276.  
  277.  
  278.               a[A].o[B] = fx(choose(rows, A) * a[A].w[B] /*+ a[A].bw[B] * a[A].b[B]*/ );
  279.               ato[B] += a[A].o[B];
  280.              // atoTOTAL[B] += a[A].o[B] ;
  281.  
  282.  
  283.  
  284.             btoTOTAL[0] += a[A].o[B] * b[B].w[0];
  285.           }
  286.  
  287.           b[B].o[0] = fx(btoTOTAL[0] /*+ b[B].bw[C] * b[B].b[C]*/ );
  288.  
  289.  
  290.         }
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.       xerrtotal = 0;
  299.       if (training_mode==true) {
  300.         for (showoutputs = 0; showoutputs < nr_neurons_B; showoutputs++) {
  301.           xerr[showoutputs] = 2 * (b[showoutputs].o[0] - train_output[rows][showoutputs]);
  302.  
  303.           xerrtotal += (xerr[showoutputs]);
  304.         }
  305.  
  306.  sqerrtot = xerrtotal;
  307.  
  308.  
  309.  
  310.               avgtot = 0;
  311.          int tellertje = 0;
  312.         xDD = learn * xerrtotal;
  313.         gDD_times = 0;
  314.  
  315.  
  316.         aw = 0; bw = 0; cw = 0; dw = 0; ew = 0; fw = 0; gw = 0;
  317.         bDD_times = 0; cDD_times = 0;dDD_times = 0;eDD_times = 0;fDD_times = 0;gDD_times = 0;
  318.  
  319.  
  320.         int Gi,Ga,Fi,Fa,Ea,Da;
  321.         avgtot = 0; aw = 0; bw = 0; cw = 0; dw = 0; ew = 0; fw = 0; gw = 0;
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.                   for (B = 0; B < nr_neurons_B; B++) {
  330.                     cDD_wupd =  ato[B] * learn * xerr[B];
  331.                     tussen = fd(b[B].o[0]) * cDD_wupd;
  332.                     b[B].w[0] -= tussen;
  333.  
  334.                 //  bw += tussen;
  335.                //       avgtot += tussen; tellertje++;
  336.                //               if (B==0) row_0 += tussen;
  337.          // if (B==1) row_1 += tussen;
  338.         //  if (B==2) row_2 += tussen;
  339.         //  if (B==3) row_3 += tussen;
  340.  
  341.                     }
  342.  
  343.  
  344.                                 for (B = 0; B < nr_neurons_B; B++) {bDD_times += b[B].w[0]* xerr[B];
  345.                             }
  346.                             bDD_times *= learn;
  347.                    tellertje = 0;
  348.  
  349.  
  350.             for (B = 0; B < nr_neurons_B; B++) {
  351.  
  352.  
  353.                     for (A = 0; A < nr_neurons_A; A++) {
  354.  
  355.                       tussen = train_input[rows][A] * fd(a[A].o[B]) * bDD_times;
  356.                       a[A].w[B] -= tussen;
  357.  
  358.                    //   aw += tussen;
  359.                    //     avgtot += tussen;  tellertje++;
  360.                     //          if (A==0) row_0 += tussen;
  361.          // if (A==1) row_1 += tussen;
  362.          // if (A==2) row_2 += tussen;
  363.           //if (A==3) row_3 += tussen;
  364.  
  365.                       }
  366.             }
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.             }
  374.  
  375.  
  376.  
  377.  
  378.              if (temp > showtime) {
  379.  
  380.         if (training_mode) {
  381.           printf("\n-");
  382.           for (showoutputs = 0; showoutputs < nr_neurons_B; showoutputs++) {
  383.  
  384.             printf("\n input: %.2lf The current output_%d for training set[%d] is %.28lf of (%lf)", train_input[rows][showoutputs], showoutputs, rows, b[showoutputs].o[0], train_output[rows][showoutputs]);
  385.             //if (rows==4) printf("\n(loop: % d) output_%d for requested data (%.28lf) is currently : %.8lf  (should be:%lf)", temp, showoutputs, requested1[showoutputs], b[showoutputs].o[0], output_for_requested_should_be[0][showoutputs] );
  386.  
  387.  
  388.           }
  389.                 } else {
  390.  
  391.           for (showoutputs = 0; showoutputs < nr_neurons_B; showoutputs++) {
  392.  
  393.             printf("\n output for requested data : %lf  (should be:%lf)", b[showoutputs].o[0], output_for_requested_should_be[0][showoutputs] );
  394.  
  395.           }
  396.  
  397.         }
  398.  
  399.       }
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.     if (!(temp % (showtime/100))) printf("\n %d %%",++Z);
  417.  
  418.     sqcalc += sqerrtot;
  419.  
  420.     if (temp % 1000 == 0) {
  421.       gemiddelde = sqcalc / 1000;
  422.       sqcalc = 0;
  423.     }
  424.  
  425.     sqerrtot = 0;
  426.  
  427.   }
  428.  
  429.   printf("\nEnd.");
  430.   scanf("%d", &Z);
  431.  
  432.   return 0;
  433.  
  434. }
Advertisement
Add Comment
Please, Sign In to add comment