Advertisement
Hexadroid

EliSH-2

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