-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
342 lines (312 loc) · 11.9 KB
/
Copy pathModel.cpp
File metadata and controls
342 lines (312 loc) · 11.9 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
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
#include "include/Model.hpp"
#include "include/CsvToVector.hpp"
#include <iostream>
#include <sstream>
#include <cmath>
#include <time.h> // randomize seed
#include <stdlib.h> // RANDOMIZE
typedef unsigned int uint;
Model::Model(std::vector<std::vector<float>>& features, uint hiddenLayers, uint neuronsPerLayer, float lr) {
// assign learning rate member to constructor parameter
this->learningRate = lr;
std::cout << "Learning Rate=" << learningRate << '\n';
std::cout << "Single Feature Size=" << features[0].size() << '\n';
float summationLoss = 0;
// create weight layer and gradient for input
weights.push_back(new std::vector<std::vector<float>*>);
nablaWeights.push_back(new std::vector<std::vector<float>*>);
// create input weights
for (int x = 0; x < features[0].size(); ++x) {
weights[0]->push_back(new std::vector<float>);
nablaWeights[0]->push_back(new std::vector<float>);
for (int y = 0; y < neuronsPerLayer; ++y) {
(*weights[0])[x]->push_back(0.5);
(*nablaWeights[0])[x]->push_back(0.5);
}
}
for (int x = 0; x < hiddenLayers; ++x) {
weights.push_back(new std::vector<std::vector<float>*>);
nablaWeights.push_back(new std::vector<std::vector<float>*>);
cache.push_back(new std::vector<float>);
nablaCache.push_back(new std::vector<float>);
biases.push_back(new std::vector<float>);
nablaBiases.push_back(new std::vector<float>);
activated.push_back(new std::vector<float>);
for (int y = 0; y < neuronsPerLayer; ++y) {
weights[x + 1]->push_back(new std::vector<float>);
nablaWeights[x + 1]->push_back(new std::vector<float>);
cache[x]->push_back(0.5);
nablaCache[x]->push_back(0.5);
biases[x]->push_back(0.5);
nablaBiases[x]->push_back(0.5);
activated[x]->push_back(0.5);
if (x < hiddenLayers - 1) {
for (int z = 0; z < neuronsPerLayer; ++z) {
(*weights[x + 1])[y]->push_back(0.5);
(*nablaWeights[x + 1])[y]->push_back(0.5);
}
}
else {
for (int z = 0; z < 2; ++z) {
(*weights[x + 1])[y]->push_back(0.5);
(*nablaWeights[x + 1])[y]->push_back(0.5);
}
}
}
// last hidden layer
if (x == hiddenLayers - 1) {
// create members for output nodes
cache.push_back(new std::vector<float>);
nablaCache.push_back(new std::vector<float>);
biases.push_back(new std::vector<float>);
nablaBiases.push_back(new std::vector<float>);
activated.push_back(new std::vector<float>);
for (int y = 0; y < 2; ++y) {
cache[x + 1]->push_back(0.5);
nablaCache[x + 1]->push_back(0.5);
biases[x + 1]->push_back(0.5);
nablaBiases[x + 1]->push_back(0.5);
activated[x + 1]->push_back(0.5);
}
}
}
}
float Model::sigmoid(const float& in, bool derivative) {
// using derivative when already passed through sigmoid function
if (derivative == true) {
return sigmoid(in) * (1 - sigmoid(in));
}
return 1/(1 + std::exp(-in));
}
float Model::ReLU(const float& in) {
/* Doesnt need a derivative parameter since any value greater than zero
* has a derivative of one, values zero or less have a derivative of zero */
return (in > 0) ? in : 0;
}
void Model::forward(const std::vector<float>& feature, const std::vector<float>& label) {
// zero values of each cache
for (int x = 0; x < cache.size(); ++x) {
for (int y = 0; y < cache[x]->size(); ++y) {
(*cache[x])[y] = 0;
}
}
// forward feed
for (int x = 0; x < weights.size(); ++x) {
for (int y = 0; y < weights[x]->size(); ++y) {
for (int z = 0; z < (*weights[x])[y]->size(); ++z) {
// weights from input
if (x == 0) {
(*cache[x])[z] += (*(*weights[x])[y])[z] * feature[y]; // add weight * value to cache value
}
// weights from hidden nodes
else {
(*cache[x])[z] += (*(*weights[x])[y])[z] * (*activated[x - 1])[y]; // add weight * value to cache value
}
// last group of weights in layer
if (y == weights[x]->size() - 1) {
(*cache[x])[z] += (*biases[x])[z]; // add biases to cache
// not last weight layer
if (x != weights.size() - 1) {
(*activated[x])[z] = (*cache[x])[z]; // activated equal to cache through activation function
}
// last weight layer
// else {
// (*activated[x])[z] = sigmoid((*cache[x])[z]); // activated equal to cache through activation function
// }
}
}
}
}
*activated.back() = softmax(*cache.back());
float loss = crossEntropy(*activated.back(), label); // calc loss
// determine if prediction was correct
if (argmax(*activated.back()) == label)
correct++;
summationLoss += loss;
}
void Model::backward(const std::vector<float>& feature, const std::vector<float>& label) {
// start from end, weight layers
for (int x = weights.size() - 1; x >= 0; x--) {
if (x == weights.size() - 1) {
// assign gradient of cache to partial derivative of activated value from output nodes
(*nablaCache[x])[0] = crossEntropy(*activated.back(), label, true, 0);
// std::cout << "(*nablaCache[x])[0]="<<(*nablaCache[x])[0]<<'\n';
(*nablaCache[x])[1] = crossEntropy(*activated.back(), label, true, 1);
// std::cout << "(*nablaCache[x])[1]="<<(*nablaCache[x])[1]<<'\n';
(*nablaBiases[x])[0] = (*nablaCache[x])[0];
(*nablaBiases[x])[1] = (*nablaCache[x])[1];
}
// iterate through each weight vector in weight layer
for (int y = 0; y < weights[x]->size(); ++y) {
float summationActivation = 0; // holds summation of partial deriv of loss with respect to activated node values
for (int z = 0; z < (*weights[x])[y]->size(); ++z) {
// hidden weight layers
if (x > 0) {
(*(*nablaWeights[x])[y])[z] = (*nablaCache[x])[z] * (*activated[x - 1])[y];
summationActivation += (*(*weights[x])[y])[z] * (*nablaCache[x])[z];
}
// input weight layer
else {
(*(*nablaWeights[x])[y])[z] = (*nablaCache[x])[z] * feature[y];
}
}
if (x > 0) {
(*nablaCache[x - 1])[y] = summationActivation;
(*nablaBiases[x - 1])[y] = (*nablaCache[x - 1])[y];
}
}
}
// update weights by gradients
for (int x = 0; x < weights.size(); ++x) {
for (int y = 0; y < weights[x]->size(); ++y) {
for (int z = 0; z < (*weights[x])[y]->size(); ++z) {
(*(*weights[x])[y])[z] -= (*(*nablaWeights[x])[y])[z] * learningRate;
}
}
}
// update biases by gradients
for (int x = 0; x < biases.size(); ++x) {
for (int y = 0; y < biases[x]->size(); ++y) {
(*biases[x])[y] -= (*nablaBiases[x])[y] * learningRate;
}
}
}
float Model::MSE(std::vector<float> output, std::vector<float> label, bool derivative, int element) {
if (derivative == true) {
// std::cout << "output[element] - label[element] = "<< output[element] << " - " << label[element] << '\n';
return output[element] - label[element];
}
float sum = 0;
int size = output.size();
for (int x = 0; x < size; ++x) {
float squared = std::pow(label[x] - output[x], 2) ;
sum += squared;
}
return (sum / size);
}
std::vector<float> Model::argmax(std::vector<float> output) {
if (output[0] > output[1]) {
output[0] = 1;
output[1] = 0;
}
else {
output[0] = 0;
output[1] = 1;
}
return output;
}
float Model::crossEntropy(std::vector<float> output, std::vector<float> label, bool derivative, int element) {
// only use with softmax! //
if (derivative == true) {
return output[element] - label[element];
}
float epsilon = 0.000001;
return label[0] == 1 ? -log(output[0] + epsilon): -log(output[1] + epsilon); // log is natural log
}
std::vector<float> Model::softmax(std::vector<float> output) {
std::vector<float> temp = output;
float sub;
if (temp[0] > temp[1])
sub = temp[0];
else
sub = temp[1];
temp[0] -= sub;
temp[1] -= sub;
float sum = exp(temp[0]) + exp(temp[1]);
output[0] = exp(temp[0])/sum;
output[1] = exp(temp[1])/sum;
return output;
}
std::ostream& operator<<(std::ostream& out, Model& mod) {
out << mod.str2(mod.nablaBiases, "nablaBiases")
<< mod.str2(mod.nablaCache, "nablaCache")
<< mod.str3(mod.nablaWeights, "nablaWeights")
<< mod.str3(mod.weights, "weights")
<< mod.str2(mod.biases, "biases")
<< mod.str2(mod.cache, "cache")
<< mod.str2(mod.activated, "activated");
return out;
}
template <typename T> std::string Model::toStr(const T& t) {
std::ostringstream os;
os << t;
return os.str();
}
std::string Model::str2(const std::vector<std::vector<float>*>& vec, const std::string name) {
std::string str = "[";
for (int x = 0; x < vec.size(); ++x) {
if (x > 0)
str += " [";
else
str += "[";
for (int y = 0; y < vec[x]->size(); ++y) {
if (y < vec[x]->size() - 1) {
str += toStr((*vec[x])[y]);
str += ", ";
}
else
str += toStr((*vec[x])[y]);
}
if (x < vec.size() - 1)
str += "],\n";
else
str += "]";
}
str += "(" + name + ")]\n\n";
return str;
}
std::string Model::str3(const std::vector<std::vector<std::vector<float>*>*>& vec, const std::string name) {
std::string str = "[";
for (int x = 0; x < vec.size(); ++x) {
if (x > 0)
str += " [";
else
str += "[";
int nodeLayerSize = vec[x]->size();
for (int y = 0; y < nodeLayerSize; ++y) {
str += "[";
int nodeSize = (*vec[x])[y]->size();
for (int z = 0; z < nodeSize; ++z) {
if (z < nodeSize - 1) {
str += toStr((*(*vec[x])[y])[z]);
str += ", ";
}
else
str += toStr((*(*vec[x])[y])[z]);
}
if (y < nodeLayerSize - 1)
str += "], ";
else
str += "]";
}
if (x < vec.size() - 1)
str += "],\n";
else
str += "]";
}
str += "(" + name + ")]\n\n";
return str;
}
void Model::printLoss(std::vector<std::vector<float>>& features) {
std::cout << "avg loss=" << (summationLoss / features.size()) << " : " << correct << "/"
<< features.size() << " " << (float) correct * 100 / features.size() << "%\n";
correct = 0;
summationLoss = 0;
}
void Model::randomize(int seed) {
seed != 0 ? srand(seed) : srand(time(NULL));
// randomize weights
for (int x = 0; x < weights.size(); ++x) {
for (int y = 0; y < weights[x]->size(); ++y) {
for (int z = 0; z < (*weights[x])[y]->size(); ++z) {
(*(*weights[x])[y])[z] = (float) rand() / (float) RAND_MAX;
}
}
}
// randomize biases
for (int x = 0; x < biases.size(); ++x) {
for (int y = 0; y < biases[x]->size(); ++y) {
(*biases[x])[y] = (float) rand() / (float) RAND_MAX;
}
}
}