Hexadroid

Untitled

Dec 31st, 2020
1,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.25 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. const int nr_neurons_A = 4;
  10. const int nr_neurons_B = 4;
  11. const int nr_neurons_C = 4;
  12. const int nr_neurons_D = 4;
  13. const int nr_neurons_E = 4;
  14. const int nr_neurons_F = 4;
  15. const int nr_neurons_G = 4;
  16. int tota = 4;//nr_neurons_A + nr_neurons_B + nr_neurons_C + nr_neurons_D + nr_neurons_E + nr_neurons_F + nr_neurons_G;
  17.  
  18. int Z;
  19. int A, B, C, D, E, F, G;
  20. int loops = 5900000;
  21. int showtime = 0.98*loops;
  22. int zrows = 99;
  23. int temp, i1, i2, rows, shuffled;
  24. int i, j;
  25. int showoutputs;
  26.  
  27. double xDD, gDD, fDD, eDD, dDD, cDD, bDD, zDD;
  28. double bDD_xupd, bDD_xbupd;
  29. double cDD_wupd, cDD_bupd;
  30. double dDD_wupd, dDD_bupd;
  31. double eDD_wupd, eDD_bupd;
  32. double fDD_wupd, fDD_bupd;
  33. double gDD_wupd, gDD_bupd;
  34. double zDD_wupd, zDD_bupd;
  35.  
  36. double membDD_xupd[nr_neurons_A][nr_neurons_B];
  37. double memBWC_xupd[nr_neurons_B][nr_neurons_C];
  38. double memCWD_xupd[nr_neurons_C][nr_neurons_D];
  39. double memDWD_xupd[nr_neurons_D][nr_neurons_E];
  40. double memEWD_xupd[nr_neurons_E][nr_neurons_F];
  41. double memFWD_xupd[nr_neurons_F][nr_neurons_G];
  42. double memGWD_xupd[nr_neurons_G];
  43.  
  44. double sqcalc, gemiddelde;
  45. double TOTAL, TOTALcCoD, TOTALao, TOTALbo, TOTALco, TOTALdo, TOTALeo, TOTALfo;
  46. double learn = 0.00000001;
  47. double xerr[nr_neurons_G], xerrtotal;
  48. double sqerrtot = 0;
  49.  
  50. double au_input[5];
  51. double bu_input[5];
  52. double cu_input[5];
  53. double du_input[5];
  54. double eu_input[5];
  55. double fu_input[5];
  56.  
  57. int tijdelijk = 0;
  58.  
  59.  
  60. bool training_mode = false;
  61.  
  62. double train_input[100][nr_neurons_A];
  63. double train_output[100][nr_neurons_G];
  64. double requested1[nr_neurons_A];
  65.  
  66. double gDD_times, fDD_times, eDD_times, dDD_times, cDD_times, bDD_times;
  67. double avgtot, aw, bw, cw, dw, ew, fw, gw;
  68. double tussen, divx;
  69. double rown[4];
  70.  
  71. struct MiddleNode {
  72.  
  73.  std::vector<double> w = std::vector<double>(256, 0.00);
  74.  std::vector<double> o = std::vector<double>(256, 0.00);
  75.  std::vector<double> bw = std::vector<double>(256, 0.00);
  76.  std::vector<double> b = std::vector<double>(256, 0.00);
  77.  
  78. };
  79.  
  80. struct InputNode {
  81.  
  82.  std::vector<double> w = std::vector<double>(256, 0.00);
  83.  std::vector<double> o = std::vector<double>(256, 0.00);
  84.  std::vector<double> bw = std::vector<double>(256, 0.00);
  85.  std::vector<double> b = std::vector<double>(256, 0.00);
  86.  
  87. };
  88.  
  89. struct OutputNode {
  90.  
  91.  std::vector<double> w = std::vector<double>(256, 0.00);
  92.  std::vector<double> o = std::vector<double>(256, 0.00);
  93.  std::vector<double> bw = std::vector<double>(256, 0.00);
  94.  std::vector<double> b = std::vector<double>(256, 0.00);
  95.  
  96. };
  97.  
  98. double choose(int x, int aa) {
  99.  
  100.   if (training_mode) return train_input[x][aa];
  101.   else return requested1[aa];
  102.  
  103. }
  104.  
  105.  
  106. double initialize() {
  107.  
  108.   return 0.54321123456789 ;
  109.   //return ((rand()%10000)/10000.00) + 0.001;
  110.  
  111. }
  112.  
  113. double fd(double x) {
  114.  
  115.   return (x >= 0) ? (1) : (0);
  116.  
  117. }
  118.  
  119. double fx(double x) {
  120.  
  121.   return (x >= 0) ? (x) : (0);
  122.  
  123. }
  124.  
  125. int _tmain(int argc, _TCHAR * argv[]) {
  126.  
  127.   srand (time(NULL));
  128.  
  129.   std::random_device rd;
  130.   std::mt19937 gh(rd());
  131.  
  132.   divx = 0;
  133.  
  134.   InputNode a[nr_neurons_A];
  135.   MiddleNode b[nr_neurons_B];
  136.   MiddleNode c[nr_neurons_C];
  137.   MiddleNode d[nr_neurons_D];
  138.   MiddleNode e[nr_neurons_E];
  139.   MiddleNode f[nr_neurons_F];
  140.   OutputNode g[nr_neurons_G];
  141.  
  142.   for (i = 0; i < zrows; i++) {
  143.  
  144.     for (j = 0; j < nr_neurons_A; j++) {
  145.  
  146.       train_input[i][j] = (i + 1.00) / (j + 1);
  147.       train_output[i][j] = 2.33 * pow(train_input[i][j],2);
  148.  
  149.     }
  150.  
  151.   }
  152.  
  153.   for (j = 0; j < nr_neurons_A; j++) {
  154.  
  155.     requested1[j] = 1.50 / (j + 1.00);
  156.  
  157.   }
  158.  
  159.   double input = 0;
  160.  
  161.   int i1, i2;
  162.  
  163.   for (i1 = 0; i1 < nr_neurons_A; i1++)
  164.     for (i2 = 0; i2 < nr_neurons_B; i2++) {
  165.       a[i1].w[i2] = initialize();
  166.       a[i1].bw[i2] = initialize();
  167.       a[i1].b[i2] = initialize();
  168.     }
  169.  
  170.   for (i1 = 0; i1 < nr_neurons_B; i1++)
  171.     for (i2 = 0; i2 < nr_neurons_C; i2++) {
  172.       b[i1].w[i2] = initialize();
  173.       b[i1].bw[i2] = initialize();
  174.       b[i1].b[i2] = initialize();
  175.     }
  176.  
  177.   for (i1 = 0; i1 < nr_neurons_C; i1++)
  178.     for (i2 = 0; i2 < nr_neurons_D; i2++) {
  179.       c[i1].w[i2] = initialize();
  180.       c[i1].bw[i2] = initialize();
  181.       c[i1].b[i2] = initialize();
  182.     }
  183.  
  184.   for (i1 = 0; i1 < nr_neurons_D; i1++)
  185.     for (i2 = 0; i2 < nr_neurons_E; i2++) {
  186.       d[i1].w[i2] = initialize();
  187.       d[i1].bw[i2] = initialize();
  188.       d[i1].b[i2] = initialize();
  189.     }
  190.  
  191.   for (i1 = 0; i1 < nr_neurons_E; i1++)
  192.     for (i2 = 0; i2 < nr_neurons_F; i2++) {
  193.       e[i1].w[i2] = initialize();
  194.       e[i1].bw[i2] = initialize();
  195.       e[i1].b[i2] = initialize();
  196.     }
  197.  
  198.   for (i1 = 0; i1 < nr_neurons_F; i1++)
  199.     for (i2 = 0; i2 < nr_neurons_G; i2++) {
  200.       f[i1].w[i2] = initialize();
  201.       f[i1].bw[i2] = initialize();
  202.       f[i1].b[i2] = initialize();
  203.     }
  204.  
  205.   for (i1 = 0; i1 < nr_neurons_G; i1++) {
  206.     g[i1].w[0] = initialize();
  207.     g[i1].bw[0] = initialize();
  208.     g[i1].b[0] = initialize();
  209.   }
  210.  
  211.   sqcalc = 99999999.99;
  212.   for (temp = 0; temp < loops; temp++) {
  213.  
  214.     //for (rows = 0; rows < zrows+1; rows++)  {
  215.    //   rows = rand() % (zrows + 1); {
  216.     rows = tijdelijk; {
  217.     tijdelijk ++ ;
  218.     if (tijdelijk>zrows) { tijdelijk=0; rows=0;}
  219.  
  220.       shuffled = rows;
  221.  
  222.       if (shuffled == zrows) training_mode = false;
  223.       else training_mode = true;
  224.  
  225.  
  226.  
  227.             std::fill(au_input, au_input+5, 0);
  228.             std::fill(bu_input, bu_input+5, 0);
  229.             std::fill(cu_input, cu_input+5, 0);
  230.             std::fill(du_input, du_input+5, 0);
  231.             std::fill(eu_input, eu_input+5, 0);
  232.             std::fill(fu_input, fu_input+5, 0);
  233.  
  234.  
  235.     for (B = 0; B < nr_neurons_B; B++)
  236.         {
  237.             for (A = 0; A < nr_neurons_A; A++)
  238.                 {
  239.                     a[A].o[B] =  fx(choose(shuffled, A) * a[A].w[B] /*+ a[A].bw[B] * a[A].b[B]*/ );
  240.                     au_input[B] += a[A].o[B]; //the input for B
  241.  
  242.                 }
  243.  
  244.         }
  245.  
  246.     for (C = 0; C < nr_neurons_C; C++)
  247.         {
  248.             for (B = 0; B < nr_neurons_B; B++)
  249.                 {
  250.                     b[B].o[C] = fx( au_input[B] * b[B].w[C]);
  251.                     bu_input[C] += b[B].o[C];
  252.  
  253.                 }
  254.  
  255.         }
  256.  
  257.  
  258.     for (D = 0; D < nr_neurons_D; D++)
  259.         {
  260.             for (C = 0; C < nr_neurons_C; C++)
  261.                 {
  262.                     c[C].o[D] = fx( bu_input[C] * c[C].w[D]);
  263.                     cu_input[D] += c[C].o[D];
  264.  
  265.                 }
  266.  
  267.         }
  268.  
  269.  
  270.     for (E = 0; E < nr_neurons_E; E++)
  271.         {
  272.             for (D = 0; D < nr_neurons_D; D++)
  273.                 {
  274.                     d[D].o[E] = fx( cu_input[D] * d[D].w[E]);
  275.                     du_input[E] += d[D].o[E];
  276.  
  277.                 }
  278.  
  279.         }
  280.  
  281.  
  282.     for (F = 0; F < nr_neurons_F; F++)
  283.         {
  284.             for (E = 0; E < nr_neurons_E; E++)
  285.                 {
  286.                     e[E].o[F] = fx( du_input[E] * e[E].w[F]);
  287.                     eu_input[F] += e[E].o[F];
  288.  
  289.                 }
  290.  
  291.         }
  292.  
  293.     for (G = 0; G < nr_neurons_G; G++)
  294.         {
  295.             for (F = 0; F < nr_neurons_F; F++)
  296.                 {
  297.                     f[F].o[G] = fx( eu_input[F] * f[F].w[G]);
  298.                     fu_input[G] += f[F].o[G];
  299.  
  300.                 }
  301.  
  302.         }
  303.  
  304.  
  305.     for (G = 0; G < nr_neurons_G; G++)
  306.         {
  307.             g[G].o[0] = fx( fu_input[G] * g[G].w[0]);
  308.             //gu_input[H] ..... N/A
  309.         }
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.       if (temp > showtime) {
  318.  
  319.         if (training_mode) {
  320.           printf("\n-");
  321.           for (showoutputs = 0; showoutputs < nr_neurons_G; showoutputs++) {
  322.             divx = divx +  abs(g[showoutputs].o[0] - train_output[shuffled][showoutputs]);
  323.             printf("\n %lf input: %.2lf The current output_%d for training set[%d] is %.28lf of (%lf)",divx, train_input[shuffled][showoutputs], showoutputs, shuffled, g[showoutputs].o[0], train_output[shuffled][showoutputs]);
  324.  
  325.  
  326.           }
  327.         } else {
  328.  
  329.           for (showoutputs = 0; showoutputs < nr_neurons_G; showoutputs++) {
  330.  
  331.             printf("\n(loop: % d) output_%d for requested data (%.28lf) is currently : %.8lf  (should be:%lf)", temp, showoutputs, requested1[showoutputs], g[showoutputs].o[0], 2.00 * pow(requested1[showoutputs],2) );
  332.  
  333.           }
  334.  
  335.         }
  336.  
  337.       }
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.       xerrtotal = 0;
  345.       if (training_mode) {
  346.         for (showoutputs = 0; showoutputs < nr_neurons_G; showoutputs++) {
  347.           xerr[showoutputs] = 2 * (g[showoutputs].o[0] - train_output[shuffled][showoutputs]);
  348.  
  349.           xerrtotal += (xerr[showoutputs]);
  350.         }
  351.  
  352.  sqerrtot = xerrtotal;
  353.  
  354.  
  355.  
  356.         xDD = learn * xerrtotal;
  357.         gDD_times = xDD;
  358.         avgtot = 0; aw = 0; bw = 0; cw = 0; dw = 0; ew = 0; fw = 0; gw = 0;
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.         for (G = 0; G < nr_neurons_G; G++) gDD_times *= g[G].w[0];
  366.         fDD_times = gDD_times;
  367.         for (G = 0; G < nr_neurons_G; G++) { for (F = 0; F < nr_neurons_F; F++) fDD_times *= f[F].w[G]; }
  368.         eDD_times =  fDD_times;
  369.         for (F = 0; F < nr_neurons_F; F++) { for (E = 0; E < nr_neurons_E; E++) eDD_times *= e[E].w[F]; }
  370.         dDD_times = eDD_times;
  371.         for (E = 0; E < nr_neurons_E; E++) { for (D = 0; D < nr_neurons_D; D++) dDD_times *= d[D].w[E]; }
  372.         cDD_times = dDD_times;
  373.         for (D = 0; D < nr_neurons_D; D++) { for (C = 0; C < nr_neurons_C; C++) cDD_times *= c[C].w[D]; }
  374.         bDD_times = cDD_times;
  375.         for (C = 0; C < nr_neurons_C; C++) { for (B = 0; B < nr_neurons_B; B++) bDD_times *= b[B].w[C]; }
  376.  
  377.         rown[0] = 0.00; rown[1] = 0.00; rown[2] = 0.00; rown[3] = 0.00;// tijdelijk = 0;
  378.  
  379.         double row_0, row_1, row_2, row_3;
  380.  
  381.  
  382.         for (G = 0; G < nr_neurons_G; G++) {
  383.  
  384.           zDD_wupd = fu_input[G] * learn * xerr[G];
  385.  
  386.           gDD = xDD * g[G].w[0];
  387.  
  388.           tussen = fd(g[G].o[0]) * zDD_wupd;
  389.            g[G].w[0] -= tussen;
  390.           gw += tussen;
  391.           avgtot += tussen;
  392.           rown[G] += tussen;
  393.  
  394.           for (F = 0; F < nr_neurons_F; F++) {
  395.             gDD_wupd = eu_input[F] * gDD /xerrtotal * xerr[G];
  396.  
  397.  
  398.             tussen = fd(f[F].o[G]) * gDD_wupd/4;
  399.              f[F].w[G] -= tussen;
  400.             //f[F].w[G] -= new_avgtot;
  401.             //f[F].w[G] -= new_fw;
  402.             fw += tussen;
  403.             avgtot += tussen;
  404.             rown[F] += tussen;
  405.              }
  406.         }
  407.  
  408.  
  409.  
  410.  
  411.          for (F = 0; F < nr_neurons_F; F++) {
  412.  
  413.  
  414.             for (E = 0; E < nr_neurons_E; E++) {
  415.               fDD_wupd = du_input[E] * fDD_times;
  416.  
  417.  
  418.               tussen = fd(e[E].o[F]) * fDD_wupd/4;
  419.                e[E].w[F] -= tussen;
  420.               //e[E].w[F] -= new_avgtot;
  421.               //e[E].w[F] -= new_ew;
  422.               ew += tussen;
  423.               avgtot += tussen;
  424.               rown[E] += tussen;
  425.  
  426.  
  427.               }
  428.          }
  429.  
  430.  
  431.  
  432.             for (E = 0; E < nr_neurons_E; E++) {
  433.  
  434.  
  435.               for (D = 0; D < nr_neurons_D; D++) {
  436.                 eDD_wupd = cu_input[D] * eDD_times;
  437.  
  438.  
  439.  
  440.                 tussen = fd(d[D].o[E]) * eDD_wupd/4;
  441.                 d[D].w[E] -= tussen;
  442.                 //d[D].w[E] -= new_avgtot;
  443.                 //d[D].w[E] -= new_dw;
  444.                 dw += tussen;
  445.                 avgtot += tussen;
  446.                 rown[D] += tussen;
  447.  
  448.  
  449.                 }
  450.             }
  451.  
  452.  
  453.           for (D = 0; D < nr_neurons_D; D++) {
  454.  
  455.  
  456.                 for (C = 0; C < nr_neurons_C; C++) {
  457.                   dDD_wupd = bu_input[C] * dDD_times;
  458.  
  459.  
  460.                   tussen = fd(c[C].o[D]) * dDD_wupd/4;
  461.                    c[C].w[D] -= tussen;
  462.                   //c[C].w[D] -= new_avgtot;
  463.                   //c[C].w[D] -= new_cw;
  464.                   cw += tussen;
  465.                   avgtot += tussen;
  466.                   rown[C] += tussen;
  467.  
  468.                   }
  469.           }
  470.  
  471.  
  472.            for (C = 0; C < nr_neurons_C; C++) {
  473.  
  474.  
  475.                   for (B = 0; B < nr_neurons_B; B++) {
  476.                     cDD_wupd =  au_input[B] * cDD_times;
  477.                     tussen = fd(b[B].o[C]) * cDD_wupd/4;
  478.                     b[B].w[C] -= tussen;
  479.                     //b[B].w[C] -= new_avgtot;
  480.                     //b[B].w[C] -= new_bw;
  481.                     bw += tussen;
  482.                     avgtot += tussen;
  483.                     rown[B] += tussen;
  484.  
  485.  
  486.                     }
  487.            }
  488.  
  489.  
  490.  
  491.  
  492.             for (B = 0; B < nr_neurons_B; B++) {
  493.  
  494.  
  495.                     for (A = 0; A < nr_neurons_A; A++) {
  496.  
  497.                       tussen = train_input[shuffled][A] * fd(a[A].o[B]) * bDD_times/4;
  498.                       a[A].w[B] -= tussen;
  499.                       //a[A].w[B] -= new_avgtot;
  500.                       //a[A].w[B] -= new_aw;
  501.                       aw += tussen;
  502.                       avgtot += tussen;
  503.                       rown[A] += tussen;
  504.  
  505.  
  506.                       }
  507.             }
  508.  
  509.  
  510.        //printf("\ntijdelijk %d",tijdelijk);
  511.  
  512.        avgtot = avgtot / 96;
  513.  
  514.        aw = aw / (nr_neurons_A*4);
  515.        bw = bw / (nr_neurons_B*4);
  516.        cw = cw / (nr_neurons_C*4);
  517.        dw = dw / (nr_neurons_D*4);
  518.        ew = ew / (nr_neurons_E*4);
  519.        fw = fw / (nr_neurons_F*4);
  520.        gw = gw / (nr_neurons_G);
  521.  
  522.         rown[0] = rown[0] / 24;
  523.         rown[1] = rown[1] / 24;
  524.         rown[2] = rown[2] / 24;
  525.         rown[3] = rown[3] / 24;
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.       }
  541.  
  542.  
  543.     }
  544.  
  545.  
  546.  
  547.     if (!(temp % (showtime/100))) printf("\n %d %%",++Z);
  548.  
  549.     sqcalc += sqerrtot;
  550.  
  551.     if (temp % 1000 == 0) {
  552.       gemiddelde = sqcalc / 1000;
  553.       sqcalc = 0;
  554.     }
  555.  
  556.     sqerrtot = 0;
  557.  
  558.   }
  559.  
  560.   printf("\nEnd.");
  561.   scanf("%d", &Z);
  562.  
  563.   return 0;
  564.  
  565. }
Advertisement
Add Comment
Please, Sign In to add comment