Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma hdrstop
- #pragma argsused
- #ifdef _WIN32
- #include <tchar.h>
- #else
- typedef char _TCHAR;
- #define _tmain main
- #endif
- #include <stdio.h>
- #include <math.h>
- int i;
- const long double version = 0.000000000000003;
- const double learning_rate = 0.1;
- double bias = 0.025;
- 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;
- double node_a_input, node_b_input, node_c_input;
- double node_a_weight_to_b, node_b_weight, node_c_weight;
- double node_a_weight_to_w;
- 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;
- 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;
- 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;
- 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;
- double node_c_cost_function_gradient_layer3, node_c_cost_function_gradient_layer2,node_c_cost_function_gradient_layer1 ;
- double node_a_part_to_b, node_a_part_to_w, layer3_C_node_c;
- // "node b"
- //NETWORK SIMPLE "node a" input->O-----------O------------\
- // x O "node c" --> output
- //NETWORK SIMPLE "node u" input->O-----------O------------/
- // "node w"
- // x:the output of node a also goes to node w, the output of node u also goes to node b.
- // 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.
- // 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.
- double another_node_function (double x)
- {
- return exp(x)-exp(-x)/(exp(x)+exp(-x));
- }
- double another_node_function_derivative (double x)
- {
- return 4/pow((exp(x)+exp(-x)),2);
- }
- double node_x_function(double x)
- {
- return 1 / (1 + exp(-(x)));
- }
- double node_x_function_derivative(double x)
- {
- return x * (1 - x);
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- node_a_input = 0.02;
- node_u_input = 0.09;
- node_a_weight_to_b = 0.32;
- node_a_weight_to_w = 0.70;
- 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;
- node_c_desired_output = 0.005;
- for (i=1; i < 24730000; i++) {
- //node_a_input is constantly the same, example.
- node_a_part_to_b = node_a_input*node_a_weight_to_b + bias;
- node_a_part_to_w = node_a_input*node_a_weight_to_w + bias;
- node_a_output_to_b = node_x_function(node_a_part_to_b);
- node_a_output_to_w = node_x_function(node_a_part_to_w);
- //node_u_input is constantly the same, example.
- node_u_part_to_w = node_u_input*node_u_weight_to_w + bias;
- node_u_part_to_b = node_u_input*node_u_weight_to_b + bias;
- node_u_output_to_w = node_x_function(node_u_part_to_w);
- node_u_output_to_b = node_x_function(node_u_part_to_b);
- layer1_C_node_a_to_b = 2*(node_c_desired_output - node_a_part_to_b);
- layer1_C_node_a_to_w = 2*(node_c_desired_output - node_a_part_to_w);
- layer1_C_node_u_to_b = 2*(node_c_desired_output - node_u_part_to_b);
- layer1_C_node_u_to_w = 2*(node_c_desired_output - node_u_part_to_w);
- 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;
- node_b_input = node_a_output_to_b+ node_u_output_to_b;
- node_b_part_to_c = node_b_input*node_b_weight + bias;
- node_b_output_to_c = node_x_function(node_b_part_to_c);
- node_w_input = node_a_output_to_w+node_u_output_to_w;
- node_w_part_to_c = node_w_input*node_w_weight + bias;
- node_w_output_to_c = another_node_function(node_w_part_to_c); //use another function for w-node
- layer2_C_node_w = 2*(node_c_desired_output - node_w_part_to_c);//pow(node_c_desired_output - node_w_part,2);
- layer2_C_node_b = 2*(node_c_desired_output - node_b_part_to_c);//pow(node_c_desired_output - node_b_part,2);
- layer2_C = layer2_C_node_w + layer2_C_node_b;
- node_c_input = node_b_output_to_c+node_w_output_to_c;
- node_c_part = node_c_input*node_c_weight + bias;
- node_c_output = node_x_function(node_c_part);
- layer3_C_node_c = 2*(node_c_desired_output - node_c_part);
- layer3_C = layer3_C_node_c; // +layer3_C_node_x +layer3_C_node_y etc..
- //needs forward propagation in here, tbd.
- //
- //multi layer 'network' , backward propagation test phase
- node_c_cost_function = pow(node_c_desired_output - node_c_output,2);
- node_c_cost_function_derivative = 2*(node_c_desired_output - node_c_output);
- node_c_cost_function_gradient_layer3 = node_x_function_derivative(node_c_output) * node_c_cost_function_derivative;
- node_c_cost_function_gradient_layer2 = node_x_function_derivative(node_c_output) * 2*(node_c_desired_output-layer2_C);
- node_c_cost_function_gradient_layer1 = node_x_function_derivative(node_c_output) * 2*(node_c_desired_output-layer1_C) ;
- node_b_cost_function_gradient = node_x_function_derivative(node_b_output_to_c) *node_c_weight * node_c_cost_function_gradient_layer2;
- node_w_cost_function_gradient = another_node_function_derivative(node_w_input) *node_c_weight * node_c_cost_function_gradient_layer2;
- //there are two possible pathways starting from node a a->b->c and a->w->c
- 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;
- 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;
- //there are two possible pathways starting from node u u->w->c and u->b->c
- 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;
- 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;
- node_c_weight = node_c_weight + learning_rate * node_c_cost_function_gradient_layer3;
- node_b_weight = node_b_weight + learning_rate * node_b_cost_function_gradient ;
- node_w_weight = node_w_weight + learning_rate * node_w_cost_function_gradient ;
- node_a_weight_to_b = node_a_weight_to_b + learning_rate * node_a_cost_function_gradient_to_b ;
- node_a_weight_to_w = node_a_weight_to_w + learning_rate * node_a_cost_function_gradient_to_w ;
- node_u_weight_to_b = node_u_weight_to_b + learning_rate * node_u_cost_function_gradient_to_b;
- node_u_weight_to_w = node_u_weight_to_w + learning_rate * node_u_cost_function_gradient_to_w;
- if (i>600000) {
- //printf("\nout: %.15lf %.15lf %.15lf %.15lf %.15lf", node_a_input, node_b_input, node_a_output, node_b_output, node_c_desired_output);
- //printf("\nout: %.15lf %.15lf", node_c_cost_function, node_c_cost_function_derivative);
- 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);
- }
- }
- scanf("%d",&i);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement