-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDigitClassifier.cpp
449 lines (338 loc) · 9 KB
/
DigitClassifier.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include "pch.h"
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
#include <chrono>
#include <thread>
#include <stdlib.h>
#include <vector>
#include <array>
#include <algorithm>
#include <iterator>
#include "FileLoader.h"
// JUST FOR DEBUGGING PURPOSES!
#define space std::cout << "\n"
#define print(X) std::cerr << X << "\n"
// Random weight value for network initialization.
#define RAND_WEIGHT (((double)rand() / (double)RAND_MAX) - 0.5)
//TIME FUNCTIONS
#pragma region MyRegion
/***************************************************************************************************/
typedef std::chrono::time_point<std::chrono::steady_clock> vreme;
vreme timeNow()
{
return std::chrono::high_resolution_clock::now();
}
void timeElapsed(vreme& start, vreme& end)
{
double elap = (1e-9)*(std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count());
std::cout << "Time elapsed: " << elap << "\n";
}
/***************************************************************************************************/
#pragma endregion
const int input_n{ 785 };
auto* input_p = &input_n; // Number of inputs (784 + 1, one for label, one for each pixel and one for bias).
const int output_n{ 10 };
auto* output_p = &output_n; // Number of outputs, one for each digit.
const int hidden_n{ 519 };
auto* hidden_p = &hidden_n;// Number of hidden neurons. 518 hidden neurons + 1 bias. Number of hidden layers and neurons is taken from an article, link in the Readme.
const double lRate{ 0.01 };
auto* learn_rate = &lRate; // Neural network learning rate.
// Network inputs.
std::vector<double>* inputs = new std::vector<double>(*input_p);
// Network ouputs.
std::vector<double>* outputs = new std::vector<double>(*output_p);
// Hidden layer values.
std::vector<double>* hidden = new std::vector<double>(*hidden_p);
// Input-hidden weights.
auto* weights_I_H = new std::vector<std::vector<double>>(*hidden_p, std::vector<double>(*input_p));
// Output-hidden weights.
auto* weights_H_O = new std::vector<std::vector<double>>(*output_p, std::vector<double>(*hidden_p));
// Reference to output error vector.
std::vector<double>* error_output = new std::vector<double>;
// Reference to hidden error vector.
std::vector<double>* error_hidden = new std::vector<double>;
// Label vector.
std::unique_ptr<std::vector<int>> out{ new std::vector<int> };
// Sigmoid function function.
double sigmoid(double& in)
{
double exp_value;
// Calculating exponential
exp_value = exp(-in);
// Sigmoid funciton calculation
return (1 / (1 + exp_value));
}
// Derivative of sigmoid function.
double sigmoid_d(double& sig)
{
return sig*(1 - sig);
}
// Feeding inputs forward.
void feed_forward()
{
// Calculation for hidden layer.
for (int i = 0; i < *hidden_p-1; i++)
{
for (int j = 0; j < *input_p; j++)
{
// Calculating weights.
hidden->at(i) += (weights_I_H->at(i).at(j) * inputs->at(j));
}
hidden->at(i) = sigmoid(hidden->at(i));
}
// Calculation for output layer.
for (int i = 0; i < *output_p; i++)
{
for (int j = 0; j < *hidden_p; j++)
{
// Calculation weigths.
outputs->at(i) += (weights_H_O->at(i).at(j) * hidden->at(j));
}
outputs->at(i) = sigmoid(outputs->at(i));
}
}
// Converts label to vector of size = 10 where 1 represents the given digit.
auto label_vector(double& label)
{
out->clear();
out->resize(10);
out->at(label) = 1;
return *out;
}
// Backpropagating errors.
void backpropagation()
{
error_output->resize(*output_p);
error_hidden->resize(*hidden_p-1);
// Calculating errors of the output neurons.
for (int i = 0; i < *output_p; i++)
{
error_output->at(i) = (label_vector(inputs->at(0)).at(i) - outputs->at(i)) * sigmoid_d(outputs->at(i));
}
// Calculating errors for the hidden neurons.
for (int i = 0; i < *hidden_p-1; i++)
{
for (int j = 0; j < *output_p; j++)
{
error_hidden->at(i) += error_output->at(j) * weights_H_O->at(j).at(i);
}
error_hidden->at(i) *= sigmoid_d(hidden->at(i));
}
// Ajusting weights from hidden to output layer.
for (int i = 0; i < *output_p; i++)
{
for (int j = 0; j < *hidden_p; j++)
{
weights_H_O->at(i).at(j) += *learn_rate * error_output->at(i) * hidden->at(j);
}
}
// Ajusting weights from input to hidden layer.
for (int i = 0; i < *hidden_p-1; i++)
{
for (int j = 0; j < *input_p; j++)
{
weights_I_H->at(i).at(j) += *learn_rate * error_hidden->at(i) * inputs->at(j);
}
}
}
// Calculates the Mean Square Error of a given input.
double calculate_MSE()
{
double mse = 0.0;
for (int i = 0; i < *output_p; i++)
{
mse += pow(label_vector(inputs->at(0)).at(i) - outputs->at(i),2);
}
return (mse / static_cast<double>(*output_p));
}
// calculate Adam
// Network initialization.
void init_network()
{
// Setting the Bias neuron value.
inputs->resize(*input_p);
hidden->resize(*hidden_p);
outputs->resize(*output_p);
weights_H_O->resize(*output_p, std::vector<double>(*hidden_p));
weights_I_H->resize(*hidden_p, std::vector<double>(*input_p));
inputs->at(*input_p-1) = 1.0;
hidden->at(*hidden_p-1) = 1.0;
// Initializing input to hidden weights.
for (int i = 0; i < *hidden_p-1; i++)
{
for (int j = 0; j < *input_p; j++)
{
weights_I_H->at(i).at(j) = RAND_WEIGHT;
}
}
// Initializing hidden to output weights.
for (int i = 0; i < *output_p; i++)
{
for (int j = 0; j < *hidden_p; j++)
{
weights_H_O->at(i).at(j) = RAND_WEIGHT;
}
}
}
// Sets network inputs so that each neuron in the input layer matches one feature from .csv file.
void set_inputs(std::vector<double>& row)
{
for (auto j = 0; j < inputs->size(); j++)
{
inputs->at(j) = row.at(j);
}
}
// Deallocates all dynamically allocated objects.
void Delete()
{
delete inputs;
inputs = nullptr;
if (inputs != NULL)
{
print("inputs - DEALLOCATION FAILED.");
}
delete outputs;
outputs = nullptr;
if (outputs != NULL)
{
print("outputs - DEALLOCATION FAILED.");
}
delete hidden;
hidden = nullptr;
if (hidden != NULL)
{
print("hidden - DEALLOCATION FAILED.");
}
delete weights_H_O;
weights_H_O = nullptr;
if (weights_H_O != NULL)
{
print("weights_H_O - DEALLOCATION FAILED.");
}
delete weights_I_H;
weights_I_H = nullptr;
if (weights_I_H != NULL)
{
print("weights_I_H - DEALLOCATION FAILED.");
}
delete error_output;
error_output = nullptr;
if (error_output != NULL)
{
print("error_output - DEALLOCATION FAILED.");
}
delete error_hidden;
error_hidden = nullptr;
if (error_hidden != NULL)
{
print("error_hidden - DEALLOCATION FAILED.");
}
}
// Training the neural network until the criteria is set.
void train(int& iter)
{
auto start = timeNow();
std::cout << "Training start...\n";
FileLoader fileldr;
double mse;
// Single row from the train.csv file.
std::vector<double> row;
init_network();
// Maximum number of lines in mnist_train.csv file.
// When loop reaches end of file, it starts over untill the number of iterations is reached.
const int max_lines = 60000;
for (int i = 0; i < iter; i++)
{
if (i > max_lines)
{
i = 0;
}
row = fileldr.CSVFileRead("mnist_train.csv", i);
set_inputs(row);
feed_forward();
backpropagation();
mse = calculate_MSE();
if (i % 50 == 0 && i != 0)
{
std::cout << "epoch: " << i << "...\n";
}
}
/*int i = 0;
do
{
row = fileldr.CSVFileRead("mnist_train.csv", i);
set_inputs(row);
//print("inputs set");
feed_forward();
//print("fed forward");
backpropagation();
//print("back-propragated");
mse = calculate_MSE();
//print("MSE calculated");
UI.epoch_output(i);
i++;
} while (mse > 0.001);*/
auto end = timeNow();
timeElapsed(start, end);
std::cout << "Training done.\n";
/*FileLoader fl;
fl.Save_weights(*weights_I_H, *weights_H_O);*/
}
// Sets up previously trained network.
void load_network()
{
}
// Finds the largest output.
int classifier()
{
// Largest element in vector.
double best = *std::max_element(outputs->begin(), outputs->end());
// Finds index of largerst element.
auto iter = std::find(outputs->begin(), outputs->end(), best);
// Returns index of largest element.
return std::distance(outputs->begin(), iter);
}
void test_NN(int& i)
{
FileLoader fl;
std::vector<double> row;
row = fl.CSVFileRead("mnist_test.csv", i);
set_inputs(row);
feed_forward();
int classified = classifier();
std::cout << "\nReal value: " << row.at(0) << "\n";
std::cout << "Classified as: " << classified << " , at " << outputs->at(classified) << "\n";
for (auto i = 0; i < *output_p; i++)
{
std::cout << outputs->at(i) << ", ";
}
space;
}
int main()
{
system("title MNIST Digit Classifier");
int train_iter;
std::cout << "Insert number of learning iterations: ";
std::cin >> train_iter;
double completion_time = train_iter * 1.55 * 0.8;
std::cout << "Estimated training time: " << completion_time << "s\n";
system("pause");
train(train_iter);
space;
space;
char generateMore;
int i = 0;
do
{
test_NN(i);
space;
std::cout << "\nAnother test? (y/n): ";
std::cin >> generateMore;
i++;
} while (generateMore != 'n' && i < 10000);
space;
Delete();
// do while iterator and mse > 0.001
}