Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- double fx(double x) //Sigmoid function
- {
- return 1.00 / (1.00 + exp(-(x)));
- }
- double fd(double x) //Sigmoid derivative
- {
- return x * (1.00 - x);
- }
- int _tmain(int argc, char* argv[])
- {
- double f_out1 = 0,f_out2 = 0, f_out1a = 0, f_out1_to_node3=0, f_out1a_to_node3,f_out_node3; //output
- double weight=0.10,weight1a=0.61, weight2=0.33, weight3=0.88;
- double weight_to_node3 = 0.64, weight1a_to_node3 = 0.23; //i still need to make array of node weight values
- double learn=0.01;//learning-rate
- double desired_output=0.12;// desired output for node 2
- double desired_output_node3=0.19;//desired output for node 3
- double input=0.50, input1a=0.70;
- double requested_data1=0.20; //requested data, looking at node 2 for the output
- double requested_data2=0.78; //requested data, looking at node 3 for the output
- double err_part1, err_part2, err_TOTAL; //error value calculation, the error function 2(out-desired) is the derivative of the cost function C = (out-desired)^2
- long double i; // iteration
- long double till = 37150; //iteration max
- for (i = 0; i <= till; i++) {
- f_out1 = fx(input * weight); //node 1_output to node 2
- f_out1_to_node3 = fx(input * weight_to_node3); //node 1_output to node 3
- //node 1a_output to node 2 and node 3
- f_out1a = fx(input1a * weight1a); //node 1a_output to node 2
- f_out1a_to_node3 = fx(input1a * weight1a_to_node3); //node 1a_output to node 3
- //TWO OUTPUTS
- //node 2_output
- f_out2 = fx(f_out1 * weight2 + f_out1a * weight2);
- //node 3_output
- f_out_node3 = fx(f_out1_to_node3 * weight3 + f_out1a_to_node3 * weight3);
- //error calculation
- err_part1 = 2*(f_out2 - desired_output); //node 2 error
- err_part2 = 2*( f_out_node3 - desired_output_node3); //node 3 error
- err_TOTAL = (err_part1 + err_part2)/2;
- // weight - l.rate * error * derivative_output * input * [all possible weights it still needs to pass through before the output, plus the output weight itself:which in this case only weight 2]
- weight = weight - learn * err_part1 *fd(f_out1) * input * weight2;
- // weight - l.rate * error * derivative_output * input * [all possible weights it still needs to pass through before the output, plus the output weight itself:which in this case only weight 3]
- weight_to_node3 = weight_to_node3 - learn * err_part2 *fd(f_out1_to_node3) * input * weight3;
- // weight - l.rate * error * derivative_output * input * [all possible weights it still needs to pass through before the output, plus the output weight itself:which in this case only weight 2]
- weight1a = weight1a - learn * err_part1 *fd(f_out1a) * input1a * weight2;
- // weight - l.rate * error * derivative_output * input * [all possible weights it still needs to pass through before the output, plus the output weight itself:which in this case only weight 3]
- weight1a_to_node3 = weight1a_to_node3 - learn * err_part2 *fd(f_out1a_to_node3) * input1a * weight3;
- // weight - l.rate * error * derivative_output * input
- weight2 = weight2 - learn * err_part1 *fd(f_out2) * (f_out1+f_out1a);
- // weight - l.rate * error * derivative_output * input
- weight3 = weight3 - learn * err_part2 *fd(f_out_node3) * (f_out1_to_node3+f_out1a_to_node3);
- // (i>(till-10)) {
- /* printf("\n%Lf. desired output is %.2lf",i, desired_output);
- printf("\nweight %.9lf error %.9lf", weight, err_TOTAL); */
- printf("\n----------------------------------------------------------------");
- printf("\nweights: weight %lf weight_to_node3 %lf weight1a %lf weight1a_to_node3 %lf weight2 %lf weight3 %lf",weight,weight_to_node3,weight1a,weight1a_to_node3, weight2, weight3);
- printf("\n\nRequested new data, Output from node2 (when input = %lf) : %.9lf",requested_data1, fx((fx(requested_data1*weight)+fx(input1a*weight1a))*weight2));
- printf("\nRequested new data, Output from node2 (when input1a = %lf) : be %.9lf\n",requested_data1, fx((fx(input*weight)+fx(requested_data1*weight1a))*weight2));
- printf("\nRequested new data, Output from node3 (when input = %lf) : %.9lf",requested_data2, fx((fx(requested_data2*weight_to_node3)+fx(input1a*weight1a_to_node3))*weight3));
- printf("\nRequested new data, Output from node3 (when input1a = %lf) : %.9lf\n\n\n\n",requested_data2, fx((fx(input*weight_to_node3)+fx(requested_data2*weight1a_to_node3))*weight3));
- printf("Step %.0Lf : multiple output nodes (2)",i);
- printf("\nOutput for the current input+input1a by node 2(%.2lf + %.2lf) is %.9lf",input,input1a, f_out2);
- printf("\nOutput for the current input+input1a by node 3(%.2lf + %.2lf) is %.9lf\n\n\n\n",input,input1a, f_out_node3);
- //Sleep(1000);
- //}
- }
- printf("\n\n Findings seem to be correct, fill in the original input value or input1a values (0.50 or 0.70) into requested data and it outputs the same, assuming the input to the other node stays the same.");
- //all numbers need to be between -1 and 1. in case you have other inputs like 400, invert them (1/x) and convert them back at the end.
- scanf("%lf", &input);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment