-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguration.cpp
86 lines (73 loc) · 2.27 KB
/
Configuration.cpp
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
#include <cassert>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cctype>
#include "Configuration.h"
#include "LRModel.h"
#include "BPNNModel.h"
bool Configuration::Load(const std::string& filePath)
{
std::ifstream fin(filePath.c_str(), std::ifstream::binary);
if (!fin)
return false;
std::vector<float> config_model_param;
std::vector<std::string> config_file_param;
int count = 0;
while (!fin.eof())
{
//float num;
std::string s;
getline(fin, s);
size_t i = 0;
for (; i < s.length(); i++) { if (s[i] == ':') break; }
// remove the first chars, which aren't digits
s = s.substr(i+1, s.length() - i);
s.erase(remove_if(s.begin(), s.end(), ::isspace), s.end());
if (count < 5)
{
// convert the remaining text to an integer
config_model_param.push_back(std::stof(s.c_str()));
}
else
{
config_file_param.push_back(s);
}
++count;
}
fin.close();
this->feature_number = config_model_param[0];
this->category_number = config_model_param[1];
this->batchSize = config_model_param[2];
this->learning_rate = config_model_param[3];
this->train_epoch = config_model_param[4];
this->type = config_file_param[0].compare("LR") == 0 ? ModelType::ModelType_LR : ModelType::ModelType_BPNN;
this->modelSavePath = config_file_param[1];
std::cout << "Config data loaded successfully!" << std::endl;
return true;
}
bool Configuration::Save(const std::string& filePath)
{
return true;
}
std::unique_ptr<Model> Configuration::CreateModel()
{
return std::unique_ptr<Model>(new LRModel(*this));
}
std::unique_ptr<Configuration> Configuration::CreateConfiguration(const std::string& filePath)
{
Configuration basicConfig;
basicConfig.Load(filePath);
std::unique_ptr<Configuration> config = NULL;
if (basicConfig.type == ModelType::ModelType_LR)
{
config = std::unique_ptr<Configuration>(new Configuration());
}
else if (basicConfig.type == ModelType::ModelType_BPNN)
{
config = std::unique_ptr<Configuration>(new BPNNConfiguration());
}
assert(config != NULL);
config->Load(filePath);
return config;
}