-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_NNv1.0.ino
345 lines (302 loc) · 12.8 KB
/
arduino_NNv1.0.ino
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
/*Y = R+B+G
*Author: Prashant Dandriyal
*Date: 14 Dec, 2019
*-Added BUILTIN_LED glow feature for post-training
*-Corrected activation of output layer from tanh to lin
*-Last: 0.05 @250 epochs,6 Hidden Neurons
*
*-Edit (24-01-2020)
*-Added Elu+Linear activation and corrected some parts of normalization. Got high accuracy.
*/
#define PI 3.141592653589793238463
#define N
#define epsilon 0.05
#define alpha 1.0f
#define epoch 3000
///ELU ACTIVATION DEFINITIONS
float elu(float x) { if(x>0) return x;
else return alpha*(exp(x)-1.0f);
}
float delu(float x) { if(x>0) return 1.0f;
else return alpha*exp(x);
}
///LINEAR ACTIVATION DEFINITIONS
float lin(float x) { return x;}
float dlin(float x) { return 1.0f;}
///tanh ACTIVATION DEFINITIONS
float Tanh_(float x) { return (exp(x)-exp(-x))/(exp(x)+exp(-x)) ;}
float dTanh_(float x) {return 1.0f - x*x ;}
///WEIGHT INITIALIZER
float init_weight() { return (2.*rand()/RAND_MAX -1); }
float MAXX = -9999999999999999; //maximum value of input example
static const int numInputs = 3;
static const int numHiddenNodes = 9;
static const int numOutputs = 1;
static const int numTrainingSets = 8;
static const int numTestSets = 16;
const float lr = 0.05f;
float hiddenLayer[numHiddenNodes];
float outputLayer[numOutputs];
float hiddenLayerBias[numHiddenNodes]; ///BIASES OF HIDDEN LAYER (c)
float outputLayerBias[numOutputs]; ///BIASES OF OUTPUT LAYER (b)
float hiddenWeights[numInputs][numHiddenNodes]; ///WEIGHTS OF HIDDEN LAYER (W)
float outputWeights[numHiddenNodes][numOutputs]; ///WEIGHTS OF OUTPUT LAYER (V)
float training_inputs[numTrainingSets][numInputs] = { {255,255,255},
{255,218,185},
{245,255,250},
{230,230,250},
{0,255,0},
{0,100,0},
{46,139,87},
{127,255,0}
};
float test_inputs[numTestSets][numInputs] = { {0,0,255},
{135,206,235},
{175,238,238},
{127,255,212},
{255,0,0},
{255,69,0},
{255,127,0},
{255,165,0},
{0,0,0},
{105,105,105},
{112,112,112},
{169,169,169},
{155,48,255},
{139,35,35},
{205,51,51},
{255,246,143},
};
float training_outputs[numTrainingSets][numOutputs] ={ {765},
{658},
{750},
{710},
{255},
{100},
{272},
{382}
};
float test_outputs[numTestSets][numOutputs] = { {255},
{576},
{651},
{594},
{255},
{324},
{382},
{420},
{0},
{315},
{336},
{507},
{458},
{209},
{307},
{664}
};
void shuffle(int *array, size_t n)
{
if (n > 1) //If no. of training examples > 1
{
size_t i;
for (i = 0; i < n - 1; i++)
{
size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
int t = array[j];
array[j] = array[i];
array[i] = t;
}
}
}
void predict(float test_sample[])
{
for (int j=0; j<numHiddenNodes; j++)
{
float activation=hiddenLayerBias[j];
for (int k=0; k<numInputs; k++)
{
activation+=test_sample[k]*hiddenWeights[k][j];
}
hiddenLayer[j] = elu(activation);
}
for (int j=0; j<numOutputs; j++)
{
float activation=outputLayerBias[j];
for (int k=0; k<numHiddenNodes; k++)
{
activation+=hiddenLayer[k]*outputWeights[k][j];
}
outputLayer[j] = lin(activation);
}
//std::cout<<outputLayer[0]<<"\n";
//return outputLayer[0];
//std::cout << "Input:" << training_inputs[i][0] << " " << training_inputs[i][1] << " Output:" << outputLayer[0] << " Expected Output: " << training_outputs[i][0] << "\n";
}
void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
///TRAINING DATA GENERATION
for (int i = 0; i < numTrainingSets; i++)
{
/*
float p = (2*PI*(float)i/numTrainingSets);
training_inputs[i][0] = p;
training_outputs[i][0] = (0.2+0.4*pow(p, 2)+0.3*p*sin(15*p)+0.05*cos(50*p))/100.;
*/
///APPLYING CHROMATICITY CALCULATION
training_inputs[i][0] = training_inputs[i][0]*(-0.14282) + training_inputs[i][1]*(1.54924) + training_inputs[i][2]*(-0.95641);
training_inputs[i][1] = training_inputs[i][0]*(-0.32466) + training_inputs[i][1]*(1.57837) + training_inputs[i][2]*(-0.73191);
training_inputs[i][2] = training_inputs[i][0]*(-0.68202) + training_inputs[i][1]*(0.77073) + training_inputs[i][2]*(0.56332);
training_outputs[i][0] = training_inputs[i][0]+training_inputs[i][1]+training_inputs[i][2];
/***************************Try Avoiding Edits In This part*******************************/
///FINDING NORMALIZING FACTOR
for(int m=0; m<numInputs; ++m)
if(MAXX < training_inputs[i][m])
MAXX = training_inputs[i][m];
for(int m=0; m<numOutputs; ++m)
if(MAXX < training_outputs[i][m])
MAXX = training_outputs[i][m];
//cout<<"In: "<<training_inputs[i][0]<<" out: "<<training_outputs[i][0]<<endl;
}
///NORMALIZING
for (int i = 0; i < numTrainingSets; i++)
{
for(int m=0; m<numInputs; ++m)
training_inputs[i][m] /= 1.0f*MAXX*numInputs;
for(int m=0; m<numOutputs; ++m)
training_outputs[i][m] /= 1.0f*MAXX*numInputs;
Serial.print("In: ");
Serial.print(training_inputs[i][0], 3);
Serial.print(" out: ");
Serial.println(training_outputs[i][0], 3);
}
///WEIGHT & BIAS INITIALIZATION
for (int i=0; i<numInputs; i++) {
for (int j=0; j<numHiddenNodes; j++) {
hiddenWeights[i][j] = init_weight();
}
}
for (int i=0; i<numHiddenNodes; i++) {
hiddenLayerBias[i] = init_weight();
for (int j=0; j<numOutputs; j++) {
outputWeights[i][j] = init_weight();
}
}
for (int i=0; i<numOutputs; i++) {
//outputLayerBias[i] = init_weight();
outputLayerBias[i] = 0;
}
///FOR INDEX SHUFFLING
int trainingSetOrder[numTrainingSets];
for(int j=0; j<numTrainingSets; ++j)
trainingSetOrder[j] = j;
///TRAINING
//std::cout<<"start train\n";
///vector<float> performance, epo; ///STORE MSE, EPOCH
for (int n=0; n < epoch; n++)
{
float MSE = 0;
shuffle(trainingSetOrder,numTrainingSets);
Serial.print("\nepoch : ");Serial.print(n);
for (int x=0; x<numTrainingSets; x++)
{
int i = trainingSetOrder[x];
//std::cout<<"Training Set :"<<x<<"\n";
/// Forward pass
for (int j=0; j<numHiddenNodes; j++)
{
float activation=hiddenLayerBias[j];
//std::cout<<"Training Set :"<<x<<"\n";
for (int k=0; k<numInputs; k++) {
activation+=training_inputs[i][k]*hiddenWeights[k][j];
}
hiddenLayer[j] = elu(activation);
}
for (int j=0; j<numOutputs; j++) {
float activation=outputLayerBias[j];
for (int k=0; k<numHiddenNodes; k++)
{
activation+=hiddenLayer[k]*outputWeights[k][j];
}
outputLayer[j] = lin(activation);
}
//std::cout << "Input:" << training_inputs[x][0] << " " << " Output:" << outputLayer[0] << " Expected Output: " << training_outputs[x][0] << "\n";
for(int k=0; k<numOutputs; ++k)
MSE += (1.0f/numOutputs)*pow( training_outputs[i][k] - outputLayer[k], 2);
/// Backprop
/// For V
float deltaOutput[numOutputs];
for (int j=0; j<numOutputs; j++) {
float errorOutput = (training_outputs[i][j]-outputLayer[j]);
deltaOutput[j] = errorOutput*dlin(outputLayer[j]);
}
/// For W
float deltaHidden[numHiddenNodes];
for (int j=0; j<numHiddenNodes; j++) {
float errorHidden = 0.0f;
for(int k=0; k<numOutputs; k++) {
errorHidden+=deltaOutput[k]*outputWeights[j][k];
}
deltaHidden[j] = errorHidden*delu(hiddenLayer[j]);
}
///Updation
/// For V and b
for (int j=0; j<numOutputs; j++) {
//b
outputLayerBias[j] += deltaOutput[j]*lr;
for (int k=0; k<numHiddenNodes; k++)
{
outputWeights[k][j]+= hiddenLayer[k]*deltaOutput[j]*lr;
}
}
/// For W and c
for (int j=0; j<numHiddenNodes; j++) {
//c
hiddenLayerBias[j] += deltaHidden[j]*lr;
//W
for(int k=0; k<numInputs; k++) {
hiddenWeights[k][j]+=training_inputs[i][k]*deltaHidden[j]*lr;
}
}
}
//Averaging the MSE
MSE /= 1.0f*numTrainingSets;
Serial.print("\t Error: ");Serial.println(MSE*100,8);
}
//TRAINING COMPLETE
digitalWrite(13,HIGH);
Serial.print("Final Hidden Weights\n");
for (int j=0; j<numHiddenNodes; j++) {
Serial.print("[ ");
for(int k=0; k<numInputs; k++) {
Serial.print(hiddenWeights[k][j], 9);
}
Serial.print("] \n");
}
//Predict
//int numTestSets = numTrainingSets;
for (int i = 0; i < numTestSets; i++)///Note i
{
//float p = (2*PI*(float)i/numTestSets);
///APPLYING CHROMATICITY CALCULATION
test_inputs[i][0] = test_inputs[i][0]*(-0.14282) + test_inputs[i][1]*(1.54924) + test_inputs[i][2]*(-0.95641);
test_inputs[i][1] = test_inputs[i][0]*(-0.32466) + test_inputs[i][1]*(1.57837) + test_inputs[i][2]*(-0.73191);
test_inputs[i][2] = test_inputs[i][0]*(-0.68202) + test_inputs[i][1]*(0.77073) + test_inputs[i][2]*(0.56332);
test_outputs[i][0] = test_inputs[i][0]+test_inputs[i][1]+test_inputs[i][2];
///Actual Result
Serial.print("Expected: ");
Serial.print(test_outputs[i][0], 3);
test_inputs[i][0] /= MAXX*numInputs;
test_inputs[i][1] /= MAXX*numInputs;
test_inputs[i][2] /= MAXX*numInputs;
//Make Prediction
predict(test_inputs[i]);
float res = MAXX*numInputs*outputLayer[0];
Serial.print("\tPredicted: ");Serial.println(res,3);
// Serial.println(2.*rand()/RAND_MAX -1);
}
}
void loop()
{
// put your main code here, to run repeatedly:
}