Hexadroid

pow2

Sep 11th, 2020
1,256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.79 KB | None | 0 0
  1. #include <algorithm>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <iostream>
  5.  
  6. using std::cin;
  7. using std::cout;
  8. using std::endl;
  9.  
  10.     const long double learningrate=0.001;
  11.     const long double a = 3;
  12.     const long double k = 1.555;
  13.     const long double pi = 2 * acos(0.00);
  14.     const long double version = 0.001921;
  15.     const long double cyclesi = 25000; // 1% cycles per printout
  16.     const long double sqrt2 = pow(2,0.5);
  17.     const int training_inputs=200;
  18.     const int mt = 30;
  19.     const int mt_implemented=10; //nr of fully implemented methods
  20.     const long double TESTbooster = 31; // set to 1. Only set to higher values when testing !   speeds up the training. typical values between 1 and 40
  21.  
  22.     const char *colour[mt_implemented+1] = { "Unused", "Expiri", "Softplus", "Sigmoid", "TanH", "Arctan", "RELU", "Square", "EliSH", "Softsign", "Test 1"};
  23.  
  24.     long double training_set_inputs[training_inputs][3];
  25.     long double training_set_outputs[training_inputs], new_situation[training_inputs][3];
  26.  
  27.     int mc=1;
  28.  
  29.     long double xSynapticWeight[mt][4];
  30.     long double xOutput5[mt], xDOToutput5[mt], xoutput[mt];
  31.     long double Bxoutput[mt][training_inputs], BxPart[mt][training_inputs], Bx_DerivativePart[mt][training_inputs], BxerrorPart[mt][training_inputs], Bx[mt][training_inputs], BxadjustSynapticWeight[mt][4], BxadjustmentPart[mt][training_inputs];
  32.     long double oldoutput, cool, indicator,i=1;
  33.     int m,q,qq, create;
  34.  
  35.     int f,g,h,y;
  36.  
  37.  
  38. long double amethod(long double fx, long double x, int b, int c) //function, direct-input,method number,1fx or 2 derivative.
  39.     {
  40.  
  41.             if (c==1) {
  42.  
  43.                       switch(b) {
  44.                                         case 0   :  return 1;           //fx : not used
  45.  
  46.  
  47.                                         case 1   :  return pow(139,fx);      //fx : test
  48.  
  49.  
  50.                                         case 2   :  return logl(1+expl(fx))/k;        //fx : softplus
  51.  
  52.  
  53.                                         case 3   :  return 1 / (1 + expl(-(fx)));       //fx : sigmoid
  54.  
  55.  
  56.                                         case 4   :  return expl(fx)-expl(-fx)/(expl(fx)+expl(-fx));;      //fx : TanH
  57.  
  58.  
  59.                                         case 5   :  return atan(fx);                                //fx : Arctan
  60.  
  61.  
  62.                                         case 6   : return (fx>=0)?(fx):(0);                  //fx : RELU
  63.  
  64.  
  65.                                         case 7  :   return fx / powl( (1+a*fx*fx),0.5);               //fx : Inverse Square Root Linear Units
  66.  
  67.  
  68.                                         case 8  :   return (fx>=0)?(fx/(fx+expl(-fx))):((expl(fx)-1)/(1+expl(-fx)));  // fx : ELiSH
  69.  
  70.  
  71.                                         case 9  :   return fx/(1+abs(fx));           // fx ElliotSig / Softsign
  72.  
  73.  
  74.                                         case 10 :   return expl(sqrt2*fx)+1;       // fx : Expirimental
  75.  
  76.  
  77.                                        default :
  78.                                                      cout << "Error !" << endl;
  79.                                 }
  80.  
  81.  
  82.                         return 0;
  83.                       }
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.             if (c==2) {
  93.  
  94.                                    //fx and x names are meant for the derivatives.
  95.                          switch(b) {
  96.                                         case 0   :  return 1;           //derivative : not used
  97.  
  98.  
  99.                                         case 1   :  return powl(139,fx)*log10l(139);     //derivative : testing
  100.  
  101.  
  102.                                         case 2   :  return expl(x)/(k*expl(x)+k);        //derivative: softplus
  103.  
  104.  
  105.                                         case 3   :  return fx * (1 - fx);      //derivative : sigmoid
  106.  
  107.  
  108.                                         case 4   :  return 4/powl((expl(fx)+expl(-fx)),2);     //derivative : TanH
  109.  
  110.  
  111.                                         case 5   :  return 1/((x*x)+1);                  //derivative : Arctan
  112.  
  113.  
  114.                                         case 6   :  return (x>=0)?(1):(0);       //derivative : RELU
  115.  
  116.  
  117.                                         case 7  :   return powl(1 / powl( (1+a*x*x),0.5),3);    // derivative : Inverse Square Root Linear Units
  118.  
  119.  
  120.                                         case 8  :   return   (x>=0)?((expl(x)*(x+expl(x)+1))/powl(expl(x)+1,2)):(expl(x)*(2*expl(x)+expl(2*x)-1)/powl(expl(x)+1,2));      // derivative : ELiSH
  121.  
  122.  
  123.                                         case 9 :   return 1/powl(1 + abs(fx),2);        // derivative ElliotSig / Softsign
  124.  
  125.  
  126.                                         case 10 : return sqrt2*exp(sqrt2*fx); // still to be determined
  127.  
  128.  
  129.                                        default :
  130.                                                      cout << "Error !" << endl;
  131.                                 }
  132.  
  133.  
  134.  
  135.                       }
  136.  
  137.  
  138.  
  139.  
  140.     return 0;
  141.  
  142.     }
  143.  
  144.  
  145. long double outt(long double (*array)[training_inputs][3], int b)
  146. {
  147.     long double calc;
  148.  
  149.        calc= pow((*array)[b][0],2)  +  7*pow((*array)[b][1],2)  +  11.55*(*array)[b][2];
  150.        //calc= 10*(*array)[b][0]  +  20.5*(*array)[b][1]  +  14.77*(*array)[b][2];
  151.  
  152.         if (calc==0) { calc=1;  printf("\ntest\n");
  153.                      }
  154.  
  155.  
  156.     return calc;
  157. }
  158.  
  159.  
  160.  int _tmain(int argc, _TCHAR* argv[])
  161. {
  162.  
  163.     srand (time(NULL));
  164.  
  165.     new_situation[0][0]=52.00;new_situation[0][1]=32.00;new_situation[0][2]=68.00;
  166.     cool = outt(&new_situation,0);
  167.  
  168.        printf("\n the output should be approx %.2Lf\n",cool);
  169.  
  170. for (create = 0; create < training_inputs; create++) {
  171.  
  172.                 bool redo=false;
  173.  
  174.                 do
  175.                 {
  176.  
  177.                 training_set_inputs[create][0]= (rand() % 60) + 20.00L;
  178.                 training_set_inputs[create][1]= (rand() % 60) + 20.00L;
  179.                 training_set_inputs[create][2]= (rand() % 60) + 20.00L;
  180.  
  181.  
  182.                 training_set_outputs[create] = outt(&training_set_inputs, create);
  183.                    if (training_set_outputs[create]==0) redo=true;
  184.  
  185.  
  186.                 }  while (redo==true);
  187.  
  188.                 //printf("\ntr %Lf %Lf %Lf %Lf",training_set_inputs[create][0], training_set_inputs[create][1], training_set_inputs[create][2], training_set_outputs[create]);Sleep(10);
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.             }
  196.  
  197.  
  198.     new_situation[0][0]= 1/new_situation[0][0];
  199.     new_situation[0][1]= 1/new_situation[0][1];
  200.     new_situation[0][2]= 1/new_situation[0][2];
  201.  
  202.  
  203.     for(auto& rows: training_set_inputs)
  204. {
  205.     for(auto& elem: rows)
  206.     {
  207.         elem =  1/elem;
  208.  
  209.     }
  210. }
  211.  
  212.  
  213.     for (auto& number : training_set_outputs)
  214.     {
  215.         number =  1/number;
  216.  
  217.     }
  218.  
  219.  
  220.     for (mc = 1; mc <= mt_implemented; mc++) {
  221.  
  222.  
  223.                                     // xSynapticWeight[mc][1]=(rand() % 2000)/2000.00;
  224.                                     // xSynapticWeight[mc][2]=(rand() % 2000)/2000.00;
  225.                                     // xSynapticWeight[mc][3]=(rand() % 2000)/2000.00;
  226.  
  227.  
  228.                                     xSynapticWeight[mc][1]=(rand()%1000)/1000L;
  229.                                     xSynapticWeight[mc][2]=(rand()%1000)/1000L;
  230.                                     xSynapticWeight[mc][3]=(rand()%1000)/1000L;
  231.  
  232.                                     //printf("\nh%.6Lf %.6Lf %.6Lf", xSynapticWeight[mc][1], xSynapticWeight[mc][2], xSynapticWeight[mc][3]);
  233.  
  234.  
  235.                                 }
  236.  
  237.  
  238.  
  239.  
  240.  
  241.     while (i++) {
  242.  
  243.  
  244.         //for( q = 1; q <= training_inputs; q++)
  245.         q = rand() % training_inputs + 1;    //trains faster : for testing only
  246.                                         {
  247.                                                 for (mc = 1; mc <= mt_implemented; mc++) {
  248.  
  249.                                                         Bxoutput[mc][q] = training_set_inputs[q-1][0]+training_set_inputs[q-1][1]+training_set_inputs[q-1][2]+training_set_inputs[q-1][0]*xSynapticWeight[mc][1]+training_set_inputs[q-1][1]*xSynapticWeight[mc][2]+training_set_inputs[q-1][2]*xSynapticWeight[mc][3];
  250.  
  251.                                                         Bxoutput[mc][q] += (training_set_inputs[q-1][0]+training_set_inputs[q-1][1]+training_set_inputs[q-1][2])*xSynapticWeight[mc][1];
  252.                                                         Bxoutput[mc][q] += (training_set_inputs[q-1][0]+training_set_inputs[q-1][1]+training_set_inputs[q-1][2])*xSynapticWeight[mc][2];
  253.                                                         Bxoutput[mc][q] += (training_set_inputs[q-1][0]+training_set_inputs[q-1][1]+training_set_inputs[q-1][2])*xSynapticWeight[mc][3];
  254.  
  255.                                                         Bxoutput[mc][q] += (training_set_inputs[q-1][0]+training_set_inputs[q-1][1])*xSynapticWeight[mc][1];
  256.                                                         Bxoutput[mc][q] += (training_set_inputs[q-1][0]+training_set_inputs[q-1][1])*xSynapticWeight[mc][2];
  257.                                                         Bxoutput[mc][q] += (training_set_inputs[q-1][0]+training_set_inputs[q-1][1])*xSynapticWeight[mc][3];
  258.  
  259.                                                         Bxoutput[mc][q] += (training_set_inputs[q-1][0]+training_set_inputs[q-1][2])*xSynapticWeight[mc][1];
  260.                                                         Bxoutput[mc][q] += (training_set_inputs[q-1][0]+training_set_inputs[q-1][2])*xSynapticWeight[mc][2];
  261.                                                         Bxoutput[mc][q] += (training_set_inputs[q-1][0]+training_set_inputs[q-1][2])*xSynapticWeight[mc][3];
  262.  
  263.                                                         Bxoutput[mc][q] += (training_set_inputs[q-1][1]+training_set_inputs[q-1][2])*xSynapticWeight[mc][1];
  264.                                                         Bxoutput[mc][q] += (training_set_inputs[q-1][1]+training_set_inputs[q-1][2])*xSynapticWeight[mc][2];
  265.                                                         Bxoutput[mc][q] += (training_set_inputs[q-1][1]+training_set_inputs[q-1][2])*xSynapticWeight[mc][3];
  266.  
  267.                                                         BxPart[mc][q] = amethod(Bxoutput[mc][q],0,mc,1);
  268.  
  269.  
  270.                                                         Bx_DerivativePart[mc][q] = TESTbooster * amethod(BxPart[mc][q],Bxoutput[mc][q],mc,2);
  271.  
  272.  
  273.                                                         BxerrorPart[mc][q] = (amethod(training_set_outputs[q-1],0,mc,1) - BxPart[mc][q]);
  274.  
  275.  
  276.                                                         BxadjustmentPart[mc][q] =  BxerrorPart[mc][q] * Bx_DerivativePart[mc][q];
  277.  
  278.  
  279.                                             }
  280.  
  281.  
  282.                                                 }
  283.  
  284.  
  285.  
  286.         for( q = 1; q <= 3; q++) {
  287.                                     for (mc = 1; mc <= mt_implemented; mc++) {
  288.  
  289.  
  290.                                                                 BxadjustSynapticWeight[mc][q]=0;
  291.  
  292.  
  293.                                                                 //for( qq = 1; qq <= training_inputs; qq++) {  BxadjustSynapticWeight[mc][q] += (training_set_inputs[qq-1][q-1]*BxadjustmentPart[mc][qq])/training_inputs;}
  294.                                                                 for( qq = 1; qq <= training_inputs; qq++) {  BxadjustSynapticWeight[mc][q] += (amethod(training_set_inputs[qq-1][q-1],0,mc,1)*BxadjustmentPart[mc][qq])/training_inputs;}
  295.  
  296.  
  297.                                                                 xSynapticWeight[mc][q]=xSynapticWeight[mc][q]+BxadjustSynapticWeight[mc][q]*learningrate;
  298.  
  299.                                                                 }
  300.                                  }
  301.  
  302.  
  303.  
  304.         if ((floor(i/cyclesi*100))!=indicator)
  305.                 {
  306.                 for (mc = 1; mc <= mt_implemented; mc++) {
  307.  
  308.                                             xOutput5[mc]=new_situation[0][0]+new_situation[0][1]+new_situation[0][2]+new_situation[0][0]*xSynapticWeight[mc][1]+new_situation[0][1]*xSynapticWeight[mc][2]+new_situation[0][2]*xSynapticWeight[mc][3];
  309.                                             xOutput5[mc]+= (new_situation[0][0]+new_situation[0][1]+new_situation[0][2])*xSynapticWeight[mc][1];
  310.                                             xOutput5[mc]+= (new_situation[0][0]+new_situation[0][1]+new_situation[0][2])*xSynapticWeight[mc][2];
  311.                                             xOutput5[mc]+= (new_situation[0][0]+new_situation[0][1]+new_situation[0][2])*xSynapticWeight[mc][3];
  312.  
  313.                                             xOutput5[mc]+= (new_situation[0][0]+new_situation[0][1])*xSynapticWeight[mc][1];
  314.                                             xOutput5[mc]+= (new_situation[0][0]+new_situation[0][1])*xSynapticWeight[mc][2];
  315.                                             xOutput5[mc]+= (new_situation[0][0]+new_situation[0][1])*xSynapticWeight[mc][3];
  316.  
  317.                                             xOutput5[mc]+= (new_situation[0][0]+new_situation[0][2])*xSynapticWeight[mc][1];
  318.                                             xOutput5[mc]+= (new_situation[0][0]+new_situation[0][2])*xSynapticWeight[mc][2];
  319.                                             xOutput5[mc]+= (new_situation[0][0]+new_situation[0][2])*xSynapticWeight[mc][3];
  320.  
  321.                                             xOutput5[mc]+= (new_situation[0][1]+new_situation[0][2])*xSynapticWeight[mc][1];
  322.                                             xOutput5[mc]+= (new_situation[0][1]+new_situation[0][2])*xSynapticWeight[mc][2];
  323.                                             xOutput5[mc]+= (new_situation[0][1]+new_situation[0][2])*xSynapticWeight[mc][3];
  324.  
  325.                                                                         //xOutput5[mc]=new_situation[0][0]*xSynapticWeight[mc][1]+new_situation[0][1]*xSynapticWeight[mc][2]+new_situation[0][2]*xSynapticWeight[mc][3];
  326.                                             if (xOutput5[mc]!=0) xoutput[mc] = 1/xOutput5[mc];
  327.  
  328.                                             printf("\nmethod:%s\t %.0Lf iterations) has output %.2Lf  (targetvalue = %.2Lf)",colour[mc],i, xoutput[mc],cool);
  329.  
  330.                                             }
  331.                           printf("\n");
  332.                 }
  333.  
  334.  
  335.  
  336.         indicator = floor(i/cyclesi*100);
  337.  
  338.  
  339.     }
  340.  
  341.  
  342.  
  343.     cin.get();
  344.  
  345.  
  346.  
  347.  
  348.  
  349.     return 0;
  350. }
Advertisement
Add Comment
Please, Sign In to add comment