-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNN.cpp
More file actions
71 lines (57 loc) · 2.21 KB
/
Copy pathNN.cpp
File metadata and controls
71 lines (57 loc) · 2.21 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
#include <iostream>
#include <vector>
#include "NeuralStructures/NeuralNetwork.h"
#include "DatasetManagement/DatasetLoader.cpp" // Ensure the path is correct
int main() {
int ep = 8000;
DatasetLoader loader;
loader.loadFromCSV("archive/IRIS.csv", 4, 3);
loader.shuffle();
// Split the data
auto testSet = loader.getTestBatch(0.2);
auto trainingSet = loader.getData(); // This returns std::vector<DataInstance>
std::vector<int> topology = {4, 8, 3};
NeuralNetwork nn(topology, 0.01);
std::cout << "--- Starting Training (Iris-Dataset) ---" << std::endl;
for (int epoch = 0; epoch < ep; epoch++) {
//loader.shuffle();
for (const auto& data : trainingSet) {
nn.forward(data.inputs); // Using 'inputs' from DataInstance
nn.backward(data.targets); // Using 'targets' from DataInstance
}
if (epoch % 1000 == 0) {
std::cout << "Epoch " << epoch << " complete...." << std::endl;
}
}
std::cout << "\n--- Testing Phase ---" << std::endl;
int correct = 0;
for (const auto& data : testSet) {
nn.forward(data.inputs);
// The output layer is the last matrix in NeuronLayer
Matrix output = nn.NeuronLayer.back();
int predictedLabel = 0;
double maxVal = -1.0;
int actualLabel = 0;
// Finding the index of the highest output (The Prediction)
for (int j = 0; j < output.getRows(); j++) {
if (output.get(j, 0) > maxVal) {
maxVal = output.get(j, 0);
predictedLabel = j;
}
}
// Find the index of the 1.0 in targets (The Truth)
for (int j = 0; j < data.targets.size(); j++) {
if (data.targets[j] == 1.0) {
actualLabel = j;
break;
}
}
if ((predictedLabel == actualLabel)) {
correct++;
}
}
double accuracy = (double)correct / testSet.size() * 100.0;
std::cout << "Final Test Accuracy: " << accuracy << "% ("
<< correct << "/" << testSet.size() << " correct)" << std::endl;
return 0;
}