Guest User

Untitled

a guest
Apr 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.49 KB | None | 0 0
  1. //--------------------------------------------------------------------------------------------------------------            
  2. //                                                                                                                          
  3. //           --> w1[0][0]---------> net1[0] > tanh(sum...)-->out1[0]--> w2[0]-------|                                      
  4. //      X -----> w1[0][1]--    |    net1[1]                                         |                                      
  5. //           --> w1[0][2]--    |    net1[2] ----  "  --------out1[2]--> w2[2]-------|                                      
  6. //               ...           |                             ...                    --> out2 (=tanh(sum..))-- sign()--->  +1
  7. //                             |                            w2[N-1]                 |                               |      
  8. //           --> w1[1][0]-------                                                    |                               --> -1  
  9. //      Y -----> w1[1][0]--    |                                                    |                                      
  10. //           --> w1[1][0]--    |                                                    |                                      
  11. //                ...          |                                                    |                                      
  12. //                             |                      |-1| --> wb-------------------|                                      
  13. //           --> w1[2][0]-------                               (für bias)                                                  
  14. //    |-1| ----> w1[2][0]--                                                                                                
  15. //           --> w1[2][0]--                                                                                                
  16. //                                                                                                                          
  17. //                                                                                                                          
  18. //--------------------------------------------------------------------------------------------------------------            
  19.  
  20. #include<stdio.h>
  21. #include<stdlib.h>
  22. #include<math.h>
  23. #include<time.h>
  24.  
  25. //==========================================//
  26. #define N 20 // Neurons in Hidden Layer     //<<===== Dynamic
  27. #define maxrounds 30000
  28. #define tanhd(x) (1-tanh(x)*tanh(x))            //
  29. double winit = 0.2;    //   [-;+]           //
  30. double etha = 0.001;                             //
  31. //==========================================//
  32.  
  33. //---------------------------------------------------//
  34.  
  35. double x[1000] = {0};
  36. double y[1000] = {0};
  37. double z[1000] = {0};
  38.  
  39. //---------------------------------------------------//
  40. double bias = -1;
  41. double w1[3][N];
  42. double w2[N];
  43. double wb;
  44.  
  45. double delta1[N];
  46. double delta2;
  47. double out1[N];
  48. double out2;
  49. double net1[N];
  50. double net2;
  51. // ----------------FUNCTIONS ------------------------//
  52. double randdouble(double a, double b);
  53. void read_input();
  54. void init();
  55. double forward(double x,double y);
  56. void plotnet();
  57. void plotw1w2n();
  58. double sign(double x);
  59. void train_net();
  60. void evaluate();
  61.  
  62. // --------------------------------------------------//
  63.  
  64.  
  65. int main(){
  66.  
  67.     srand(time(NULL));
  68.     read_input();
  69.     init();
  70.    
  71.  
  72.     train_net();
  73.    
  74.     //plotw1w2n();
  75.    
  76.  
  77.  
  78.     evaluate();
  79.  
  80.     return 0;
  81.  
  82. }  // END MAIN
  83.  
  84. // --------------Subfunctions------------------------//
  85.  
  86. void plot_tanhd(){
  87.     for(int i = 0; i < 100; i++){
  88.         double bb = (i-50)/5.0;
  89.         printf("%7.3lf : %lf\n",bb,tanhd(bb));  
  90.     }
  91. }
  92.  
  93. void evaluate(){
  94.     double a,b;
  95.     int flag;
  96.     flag = scanf("%lf,%lf\n", &a,&b);
  97.              
  98.     while(flag > 0){
  99.         if(sign(forward(a,b)) == 1)
  100.             printf("+1\n");
  101.         else    
  102.             printf("-1\n");
  103.         flag = scanf("%lf,%lf\n", &a,&b);
  104.     }
  105. }
  106.  
  107. double randdouble(double a, double b){
  108.     double x = rand()/(double)RAND_MAX;
  109.     x=x*(b-a)+a;
  110.     return x;
  111. }
  112.  
  113. void read_input(){
  114.    
  115.     for(int i = 0; i < 1000; i++){
  116.         scanf("%lf,%lf,%lf\n",&x[i],&y[i],&z[i]);
  117.         if(x[i]==0 && y[i]==0 && z[i]==0)
  118.             break;
  119.     }
  120. }
  121.  
  122. void init(){
  123.     for(int i = 0; i < N; i++){
  124.         w1[0][i] = randdouble(-winit,winit); //for x
  125.         w1[1][i] = randdouble(-winit,winit); //for y
  126.         w1[2][i] = randdouble(-winit,winit); //for bias
  127.         w2[i] = randdouble(-winit,winit);   // for n[i]
  128.     }
  129.     wb = randdouble(-winit,winit);  //for bias
  130. }
  131.  
  132. double forward(double x,double y){
  133.     //printf("x:%lf y:%lf\n",x,y);
  134.    
  135.     for(int i = 0; i < N; i++){
  136.         net1[i] = ( w1[0][i]*x + w1[1][i]*y + w1[2][i]*bias );
  137.         out1[i] = tanh(net1[i]);
  138.         // printf("n1 %lf,",n[i]);
  139.     }
  140.    
  141.     net2 = 0; // then sum all together
  142.     for(int i = 0; i < N; i++){
  143.             net2 = net2 + w2[i]*out1[i];
  144.     }
  145.     net2 = net2 + wb*bias;  // sumation finished
  146.    
  147.     out2 = tanh(net2); // linear or tanh ???
  148.     return out2;
  149.    
  150. }
  151.  
  152. void plotnet(){
  153.     double x,y;
  154.     for(int i = 50; i >= -50; i--){
  155.         y = i/5.0;
  156.         for(int k = -50; k <= 50; k++){
  157.             x = k/5.0;
  158.             if(x == 0 && y == 0)
  159.                 printf("0");
  160.             else if(y == 0 && ( x == 1 || x == -1) )
  161.                 printf("1");
  162.             else if(y == 0 && ( x == 5 || x == -5) )
  163.                 printf("5");
  164.             else if(x == 0 && ( y == 5 || y == -5) )
  165.                 printf("5");
  166.             else if(x == 0 && ( y == 1 || y == -1) )
  167.                 printf("1");
  168.             else if(sign(forward(x,y))==1)
  169.                 printf("+");
  170.             else   
  171.                 printf("-");
  172.         }
  173.         printf("\n");
  174.     }
  175. }
  176.  
  177. void plotw1w2n(){
  178.     for(int i = 0; i < N; i++){
  179.         printf("w1x:%lf, w1y:%lf, w1b:%lf, w2:%lf, n:%lf\n",w1[0][i],w1[1][i],w1[2][i],w2[i],net1[i]);
  180.     }    
  181. }
  182.  
  183. double sign(double val){
  184.     if(val >= 0) return 1.0;
  185.     else return -1.0;
  186. }
  187.  
  188. void train_net(){
  189.    
  190.  
  191.    
  192.     int count = 0;
  193.     int flag;
  194.    
  195.  
  196.    
  197.     while(1){
  198.         //printf("w1[0][0],  w1[1][0],  w1[2][0],  w2[0],  n[0]\n");
  199.         //printf("%8.4lf %8.4lf %8.4lf %8.4lf %8.4lf  INIT\n",w1[0][0],w1[1][0],w1[2][0],w2[0],n[0]);
  200.        
  201.         flag = 1;
  202.        
  203.         for(int i = 0; (x[i]!=0) && (y[i]!=0) && (z[i]!=0) ; i++){
  204.            
  205.             double tempout2 = forward(x[i],y[i]);  // global out1[n], out2, net1[n], net2 are changed
  206.            
  207.             if( 1){
  208.                
  209.                 flag = 0;          
  210.                
  211.                 delta2 = z[i] - tempout2; // expected - current
  212.                
  213.                 for(int k = 0; k < N; k++){
  214.                     w2[k] = w2[k] + etha * out1[k] * tanhd(net2) * delta2;
  215.                 }
  216.                
  217.                 //input to hidden
  218.                 for(int k = 0; k < N; k++){
  219.                     delta1[k] = delta2 * w2[k];        //da layer 2 nur 1 neuron
  220.  
  221.                    
  222.                     w1[0][k] = w1[0][k] + etha * x[i] * ( tanhd(net1[k]) ) * delta1[k];
  223.                     w1[1][k] = w1[1][k] + etha * y[i] * ( tanhd(net1[k]) ) * delta1[k];
  224.                     w1[2][k] = w1[2][k] + etha * bias * ( tanhd(net1[k]) ) * delta1[k];
  225.                  
  226.                 }
  227.                
  228.                 //printf("%8.4lf %8.4lf %8.4lf %8.4lf %8.4lf\n",w1[0][k],w1[1][k],w1[2][k],w2[k],n[k]);
  229.            
  230.             }
  231.            
  232.         }
  233.        
  234.  
  235.                              
  236.  
  237.         if(flag == 1)
  238.             break;
  239.         if(count == maxrounds)
  240.             break;
  241.         count++;
  242.     }
  243.    
  244.     //printf("count: %i\n",count);
  245. }
Add Comment
Please, Sign In to add comment