-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFinalNN.cpp
355 lines (262 loc) · 8.11 KB
/
FinalNN.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**********************************************************************
* NEURAL NETWORK WITH 8 INPUT ,8 HIDDEN AND 2 OUTPUT NODES
* Made for LINE FOLLOWER robots.
* Designed by :
______
/ ____/___ ___ ___________ __ __
/ / __/ __ `/ / / / ___/ __ `/ | / /
/ /_/ / /_/ / /_/ / / / /_/ /| |/ /
\____/\__,_/\__,_/_/ \__,_/ |___/
__ __ __ _
/ //_// /_ ____ _(_)________ ____ ______
/ ,< / __ \/ __ `/ / ___/ __ \/ __ `/ ___/
/ /| |/ / / / /_/ / / / / / / / /_/ / /
/_/ |_/_/ /_/\__,_/_/_/ /_/ /_/\__,_/_/
****************************************************************
// 4 th July 2019
@ Website : gaurav1620.rf.gd
@ Github : gaurav1620
@ Insta : may_be_gaurav
@ Gmail : gauravak007@gmail.com
*/
#include<iostream>
#include<vector>
#include <Eigen/Dense>
#include <math.h>
using namespace std;
using namespace Eigen;
//************************************************************************
//Change this to give more number of training cases than 2;
int trainCases = 11;
//And this too
Matrix<Matrix<float,8,1>,11,1> matInput;
Matrix<Matrix<float,2,1>,11,1> matExpectedOutput;
//************************************************************************
Matrix<float,8,1> inputs;
Matrix<float,8,1> Ain;
Matrix<float,8,1> Aout;
Matrix<float,8,8> weightsA;
Matrix<float,8,1> biasA;
Matrix<float,2,1> Bin;
Matrix<float,2,1> Bout;
Matrix<float,2,8> weightsB;
Matrix<float,2,1> biasB;
Matrix<float,2,1> expectedOutputs;
float lr = 0;
float sigmoid(float x){
float denominator = (1 + exp(-x));
return 1/denominator;
}
/***************************************************************************************************/
void init(){
//TAKING INPUT VALUES
cout<<"\nEnter the 8 input values for each Training case : \n";
for(int i = 0;i < trainCases;i++){
Matrix<float,8,1>tmp;
for(int j = 0;j < 8;j++){
cout<<"\nEnter the value for Training Case : "<<i+1<<" and Input number : "<<j+1<<" : ";
float var;
cin>>var;
tmp(j,0) = var;
}
matInput(i,0) = tmp;
//***************
Matrix<float,2,1> tmp2;
for(int j = 0;j < 2;j++){
cout<<"\nEnter the desired output for Case : "<<i+1<<" and index : "<<j+1<<" : ";
cin>>tmp2(j,0);
}
matExpectedOutput(i,0) = tmp2;
//**********************
}
//****************
// //TAKING DESIRED VALUES
// cout<<"\nEnter the 2 Desired values for Each test case: \n";
// for(int i = 0;i < trainCases;i++){
// Matrix<float,2,1> tmp;
// for(int j = 0;j < 2;j++){
// cout<<"\nEnter the desired output for Case : "<<i+1<<" and index : "<<j+1<<" : ";
// cin>>tmp(j,0);
// }
// matExpectedOutput(i,0) = tmp;
// }
// //cin>>expectedOutputs(0,0)>>expectedOutputs(1,0);
//******************
//GET THE LEARNING RATE
cout<<"\nEnter the learning rate : \n";
cin>>lr;
// GIVING RANDOM WTS FOR LAYER 1
srand ( time(NULL) );
for(int i = 0;i < 8 ;i++){
for(int j = 0;j < 8;j++){
//Gives a random value between -1 and 1
float den = rand()%5 + 1;
float num = rand()%3 -1;
//Inportant step
weightsA(i,j) = (num/den);
}
}
//GIVING RANDOM WTS FOR LAYER 2
srand ( time(NULL) );
for(int i = 0;i < 2 ;i++){
for(int j = 0;j < 8;j++){
//Gives a random value between -1 and 1
float den = rand()%5 + 1;
float num = rand()%3 -1;
//Inportant step
weightsB(i,j) = (num/den);
}
}
//ASSIGNING RANDOM BIASES FOR LAYER 1 (Bias here are taken between 0 and 1)
srand ( time(NULL) );
for(int i = 0;i < 8 ;i++){
float den = rand()%5 + 1;
float num = 1;
biasA(i,0) = num/den;
}
//ASSIGNING RANDOM BIASES FOR LAYER 2 (Bias here are taken between 0 and 1)
srand ( time(NULL) );
for(int i = 0;i < 2 ;i++){
float den = rand()%5 + 1;
float num = 1;
biasB(i,0) = num/den;
}
}
/***************************************************************************************************/
void feedForward(){
//OUTPUT 1 IS THE WEIGHTED SUM OF THE INPUTS
Ain = (weightsA*inputs)+biasA;
//INPUTS 2 IS THE OUTPUT OF LAYER 1 AFTER PASSING THROUGH SIGMOID
for(int i = 0;i < 8;i++){
Aout(i,0) = sigmoid(Ain(i,0));
}
//OUTPUT 2 THE WEIGHTED SUM FOR LAYER 2
Bin =( weightsB*Aout )+ biasB;
//FINAL OUTPUT IS THE FINAL OUTPUT (LOL !)
for(int i = 0;i < 2;i++){
Bout(i,0) = sigmoid(Bin(i,0));
}
//cout<<"\nExpected ouptputs : \n"<<expectedOutputs(0,0)<< " and "<<expectedOutputs(1,0)<<endl;
//cout<<"\nFinal outputs : "<<Bout(0,0)<<" and "<<Bout(1,0)<<endl;
}
/***************************************************************************************************/
void backProp(){
//WE JUST NEED TO ADJUST AND TUNE THE WT'S AND BIASES IN THE BACK PROP
//SUM OF TOTAL SQUARED ERROR
float Cost = 0;
for(int i = 0;i < 2; i++){
Cost += (Bout(i,0)-expectedOutputs(i,0))*(Bout(i,0)-expectedOutputs(i,0));
}
Matrix<float,8,8> deltaA; // with learning rate
Matrix<float,2,8> deltaB; // with learning rate
//BackProp for LAYER B
for(int i = 0;i < 2;i++){
for(int j = 0;j < 8;j++){
float delta = 0;
//CHECK THIS CHECK THIS CHECK THIS CHECK THIS CHECK THIS
float a = 2*(Bout(i,0) - expectedOutputs(i,0)); // dC / dBout
float b = Bout(i,0)*(1-Bout(i,0)); // dBout / dBin
float c = Aout(j,0); // dBin / dW(i)
delta = a*b*c;
deltaB(i,j) = lr *delta;
//CHECK THIS CHECK THIS CHECK THIS CHECK THIS CHECK THIS
}
}
//BackProp for LAYER A
for(int i = 0;i < 8;i++){
for(int j = 0;j < 8;j++){
float delta = 0;
//CHECK THIS CHECK THIS CHECK THIS CHECK THIS CHECK THIS
float c = inputs(j,0); // dAin / dW
float b = Aout(i,0)*(1 - Aout(i,0)); // dAout / dAin
float a; //dC / dAout
float a0;
float a1;
float a00 = 2*(Bout(0,0) - expectedOutputs(0,0)); //dC0/dBout
float a01 = Bout(0,0)*(1 - Bout(0,0)); //dBout/dBin
float a02 = weightsB(0,i);
a0 = a00*a01*a02;
float a10 = 2*(Bout(1,0) - expectedOutputs(1,0)); //dC0/dBout
float a11 = Bout(1,0)*(1 - Bout(1,0)); //dBout/dBin
float a12 = weightsB(1,i);
a1 = a10*a11*a12;
a = a0+a1;
delta = a*b*c;
deltaA(i,j) = lr * delta;
//CHECK THIS CHECK THIS CHECK THIS CHECK THIS CHECK THIS
}
}
//UPDATING WEIGHTS FOR LAYER B
for(int i = 0;i < 2;i++){
for(int j = 0;j < 8;j++){
weightsB(i,j) -= deltaB(i,j);
}
}
//UPDATING WEIGHTS FOR LAYER A
for(int i = 0;i < 8;i++){
for(int j = 0;j < 8;j++){
weightsA(i,j) -= deltaA(i,j);
}
}
}
/***************************************************************************************************/
void debugPrint(){
// DEBUG
cout<<"\ninputs : \n"<<inputs<<endl;
cout<<"\nweightsA : \n"<<weightsA<<endl;
cout<<"\nbiasA : \n"<<biasA<<endl;
cout<<"\nAin : \n"<<Ain<<endl;
cout<<"\nAout : \n"<<Aout<<endl;
cout<<"\nweightsB : \n"<<weightsB<<endl;
cout<<"\nbiasB : \n"<<biasB<<endl;
cout<<"\nBin : \n"<<Bin<<endl;
cout<<"\nBout : \n"<<Bout<<endl;
cout<<"\n\nExpected outputs : "<<expectedOutputs<<endl;
}
/***************************************************************************************************/
void train(int epoch){
//*********DEBUG************
//for(int j = 0;j < trainCases;j++){
for(int i = 0;i < epoch;i++){
int j = i%trainCases;
inputs = matInput(j,0);
expectedOutputs = matExpectedOutput(j,0);
feedForward();
backProp();
feedForward();
// cout<<"\n\n";
// debugPrint();
// cout<<"\n\n";
}
//}
}
/***************************************************************************************************/
void CheckTraining(){
cout<<"Enter the 8 inputs : \n";
Matrix<float,8,1> tst;
for(int i = 0;i < 8;i++){
cin>>tst(i,0);
}
inputs = tst;
feedForward();
cout<<Bout(0,0)<<" and "<<Bout(1,0)<<endl;
}
/***************************************************************************************************/
int main(void){
//INITIALISATION
init();
feedForward();
//debugPrint();
train(100000);
// inputs = matInput(0,0);
// expectedOutputs = matExpectedOutput(0,0);
// feedForward();
// inputs = matInput(1,0);
// expectedOutputs = matExpectedOutput(1,0);
// feedForward();
int i = 0;
while(i = 1){
CheckTraining();
debugPrint();
}
}