-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeedForwardNetwork.cpp
193 lines (172 loc) · 6.92 KB
/
feedForwardNetwork.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
184
185
186
187
188
189
190
191
192
193
#include "feedForwardNetwork.h"
void FeedForwardNetwork::initNet(int numberOfInputNeurons, int numberOfOutputNeurons)
{
inputLayer.clear();
hiddenLayer.clear();
outputLayer.clear();
for (int i = 0; i < numberOfInputNeurons; ++i)
{
Neuron *neuron = new Neuron();
inputLayer.push_back(neuron);
Neuron *neuronHidden = new Neuron();
hiddenLayer.push_back(neuronHidden);
}
for (int i = 0; i < numberOfOutputNeurons; ++i)
{
Neuron *neuronOutput = new Neuron();
outputLayer.push_back(neuronOutput);
}
Neuron *inputLayerBias = new Neuron();
inputLayerBias->value = 1.0;
inputLayerBias->isBias = true;
inputLayer.push_back(inputLayerBias);
Neuron *hiddenLayerBias = new Neuron();
hiddenLayerBias->value = 1.0;
hiddenLayerBias->isBias = true;
hiddenLayer.push_back(hiddenLayerBias);
for (auto &inputNeuron: inputLayer)
for (auto &hiddenNeuron: hiddenLayer)
if (!hiddenNeuron->isBias)
inputNeuron->linkTo(hiddenNeuron);
for (auto &hiddenNeuron: hiddenLayer)
for (auto &outputNeuron: outputLayer)
if (!outputNeuron->isBias)
hiddenNeuron->linkTo(outputNeuron);
}
void FeedForwardNetwork::feedForward()
{
for (auto &hiddenNeuron: hiddenLayer)
if (!hiddenNeuron->isBias)
{
double sum = 0;
for (int i = 0; i < hiddenNeuron->inputNeurons.size(); ++i)
sum += hiddenNeuron->inputNeurons[i]->value * hiddenNeuron->inputWeights[i];
hiddenNeuron->value = Utils::sigmoid(sum);
}
for (auto &outputNeuron: outputLayer)
{
double sum = 0;
for (int i = 0; i < outputNeuron->inputNeurons.size(); ++i)
sum += outputNeuron->inputNeurons[i]->value * outputNeuron->inputWeights[i];
outputNeuron->value = Utils::sigmoid(sum);
}
}
double FeedForwardNetwork::calculateTotalError(std::vector<double>* expectedOutputs)
{
if (expectedOutputs->size() != outputLayer.size())
throw std::invalid_argument("Output layer size differs from expected outputs.");
double error = 0;
for (int i = 0; i < outputLayer.size(); ++i)
error += pow(expectedOutputs->at(i) - outputLayer[i]->value, 2) / 2;
return error;
}
void FeedForwardNetwork::printNet()
{
std::cout<<"~Input layer:~"<<std::endl;
for (auto& neuron: inputLayer)
{
if (neuron->isBias)
std::cout<<"Bias neuron: ";
std::cout<<neuron->value<<std::endl;
}
std::cout<<std::endl;
std::cout<<"~Hidden layer:~"<<std::endl;
for (auto& neuron: hiddenLayer)
{
if (neuron->isBias)
std::cout<<"Bias neuron: ";
std::cout<<neuron->value<<std::endl;
if (neuron->inputWeights.size() != 0)
{
std::cout<<"Weights:"<<std::endl;
for (auto& weight: neuron->inputWeights)
std::cout<<weight<<std::endl;
}
std::cout<<std::endl;
}
std::cout<<"~Output layer:~"<<std::endl;
for (auto& neuron: outputLayer)
{
std::cout<<neuron->value<<std::endl;
std::cout<<"Weights:"<<std::endl;
for (auto& weight: neuron->inputWeights)
std::cout<<weight<<std::endl;
std::cout<<std::endl;
}
}
double FeedForwardNetwork::propagateBackwards(std::vector<double>* expectedOutputs, double learningRate)
{
double error_total = calculateTotalError(expectedOutputs);
std::vector<double> delta;
delta.clear();
for (int outputLayerIterator = 0; outputLayerIterator < outputLayer.size(); ++outputLayerIterator)
{
double out = outputLayer.at(outputLayerIterator)->value;
double target = expectedOutputs->at(outputLayerIterator);
delta.push_back((out - target) * out * (1 - out));
}
//Output layer weights deltas
std::vector<double> weightOverwriteOutputLayer;
weightOverwriteOutputLayer.clear();
for (int outputLayerIterator = 0; outputLayerIterator < outputLayer.size(); ++outputLayerIterator)
{
for (int weightsIterator = 0;
weightsIterator < outputLayer.at(outputLayerIterator)->inputWeights.size();
++weightsIterator)
{
double currentWeight = outputLayer.at(outputLayerIterator)->inputWeights.at(weightsIterator);
double deltaError_div_deltaWeight = delta.at(outputLayerIterator) * outputLayer.at(outputLayerIterator)->inputNeurons.at(weightsIterator)->value;
double weightOverwrite = currentWeight - learningRate * deltaError_div_deltaWeight;
weightOverwriteOutputLayer.push_back(weightOverwrite);
}
}
//Hidden layer weights deltas
std::vector<double> weightOverwriteHiddenLayer;
weightOverwriteHiddenLayer.clear();
for (int hiddenLayerIterator = 0; hiddenLayerIterator < hiddenLayer.size(); ++hiddenLayerIterator)
{
if (hiddenLayer.at(hiddenLayerIterator)->isBias)
continue;
for (int weightsIterator = 0;
weightsIterator < hiddenLayer.at(hiddenLayerIterator)->inputWeights.size();
++weightsIterator)
{
double currentWeight = hiddenLayer.at(hiddenLayerIterator)->inputWeights.at(weightsIterator);
double deltaError_div_deltaWeight = 0;
for (int deltaIterator = 0; deltaIterator < delta.size(); ++deltaIterator)
{
deltaError_div_deltaWeight += (delta.at(deltaIterator) * outputLayer.at(deltaIterator)->inputWeights.at(0));
}
double out_hidden = hiddenLayer.at(hiddenLayerIterator)->value;
deltaError_div_deltaWeight *= out_hidden * (1 - out_hidden);
double input_value = hiddenLayer.at(hiddenLayerIterator)->inputNeurons.at(0)->value;
deltaError_div_deltaWeight *= input_value;
double weightOverwrite = currentWeight - learningRate * deltaError_div_deltaWeight;
weightOverwriteHiddenLayer.push_back(weightOverwrite);
}
}
int couter = 0;
for (int outputLayerIterator = 0; outputLayerIterator < outputLayer.size(); ++outputLayerIterator)
{
for (int weightsIterator = 0;
weightsIterator < outputLayer.at(outputLayerIterator)->inputWeights.size();
++weightsIterator)
{
outputLayer.at(outputLayerIterator)->inputWeights.at(weightsIterator) = weightOverwriteOutputLayer.at(couter);
couter++;
}
}
couter = 0;
for (int hiddenLayerIterator = 0; hiddenLayerIterator < hiddenLayer.size(); ++hiddenLayerIterator)
{
for (int weightsIterator = 0;
weightsIterator < hiddenLayer.at(hiddenLayerIterator)->inputWeights.size();
++weightsIterator)
{
hiddenLayer.at(hiddenLayerIterator)->inputWeights.at(weightsIterator) = weightOverwriteHiddenLayer.at(couter);
couter++;
}
}
feedForward();
return calculateTotalError(expectedOutputs);
}