-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNetwork.cpp
More file actions
348 lines (213 loc) · 9.07 KB
/
Copy pathNeuralNetwork.cpp
File metadata and controls
348 lines (213 loc) · 9.07 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
343
344
345
346
347
348
#include "NeuralNetwork.h"
#include <cmath>
#include <eigen3/Eigen/Dense>
#include <vector>
#include <fstream>
namespace EigenLibrary
{
HiddenLayer::HiddenLayer(int inputSize, int outputSize)
{
double limit = sqrt(6.0 / (inputSize + outputSize));
m_weights = Eigen::MatrixXd::Random(outputSize, inputSize) * limit;
m_biases = Eigen::RowVectorXd::Zero(outputSize);
m_outputs = Eigen::VectorXd::Zero(outputSize);
}
Eigen::VectorXd HiddenLayer::Forward(const Eigen::VectorXd& input)
{
m_inputs = input;
// Linear transformation
Eigen::VectorXd linearOutput = m_weights * input + m_biases.transpose();
ActivationFunction activationFunction;
m_outputs = activationFunction.Sigmoid(linearOutput);
return m_outputs;
}
Eigen::VectorXd HiddenLayer::Backward(const Eigen::VectorXd& gradOutput, double learningRate)
{
Eigen::VectorXd sigmoidDeriv = ActivationFunction().SigmoidDerivate(m_outputs);
// DELTA = gradOutput * sigmoidDeriv
Eigen::VectorXd gradDelta = gradOutput.array() * sigmoidDeriv.array();
// gradInput = weights * gradDelta
Eigen::VectorXd gradInput = m_weights.transpose() * gradDelta;
// Update weights and biases
m_weights -= learningRate * gradDelta * m_inputs.transpose();
m_biases -= learningRate * gradDelta.transpose();
return gradInput;
}
Eigen ::VectorXd OutputLayer::Forward(const Eigen::VectorXd& input)
{
m_inputs = input;
Eigen::VectorXd linearOutput = m_weights.transpose() * input + m_biases.transpose();
ActivationFunction activationFunction;
m_outputs = activationFunction.Sigmoid(linearOutput);
return m_outputs;
}
Eigen::VectorXd OutputLayer::Backward(const Eigen::VectorXd& gradOutput, double learningRate)
{
Eigen::VectorXd sigmoidDeriv = ActivationFunction().SigmoidDerivate(m_outputs);
Eigen::VectorXd gradDelta = gradOutput.array() * sigmoidDeriv.array();
Eigen::VectorXd gradInput = m_weights * gradDelta;
m_weights -= learningRate * m_inputs * gradDelta.transpose();
m_biases -= learningRate * gradDelta.transpose();
return gradInput;
}
NeuralNetwork::NeuralNetwork(const vector<int>& layerSizes, double learningRate) : m_LearningRate(learningRate)
{
if (layerSizes.size() <= 2)
throw invalid_argument(" Required at least 3 (input+ output)");
// layer n-2
for (size_t i = 0; i + 2 < layerSizes.size(); i++)
m_hiddenLayers.push_back(make_unique<HiddenLayer>(layerSizes[i], layerSizes[i + 1]));
int lastHiddenLayerOutputSize = layerSizes[layerSizes.size() - 2];
int outputLayerOutputSize = layerSizes[layerSizes.size() - 1];
m_outputLayer = make_unique<OutputLayer>(lastHiddenLayerOutputSize, outputLayerOutputSize);
//last input
layerSizes.size() - 1 >= 0 ? outputLayerOutputSize = layerSizes[layerSizes.size() - 1] : throw invalid_argument("Invalid layer size");
}
Eigen::VectorXd NeuralNetwork::Forward(const Eigen::VectorXd& input)
{
Eigen::VectorXd output = input;
for (const auto& hiddenLayer : m_hiddenLayers)
output = hiddenLayer->Forward(output);
output = m_outputLayer->Forward(output);
return output;
}
// lost function = (output - target) ^ 2
static double MeanSquaredError(const Eigen::VectorXd& output, const Eigen::VectorXd& target)
{
return(output - target).squaredNorm() / output.size(); // squarednorm fonksiyonu vektörün karelerinin toplamýný verir, yani (output - target) ^ 2'nin toplamýný verir. output.size() ile bölerek ortalama kare hatasýný hesaplýyoruz. bu fonksiyon, modelin tahminlerinin ne kadar doðru olduðunu ölçmek için kullanýlýr. Tahminler hedef deðerlerden ne kadar uzak olduðunu gösterir. MSE deðeri ne kadar küçükse, modelin tahminleri o kadar doðru olur.
}
//MSE Gradient: dL/dy = 2/n * (output - target)
static Eigen::VectorXd MeanSquaredErrorGradient(const Eigen::VectorXd& output, const Eigen::VectorXd& target)
{
return 2.0 * (output - target) / output.size(); // MSE'nin çýktýya göre türevidir. Bu, modelin tahminlerinin hedef deðerlerden ne kadar uzak olduðunu gösterir. Tahminler hedef deðerlerden ne kadar uzaksa, gradyan o kadar büyük olur, bu da modelin aðýrlýklarýný daha hýzlý güncellemesine neden olur.
}
void NeuralNetwork::Train(const vector<Eigen::VectorXd>& inputs, const vector<Eigen::VectorXd>& targets, int epochs)
{
if (inputs.size() != targets.size())
throw invalid_argument("Number of inputs and targets must be the same.");
try
{
double epochLoss = 0.0;
for (int epoch = 0; epoch < epochs; epoch++)
{
for (size_t i = 0; i < inputs.size(); i++)
{
//forward pass
Eigen::VectorXd output = Forward(inputs[i]);
//loss calculation
double mseLoss = MeanSquaredError(output, targets[i]);
epochLoss += mseLoss;
Eigen::VectorXd gradOutput = MeanSquaredErrorGradient(output, targets[i]);
// Backward pass
gradOutput = m_outputLayer->Backward(gradOutput, m_LearningRate);
//hidden layers backward pass
for (int j = static_cast<int>(m_hiddenLayers.size()) - 1; j >= 0; j--)
{
gradOutput = m_hiddenLayers[j]->Backward(gradOutput, m_LearningRate);
}
}
if (epochs + 1 % 500 == 0)
cout << "Epoch[" << (epochs + 1) << "/500] Loss:" << epochLoss / inputs.size() << endl;
}
}
catch (const std::exception& e)
{
cerr << "Error during training: " << e.what() << endl;
}
}
Eigen::VectorXd NeuralNetwork::Predict(const Eigen::VectorXd& features) const
{
Eigen::VectorXd current = features;
for ( const auto& layer : m_hiddenLayers)
{
current = layer->Forward(current);
}
current = m_outputLayer->Forward(current);
return current;
}
Eigen::MatrixXd NeuralNetwork::Evaluate(const vector<Eigen::VectorXd>& inputs, const vector<Eigen::VectorXd>& targets) const
{
double totalMse = { 0.0 };
for (size_t i = 0; i < inputs.size(); i++)
{
Eigen::VectorXd output = Predict(inputs[i]);
totalMse += MeanSquaredError(output, targets[i]);
}
double x = static_cast<double>(inputs.size());
//Result on Matrix
Eigen::MatrixXd result(1, 2);
result(0, 0)= totalMse / static_cast<double>(inputs.size());
std::cout << "Evulate Result -> Avarage MSE" << result(0, 0)<<std::endl;
return result;
}
void NeuralNetwork::SaveModel(const string& filename) const
{
ofstream file(filename,ios::binary);
if (!file.is_open())
{
throw runtime_error("File Error on Saving" + filename);
}
int hiddenNum = static_cast<int>(m_hiddenLayers.size());
file.write(reinterpret_cast<const char*>(&hiddenNum), (sizeof(int)));
auto writeMatrix = [&](const Eigen::MatrixXd& matrix) //Lambda Fonks
{
int rows = matrix.rows(), cols = matrix.cols();
file.write(reinterpret_cast<const char*> (&rows), sizeof(int));
file.write(reinterpret_cast<const char*>(&cols), (sizeof(int)));
file.write(reinterpret_cast<const char*>(matrix.data()), rows * cols * sizeof(double));
};
auto writeRowVector = [&](const Eigen::RowVectorXd& vector)
{
int cols = vector.cols();
file.write(reinterpret_cast<const char*> (&cols), sizeof(int));
file.write(reinterpret_cast<const char*>(vector.data()), cols * sizeof(double));
};
for (const auto& layer : m_hiddenLayers)
{
writeMatrix(layer->GetWieghts());
writeRowVector(layer->GetBiases());
}
writeMatrix(m_outputLayer->GetWieghts());
writeRowVector(m_outputLayer->GetBiases());
cout << "Model Saved" + filename;
}
void NeuralNetwork::LoadModel(const string& filename)
{
ifstream file(filename, ios::binary);
if (!file.is_open())
throw runtime_error("Erorr File" + filename);
int hiddenNum = { 0 };
file.read(reinterpret_cast<char*> (&hiddenNum), sizeof(int));
auto readMatrix = [&]()
{
int rows, cols;
file.read(reinterpret_cast<char*>(&rows), sizeof(int));
file.read(reinterpret_cast<char*>(&cols), sizeof(int));
Eigen::MatrixXd matrix(rows, cols);
file.read(reinterpret_cast<char*>(matrix.data()), rows * cols * sizeof(double));
return matrix;
};
auto readRowVector = [&]()
{
int cols{};
file.read(reinterpret_cast<char*>(&cols), sizeof(int));
Eigen::RowVectorXd vector(cols);
file.read(reinterpret_cast<char*>(vector.data()), cols * sizeof(double));
return vector;
};
if (static_cast<int>(m_hiddenLayers.size()) != hiddenNum)
throw runtime_error("Architecture Layer does not match");
for (auto& layer : m_hiddenLayers)
{
Eigen::MatrixXd w = readMatrix();
Eigen::RowVectorXd b = readRowVector();
layer->SetWeights(w);
layer->SetBiases(b);
}
Eigen::MatrixXd outputLayerWeight = readMatrix();
Eigen::MatrixXd outputLayerBias = readRowVector();
m_outputLayer->SetWeights(outputLayerWeight);
m_outputLayer->SetBiases(outputLayerBias);
cout << "Model Loaded Congrats" << filename << endl;
}
}