Repository navigation
Expand file tree
/
Copy pathMLI.cpp
More file actions
170 lines (134 loc) · 4.12 KB
/
Copy pathMLI.cpp
File metadata and controls
170 lines (134 loc) · 4.12 KB
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
//============================================================================
// Name : MLI.cpp
// Author : Wade Burch
// Version :
// Copyright : TBD
// Description : Hello World in C++, Ansi-style
//============================================================================
#include "./MLI.hpp"
using namespace std;
MLI::MLI(vector<uint> top, float givenRate)
: topology(top), learnRate(givenRate), depth(0), cost(0) {
random_device rd;
mt19937 rgen(rd());
for (uint i = 0; i < topology.size(); i++) {
uint t = topology[i];
// Create Neuron, Cache, and Delta layers
if (i == topology.size() - 1) {
neurons.push_back(new ColVector(t));
deltas.push_back(new ColVector(t));
} else {
neurons.push_back(new ColVector(t + 1));
deltas.push_back(new ColVector(t + 1));
neurons.back()->coeffRef(t) = 1.0;
}
caches.push_back(new ColVector(t));
// Initialize weights (using He)
if (i > 0) {
uint p = neurons[i-1]->size();
uint c = neurons[i]->size();
if (i != topology.size() - 1)
c -= 1;
normal_distribution<float> dist(0.0, sqrt(2.0/p));
auto he = [&](){return dist(rgen);};
Matrix m = Eigen::MatrixXf::NullaryExpr(c, p, he);
weights.push_back(new Matrix(c, p));
(*weights.back()) = m;
}
this->depth = (t+1 > depth) ? t+1 : depth;
}
}
/* Currently using ReLU activation */
Scalar activationFunc(Scalar f) {
return max((Scalar) 0.0, f);
}
Scalar activationFuncDeriv(Scalar f) {
return ((f>0) ? 1.0 : 0.0);
}
void MLI::forwardProp(ColVector& input) {
// Add input to first nodes (input layer)
neurons.front()->block(0, 0, topology[0], 1) = input;
// Propagate forward, store unactivated input as cache, and activate
for (uint i = 1; i < topology.size(); i++) {
auto s = topology[i];
neurons[i]->block(0, 0, s, 1) = ((*weights[i-1]) * (*neurons[i-1]));
(*caches[i]) = neurons[i]->block(0, 0, s, 1);
neurons[i]->block(0, 0, s, 1) = neurons[i]->block(0, 0, s, 1).unaryExpr(ptr_fun(activationFunc));
}
}
void MLI::backwardProp(ColVector& output) {
this->errCalc(output);
this->updateWeights();
}
void MLI::errCalc(ColVector& output) {
// Diff output layer and expected values, obtain cost
(*deltas.back()) = output - (*neurons.back());
// Calculate MSE
//this->cost = deltas.back()->array().square().sum() / 2.0;
this->cost = deltas.back()->array().square().sum() / deltas.back()->size();
// Clear deltas for n-1 (prep for accumulation)
for (uint i = topology.size() - 2; i > 0; i--) {
(*deltas[i]) = deltas[i]->Zero(deltas[i]->size());
}
}
void MLI::updateWeights() {
for (int i = topology.size() - 2; i >= 0; i--) {
for (uint r = 0; r < weights[i]->rows(); r++) {
for (uint c = 0; c < weights[i]->cols(); c++) {
auto dEdO = deltas[i+1]->coeffRef(r);
auto dOdN = activationFuncDeriv(caches[i+1]->coeffRef(r));
auto dNdW = neurons[i]->coeffRef(c);
deltas[i]->coeffRef(c) += dEdO * dOdN * weights[i]->coeffRef(r,c);
weights[i]->coeffRef(r,c) += learnRate * dEdO * dOdN * dNdW;
}
}
}
}
void MLI::train(vector<ColVector*> input_data, vector<ColVector*> output_data) {
for (uint i = 0; i < input_data.size(); i++) {
this->forwardProp(*input_data[i]);
this->backwardProp(*output_data[i]);
cout << i << "," << this->cost << endl;
}
}
void MLI::printLayers() {
for (uint d = 0; d < depth; d++) {
for (uint i = 0; i < neurons.size(); i++) {
if (neurons[i]->size() > d) {
cout << setw(12) << neurons[i]->coeffRef(d);
} else {
cout << setw(12) << "-";
}
}
cout << endl;
}
}
void MLI::printWeights() {
for (uint i = 0; i < weights.size(); i++) {
cout << *weights[i] << "\n" << endl;
}
}
void MLI::printDeltas() {
for (uint d = 0; d < depth; d++) {
for (uint i = 0; i < deltas.size(); i++) {
if (deltas[i]->size() > d) {
cout << setw(12) << deltas[i]->coeffRef(d);
} else {
cout << setw(12) << "-";
}
}
cout << endl;
}
}
void MLI::printCaches() {
for (uint d = 0; d < depth-1; d++) {
for (uint i = 0; i < caches.size(); i++) {
if (caches[i]->size() > d) {
cout << setw(12) << caches[i]->coeffRef(d);
} else {
cout << setw(12) << "-";
}
}
cout << endl;
}
}