forked from pgomoluch/simple-nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cxx
90 lines (78 loc) · 1.9 KB
/
config.cxx
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
#include "config.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <map>
using namespace std;
const char comment_sign = '#';
const string iterations_key = "iterations";
const string learning_rate_key = "learning_rate";
const string features_train_key = "features_train";
const string labels_train_key = "labels_train";
const string hidden_layers_key = "hidden_layers";
const string network_file_key = "network_file";
bool Config::load(const char *path)
{
ifstream file(path);
if (!file)
{
file.close();
return false;
}
map<string, string> params;
string line;
while (getline(file, line))
{
size_t comment_index = line.find("#");
if (comment_index < line.size())
line.erase(comment_index);
if (line.size() == 0)
continue;
size_t equals_index = line.find("=");
if (equals_index < line.size())
line[equals_index] = ' ';
parse_key_value(line);
}
file.close();
return true;
}
bool Config::parse_key_value(const string &line)
{
stringstream stream(line);
string key;
stream >> key;
if (key == iterations_key)
{
stream >> iterations;
}
else if (key == learning_rate_key)
{
stream >> learning_rate;
}
else if (key == features_train_key)
{
stream >> features_train;
}
else if (key == labels_train_key)
{
stream >> labels_train;
}
else if (key == network_file_key)
{
stream >> network_file;
}
else if (key == hidden_layers_key)
{
vector<unsigned> layers;
unsigned u;
while (stream >> u)
{
layers.push_back(u);
stream >> skipws;
if (stream.peek() == ',')
stream.get();
}
hidden_layers = layers;
}
return true;
}