forked from gast04/NeuralNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
110 lines (87 loc) · 2.6 KB
/
main.cpp
File metadata and controls
110 lines (87 loc) · 2.6 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
106
107
108
109
/*
* File: main.cpp
* Author: NiKu
* https://vimeo.com/19569529
* Created on June 26, 2015, 11:18 PM
*/
#include <cstdlib>
#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include "Neuron.h"
#include "Net.h"
void showVectorVals(std::string label, std::vector<double> &v) {
std::cout << label << " ";
for (unsigned i = 0; i < v.size(); ++i)
{
std::cout << v[i] << " ";
}
std::cout << std::endl;
}
int main(int argc, char** argv) {
// learn the Net the work of a AND-Function
// 00 -> 0
std::vector<double> input00;
input00.push_back(0.0);
input00.push_back(0.0);
std::vector<double> output00;
output00.push_back(0.0);
// 11 -> 1
std::vector<double> input11;
input11.push_back(1.0);
input11.push_back(1.0);
std::vector<double> output11;
output11.push_back(1.0);
// 10 -> 0
std::vector<double> input10;
input10.push_back(1.0);
input10.push_back(0.0);
std::vector<double> output10;
output10.push_back(0.0);
// 01 -> 0
std::vector<double> input01;
input01.push_back(0.0);
input01.push_back(1.0);
std::vector<double> output01;
output01.push_back(0.0);
// for a AND-Gate a 2 1 Topology would be enough
// simple 2 3 1 Topology
std::vector<unsigned> topology;
topology.push_back(2);
topology.push_back(3);
topology.push_back(1);
Net myNet(topology);
std::vector<double> inputVals, targetVals, resultVals;
for(int i = 0; i < 2000; i++ ){
std::cout << std::endl << "Pass " << i << std::endl;
if(i%4 == 0){
inputVals = input11;
targetVals = output11;
}
else if(i%3 == 0) {
inputVals = input10;
targetVals = output10;
}
else if(i%2 == 0){
inputVals = input01;
targetVals = output01;
}
else {
inputVals = input00;
targetVals = output00;
}
showVectorVals(": Inputs:", inputVals);
myNet.feedForward(inputVals);
myNet.getResults(resultVals);
showVectorVals("Outputs:", resultVals);
showVectorVals("Targets:", targetVals);
assert(targetVals.size() == topology.back());
myNet.backProp(targetVals);
std::cout << "Net recent average error: " << myNet.getRecentAverageError() << std::endl;
}
// print the connection of all neurons
std::cout << "\n start printing weights: \n" << std::endl;
myNet.printAllConnections();
return 0;
}