-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuralNetwork.cpp
293 lines (246 loc) · 9.07 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//
// Simple implementation of standard multilayered neural network.
//
// NB: gnuplot required for graphical output of trained 3D function
//
// Created by Matej Hamas in September 2014.
// Copyright (c) 2014 Matej Hamas. All rights reserved.
// Licensed under BSD
#include "NeuralNetwork.h"
#include <random>
#include <fstream>
#include <sstream>
#include <unistd.h>
class Edge;
//#define VERBOSE
using namespace std;
namespace NeNet
{
NeuralNetwork::NeuralNetwork(const int numOfInputs,
const std::vector<int> numsOfPerceptrons) :
_numOfInputs(numOfInputs),
_numOfLayers((int)numsOfPerceptrons.size()),
_numsOfPerceptrons(numsOfPerceptrons)
{
/* Creating the network skelet */
for (int i = 0; i < _numOfLayers; i++)
{
vector<shared_ptr<Perceptron>> temp;
for (auto j = 0; j < _numsOfPerceptrons[i]; j++)
{
Type t;
if (i == 0) {
t = INPUT;
} else if (i == _numOfLayers - 1) {
t = OUTPUT;
} else {
t = HIDDEN;
}
temp.push_back(make_shared<Perceptron>(t, i, j));
}
_network.push_back(temp);
}
/* Initializing INPUT layer*/
for (int i = -1; i < numOfInputs; i++)
{
for (int perceptron = 0; perceptron < _network[0].size(); perceptron++)
{
_edges.push_back(make_shared<Edge>(nullptr, _network[0][perceptron], (i == -1) ? FIXED_VALUE : VARIABLE_VALUE));
_network[0][perceptron]->addPredecessor(_edges.back());
}
}
/* Initializing HIDDEN layers */
for (int layer = 0; layer < _numOfLayers - 1; layer++)
{
const int originLayerSize = (int)_network[layer].size();
const int destinationLayerSize = (int)_network[layer + 1].size();
for (int i = -1; i < originLayerSize; i++)
{
for (int j = 0; j < destinationLayerSize; j++)
{
if (i == -1) {
_edges.push_back(make_shared<Edge>(nullptr, _network[layer + 1][j], FIXED_VALUE));
} else {
_edges.push_back(make_shared<Edge>(_network[layer][i], _network[layer + 1][j], VARIABLE_VALUE));
}
if (i >= 0)
{
_network[layer][i]->addSuccessor(_edges.back());
}
_network[layer + 1][j]->addPredecessor(_edges.back());
}
}
}
/* Initializing OUTPUT layer */
for (int i = 0; i < _network[_numOfLayers-1].size(); i++)
{
_outputEdges.push_back(make_shared<Edge>(_network[_numOfLayers - 1][i], nullptr, VARIABLE_VALUE));
_network[_numOfLayers - 1][i]->addSuccessor(_outputEdges.back());
}
}
double NeuralNetwork::forwardPropagateWithError(const vector<double>& input, double output)
{
forwardPropagate(input);
return pow(useForSingleOutput(input) - output, 2);
}
void NeuralNetwork::forwardPropagate(const vector<double>& input)
{
/* Place input values on input edges */
for (int i = 0; i < _numOfInputs; i++)
{
for (int j = 0; j < _network[0].size() ; j++)
{
_edges[(i + 1) * _network[0].size() + j]->setValue(input[i]);
}
}
/* Propagate through network */
for (const auto &layer : _network)
{
for (const auto &perceptron : layer)
{
perceptron->processInputs();
}
}
}
void NeuralNetwork::backwardPropagate(const double sampleOutput)
{
for (int i = _numOfLayers - 1; i >= 0; i--)
{
for (auto perceptron : _network[i])
{
perceptron->calculateDelta(sampleOutput);
}
}
}
void NeuralNetwork::train(const vector<pair<vector<double>, double>>& patterns,
const int numOfEpochs,
const double lowerBound,
const double upperBound,
double stepSize,
const bool decreaseLearningRate,
const double minStepSize)
{
const double stepSizeDecrease = (stepSize - minStepSize) / numOfEpochs;
/* Initializing random weights to edges */
srand((unsigned int)time(nullptr));
for (auto edge : _edges)
{
double random = ((double)rand() / RAND_MAX) * (upperBound - lowerBound) + lowerBound;
edge->setWeight(random);
}
#ifdef VERBOSE
cout << "-----INITIAL WEIGHTS-----\n";
for(const auto& edge : _edges)
{
cout << "w: " << edge->getWeight() << " v: " << edge->getValue() << " e: " << edge->getError() << endl;
}
cout << endl;
#endif
/* Running sequential training on the neural network */
int index = 0;
for(int i = 0; i < numOfEpochs; i++) {
double error = 0;
for (const auto& pattern : patterns)
{
error += forwardPropagateWithError(pattern.first, pattern.second);
backwardPropagate(pattern.second);
for (auto edge : _edges)
{
edge->setWeight(edge->getWeight() - edge->getError() * stepSize);
}
#ifdef VERBOSE
cout << "----------PATTERN " << index << "----------\n";
cout << "-----EDGES INFORMATION-----\n";
for (const auto& edge : _edges)
{
cout << "w: " << edge->getWeight() << " v: " << edge->getValue() << " e: " << edge->getError() << endl;
}
cout << "--PERCEPTRONS INFORMATION--\n";
for (int i = 0; i < _numOfLayers; i++)
{
for (int j = 0; j < _numsOfPerceptrons[i]; j++)
{
cout << "P[" << i << ", " << j << "]: ";
auto p = _network[i][j];
cout << "o: " << p->getOutput() << " d: " << p->getDelta() << endl;
}
}
cout << "---------------------------\n";
cout << endl;
#endif
index++;
}
cout << i << ": " << error / patterns.size() << endl;
if (decreaseLearningRate)
{
stepSize -= stepSizeDecrease;
}
}
}
vector<double> NeuralNetwork::use(const vector<double>& input)
{
forwardPropagate(input);
vector<double> output;
for(auto outputPerceptron : _network[_numOfLayers - 1])
{
output.push_back(outputPerceptron->getOutput());
}
return output;
}
function<double(double, double)> NeuralNetwork::get3DFunction()
{
return [this](double x, double y) -> double {
const vector<double> input = {x, y};
// TODO change back
// _network[1][0]->_activationFun = [](double x) {return x; };
// _network[1][0]->_activationFunDer = [](double x) { return 1; };
return useForSingleOutput(input);
};
}
void NeuralNetwork::createDataFile3D(const double minX, const double maxX, const double numOfXPoints,
const double minY, const double maxY, const double numOfYPoints,
const std::string &filePath)
{
const function<double(double, double)> trainedFunction = get3DFunction();
const double stepX = (maxX - minX) / (numOfXPoints - 1);
const double stepY = (maxY - minY) / (numOfYPoints - 1);
ofstream dataFile;
dataFile.open(filePath);
dataFile << "#####################################################\n";
dataFile << "# Data file for 3D function trained by neural network\n";
dataFile << "#x : [" << minX << ", " << maxX << "], " << numOfXPoints << " points\n";
dataFile << "#y : [" << minY << ", " << maxY << "], " << numOfYPoints << " points\n";
dataFile << "#####################################################\n";
for (double x = minX; x <= maxX; x += stepX)
{
for (double y = minY; y <= maxY; y += stepY)
{
dataFile << x << " " << y << " " << trainedFunction(x, y) << endl;
}
}
dataFile.close();
}
void NeuralNetwork::plot3DWithGnuplot(const double minX, const double maxX, const double numOfXPoints,
const double minY, const double maxY, const double numOfYPoints,
const std::string& filePath,
const std::string& outputPNGPath)
{
FILE* gnuplot = popen("/usr/local/bin/gnuplot --persist","w");
if(!gnuplot) {
return;
}
stringstream ss;
ss << "set xrange [" << minX << ":" << maxX << "]\n";
ss << "set yrange [" << minY << ":" << maxY << "]\n";
ss << "set dgrid3d " << numOfXPoints << "," << numOfYPoints << endl;
ss << "set hidden3d\n";
ss << "splot" << "\"" << filePath << "\" with lines notitle\n";
fprintf(gnuplot, ss.str().c_str());
stringstream ssPNG;
ssPNG << "set terminal png\n";
ssPNG << "set output " << "\"" << outputPNGPath << "\"\n";
fprintf(gnuplot, ssPNG.str().c_str());
fprintf(gnuplot, ss.str().c_str());
pclose(gnuplot);
}
}