Advertisement
Hexadroid

Untitled

Sep 13th, 2020 (edited)
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.01 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 <stdio.h>
  12. #include <math.h>
  13.  
  14.  
  15. int i;
  16. const long double version = 0.000000000000003;
  17. const double learning_rate = 0.1;
  18. double bias = 0.025;
  19. double node_c_cost_function, node_c_cost_function_derivative,node_c_cost_function_gradient ,node_b_cost_function_gradient, node_a_cost_function_gradient_to_b, node_a_cost_function_gradient_to_w;
  20. double node_a_input, node_b_input, node_c_input;
  21. double node_a_weight_to_b, node_b_weight, node_c_weight;
  22. double node_a_weight_to_w;
  23. double node_a_output_to_b, node_a_output_to_w, node_b_output_to_c, node_c_output, node_u_cost_function_gradient_to_b, node_u_cost_function_gradient_to_w, node_w_cost_function_gradient;
  24. double node_u_input, node_w_input, node_u_weight_to_w, node_u_weight_to_b, node_w_weight, node_u_output_to_w, node_u_output_to_b , node_w_output_to_c;
  25. double node_c_desired_output, node_u_part_to_w, node_u_part_to_b, node_b_part_to_c, node_w_part_to_c, node_c_part;
  26.  
  27. double layer1_C_node_a_to_b, layer1_C_node_a_to_w, layer1_C_node_u_to_b, layer1_C_node_u_to_w, layer2_C_node_w, layer2_C_node_b, layer1_C, layer2_C, layer3_C;
  28.  
  29. double node_c_cost_function_gradient_layer3, node_c_cost_function_gradient_layer2,node_c_cost_function_gradient_layer1 ;
  30.  
  31. double node_a_part_to_b, node_a_part_to_w, layer3_C_node_c;
  32.  
  33. //                                                "node b"
  34. //NETWORK SIMPLE         "node a"  input->O-----------O------------\
  35. //                                             x                    O "node c" --> output
  36. //NETWORK SIMPLE         "node u"  input->O-----------O------------/
  37. //                                                "node w"
  38.  
  39. //               x:the output of node a also goes to node w, the output of node u also goes to node b.
  40. //               in this case, both the 2 outputs of "node u" are exactly the same. will be another test to see what happens in case there are 2 different outputs per input-node.
  41.  
  42. //               code 'seems' to be working, but sometimes this is an illusion ; in many cases some parameters are not perfectly set or mixed up, so testing is necessary.
  43.  
  44.  
  45. double another_node_function (double x)
  46. {
  47.   return exp(x)-exp(-x)/(exp(x)+exp(-x));
  48. }
  49.  
  50. double another_node_function_derivative (double x)
  51. {
  52.     return 4/pow((exp(x)+exp(-x)),2);
  53. }
  54.  
  55.  
  56. double node_x_function(double x)
  57. {
  58.     return 1 / (1 + exp(-(x)));
  59. }
  60.  
  61. double node_x_function_derivative(double x)
  62. {
  63.      return x * (1 - x);
  64. }
  65.  
  66. int _tmain(int argc, _TCHAR* argv[])
  67. {
  68.  
  69.     node_a_input = 0.02;
  70.     node_u_input = 0.09;
  71.     node_a_weight_to_b = 0.32;
  72.     node_a_weight_to_w = 0.70;
  73.  
  74.     node_b_weight = 0.15; node_c_weight = 0.45;node_u_weight_to_b = 0.8; node_u_weight_to_w=0.69; node_w_weight = 0.55;
  75.     node_c_desired_output = 0.005;
  76.  
  77.   for (i=1; i < 24730000; i++) {
  78.  
  79.                         //node_a_input is constantly the same, example.
  80.                         node_a_part_to_b = node_a_input*node_a_weight_to_b + bias;
  81.                         node_a_part_to_w = node_a_input*node_a_weight_to_w + bias;
  82.  
  83.                         node_a_output_to_b = node_x_function(node_a_part_to_b);
  84.                         node_a_output_to_w = node_x_function(node_a_part_to_w);
  85.  
  86.                         //node_u_input is constantly the same, example.
  87.                         node_u_part_to_w =  node_u_input*node_u_weight_to_w + bias;
  88.                         node_u_part_to_b =  node_u_input*node_u_weight_to_b + bias;
  89.  
  90.  
  91.                         node_u_output_to_w = node_x_function(node_u_part_to_w);
  92.                         node_u_output_to_b = node_x_function(node_u_part_to_b);
  93.  
  94.                                 layer1_C_node_a_to_b = 2*(node_c_desired_output - node_a_part_to_b);
  95.                                 layer1_C_node_a_to_w = 2*(node_c_desired_output - node_a_part_to_w);
  96.                                 layer1_C_node_u_to_b = 2*(node_c_desired_output - node_u_part_to_b);
  97.                                 layer1_C_node_u_to_w = 2*(node_c_desired_output - node_u_part_to_w);
  98.                                 layer1_C        = layer1_C_node_a_to_b+layer1_C_node_a_to_w  + layer1_C_node_u_to_b + layer1_C_node_u_to_w;
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.                         node_b_input = node_a_output_to_b+ node_u_output_to_b;
  107.                         node_b_part_to_c  = node_b_input*node_b_weight + bias;
  108.                         node_b_output_to_c = node_x_function(node_b_part_to_c);
  109.  
  110.                         node_w_input = node_a_output_to_w+node_u_output_to_w;
  111.                         node_w_part_to_c  =  node_w_input*node_w_weight + bias;
  112.                         node_w_output_to_c = another_node_function(node_w_part_to_c);   //use another function for w-node
  113.  
  114.                                 layer2_C_node_w = 2*(node_c_desired_output - node_w_part_to_c);//pow(node_c_desired_output - node_w_part,2);
  115.                                 layer2_C_node_b = 2*(node_c_desired_output - node_b_part_to_c);//pow(node_c_desired_output - node_b_part,2);
  116.                                 layer2_C    = layer2_C_node_w + layer2_C_node_b;
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.                         node_c_input =  node_b_output_to_c+node_w_output_to_c;
  125.                         node_c_part =  node_c_input*node_c_weight + bias;
  126.                         node_c_output = node_x_function(node_c_part);
  127.                                 layer3_C_node_c = 2*(node_c_desired_output - node_c_part);
  128.                                 layer3_C = layer3_C_node_c; // +layer3_C_node_x +layer3_C_node_y etc..
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.                         //needs forward propagation in here, tbd.
  136.  
  137.                         //
  138.  
  139.  
  140.  
  141.  
  142.  
  143.                         //multi layer 'network' , backward propagation test phase
  144.                         node_c_cost_function = pow(node_c_desired_output - node_c_output,2);
  145.                         node_c_cost_function_derivative = 2*(node_c_desired_output - node_c_output);
  146.  
  147.                         node_c_cost_function_gradient_layer3 = node_x_function_derivative(node_c_output) * node_c_cost_function_derivative;
  148.                         node_c_cost_function_gradient_layer2 = node_x_function_derivative(node_c_output) * 2*(node_c_desired_output-layer2_C);
  149.                         node_c_cost_function_gradient_layer1 = node_x_function_derivative(node_c_output) * 2*(node_c_desired_output-layer1_C) ;
  150.  
  151.  
  152.  
  153.  
  154.                         node_b_cost_function_gradient =  node_x_function_derivative(node_b_output_to_c) *node_c_weight * node_c_cost_function_gradient_layer2;
  155.                         node_w_cost_function_gradient =  another_node_function_derivative(node_w_input)  *node_c_weight * node_c_cost_function_gradient_layer2;
  156.  
  157.                         //there are two possible pathways starting from node a  a->b->c   and a->w->c
  158.                         node_a_cost_function_gradient_to_b =  node_x_function_derivative(node_a_input) *node_b_weight*  node_c_weight * node_c_cost_function_gradient_layer1;
  159.                         node_a_cost_function_gradient_to_w =  node_x_function_derivative(node_a_input) *node_w_weight*  node_c_weight * node_c_cost_function_gradient_layer1;
  160.  
  161.  
  162.                         //there are two possible pathways starting from node u  u->w->c   and u->b->c
  163.                         node_u_cost_function_gradient_to_b =  node_x_function_derivative(node_u_input) *node_b_weight* node_c_weight *  node_c_cost_function_gradient_layer1;
  164.                         node_u_cost_function_gradient_to_w =  node_x_function_derivative(node_u_input) *node_w_weight* node_c_weight *  node_c_cost_function_gradient_layer1;
  165.  
  166.  
  167.  
  168.                         node_c_weight = node_c_weight + learning_rate * node_c_cost_function_gradient_layer3;
  169.  
  170.                         node_b_weight = node_b_weight + learning_rate * node_b_cost_function_gradient ;
  171.                         node_w_weight = node_w_weight + learning_rate * node_w_cost_function_gradient ;
  172.  
  173.                         node_a_weight_to_b = node_a_weight_to_b + learning_rate * node_a_cost_function_gradient_to_b ;
  174.                         node_a_weight_to_w = node_a_weight_to_w + learning_rate * node_a_cost_function_gradient_to_w ;
  175.  
  176.                         node_u_weight_to_b = node_u_weight_to_b + learning_rate * node_u_cost_function_gradient_to_b;
  177.                         node_u_weight_to_w = node_u_weight_to_w + learning_rate * node_u_cost_function_gradient_to_w;
  178.  
  179.  
  180.  
  181.                            if (i>600000) {
  182.  
  183.  
  184.                             //printf("\nout: %.15lf %.15lf %.15lf %.15lf %.15lf", node_a_input, node_b_input, node_a_output, node_b_output, node_c_desired_output);
  185.                             //printf("\nout: %.15lf %.15lf", node_c_cost_function, node_c_cost_function_derivative);
  186.                             printf("\noutput %d %.15lf %.15lf %.15lf %.15lf %.15lf",i, node_c_output, node_a_weight_to_b, node_a_weight_to_w, node_u_weight_to_b, node_u_weight_to_w);
  187.                            }
  188.                       }
  189.  
  190.     scanf("%d",&i);
  191.     return 0;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement