-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNN.cpp
More file actions
299 lines (284 loc) · 10.3 KB
/
Copy pathNN.cpp
File metadata and controls
299 lines (284 loc) · 10.3 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
#include <fstream>
#include <iomanip>
#include <cmath>
#include "NN.hpp"
NN::NN() {
#if DEBUG_MODE
std::cout << "Neural network instance created\n";
#endif
}
NN::~NN() {
#if DEBUG_MODE
std::cout << "Neural network instance deleted\n";
#endif
}
double NN::sigmoid(double num) {
return 1.0 / (1.0 + std::exp(-num));
}
double NN::sigmoidDerivative(double num) {
return sigmoid(num) * (1.0 - sigmoid(num));
}
double NN::MeanSquaredErrorDerivative(double num, double target) {
return 2.0 * (num - target);
}
bool NN::initiate(std::string fileNameData, std::vector < unsigned int > layerNeuronAmounts) {
this -> fileNameData = fileNameData;
this -> dataSize = 0;
neuronLayers.resize(layerNeuronAmounts.size());
neuronLayersSums.resize(layerNeuronAmounts.size());
for (size_t i = 0; i < neuronLayers.size(); i++) {
neuronLayers[i].resize(layerNeuronAmounts[i]);
neuronLayersSums[i].resize(layerNeuronAmounts[i]);
}
#if DEBUG_MODE
std::cout << "Neural network instance initiated with layout:\n";
for (size_t i = 0; i < neuronLayers.size(); i++) {
std::cout << "Layer " << i << ", " << neuronLayers[i].size() << " neurons" << std::endl;
}
#endif
weightLayers.resize(layerNeuronAmounts.size() - 1); //Number of layers without input layer
biasLayers.resize(layerNeuronAmounts.size() - 1);
weightLayersGradient.resize(layerNeuronAmounts.size() - 1);
biasLayersGradient.resize(layerNeuronAmounts.size() - 1);
for (size_t i = 0; i < layerNeuronAmounts.size() - 1; i++) {
weightLayers[i].resize(layerNeuronAmounts[i] * layerNeuronAmounts[i + 1]);
biasLayers[i].resize(layerNeuronAmounts[i + 1]);
weightLayersGradient[i].resize(layerNeuronAmounts[i] * layerNeuronAmounts[i + 1]);
biasLayersGradient[i].resize(layerNeuronAmounts[i + 1]);
}
for (size_t i = 0; i < weightLayers.size(); i++) {
dataSize = dataSize + weightLayers[i].size() + biasLayers[i].size();
}
#if DEBUG_MODE
std::cout << "Data size in elements: " << dataSize << std::endl;
std::cout << "Data size in bytes: " << dataSize * sizeof(double) << std::endl;
#endif
if (!checkFileExistence(fileNameData)) {
#if DEBUG_MODE
std::cerr << "No neural network data found, randomizing...\n";
#endif
if (randomizeData()) {
#if DEBUG_MODE
std::cerr << "Data randomization error\n";
#endif
return 1;
}
} else {
#if DEBUG_MODE
std::cout << "Parsing neural network data...\n";
#endif
if (parseData()) {
#if DEBUG_MODE
std::cerr << "Data parsing error\n";
#endif
return 1;
}
}
#if DEBUG_MODE
std::cout << "Network initiated\n";
#endif
return 0;
}
bool NN::checkFileExistence(std::string fileName) {
std::ifstream fileCheckStream(fileName);
return fileCheckStream.good();
}
bool NN::randomizeData() {
srand(static_cast < unsigned int > (time(NULL)));
for (size_t i = 0; i < weightLayers.size(); i++) {
size_t n_in = neuronLayers[i].size();
size_t n_out = neuronLayers[i + 1].size();
double limit = sqrt(6.0 / (n_in + n_out));
for (size_t j = 0; j < weightLayers[i].size(); j++) {
weightLayers[i][j] = (static_cast < double > (std::rand()) / RAND_MAX) * 2 * limit - limit;
}
for (size_t j = 0; j < biasLayers[i].size(); j++) {
biasLayers[i][j] = 0.0;
}
}
saveData();
return 0;
}
bool NN::parseData() {
std::ifstream parseDataStream(fileNameData, std::ios::binary);
if (!parseDataStream) {
return 1;
}
std::vector < double > parsedData(dataSize);
parseDataStream.read(reinterpret_cast < char * > (parsedData.data()), dataSize * sizeof(double));
parseDataStream.close();
size_t parsedDataIndex = 0;
for (size_t i = 0; i < weightLayers.size(); i++) {
for (size_t j = 0; j < biasLayers[i].size(); j++) {
if (parsedDataIndex >= parsedData.size()) {
return 1;
}
biasLayers[i][j] = parsedData[parsedDataIndex];
parsedDataIndex++;
}
for (size_t j = 0; j < weightLayers[i].size(); j++) {
if (parsedDataIndex >= parsedData.size()) {
return 1;
}
weightLayers[i][j] = parsedData[parsedDataIndex];
parsedDataIndex++;
}
}
return 0;
}
bool NN::saveData() {
std::ofstream saveDataStream(fileNameData, std::ios::binary);
if (!saveDataStream) {
return 1;
}
std::vector < double > saveData;
for (size_t i = 0; i < weightLayers.size(); i++) {
for (size_t j = 0; j < biasLayers[i].size(); j++) {
saveData.push_back(biasLayers[i][j]);
}
for (size_t j = 0; j < weightLayers[i].size(); j++) {
saveData.push_back(weightLayers[i][j]);
}
}
saveDataStream.write(reinterpret_cast <
const char * > (saveData.data()), saveData.size() * sizeof(double));
saveDataStream.close();
if (!checkFileExistence(fileNameData)) {
return 1;
}
return 0;
}
bool NN::propagateForward() {
for (size_t i = 1; i < neuronLayers.size(); i++) //For all layers except input layer 0
{
for (size_t j = 0; j < neuronLayers[i].size(); j++) //For all neurons
{
double activationSum = 0;
for (size_t k = 0; k < neuronLayers[i - 1].size(); k++) //For all neurons connected to neuron
{
activationSum = activationSum + neuronLayers[i - 1][k] * weightLayers[i - 1][k + j * neuronLayers[i - 1].size()];
}
neuronLayers[i][j] = sigmoid(biasLayers[i - 1][j] + activationSum);
neuronLayersSums[i][j] = biasLayers[i - 1][j] + activationSum;
}
}
return 0;
}
bool NN::clearConsole() {
#ifdef _WIN32
system("cls"); // Windows
#else
system("clear"); // Linux/macOS
#endif
return 0;
}
bool NN::recognize(std::string fileNameRecognize, unsigned int& maxIndex, double& confidence) {
std::ifstream fileRecognizeStream(fileNameRecognize);
if (!fileRecognizeStream) {
#if DEBUG_MODE
std::cerr << fileNameRecognize << " not found\n";
#endif
return 1;
}
unsigned char dataChar;
neuronLayers[0].resize(0); //To use push_back
while (fileRecognizeStream.read(reinterpret_cast < char * > ( & dataChar), sizeof(dataChar))) {
neuronLayers[0].push_back((static_cast < unsigned int > (dataChar)) / 255.0); //Normalize 0 - 255 to 0.0 - 1.0
}
fileRecognizeStream.close();
propagateForward();
confidence = neuronLayers[neuronLayers.size() - 1][0]; //State solution
maxIndex = 0;
for (int i = 1; i < neuronLayers[neuronLayers.size() - 1].size(); ++i) {
if (neuronLayers[neuronLayers.size() - 1][i] > confidence) {
confidence = neuronLayers[neuronLayers.size() - 1][i];
maxIndex = i;
}
}
return 0;
}
bool NN::train(std::string fileNameTrainData, std::string fileNameTrainLabels, double learningRate) {
std::ifstream trainLabelsStream(fileNameTrainLabels);
if (!trainLabelsStream) {
#if DEBUG_MODE
std::cerr << fileNameTrainLabels << " not found\n";
#endif
return 1;
}
std::ifstream trainDataStream(fileNameTrainData);
if (!trainDataStream) {
#if DEBUG_MODE
std::cerr << fileNameTrainLabels << " not found\n";
#endif
return 1;
}
trainLabelsStream.seekg(0, std::ios::end);
size_t trainSetSize = trainLabelsStream.tellg(); //Input elements
trainLabelsStream.seekg(0, std::ios::beg);
for (size_t i = 0; i < trainSetSize; i++) { //For all training elements
unsigned int label = static_cast < unsigned int > (static_cast < unsigned char > (trainLabelsStream.get())); //Read current label
for (size_t j = 0; j < neuronLayers[0].size(); j++) { //Read current data
neuronLayers[0][j] = (static_cast < unsigned int > (static_cast < unsigned char > (trainDataStream.get()))) / 255.0;
}
propagateForward();
propagateBackward(label, learningRate);
#if DEBUG_MODE
if (i % (trainSetSize / 1000) == 0) //Progress info
{
clearConsole();
std::cout << std::fixed << std::setprecision(1) << "Training progress: " << ((i * 100.0) / trainSetSize) << "%\n";
}
#endif
}
trainLabelsStream.close();
trainDataStream.close();
saveData();
clearConsole();
#if DEBUG_MODE
std::cout << "Training completed\n";
#endif
return 0;
}
bool NN::propagateBackward(unsigned int label, double learningRate) {
for (size_t i = neuronLayers.size() - 1; i >= 1; i--) { // For every layer
if (i == neuronLayers.size() - 1) { // Output layer
for (size_t j = 0; j < neuronLayers[i].size(); j++) { // For every neuron
double error;
if (j == label) {
error = MeanSquaredErrorDerivative(neuronLayers[i][j], 1.0) * sigmoidDerivative(neuronLayersSums[i][j]);
} else {
error = MeanSquaredErrorDerivative(neuronLayers[i][j], 0.0) * sigmoidDerivative(neuronLayersSums[i][j]);
}
neuronLayers[i][j] = error; //Propagating error in default structure
for (size_t k = 0; k < neuronLayers[i - 1].size(); k++) {
weightLayersGradient[i - 1][k + j * neuronLayers[i - 1].size()] = error * neuronLayers[i - 1][k];
}
biasLayersGradient[i - 1][j] = error;
}
} else { // Hidden layers
for (size_t j = 0; j < neuronLayers[i].size(); j++) { // For every neuron
double backActivation = 0.0; // Calculate the error of a neuron in this layer based on the weights and the error in the next layer (backpropagation)
for (size_t k = 0; k < neuronLayers[i + 1].size(); k++) {
backActivation = backActivation + neuronLayers[i + 1][k] * weightLayers[i][k + j + k * neuronLayers[i].size()];
}
double error = sigmoidDerivative(neuronLayersSums[i][j]) * backActivation;
neuronLayers[i][j] = error;
for (size_t k = 0; k < neuronLayers[i - 1].size(); k++) {
weightLayersGradient[i - 1][k + j * neuronLayers[i - 1].size()] = error * neuronLayers[i - 1][k]; // The cost-weight gradient is the error * the activation of the previous layer, which is quite inconvenient here due to the weight indexing, which favors forward propagation
}
biasLayersGradient[i - 1][j] = error;
}
}
}
for (size_t i = 0; i < biasLayers.size(); i++) {//Update weights and biases
for (size_t j = 0; j < biasLayers[i].size(); j++) {
biasLayers[i][j] = biasLayers[i][j] - learningRate * biasLayersGradient[i][j];
}
}
for (size_t i = 0; i < weightLayers.size(); i++) {
for (size_t j = 0; j < weightLayers[i].size(); j++) {
weightLayers[i][j] = weightLayers[i][j] - learningRate * weightLayersGradient[i][j];
}
}
return 0;
}