-
Notifications
You must be signed in to change notification settings - Fork 0
/
configReader.h
134 lines (111 loc) · 3.49 KB
/
configReader.h
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
struct NeuralNetworkConfig {
std::vector<std::vector<double>> inputLayerWeights;
std::vector<std::vector<std::vector<double>>> hiddenLayerWeights;
std::vector<std::vector<double>> outputLayerWeights;
std::vector<double> inputData;
// Constructor
NeuralNetworkConfig(const std::string& filename) {
read(filename);
}
void read(const std::string& filename);
void print();
int NHiddenLayers();
int NNeuronsInLayer()
{
return inputLayerWeights[0].size();
}
};
void NeuralNetworkConfig::read(const std::string& filename) {
std::ifstream file(filename);
std::string line;
enum class Section { IL, HL, OL, ID, None };
Section currentSection = Section::None;
int hiddenLayerCount = 0;
while (std::getline(file, line)) {
if (line.empty())
continue;
if (line == "IL")
currentSection = Section::IL;
else if (line == "OL")
currentSection = Section::OL;
else if (line == "ID")
currentSection = Section::ID;
else if (line.substr(0, 2) == "HL") {
currentSection = Section::HL;
hiddenLayerCount++;
hiddenLayerWeights.emplace_back(); // Add a new hidden layer
continue;
}
std::istringstream iss(line);
std::vector<double> values;
double value;
if (currentSection == Section::ID) {
while (iss >> value) {
values.push_back(value);
if (iss.peek() == ',')
iss.ignore();
}
inputData = values;
} else {
while (iss >> value) {
values.push_back(value);
if (iss.peek() == ',')
iss.ignore();
}
switch (currentSection) {
case Section::IL:
inputLayerWeights.push_back(values);
break;
case Section::HL:
hiddenLayerWeights[hiddenLayerCount - 1].push_back(values);
break;
case Section::OL:
outputLayerWeights.push_back(values);
break;
default:
break;
}
}
}
//remove the first empy vector in inputLayerWeights
inputLayerWeights.erase(inputLayerWeights.begin());
}
void NeuralNetworkConfig::print() {
std::cout << "Input Layer Weights:\n";
for (const auto& layer : inputLayerWeights) {
for (const auto& weight : layer) {
std::cout << weight << " ";
}
std::cout << "\n";
}
std::cout << "\nHidden Layer Weights:\n";
for (const auto& layer : hiddenLayerWeights) {
for (const auto& neuron : layer) {
for (const auto& weight : neuron) {
std::cout << weight << " ";
}
std::cout << "\n";
}
std::cout << "\n";
}
std::cout << "\nOutput Layer Weights:\n";
for (const auto& layer : outputLayerWeights) {
for (const auto& weight : layer) {
std::cout << weight << " ";
}
std::cout << "\n";
}
std::cout << "\nInput Data:\n";
for (const auto& data : inputData) {
std::cout << data << " ";
}
std::cout << "\n";
}
int NeuralNetworkConfig::NHiddenLayers() {
return hiddenLayerWeights.size();
}