-
Notifications
You must be signed in to change notification settings - Fork 375
/
C+nnfs c++ part 6.cpp
708 lines (605 loc) · 17.9 KB
/
C+nnfs c++ part 6.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
#pragma once
/* This is a c++ implementation of neural networks from scratch in python series.
* In hope to do things in a similar way to the tutorial, I have created a numcpp library
* containing stuff to help us with matrix and vector calculations. Knowing how this works is
* not strictly necessary since the goal here is to learn how neural networks work, so the parts
* you may be interested in are marked as layer, activation and main (they are towards the end of the file).
* This implementation was originally in seperate files, so you will see the sections
* marked as .h and cpp.
*/
//===========================================================================================
//numcpp.h
//Header file for all the calculations involving matries and vectors.
#include "iostream"
#include <vector>
#include <random>
//Set debug to 0 to disable logging
#define DEBUG 1
#if DEBUG == 1
#define LOG(x) std::cout << x << std::endl
#else
#define LOG(x)
#endif
#define MAX(a, b) a > b ? a : b
#define MIN(a, b) a < b ? a : b
//#######################################
//# CONSTANTS #
//#######################################
#define EULERS_NUMBER pow((1.0 + 1.0 / 10000000.0), 10000000.0)
#define PI 3.14159265359
namespace numcpp {
//#######################################
//# TYPES #
//#######################################
//Vector -> contains a std::vector that holds a bunch of double
//Its called vf becasue it used to hold floats until i realised
//that a double is needed, so it really should be called Vd
struct Vf
{
std::vector<double> v;
//fill vector with num
void fill(const unsigned int size, const double num)
{
v.clear();
for (unsigned int i = 0; i < size; i++)
v.push_back(num);
}
friend std::ostream& operator << (std::ostream& out, const Vf& vec)
{
out << "[";
for (unsigned int j = 0; j < vec.v.size(); j++)
{
if (j % vec.v.size() == vec.v.size() - 1)
out << vec.v[j] << "]" << std::endl;
else
out << vec.v[j] << ", ";
}
return out;
}
};
//Matrix -> contains a std::vector that holds a bunch of numcpp::Vd
//row by column
struct Mf
{
std::vector<Vf> m;
//fill matrix with num
void fill(unsigned const int rows, unsigned const int cols, const double num)
{
m.clear();
for (unsigned int i = 0; i < rows; i++)
{
Vf tempVec;
for (unsigned int j = 0; j < cols; j++)
{
tempVec.v.push_back(num);
}
m.push_back(tempVec);
}
}
friend std::ostream& operator << (std::ostream& out, const Mf& mat)
{
out << "[" << std::endl << std::endl;
for (unsigned int i = 0; i < mat.m.size(); i++)
{
out << "[";
for (unsigned int j = 0; j < mat.m[i].v.size(); j++)
{
if (j % mat.m[i].v.size() == mat.m[i].v.size() - 1)
out << mat.m[i].v[j] << "]" << std::endl << std::endl;
else
out << mat.m[i].v[j] << ", ";
}
}
out << "]" << std::endl;
return out;
}
};
//#######################################
//# RANDOM #
//#######################################
class Random
{
private:
Random();
~Random();
Random(const Random&) = delete;
std::mt19937 _randomEngine;
public:
static Random& Get();
int Range(const int lower, const int upper);
double Range(const double lower, const double upper);
void Matrix(const unsigned int rows, const unsigned int cols, const double lower, const double upper, Mf& outmat);
void Vector(const unsigned int size, const double lower, const double upper, Vf& outvec);
};
static Random& rng = Random::Get();
//#######################################
//# UTILITY FUNCTIONS FOR VECTOR #
//#######################################
inline double vectorDot(const Vf& v1, const Vf& v2)
{
if (v1.v.size() != v2.v.size())
throw std::exception();
double total = 0.0;
for (unsigned int i = 0; i < v1.v.size(); i++)
{
total += v1.v[i] * v2.v[i];
}
return total;
}
inline double vectoGetMax(const Vf& vec)
{
double biggest = vec.v[0];
for (unsigned int i = 1; i < vec.v.size(); i++)
{
biggest = MAX(biggest, vec.v[i]);
}
return biggest;
}
inline double vectorGetMin(const Vf& vec)
{
double smallest = vec.v[0];
for (unsigned int i = 1; i < vec.v.size(); i++)
{
smallest = MIN(smallest, vec.v[i]);
}
return smallest;
}
inline Vf vectorCapMax(const double value, const Vf& vec)
{
Vf tempv;
for (unsigned int i = 0; i < vec.v.size(); i++)
{
tempv.v.push_back(MIN(value, vec.v[i]));
}
return tempv;
}
inline Vf vectorCapMin(const double value, const Vf& vec)
{
Vf tempv;
for (unsigned int i = 0; i < vec.v.size(); i++)
{
tempv.v.push_back(MAX(value, vec.v[i]));
}
return tempv;
}
inline Vf vectorExp(const Vf& vec)
{
Vf tempv;
for (unsigned int i = 0; i < vec.v.size(); i++)
{
tempv.v.push_back(pow(EULERS_NUMBER, vec.v[i]));
}
return tempv;
}
inline double vectorSum(const Vf& vec)
{
double total = 0.0;
for (unsigned int i = 0; i < vec.v.size(); i++)
total += vec.v[i];
return total;
}
inline Vf vectorNormalize(const Vf& vec)
{
Vf tempv;
double total = vectorSum(vec);
for (unsigned int i = 0; i < vec.v.size(); i++)
{
tempv.v.push_back(vec.v[i] / total);
}
return tempv;
}
//#######################################
//# UTILITY FUNCTIONS FOR MATRIX #
//#######################################
//Assumes that all the vectors in a matrix will have the same size. The only way
//for a vectors to have different sizes is if they are set manually, since all the
//functions provided in this lib only produce rectangle shaped matrices, i.e all
//vectors in the matrix are the same size
inline void matrixDot(const Mf& m1, const Mf& m2, Mf& outm)
{
if (m1.m[0].v.size() && m2.m.size())
if (m1.m[0].v.size() != m2.m.size())
{
LOG("Shape mismatch: " << "matrix1 columns: " << m1.m[0].v.size() << ", " << "matrix2 rows: " << m2.m.size());
throw std::exception();
}
unsigned int m1x = 0; unsigned int m1y = 0; unsigned int m2y = 0; //m2y = m1x
while (outm.m.size() < m1.m.size())
{
Vf tempv;
while (tempv.v.size() < m2.m[0].v.size())
{
double total = 0.0;
while (m1x < m1.m[0].v.size())
{
total += m1.m[m1y].v[m1x] * m2.m[m1x].v[m2y];
m1x++;
}
tempv.v.push_back(total);
m1x = 0;
m2y < m2.m[0].v.size() - 1 ? m2y++ : m2y = 0;
}
m1y < m1.m.size() - 1 ? m1y++ : m1y = 0;
outm.m.push_back(tempv);
}
}
//assumes that all the vectors in the matrix will be the same size
inline void matrixAdd(Mf& mat, const Vf& vec)
{
if (mat.m[0].v.size() != vec.v.size())
{
LOG("Shape mismatch: " << "matrix columns: " << mat.m[0].v.size() << ", " << "vector size: " << vec.v.size());
throw std::exception();
}
for (unsigned int i = 0; i < mat.m.size(); i++)
for (unsigned int j = 0; j < vec.v.size(); j++)
{
mat.m[i].v[j] += vec.v[j];
}
}
//value less than lower, than lower otherwise value (cap values at lower)
//used for rectified linear activation function in hidden layer
inline Mf matrixCapMin(const double value, const Mf& inmat)
{
Mf outmat;
for (unsigned int i = 0; i < inmat.m.size(); i++)
{
Vf tempv;
for (unsigned int j = 0; j < inmat.m[i].v.size(); j++)
{
tempv.v.push_back(MAX(value, inmat.m[i].v[j]));
}
outmat.m.push_back(tempv);
}
return outmat;
}
//caps max value in matrix at value
inline Mf matrixCapMax(const double value, const Mf& inmat)
{
Mf outmat;
for (unsigned int i = 0; i < inmat.m.size(); i++)
{
Vf tempv;
for (unsigned int j = 0; j < inmat.m[i].v.size(); j++)
{
tempv.v.push_back(MIN(value, inmat.m[i].v[j]));
}
outmat.m.push_back(tempv);
}
return outmat;
}
//axis = 0 means max value in each row
//axis = 1 means max value in each column
inline Vf matrixGetMaxInAxis(const Mf& mat, const int axis)
{
Vf tempv;
if (axis == 0)
{
for (unsigned int i = 0; i < mat.m[0].v.size(); i++)
{
tempv.v.push_back(mat.m[0].v[i]);
for (unsigned int j = 1; j < mat.m.size(); j++)
{
tempv.v[i] = MAX(mat.m[j].v[i], tempv.v[i]);
}
}
}
else if (axis == 1)
{
for (unsigned int i = 0; i < mat.m.size(); i++)
{
tempv.v.push_back(mat.m[i].v[0]);
for (unsigned int j = 1; j < mat.m[i].v.size(); j++)
{
tempv.v[i] = MAX(mat.m[i].v[j], tempv.v[i]);
}
}
}
else
{
LOG("axis must be either 0 or 1. Provided was: " << axis);
throw std::exception();
}
return tempv;
}
//returns biggest value from whole of the matrix
inline double matrixGetMax(const Mf& mat)
{
double biggest = mat.m[0].v[0];
for (unsigned int i = 0; i < mat.m.size(); i++)
for (unsigned int j = 0; j < mat.m[i].v.size(); j++)
{
biggest = MAX(mat.m[i].v[j], biggest);
}
return biggest;
}
//exponentiate all the elements
inline Mf matrixExp(const Mf& mat)
{
Mf outmat;
for (unsigned int i = 0; i < mat.m.size(); i++)
{
Vf tempv;
for (unsigned int j = 0; j < mat.m[i].v.size(); j++)
{
tempv.v.push_back(pow(EULERS_NUMBER, mat.m[i].v[j]));
}
outmat.m.push_back(tempv);
}
return outmat;
}
//minus value from all the elements
inline Mf matrixMinus(const double value, const Mf& mat)
{
Mf outmat;
for (unsigned int i = 0; i < mat.m.size(); i++)
{
Vf tempv;
for (unsigned int j = 0; j < mat.m[i].v.size(); j++)
{
tempv.v.push_back(mat.m[i].v[j] - value);
}
outmat.m.push_back(tempv);
}
return outmat;
}
// 0 = rows, 1 = columns
inline Mf matrixMinusMaxInAxis(const Mf& mat, const int axis)
{
Mf outmat;
if (axis == 0)
{
outmat = mat;
Vf max = matrixGetMaxInAxis(mat, 0);
for (unsigned int i = 0; i < mat.m[0].v.size(); i++)
for (unsigned int j = 0; j < mat.m.size(); j++)
{
outmat.m[j].v[i] = mat.m[j].v[i] - max.v[i];
}
}
else if (axis == 1)
{
Vf max = matrixGetMaxInAxis(mat, 1);
for (unsigned int i = 0; i < mat.m.size(); i++)
{
Vf tempv;
for (unsigned int j = 0; j < mat.m[i].v.size(); j++)
{
tempv.v.push_back(mat.m[i].v[j] - max.v[i]);
}
outmat.m.push_back(tempv);
}
}
else
{
LOG("axis must be either 0 or 1. Provided was: " << axis);
throw std::exception();
}
return outmat;
}
//axis 0 means rows are summed
//axis 1 means that columns
inline Vf matrixSumInAxis(const Mf& mat, const int axis)
{
Vf tempv;
if (axis == 0)
{
for (unsigned int i = 0; i < mat.m.size(); i++)
{
double total = 0.0;
for (unsigned int j = 0; j < mat.m[0].v.size(); j++)
{
total += mat.m[i].v[j];
}
tempv.v.push_back(total);
}
}
else if (axis == 1)
{
for (unsigned int i = 0; i < mat.m.size(); i++)
{
tempv.v.push_back(vectorSum(mat.m[i]));
}
}
else
{
LOG("axis must be either 0 or 1. Provided was: " << axis);
throw std::exception();
}
return tempv;
}
/*axis 0 means rows are summed to normalize
axis 1 means that columns are summed to normalize*/
inline Mf matrixNormalizeInAxis(const Mf& mat, int axis)
{
Mf outmat;
if (axis == 0)
{
Vf _tempv = matrixSumInAxis(mat, 0);
Vf tempv;
for (unsigned int i = 0; i < mat.m.size(); i++)
{
for (unsigned int j = 0; j < mat.m[i].v.size(); j++)
{
tempv.v.push_back(mat.m[i].v[j] / _tempv.v[j]);
}
outmat.m.push_back(tempv);
}
}
else if (axis == 1)
{
for (unsigned int i = 0; i < mat.m.size(); i++)
{
Vf tempv;
double total = vectorSum(mat.m[i]);
for (unsigned int j = 0; j < mat.m[i].v.size(); j++)
{
if (total != 0)
tempv.v.push_back(mat.m[i].v[j] / total);
else
tempv.v.push_back(1.0 / mat.m[i].v.size());
}
outmat.m.push_back(tempv);
}
}
else
{
LOG("axis must be either 0 or 1. Provided was: " << axis);
throw std::exception();
}
return outmat;
}
}
//===============================================================================================
//numcpp.cpp
//contains implementations for the random class in numcpp.h
namespace numcpp {
//#######################################
//# RANDOM #
//#######################################
// To make generate the same set of random numbers (for debugging)
//comment out: std::mt19937 randomEngine(randomSeed());
//uncomment: std::mt19937 randomEngine(3);
Random::Random()
{
std::random_device randomSeed;
//std::mt19937 randomEngine(3);
std::mt19937 randomEngine(randomSeed());
_randomEngine = randomEngine;
};
Random::~Random() {};
Random& Random::Get()
{
static Random random;
return random;
}
int Random::Range(const int lower, const int upper)
{
std::uniform_int_distribution<int> distribution(lower, upper);
return distribution(_randomEngine);
}
double Random::Range(const double lower, const double upper)
{
std::uniform_real_distribution<double> distribution(lower, upper);
return distribution(_randomEngine);
}
void Random::Matrix(const unsigned int rows, const unsigned int cols, const double lower, const double upper, Mf& outmat)
{
for (unsigned int i = 0; i < rows; i++)
{
Vf tempVec;
for (unsigned int j = 0; j < cols; j++)
{
tempVec.v.push_back(Range(lower, upper));
}
outmat.m.push_back(tempVec);
}
}
void Random::Vector(const unsigned int size, const double lower, const double upper, Vf& outvec)
{
for (unsigned int i = 0; i < size; i++)
outvec.v.push_back(Range(lower, upper));
}
}
//==============================================================================================
//layer.h
namespace neural_net {
struct LayerDense
{
numcpp::Mf weights;
numcpp::Mf output;
numcpp::Vf biases;
LayerDense(const int nInputs, const int nNeurons, double bias = 0.0);
~LayerDense();
void forward(const numcpp::Mf& inputs);
};
}
//================================================================================================
//layer.cpp
namespace neural_net {
LayerDense::LayerDense(const int nInputs, const int nNeurons, double bias)
{
numcpp::rng.Matrix(nInputs, nNeurons, 0.0, 1.0, weights);
LOG("(dense layer init) initialized with matrix: " << std::endl << weights);
biases.fill(nNeurons, bias);
LOG("(dense layer init) Filled Biases vector with a bias value of: " << bias << std::endl);
}
LayerDense::~LayerDense() {};
void LayerDense::forward(const numcpp::Mf& inputs)
{
numcpp::matrixDot(inputs, weights, output);
LOG(" (dense layer forward) inputs * weights in forward method of dense layer. output: " << std::endl << output);
numcpp::matrixAdd(output, biases);
LOG("(dense layer forward) biases added to output matrix from dot product of weights and input: " << std::endl << output);
}
}
//==========================================================================================
//activation.h
namespace neural_net {
struct ActivationReLU
{
numcpp::Mf output;
ActivationReLU();
~ActivationReLU();
void forward(const numcpp::Mf& input);
};
struct ActivationSoftmax
{
numcpp::Mf output;
ActivationSoftmax();
void forward(const numcpp::Mf& input);
};
}
//===========================================================================
//activation.cpp
namespace neural_net {
ActivationReLU::ActivationReLU() {};
ActivationReLU::~ActivationReLU() {};
void ActivationReLU::forward(const numcpp::Mf& input)
{
output = numcpp::matrixCapMin(0.0, input);
LOG("(ReLU forward) forward pass. matrix capped at min value of 0. output: " << std::endl << output);
}
ActivationSoftmax::ActivationSoftmax() {};
void ActivationSoftmax::forward(const numcpp::Mf& input)
{
numcpp::Mf negated = numcpp::matrixMinusMaxInAxis(input, 1);
LOG("(softmax forward) input matrix's highest value negated from all the elements in matrix. output: " << std::endl << negated);
numcpp::Mf exp = numcpp::matrixExp(negated);
LOG("(softmax forward) all the elements in the negated matrix exponentiated. output: " << std::endl << exp);
output = numcpp::matrixNormalizeInAxis(exp, 1);
LOG("(softmax forward) exponentiated matrix is normalized. output: " << std::endl << output);
}
}
//=======================================================================================
//main.cpp
//NOTES:
//1. matrices are ordered row by column
//2. LOG macro in numcpp.h file is responsible for logging. Set DEBUG macro to 0 to turn logging off.
int main()
{
//output up to 10 digits
std::cout.precision(10);
//create a matrix
numcpp::Mf X;
//all the matrices size will be 3x3 to make it easier to test things.
const int nMatSize = 3;
//fill X will random numbers between -1.0 and 1.0. This is the actual input.
numcpp::rng.Matrix(nMatSize, nMatSize, -1.0, 1.0, X);
//initilaize dense layer1 of size nMatSize x nMatSize
neural_net::LayerDense layer1 = neural_net::LayerDense(nMatSize, nMatSize);
//ReLU object for layer 1
neural_net::ActivationReLU ReLU1;
//forward pass of layer 1.
layer1.forward(X);
//output of layer 1 is passed to ReLU as input.
ReLU1.forward(layer1.output);
//initilaize dense layer2 of size nMatSize x nMatSize. This is an output layer.
neural_net::LayerDense layerOut = neural_net::LayerDense(nMatSize, nMatSize);
//it uses softmax activation function since its an output layer
neural_net::ActivationSoftmax softmax;
//output of ReLU1 is passed to the output layer as input.
layerOut.forward(ReLU1.output);
//output of output layer is passed through the softmax activation function.
softmax.forward(layerOut.output);
}