-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
84 lines (66 loc) · 2.53 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
#include <iostream>
#include "vector"
#include "Net.h"
#include "TrainingData.h"
#include "cassert"
// The command for listing all file in the terminal is: ls -a
// if you only want to see files with a specific ending, e.g., .txt, you can use: ls -a *.txt
using namespace std;
void showVectorVals(std::string label, std::vector<double> &v) {
std::cout << label << " ";
for (unsigned i = 0; i < v.size(); ++i) {
std::cout << v[i] << " ";
}
}
int main() {
TrainingData trainingData("shortTrainingData.txt");
std::cout << "Finished reading training data" << std::endl;
// e.g., { 3, 2, 1 } means 3 neurons in the input layer, 2 in the hidden layer, and 1 in the output layer
std::vector<unsigned> topology;
trainingData.getTopology(topology);
std::cout << topology[1] << std::endl;
std::cout << "Finished reading topology" << std::endl;
Net myNet(topology);
std::cout << "Finished creating the NET" << std::endl;
std::vector<double> inputVals, targetVals, resultVals;
int trainingPass = 0;
while (!trainingData.isEof()) {
++trainingPass;
std::cout << std::endl << "Pass: " << trainingPass;
// Get new input data and feed it forward;
if (trainingData.getNextInputs(inputVals) != topology[0]) {
break;
}
showVectorVals("; Inputs:", inputVals);
myNet.feedForward(inputVals);
myNet.getResults(resultVals);
showVectorVals("Outputs:", resultVals);
trainingData.getTargetOutputs(targetVals);
showVectorVals("Targets: ", targetVals);
assert(targetVals.size() == topology.back());
myNet.backPropagate(targetVals);
std::cout << "Net recent average error: " << myNet.getRecentAverageError() << std::endl;
}
std::cout << std::endl << "Training done! Starting testing:" << std::endl;
cout << endl << "0 and 1:" << endl;
std::vector<double> testVals = {0, 1};
myNet.feedForward(testVals);
myNet.getResults(testVals);
cout << endl << "1 and 0:" << endl;
testVals = {1, 0};
myNet.feedForward(testVals);
myNet.getResults(testVals);
cout << endl << "1 and 1:" << endl;
testVals = {1, 1};
myNet.feedForward(testVals);
myNet.getResults(testVals);
cout << endl << "0 and 0:" << endl;
testVals = {0, 0};
myNet.feedForward(testVals);
myNet.getResults(testVals);
showVectorVals("Test results: ", testVals);
cout << myNet.getRecentAverageError() << endl;
testVals = {1, 0};
myNet.printPrediction(testVals);
return 0;
}