-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralNetwork.cpp
183 lines (159 loc) · 6.26 KB
/
NeuralNetwork.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
#include "NeuralNetwork.h"
int main() {
//Create Data that will be used for training, which is a vector of vectors
vector<vector<double> > trainingData{ {-3, -2}, {20, 4}, {19, 7}, {-12, -8}, {-1, -2} };
//Create the real output values that are expected from the Training Data
vector<double> realValues{ 1, 0, 0, 1, 0};
//Initialize Network
NeuralNetwork nn;
//Print Friendly Comments
cout << endl;
cout << endl << "-----Network Initiated -----" << endl;
//Make Neurons (only 2 hidden and one output created in this example)
//How many ever neurons are supported for the 1 hidden layer
//Number of neurons should match number of variable/inputs
cout << endl << "----- Building Neurons -----" << endl;
nn.addNeuron("one", randomizedVector(), randomize()); //First Hidden Neuron
nn.addNeuron("two", randomizedVector(), randomize()); //Second Hidden Neuron
nn.addNeuron("out", randomizedVector(), randomize()); //Out Neuron
cout << endl << "----- Network Complete -----" << endl;
//Start Training
cout << endl << "----- Training Started -----" << endl;
//Parameters for .train() are
//number of training loops, interval to print value,
//the training Data, and the real output values
nn.train(300, 10, trainingData, realValues);
cout << endl << "----- Training Ended -----" << endl;
//Test the trained neural network with some cases
vector<double> test1{ -8, -3 };
vector<double> test2{ 21, 4 };
cout << endl << "Test1: " << nn.feedForward(test1, "out") << endl;
cout << endl << "Test2: " << nn.feedForward(test2, "out") << endl;
cout << endl;
return 0;
}
//Class Component Definitions
NeuralNetwork::NeuralNetwork() {
numInputs = 2;
numLayers = 2;
cout << endl << "Network has been created with " << numLayers << " layers" << endl;
}
void NeuralNetwork::addNeuron(string name, vector<double> weights, double bias) {
Neuron n(name, weights, bias);
network.push_back(n);
}
double NeuralNetwork::feedForward(vector<double> inputs, string outputNeuron) {
if (inputs.size() == numLayers) {
int limit = network.size();
//Can use recursion to support multilayered networks
vector<double> secondaryInputs;
for (int i = 0; i < network.size(); i++) {
if (network.at(i).getName() == outputNeuron) {
limit = i;
}
}
for (int j = 0; j < limit; j++) {
secondaryInputs.push_back(network.at(j).feedForward(inputs));
}
return network.at(limit).feedForward(secondaryInputs);
} else {
cout << endl << "Error: Layer and Input Dimension Mismatch." << endl;
}
return 0.0;
}
string NeuralNetwork::getNumNeurons() {
return network.at(network.size()).getName();
}
void NeuralNetwork::setNumInputs(int i) {
numInputs = i;
}
void NeuralNetwork::setNumLayers(int l) {
numLayers = l;
}
int NeuralNetwork::getNumInputs() {
return numInputs;
}
int NeuralNetwork::getNumLayers() {
return numLayers;
}
double NeuralNetwork::deriv_sigmoid(double x) {
return sigmoid(x) * (1 - sigmoid(x));
}
double randomize() {
srand(time(0));
return (rand() % 100) / 100.0;
}
vector<double> randomizedVector() {
vector<double> v{randomize(), randomize()};
return v;
}
double NeuralNetwork::mse_loss(vector<double> r, vector<double> p) {
double com = 0;
if (r.size() == p.size()) {
for (int i = 0; i < r.size(); i++) {
com = (r.at(i) - p.at(i)) * (r.at(i) - p.at(i));
com /= r.size();
}
return com;
} else {
cout << endl << "MSE Error: Dimension Mismatch" << endl;
}
return 0;
}
void NeuralNetwork::train(int epoch, int interval, vector<vector<double> > data, vector<double> real) {
//Training
double learnRate = 0.1;
if (data.size() == real.size()) {
for (int t = 0; t < epoch; t++) {
for (int x = 0; x < data.size(); x++) {
double pred = feedForward(data.at(x), "out");
double dL_dypred = -2 * (real.at(x) - pred);
vector<double> weight_derivs;
vector<double> bias_derivs;
vector<double> input_derivs;
for (int k = 0; k < network.size() - 1; k++) {
input_derivs.push_back(network.at(network.size()-1).getWeights().at(k) * deriv_sigmoid(network.at(network.size()-1).getSum()));
}
for (int n = 0; n < network.size() - 1; n++) {
for (int i = 0; i < data.at(x).size(); i++) {
weight_derivs.push_back(data.at(x).at(i) * deriv_sigmoid(network.at(n).getSum()));
}
bias_derivs.push_back(deriv_sigmoid(network.at(n).getSum()));
}
for (int j = 0; j < network.size() - 1; j++) {
weight_derivs.push_back(network.at(j).getSum() * deriv_sigmoid(network.at(network.size()-1).getSum()));
}
bias_derivs.push_back(deriv_sigmoid(network.at(network.size()-1).getSum()));
for (int e = 0; e < network.size() - 1; e++) {
vector<double> tempWeights;
for (int w = 0; w < network.size() - 1; w++) {
double tempWeight = network.at(e).getWeights().at(w) - ( learnRate * dL_dypred * input_derivs.at(e) * weight_derivs.at(2 * e + w) );
tempWeights.push_back(tempWeight);
}
network.at(e).setWeights(tempWeights);
}
for (int b = 0; b < network.size() - 1; b++) {
double tempBias = network.at(b).getBias() - ( learnRate * dL_dypred * input_derivs.at(b) * bias_derivs.at(b) );
network.at(b).setBias(tempBias);
}
vector<double> outWeights;
for (int o = 0; o < network.size() - 1; o++) {
double outWeight = network.at(network.size()-1).getWeights().at(o) - ( learnRate * dL_dypred * weight_derivs.at((network.size()-1) * 2 + o) );
outWeights.push_back(outWeight);
}
network.at(network.size()-1).setWeights(outWeights);
double outBias = network.at(network.size()-1).getBias() - ( learnRate * dL_dypred * bias_derivs.at(bias_derivs.size()-1));
}
if (t % interval == 0) {
vector<double> preds;
for (int f = 0; f < data.size(); f++) {
preds.push_back(feedForward(data.at(f), "out"));
}
double loss = mse_loss(real, preds);
cout << endl << "Epoch: " << t << " Loss: " << loss << endl;
}
}
} else {
cout << endl << "Error: Dimension Mismatch during Training" << endl;
}
}