 # Untitled

Sep 24th, 2020
887
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2. #include <math.h>
3.
4. const double version = 0.00001;
5. const int t_rows=20;
6. double training_set_inputs[t_rows], training_set_outputs[t_rows];
7. double teller, avgtot, avg, sqerrtot, f_out_requested;
8. double  reqf_out1,reqf_out1L,reqf_out1K,reqf_out1aL,reqf_out1aK,reqf_out2L,reqf_out2K,reqf_out1M,reqf_out1aM,reqf_out2M, reqf_out1a, reqf_out2, reqf_out3;
9.
10.
11.
12. double outt2(double (*array)[t_rows], int b)
13. {
14.     double calc;
15.
16.        calc= pow((*array)[b],2)+11*(*array)[b];
17.
18.
19.     return calc;
20. }
21.
22.
23.
24.
25. double fx(double x) //Tanh function
26. {
27.     return exp(x)-exp(-x)/(exp(x)+exp(-x));
28.
29. }
30.
31.
32. double fd(double x) //Tanh derivative
33. {
34.     return 4/pow((exp(x)+exp(-x)),2);
35.
36. }
37.
38.
39. int _tmain(int argc, _TCHAR* argv[])
40. {
41.   srand (time(NULL));
42.
43.
44.
45.
46.       for (int create = 0; create < t_rows; create++) {
47.
48.                 bool redo=false;
49.
50.                 do
51.                 {
52.                 training_set_inputs[create]= (rand() % 60) + 20.00;
53.                 training_set_inputs[create]= (rand() % 60) + 20.00;
54.
55.                 training_set_outputs[create] = outt2(&training_set_inputs, create);
56.
57.                    if (training_set_outputs[create]==0) redo=true;
58.
59.
60.                 }  while (redo==true);
61.
62.
63.
64.
65.             }
66.
67.
68.
69.
70.
71.
72. for(auto& rows: training_set_inputs)
73.     {
74.         for(auto& elem: rows)
75.         {
76.         //printf("\n %lf",elem);
77.         elem =  1.00/elem;
78.
79.         }
80.     }
81.
82.
83. for (auto& number : training_set_outputs)
84.     {
85.         number =  1.00/number;
86.
87.     }
88.
89.
90. double requested_data1=21;double requested_data2=31;
91.  requested_data1=1.00/requested_data1;
92.  requested_data2=1.00/requested_data2;
93.
94.
95. double weight   = 0.01+(rand()%1000)/1000.00;
96. double weightK  = 0.01+(rand()%1000)/1000.00;
97. double weightL  = 0.01+(rand()%1000)/1000.00;
98. double weightM  = 0.01+(rand()%1000)/1000.00;
99. double weight1a = 0.01+(rand()%1000)/1000.00;
100. double weight1aK= 0.01+(rand()%1000)/1000.00;
101. double weight1aL= 0.01+(rand()%1000)/1000.00;
102. double weight1aM= 0.01+(rand()%1000)/1000.00;
103. double weight2  = 0.01+(rand()%1000)/1000.00;
104. double weight2K = 0.01+(rand()%1000)/1000.00;
105. double weight2L = 0.01+(rand()%1000)/1000.00;
106. double weight2M = 0.01+(rand()%1000)/1000.00;
107. double weight3  = 0.01+(rand()%1000)/1000.00;
108.
109.
110.
111. double f_out1K, f_out1aK,f_out2K, f_out3, f_out1L, f_out1aL, f_out2L, f_out1M, f_out1aM, f_out2M;
112.
113. double f_out1 = 0,f_out2 = 0, f_out1a = 0;
114. double learn=0.001; //learning-rate
115. double err;
116. int i=0;
117.
118. teller=0;
119.
120.                 while (1)
121.                             {
122.                             i++;
123.
124.                             //int q = rand()%t_rows;   //this does not work so well anymore with multinode
125.                             for (int q=0; q < t_rows; q++)
126.
127.                              {
128.
129.
130.                             // Forward propagation, training
131.                             //node 1, input node
132.                                     //connection input node 1 with node 2
133.                                     f_out1 = fx(training_set_inputs[q] * weight);
134.                                     //connection input node 1 with node 2K
135.                                     f_out1K = fx(training_set_inputs[q] * weightK);
136.                                     //connection input node 1 with node 2L
137.                                     f_out1L = fx(training_set_inputs[q] * weightL);
138.                                     //connection input node 1 with node 2L
139.                                     f_out1M = fx(training_set_inputs[q] * weightM);
140.
141.                             //node 1a, input node
142.                                     //connection input node 1a with node 2
143.                                     f_out1a= fx(training_set_inputs[q] * weight1a);
144.                                     //connection input node 1a with node 2K
145.                                     f_out1aK= fx(training_set_inputs[q] * weight1aK);
146.                                     //connection input node 1a with node 2L
147.                                     f_out1aL= fx(training_set_inputs[q] * weight1aL);
148.                                     //connection input node 1a with node 2M
149.                                     f_out1aM= fx(training_set_inputs[q] * weight1aM);
150.
151.                             //middle nodes
152.                                     //node 2, middle node, connection with output node 3
153.                                     f_out2 = fx(f_out1 * weight2 + f_out1a * weight2);
154.                                     //node 2K, middle node, connection with output node 3
155.                                     f_out2K = fx(f_out1K * weight2K + f_out1aK * weight2K);
156.                                     //node 2L, middle node, connection with output node 3
157.                                     f_out2L = fx(f_out1L * weight2L + f_out1aL * weight2L);
158.                                     //node 2M, middle node, connection with output node 3
159.                                     f_out2M = fx(f_out1M * weight2M + f_out1aM * weight2M);
160.
161.                                     //output node 3
162.                                     f_out3 = fx(f_out2 * weight3 + f_out2K * weight3+ f_out2L * weight3 + f_out2M * weight3);
163.
164.                                     //printf("\nWEIGHTS %lf %lf %lf", weightK, weight1aK, weight2K);
165.
166.
167.
168.                             //error calculation
169.                             err = 2*( f_out3 - training_set_outputs[q] );
170.                             sqerrtot = sqerrtot + pow( f_out3 - training_set_outputs[q],2);
171.
172.
173.                             //backward prop
174.                             weight   = weight   - learn * err * training_set_inputs[q] * fd(f_out1)  * weight2 * weight3     ;
175.                             weightK  = weightK  - learn * err * training_set_inputs[q] * fd(f_out1K) * weight2K * weight3    ;
176.                             weightL  = weightL  - learn * err * training_set_inputs[q] * fd(f_out1L) * weight2L * weight3    ;
177.                             weightM  = weightM  - learn * err * training_set_inputs[q] * fd(f_out1M) * weight2M * weight3    ;
178.
179.                             weight1a = weight1a - learn * err * training_set_inputs[q] * fd(f_out1a) * weight2 * weight3     ;
180.                             weight1aK= weight1aK -learn * err * training_set_inputs[q] * fd(f_out1aK)* weight2K * weight3    ;
181.                             weight1aL= weight1aL -learn * err * training_set_inputs[q] * fd(f_out1aL)* weight2L * weight3   ;
182.                             weight1aM= weight1aM -learn * err * training_set_inputs[q] * fd(f_out1aM)* weight2M * weight3   ;
183.
184.                             weight2  = weight2  - learn * err * (f_out1+f_out1a)          * fd(f_out2)  * weight3               ;
185.                             weight2K = weight2K - learn * err * (f_out1K+f_out1aK)        * fd(f_out2K) * weight3               ;
186.                             weight2L = weight2L - learn * err * (f_out1L+f_out1aL)        * fd(f_out2L) * weight3               ;
187.                             weight2M = weight2M - learn * err * (f_out1M+f_out1aM)        * fd(f_out2M) * weight3;
188.
189.                             weight3  = weight3  - learn * err * (f_out2+f_out2K+f_out2L+f_out2M) * fd(f_out3);
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
208.                             } //q
209.
210.
211.                             if (i>20) {
212.
213.
214.
215.
216.
217.
218.
219.                             //Forward pass on requested data
220.
221.                             reqf_out1 = fx(requested_data1 * weight);
222.                             reqf_out1K = fx(requested_data1* weightK);
223.                             reqf_out1L = fx(requested_data1* weightL);
224.                             reqf_out1M = fx(requested_data1* weightM);
225.
226.                             //node 1a
227.                             reqf_out1a= fx(requested_data2 * weight1a);
228.                             reqf_out1aK= fx(requested_data2 * weight1aK);
229.                             reqf_out1aL= fx(requested_data2 * weight1aL);
230.                             reqf_out1aM= fx(requested_data2 * weight1aM);
231.
232.                             //node 2
233.                             reqf_out2 = fx(reqf_out1 * weight2 + reqf_out1a * weight2);
234.                             reqf_out2K = fx(reqf_out1K * weight2K + reqf_out1aK * weight2K);
235.                             reqf_out2L = fx(reqf_out1L * weight2L + reqf_out1aL * weight2L);
236.                             reqf_out2M = fx(reqf_out1M * weight2M + reqf_out1aM * weight2M);
237.
238.                             //node 3
239.                             reqf_out3 = fx(reqf_out2 * weight3 + reqf_out2K * weight3  + reqf_out2L * weight3 + reqf_out2M * weight3);
240.
241.
242.
243.
244.
245.                                             f_out_requested = 1.00/reqf_out3;
246.
247.                                             teller = teller +1.00;
248.                                             avgtot = avgtot + abs(f_out_requested);
249.                                             avg=avgtot/teller;
250.                                             printf("\n\nError: %.19lf Output for value %lf as new requested1 data req2 %lf ,output would be %.20lf",sqerrtot,1.00/requested_data1,1.00/requested_data2,f_out_requested );
251.                                             sqerrtot = 0;
252.                                           }
253.
254.
255.
256.
257.
258.
259.                           }
260.
261.
262.
263.     scanf("%d", &i);
264.
265.     return 0;
266. }
RAW Paste Data