-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
300 lines (249 loc) · 8.31 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include<iostream>
#include<string>
#include <vector>
#include "lib/include/matrixOperations.h"
#include "lib/include/network.h"
#include "lib/include/dense.h"
#include "lib/include/activations.h"
#include "lib/include/mnist.h"
void trainMnist(NETWORK &network, int epochs, int imageNum, int batchNum, int batchSize, double rate, bool verbose){
VECTOR2D trainData;
VECTOR2D trainResults;
VECTOR2D runData;
VECTOR2D runResults;
try{
VECTOR2D mnistTrainData;
VECTOR2D mnistTrainResults;
VECTOR2D mnistRunData;
VECTOR2D mnistRunResults;
ReadMNISTimage("datasets/mnist/training/train-images.idx3-ubyte",imageNum,784,mnistTrainData);
ReadMNISTlabel("datasets/mnist/training/train-labels.idx1-ubyte",imageNum,1,mnistTrainResults);
ReadMNISTimage("datasets/mnist/testing/t10k-images.idx3-ubyte",10,784,mnistRunData);
ReadMNISTlabel("datasets/mnist/testing/t10k-labels.idx1-ubyte",10,1,mnistRunResults);
std::cout << "Data Loading Complete!\n";
trainData.resize(batchSize);
for(int j = 0; j < batchSize; j++){
trainData[j] = mnistTrainData[j + batchNum*batchSize];
}
trainResults.resize(batchSize);
for(int i = 0; i < batchSize; i++){
trainResults[i].resize(10, 0.0);
trainResults[i][mnistTrainResults[i + batchNum*batchSize][0]] = 1.0;
}
runData = mnistRunData;
runResults.resize(mnistRunResults.size());
for(int i = 0; i < runResults.size(); i++){
runResults[i].resize(10, 0.0);
runResults[i][mnistRunResults[i][0]] = 1.0;
}
std::cout << "Starting training\n";
train(network, trainData, trainResults, 0.001, rate, epochs, verbose);
std::cout << "Training Complete\n";
//run(network, trainData, trainResults);
}
catch(std::invalid_argument const& ex){
std::cout << ex.what() << '\n';
}
}
void runMnist(NETWORK network, int imageNum){
VECTOR2D trainData;
VECTOR2D trainResults;
VECTOR2D runData;
VECTOR2D runResults;
try{
VECTOR2D mnistTrainData;
VECTOR2D mnistTrainResults;
VECTOR2D mnistRunData;
VECTOR2D mnistRunResults;
ReadMNISTimage("datasets/mnist/training/train-images.idx3-ubyte",imageNum,784,mnistTrainData);
ReadMNISTlabel("datasets/mnist/training/train-labels.idx1-ubyte",imageNum,1,mnistTrainResults);
ReadMNISTimage("datasets/mnist/testing/t10k-images.idx3-ubyte",imageNum,784,mnistRunData);
ReadMNISTlabel("datasets/mnist/testing/t10k-labels.idx1-ubyte",imageNum,1,mnistRunResults);
trainData = mnistTrainData;
trainResults.resize(mnistTrainResults.size());
for(int i = 0; i < trainResults.size(); i++){
trainResults[i].resize(10, 0.0);
trainResults[i][mnistTrainResults[i][0]] = 1.0;
}
runData = mnistRunData;
runResults.resize(mnistRunResults.size());
for(int i = 0; i < runResults.size(); i++){
runResults[i].resize(10, 0.0);
runResults[i][mnistRunResults[i][0]] = 1.0;
}
//run(network, runData, runResults);
run(network, trainData, trainResults);
}
catch(std::invalid_argument const& ex){
std::cout << ex.what() << '\n';
}
}
void storeNetwork(std::string filename, NETWORK &network){
filename.push_back('.');
//Saving the weights
std::ofstream file;
std::string wFile = filename;
wFile.push_back('w');
//std::cout << wFile;
file.open(wFile);
int sx;
int sy;
for(int i = 0; i < network.size(); i++){
sx = network.at(i)->weights.size();
if(sx > 0){
sy = network.at(i)->weights.at(0).size();
for(int x = 0; x < sx; x++){
for(int y = 0; y < sy; y++){
file << network[i]->weights.at(x).at(y) << " ";
}
file << "\n";
}
}
file << "#\n";
}
file.close();
//Saving the biases
std::string bFile = filename;
bFile.push_back('b');
file.open(bFile);
//int sx;
//int sy;
for(int i = 0; i < network.size(); i++){
sx = network.at(i)->bias.size();
if(sx > 0){
sy = network.at(i)->bias.at(0).size();
for(int x = 0; x < sx; x++){
for(int y = 0; y < sy; y++){
file << network[i]->bias.at(x).at(y) << " ";
}
file << "\n";
}
}
file << "#\n";
}
file.close();
}
std::vector<std::string> split(std::string s, std::string delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string token;
std::vector<std::string> res;
while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos) {
token = s.substr (pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back (token);
}
res.push_back (s.substr (pos_start));
return res;
}
void readNetwork(std::string filename, NETWORK &network){
std::ifstream file;
filename.push_back('.');
//Read Weights
std::string wFile = filename;
wFile.push_back('w');
file.open(wFile);
int size = network.size();
network.resize(size);
std::string delimiter = " ";
std::vector<std::string> lineV;
std::string line;
int n = 0;
int x = 0;
while(getline(file, line)){
if(!line.compare("#")){
n++;
x = 0;
}
lineV = split(line, delimiter);
for(int i = 0; i < lineV.size(); i++){
if(!lineV[i].compare("")){
x++;
}
else{
if(line.compare("#") && lineV.size() > 1){
double num = std::stod(lineV.at(i));
network.at(n)->weights.at(x).at(i) = num;
}
}
}
}
file.close();
std::ifstream file1;
//Read bias
std::string bFile = filename;
bFile.push_back('b');
line = "";
file1.open(bFile);
//int size = network.size();
network.resize(size);
//std::string delimiter = " ";
//std::vector<std::string> lineV;
//std::string line;
n = 0;
x = 0;
while(getline(file1, line)){
if(!line.compare("#")){
n++;
x = 0;
}
lineV = split(line, delimiter);
for(int i = 0; i < lineV.size(); i++){
if(!lineV[i].compare("")){
x++;
}
else{
if(line.compare("#") && lineV.size() > 1){
double num = std::stod(lineV.at(i));
network.at(n)->bias.at(x).at(i) = num;
}
}
}
}
file.close();
}
int main(int argc, char** argv){
int numEpochs, numImages;
std::string fileName;
NETWORK network = {
new Dense(28*28, 40),
new Softmax(),
new Dense(40, 10),
new Softmax()
};
int batchSize;
if(argc == 5){
numImages = atoi(argv[1]);
numEpochs = atoi(argv[2]);
if(numImages == 0 || numEpochs == 0){
std::cout << "Wrong input parameters\n";
return 0;
}else{
batchSize = atoi(argv[3]);
if(batchSize != 0){
for(int i = 0; i < numImages/batchSize; i++){
std::cout << "Training batch number: " << i << "\n";
trainMnist(network, numEpochs, numImages, i, batchSize, 0.1, true);
}
}else{
trainMnist(network, numEpochs, numImages, 0, numImages, 0.1, true);
}
storeNetwork(argv[4], network);
}
}
else if(argc == 3){
numImages = atoi(argv[1]);
if(numImages == 0){
std::cout << "Wrong input parameters\n";
return 0;
}
else{
readNetwork(argv[2], network);
runMnist(network, numImages);
}
}
else{
std::cout << "Usage: ./ml {Number of Images}, {Number of Epochs}, {batch size (0=no batch)}, {Model Output File} For training \nor\n ./ml {Number of Images} {Model Input File} for running\n";
return 0;
}
return 0;
}