 # multiple

Sep 20th, 2020
628
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2. #include <math.h>
3.
4.
5.
6.
7.
8. double fx(double x) //Sigmoid function
9. {
10.      return 1.00 / (1.00 + exp(-(x)));
11. }
12.
13.
14. double fd(double x) //Sigmoid derivative
15. {
16.     return x * (1.00 - x);
17. }
18.
19.
20. int _tmain(int argc, char* argv[])
21. {
22.
23. double f_out1 = 0,f_out2 = 0, f_out1a = 0, f_out1_to_node3=0, f_out1a_to_node3,f_out_node3;  //output
24.
25. double weight=0.10,weight1a=0.61, weight2=0.33, weight3=0.88;
26. double weight_to_node3 = 0.64, weight1a_to_node3 = 0.23; //i still need to make array of node weight values
27. double learn=0.01;//learning-rate
28. double desired_output=0.12;// desired output for node 2
29. double desired_output_node3=0.19;//desired output for node 3
30.
31. double input=0.50, input1a=0.70;
32.
33. double requested_data1=0.20;  //requested data, looking at node 2 for the output
34. double requested_data2=0.78;  //requested data, looking at node 3 for the output
35.
36. 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
37. long double i; // iteration
38. long double till = 37150; //iteration max
39.
40.
41. for (i = 0; i <= till; i++) {
42.
43.
44.                             f_out1 = fx(input * weight); //node 1_output to node 2
45.                             f_out1_to_node3 = fx(input * weight_to_node3);  //node 1_output to node 3
46.
47.
48.                             //node 1a_output to node 2 and node 3
49.                             f_out1a = fx(input1a * weight1a); //node 1a_output to node 2
50.                             f_out1a_to_node3 = fx(input1a * weight1a_to_node3); //node 1a_output to node 3
51.
52.
53.                             //TWO OUTPUTS
54.                             //node 2_output
55.                             f_out2 = fx(f_out1 * weight2 + f_out1a * weight2);
56.
57.                             //node 3_output
58.                             f_out_node3 = fx(f_out1_to_node3 * weight3 + f_out1a_to_node3 * weight3);
59.
60.
61.
62.                             //error calculation
63.                             err_part1 =  2*(f_out2 - desired_output); //node 2 error
64.                             err_part2 = 2*( f_out_node3 - desired_output_node3); //node 3 error
65.                             err_TOTAL = (err_part1 + err_part2)/2;
66.
67.
68.
69.                             //       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]
70.                             weight = weight - learn * err_part1 *fd(f_out1) * input * weight2;
71.                             //       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]
72.                             weight_to_node3 = weight_to_node3 - learn * err_part2 *fd(f_out1_to_node3) * input * weight3;
73.
74.
75.                              //       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]
76.                             weight1a = weight1a - learn * err_part1 *fd(f_out1a) * input1a * weight2;
77.                              //       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]
78.                             weight1a_to_node3 = weight1a_to_node3 - learn * err_part2 *fd(f_out1a_to_node3) * input1a * weight3;
79.
80.                             //       weight - l.rate * error * derivative_output * input
81.                             weight2 = weight2 - learn * err_part1 *fd(f_out2) * (f_out1+f_out1a);
82.                             //       weight - l.rate * error * derivative_output * input
83.                             weight3 = weight3 - learn * err_part2 *fd(f_out_node3) * (f_out1_to_node3+f_out1a_to_node3);
84.
85.
86.                              // (i>(till-10)) {
87.
88.
89.                                        /*   printf("\n%Lf. desired output is %.2lf",i, desired_output);
90.                                         printf("\nweight %.9lf  error %.9lf", weight, err_TOTAL); */
91.                                         printf("\n----------------------------------------------------------------");
92.                                         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);
93.
94.                                         printf("\n\nRequested new data, Output from node2 (when input = %lf) : %.9lf",requested_data1, fx((fx(requested_data1*weight)+fx(input1a*weight1a))*weight2));
95.                                         printf("\nRequested new data, Output from node2 (when input1a = %lf) : be %.9lf\n",requested_data1, fx((fx(input*weight)+fx(requested_data1*weight1a))*weight2));
96.
97.
98.                                         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));
99.                                         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));
100.
101.                                         printf("Step %.0Lf : multiple output nodes (2)",i);
102.                                         printf("\nOutput for the current input+input1a by node 2(%.2lf + %.2lf) is %.9lf",input,input1a, f_out2);
103.                                         printf("\nOutput for the current input+input1a by node 3(%.2lf + %.2lf) is %.9lf\n\n\n\n",input,input1a, f_out_node3);
104.                                         //Sleep(1000);
105.
106.                                     //}
107.                             }
108.
109.                           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.");
110.                           //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.
111.
112.     scanf("%lf", &input);
113.
114.     return 0;
115. }
116.
RAW Paste Data