-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
195 lines (146 loc) · 5.8 KB
/
main.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
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
#include <iostream>
#include <MLP.h>
#include <string>
#include <vulkan_init.h>
#include <classification_trainer.h>
#include <fstream>
#include <sstream>
int find_arg(char **argv, int argc, const std::string& arg_name){
for(int i = 0;i<argc;i++){
if(std::string(argv[i]).find(arg_name) != std::string::npos){
return i;
}
}
return -1;
}
void get_dataset(char **argv, int argc, std::string &data_path, std::string &labels_path, bool is_train=false) {
std::string data_arg;
std::string labels_arg;
if(is_train){
data_arg = "--train_data_path";
labels_arg = "--train_labels_path";
} else {
data_arg = "--val_data_path";
labels_arg = "--val_labels_path";
}
int data_pos = find_arg(argv, argc, data_arg);
int labels_pos = find_arg(argv, argc, labels_arg);
if(data_pos == -1){
std::stringstream ss;
ss<<"can't find parameter "<<data_arg;
throw std::runtime_error(ss.str());
}
if(labels_pos == -1){
std::stringstream ss;
ss<<"can't find parameter "<<labels_arg;
throw std::runtime_error(ss.str());
}
data_path = std::string(argv[data_pos+1]);
labels_path = std::string(argv[labels_pos+1]);
}
void get_layers(char** argv, int argc, std::vector<uint32_t>& layers){
int pos = find_arg(argv, argc, "--layers=");
if(pos == -1){
throw std::runtime_error("can't fine --layers= parameter");
}
int offset = 9; // len of --layers=
std::string param(argv[pos]);
int next_delim = param.find(',', offset);
while(offset != std::string::npos){
layers.push_back(std::stoul(param.substr(offset, next_delim-offset)));
if(next_delim != std::string::npos)offset = next_delim + 1;
else offset = next_delim;
next_delim = param.find(',', offset);
}
}
void get_activations(char** argv, int argc, std::vector<std::string>& activations){
int pos = find_arg(argv, argc, "--activations=");
if(pos == -1){
throw std::runtime_error("can't fine --activations= parameter");
}
int offset = 14; // len of activations or --activations=
std::string param(argv[pos]);
int next_delim = param.find(',', offset);
while(offset != std::string::npos){
activations.push_back(param.substr(offset, next_delim-offset));
if(next_delim != std::string::npos)offset = next_delim + 1;
else offset = next_delim;
next_delim = param.find(',', offset);
}
}
void get_num_parameter(char **argv, int argc, const std::string& param_name, int ¶m) {
int pos = find_arg(argv, argc, param_name);
if(pos == -1){
std::stringstream ss;
ss<<"can't find parameter "<<param_name;
throw std::runtime_error(ss.str());
}
param = std::stoi(std::string(argv[pos + 1]));
}
void get_num_parameter(char **argv, int argc, const std::string& param_name, float ¶m) {
int pos = find_arg(argv, argc, param_name);
if(pos == -1){
std::stringstream ss;
ss<<"can't find parameter "<<param_name;
throw std::runtime_error(ss.str());
}
param = std::stof(std::string(argv[pos + 1]));
}
void read_data(const std::string& data_path, uint32_t data_size, uint32_t data_dim, std::vector<std::vector<float>>& data){
std::ifstream data_input(data_path);
if(!data_input.is_open())throw std::runtime_error("can't read data");
data = std::move(std::vector(data_size, std::vector<float>(data_dim)));
for(int i = 0;i<data_size;i++){
for(int j = 0;j<data_dim;j++){
data_input>>data[i][j];
}
}
}
int main(int argc, char** argv) {
std::vector<uint32_t> layers;
get_layers(argv, argc, layers);
std::vector<std::string> activations;
get_activations(argv, argc, activations);
int train_dataset_size;
std::string train_data_path;
std::string train_labels_path;
int x_dim;
int y_dim;
get_num_parameter(argv, argc, "--x_dim", x_dim);
get_num_parameter(argv, argc, "--y_dim", y_dim);
get_num_parameter(argv, argc, "--train_dataset_size", train_dataset_size);
get_dataset(argv, argc, train_data_path, train_labels_path, true);
int val_dataset_size;
std::string val_data_path;
std::string val_labels_path;
get_num_parameter(argv, argc, "--val_dataset_size", val_dataset_size);
get_dataset(argv, argc, val_data_path, val_labels_path);
int batch_size;
get_num_parameter(argv, argc, "--batch_size", batch_size);
int optimization_steps;
get_num_parameter(argv, argc, "--optimization_steps", optimization_steps);
float rl;
get_num_parameter(argv, argc, "--learning_rate", rl);
MLP mlp = MLP(x_dim, batch_size, layers, activations);
mlp.forward_initialize();
std::vector<example> train_dataset(train_dataset_size);
std::vector<std::vector<float>> train_x;
std::vector<std::vector<float>> val_x;
std::vector<std::vector<float>> train_y;
std::vector<std::vector<float>> val_y;
read_data(train_data_path, train_dataset_size, x_dim, train_x);
read_data(train_labels_path, train_dataset_size, y_dim, train_y);
read_data(val_data_path, val_dataset_size, x_dim, val_x);
read_data(val_labels_path, val_dataset_size, y_dim, val_y);
for(int i = 0;i<train_dataset_size;i++){
train_dataset[i] = std::move(example{.x=train_x[i], .y=train_y[i]});
}
std::cout<<"accuracy before training: "<<mlp.evaluate(val_x, val_y)<<std::endl;
std::unordered_map<std::string, float> optimizer_params;
optimizer_params["learning_rate"] = rl;
ClassificationTrainer trainer = ClassificationTrainer(&mlp, train_dataset, optimizer_params);
std::vector<float> loss_history;
trainer.train(optimization_steps, loss_history, 100);
std::cout<<"accuracy after training: "<<mlp.evaluate(val_x, val_y)<<std::endl;
return 0;
}