-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
105 lines (77 loc) · 3.13 KB
/
Copy pathmain.cpp
File metadata and controls
105 lines (77 loc) · 3.13 KB
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
#include <iostream>
#include <chrono>
#include "pipeline/pipeline.h"
#include "loader/mnist_loader.h"
#define PERF_NET 0
#define TEST_NET 1
#define TIME_NET 1
using namespace ::std;
using namespace ::std::chrono;
int main()
{
auto inputs = Load_MNIST_File("../data/train-images.idx3-ubyte", nbTrainImages);
auto targets = GetTargetValues("../data/train-labels.idx1-ubyte", nbTrainImages);
Pipeline< Flatten<inputWidth, inputHeight>,
Dense<inputSize, 32, LeCun_Normal>,
Sigmoid<32>,
Dense<32, 10, LeCun_Normal>,
Sigmoid<10> > network;
float cumulativeAccuracy;
float cumulativeLoss;
std::cout << "TRAINING...\n";
#if TIME_NET
auto t_start = std::chrono::steady_clock::now();
#endif
for (size_t e = 0; e < epochs; ++e)
{
#if PERF_NET
std::cout << "\nEpoch " << e << " :" << endl;
cumulativeAccuracy = 0.f;
cumulativeLoss = 0.f;
#endif
for (size_t b = 0; b < minibatchSize; ++b)
{
const auto& input = inputs[e * minibatchSize + b];
const auto& target = targets[e * minibatchSize + b];
Tensor<outputSize> prediction = network.forward(input);
Tensor<outputSize> error = prediction - target;
network.backward(error);
#if PERF_NET
cumulativeAccuracy += prediction.argmax() == target.argmax();
cumulativeLoss += (abs(error)).sum() / static_cast<float>(outputSize);
#endif
}
#if PERF_NET
std::cout << "Loss : " << cumulativeLoss / static_cast<float>(minibatchSize) << endl;
std::cout << "Accuracy : " << cumulativeAccuracy / static_cast<float>(minibatchSize) << "\n" << endl;
#endif
network.update();
}
#if TIME_NET
auto t_end = std::chrono::steady_clock::now();
cout << "\nTraining Time : " << std::chrono::duration<double, std::milli>(t_end - t_start).count() << "ms" << endl;
#endif
delete[] inputs;
delete[] targets;
#if TEST_NET
cout << "\nTESTING..." << endl;
inputs = Load_MNIST_File("../data/t10k-images.idx3-ubyte", nbTestImages);
targets = GetTargetValues("../data/t10k-labels.idx1-ubyte", nbTestImages);
cumulativeAccuracy = cumulativeLoss = 0.f;
for (size_t i = 0; i < nbTestImages; ++i)
{
const auto& target = targets[i];
const auto& input = inputs[i];
Tensor<outputSize> prediction = network.forward(input);
Tensor<outputSize> error = prediction - target;
cumulativeAccuracy += prediction.argmax() == target.argmax();
cumulativeLoss += (abs(error)).sum() / static_cast<float>(outputSize);
}
cout << "Loss : " << cumulativeLoss / (float)nbTestImages << endl;
cout << "Accuracy : " << cumulativeAccuracy / (float)nbTestImages << endl;
delete[] inputs;
delete[] targets;
#endif
cout << "\nCOMPLETE !" << endl;
return 0;
}