-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
74 lines (69 loc) · 2.52 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
#include <cstdlib>
#include <exception>
#include <assert.h>
#include <sstream>
#include <fstream>
#include <iostream>
#include <cstdint>
#include <string>
#include <cmath>
#include <vector>
#include "layers.cpp"
#include "mnist_parser.cpp"
using namespace std;
void initializeNet(vector<Layer*> &layers) {
// Convolutional - Depth, spatialExtent, stride, zeroPadding
// MaxPooling - SpatialExtent, stride
// FullyConnected - Depth
layers.push_back(new Input(1, 32, 32));
layers.push_back(new ConvolutionalLayer(6, 5, 1, 0, layers.back())); // => 6 * 28 * 28
layers.push_back(new MaxPoolingLayer(2, 2, layers.back())); // => 6 * 14 * 14
layers.push_back(new ConvolutionalLayer(16, 5, 1, 0, layers.back())); // => 16 * 10 * 10
layers.push_back(new MaxPoolingLayer(2, 2, layers.back())); // => 16 * 5 * 5
layers.push_back(new ConvolutionalLayer(120, 5, 1, 0, layers.back())); // => 120 * 1 * 1
layers.push_back(new FullyConnectedLayer(10, layers.back())); // => 10 * 1 * 1
layers.push_back(new OutputLayer(layers.back()));
}
void train(vector<Layer*> layers) {
Mnist_Parser parser(".");
vector<Sample*> input = parser.load_training();
int PASSES = 60000;
for (int test = 0; test < PASSES; test++) {
int i = test % input.size();
((Input*)layers[0])->setOutput(input[i]->image);
((OutputLayer*)layers.back())->setLabel(input[i]->label);
//cout << test << ' ' << i << endl;
int iter = 0;
do {
for (int l = 0; l < layers.size(); l++) {
layers[l]->feedForward();
}
vector<float_t> nextErrors;
for (int l = layers.size() - 1; l >= 0; l--) {
layers[l]->backProp(nextErrors);
nextErrors = layers[l]->_errors;
}
//float_t x = ((OutputLayer*)layers.back())->getError();
iter++;
} while (((OutputLayer*)layers.back())->getError() > 1e-3 && iter < 1);
}
auto testInput = parser.load_testing();
int correct = 0;
for (int i = 0; i < testInput.size(); i++) {
((Input*)layers[0])->setOutput(testInput[i]->image);
((OutputLayer*)layers.back())->setLabel(testInput[i]->label);
for (int l = 0; l < layers.size(); l++) {
layers[l]->feedForward();
}
correct += ((OutputLayer*)layers.back())->_label == ((OutputLayer*)layers.back())->getPredict();
//cout << ((OutputLayer*)layers.back())->_label << ' ' << ((OutputLayer*)layers.back())->getPredict() << endl;
}
cout << correct << ' ' << testInput.size() << endl;
}
int main() {
srand(time(NULL));
vector<Layer*> layers;
initializeNet(layers);
train(layers);
return 0;
}