 # simple-backp-test

Sep 19th, 2020 (edited)
1,165
327 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2. #include <math.h>
3.
4. //        node 1         weight
5. // input--> O ---------------------- \
6. //                                     \ Node 2      weight2
7. //                                       O -------------------------> output
8. //          node 1a      weight1a      /
9. // input1a--> O -------------------- /
10. //
11. // all nodes use sigmoid function
12.
13.
14. double fx(double x) //Sigmoid function
15. {
16.     return 1 / (1 + exp(-(x)));
17. }
18.
19.
20. double fd(double x) //Sigmoid derivative
21. {
22.    return x * (1 - x);
23. }
24.
25.
26. int _tmain(int argc, _TCHAR* argv[])
27. {
28.
29. double f_out1 = 0,f_out2 = 0, f_out1a = 0;  //output
30. double input=0.50, input1a=0.70;
31. double weight=0.10,weight1a=0.61, weight2=0.33, weight2a=0.69;
32. double learn=0.1; //learning-rate
33. double desired_output=0.12;
34. double requested_data=0.65;
35. double err;  //error value calculation, the error function 2(out-desired) is the derivative of the cost function C = (out-desired)^2
36. int i; // iteration
37.
38. for (i = 0; i <= 30000; i++) {
39.
40.                             // three nodes, sigmoid backprop.
41.                             //node 1
42.                             f_out1 = fx(input * weight);
43.
44.                             //node 1a
45.                             f_out1a = fx(input1a * weight1a);
46.
47.                             //node 2
48.                             f_out2 = fx(f_out1 * weight2 + f_out1a * weight2);
49.
50.                             //error calculation
51.                             err = 2*( f_out2 - desired_output );
52.
53.
54.                             weight = weight - learn * err *fd(f_out1) * input * weight2;
55.                             weight1a = weight1a - learn * err *fd(f_out1a) * input1a * weight2;
56.                             weight2 = weight2 - learn * err *fd(f_out2) * (f_out1+f_out1a);  //f_out1+f_out1a is the input for this one
57.
58.
59.
60.                             printf("\n%d. desired output is %.2lf",i, desired_output);
61.                             printf("\nweight %.9lf  error %.9lf", weight, err);
62.                             printf("\nOutput for the current input+input1a (%.2lf + %.2lf) is %.9lf",input,input1a, f_out2);
63.                             printf("\n\nOutput for value %lf as new requested data for input would be %.9lf",requested_data, fx((fx(requested_data*weight)+fx(input1a*weight1a))*weight2));
64.                             printf("\nOutput for value %lf as new requested data for input1a would be %.9lf\n",requested_data, fx((fx(input*weight)+fx(requested_data*weight1a))*weight2));
65.
66.                             printf("\n\nweight1a %lf weight2a %lf", weight1a, weight2a);
67.
68.                           }
69.
70.                           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.");
71.
72.     scanf("%lf", &input);
73.
74.     return 0;
75. }
76.
RAW Paste Data