-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralNetwork.cc
387 lines (313 loc) · 11 KB
/
NeuralNetwork.cc
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "NeuralNetwork.hh"
NeuralNetworkParameters::NeuralNetworkParameters(
std::vector<unsigned int> nn_topology,
std::vector<std::string> nn_activation_functions,
std::string nn_cost_function,
bool nn_normalize_output)
: topology(nn_topology),
activation_functions(nn_activation_functions),
cost_function(nn_cost_function),
normalize_output(nn_normalize_output),
weight_max(0.05),
bias_initial_value(1),
batchs_for_avg_error(100) {}
NeuralNetwork::NeuralNetwork (const NeuralNetworkParameters& netData) :
total_error(0),
batchs_for_avg_error(netData.batchs_for_avg_error),
normalize_output(netData.normalize_output)
{
if (netData.topology.size() < 2)
throw InvalidTopology();
if (netData.topology.size() != netData.activation_functions.size())
throw InvalidActFuncVector();
for (auto layer_size : netData.topology)
if (layer_size < 1)
throw InvalidTopology();
if (netData.batchs_for_avg_error < 1)
throw InvalidBatchErrorAVGQty();
layer = std::vector<Layer>(netData.topology.size());
cost_function = initCostFunction(netData.cost_function);
srand(time(NULL));
for (int i = 0; i < layer.size(); i++)
{
int inputs = (i == 0 ? 0 : netData.topology[i-1] + 1);
layer[i] = std::vector<Neuron>(netData.topology[i] + 1);
for (int neuron = 0; neuron < netData.topology[i] + 1; neuron++)
{
if (neuron == netData.topology[i])
layer[i][neuron] = Neuron(netData.bias_initial_value, neuron);
else
{
layer[i][neuron] = Neuron(netData.activation_functions[i],
inputs,
netData.weight_max,
neuron);
}
}
}
}
NeuralNetwork::NeuralNetwork (std::string filePath)
{
std::fstream file;
file.open(filePath, std::ios::in);
if (not file)
throw ErrorOpeningNeuralNetwork();
if (not (file >> normalize_output))
throw ErrorLoadingNeuralNetwork();
if (not (file >> batchs_for_avg_error))
throw ErrorLoadingNeuralNetwork();
std::string cost_function_name;
if (not (file >> cost_function_name))
throw ErrorLoadingNeuralNetwork();
cost_function = initCostFunction(cost_function_name);
int topology_size;
if (not (file >> topology_size))
throw ErrorLoadingNeuralNetwork();
layer = std::vector<Layer>(topology_size);
// reading and shaping each layers' size
for (int l = 0; l < topology_size; l++)
{
int layer_size;
if (not (file >> layer_size))
throw ErrorLoadingNeuralNetwork();
layer[l] = Layer(layer_size + 1);
}
std::vector<std::string> activation_function_names(topology_size);
// activation functions are stored now in order to create the neurons later
for (int l = 0; l < topology_size; l++)
if (not (file >> activation_function_names[l]))
throw ErrorLoadingNeuralNetwork();
// create input layer and read input layer's bias
double input_layer_bias;
if (not (file >> input_layer_bias))
throw ErrorLoadingNeuralNetwork();
for (int n = 0; n < layer[0].size(); n++)
{
if (n == layer[0].size() - 1)
layer[0][n] = Neuron(input_layer_bias, n);
else
layer[0][n] = Neuron(activation_function_names[0], 0, 1, n);
}
// now read and create each neuron
for (int l = 1; l < layer.size(); l++)
{
for (int n = 0; n < layer[l].size(); n++)
{
if (n == layer[l].size() - 1)
{
double layer_bias;
if (not (file >> layer_bias))
throw ErrorLoadingNeuralNetwork();
layer[l].back() = Neuron(layer_bias, n);
}
else
{
std::vector<double> neuron_w(layer[l-1].size());
for (int w = 0; w < neuron_w.size(); w++)
if (not (file >> neuron_w[w]))
throw ErrorLoadingNeuralNetwork();
layer[l][n] = Neuron(activation_function_names[l], neuron_w, n);
}
}
}
file.close();
}
void NeuralNetwork::saveToFile (std::string filePath) const
{
std::fstream file;
file.open(filePath, std::ios::out | std::ios::trunc);
if (not file)
throw ErrorSavingNeuralNetwork();
file << normalize_output << std::endl;
file << batchs_for_avg_error << std::endl;
file << cost_function->getName() << std::endl;
// write topology
file << layer.size() << std::endl;
for (int i = 0; i < layer.size(); i++)
i == 0 ? file << (layer[i].size() - 1) :
file << " " << (layer[i].size() - 1);
file << std::endl;
// write activation functions
for (int i = 0; i < layer.size(); i++)
i == 0 ? file << layer[i][0].activationFunctionName() :
file << " " << layer[i][0].activationFunctionName();
file << std::endl;
// write all the weights of the neurons
file << layer.front().back().getOutput() << std::endl;
for (int l = 1; l < layer.size(); l++)
{
for (int n = 0; n < layer[l].size() - 1; n++)
{
std::vector<double> weights = layer[l][n].getWeights();
for (int w = 0; w < weights.size(); w++)
w == 0 ? file << weights[w] : file << " " << weights[w];
file << std::endl;
}
file << layer[l].back().getOutput() << std::endl;
}
file.close();
}
void NeuralNetwork::compute (const std::vector<double>& input)
{
if (input.size() != qtyInputs())
throw InvalidInputSize();
for (int i = 0; i < qtyInputs(); i++)
layer[0][i].setOutput(input[i]);
for (int l = 1; l < layer.size(); l++)
{
for (int n = 0; n < layer[l].size() - 1; n++)
layer[l][n].calculateOutput(layer[l-1]);
}
}
void NeuralNetwork::getOutput (std::vector<double>& result)
{
if (result.size() != qtyOutputs())
throw InvalidOutputSize();
if (normalize_output)
{
double output_sum = 0;
for (auto neuron : layer.back())
output_sum += neuron.getOutput();
output_sum -= layer.back().back().getOutput();
double default_output = 1.0/(layer.back().size() - 1);
for (int i = 0; i < qtyOutputs(); i++)
result[i] = output_sum != 0 ? layer.back()[i].getOutput()/output_sum :
default_output;
}
else
for (int i = 0; i < qtyOutputs(); i++)
result[i] = layer.back()[i].getOutput();
}
double NeuralNetwork::error (const std::vector<double>& input,
const std::vector<double>& target)
{
this->compute(input);
double this_error = cost_function->error(layer.back(), target);
computeAvgError(this_error);
return this_error;
}
double NeuralNetwork::avgError (const std::vector<double>& input,
const std::vector<double>& target)
{
double this_error = error(input, target);
return computeAvgError(this_error);
}
double NeuralNetwork::computeAvgError (double this_error)
{
total_error += this_error;
error_list.push_back(this_error);
if (batchs_for_avg_error < error_list.size())
{
total_error -= error_list.front();
error_list.pop_front();
}
return total_error / error_list.size();
}
void NeuralNetwork::clearAvgError ()
{
total_error = 0;
error_list.clear();
}
void NeuralNetwork::changeBatchsForAVGError (unsigned qty)
{
if (qty < 1)
throw InvalidBatchErrorAVGQty();
batchs_for_avg_error = qty;
while (error_list.size() > batchs_for_avg_error)
{
total_error -= error_list.front();
error_list.pop_front();
}
}
double NeuralNetwork::backPropagate (const std::vector<double>& input,
const std::vector<double>& target,
double learning_rate,
double inertia,
bool train_bias)
{
this->compute(input);
cost_function->dError(layer.back(), target);
double avg_err = computeAvgError(cost_function->error(layer.back(), target));
// calculate the gradients in the entire network
for (int l = layer.size() - 2; l >= 1; l--)
for (int n = 0; n < layer[l].size() - not train_bias; n++)
layer[l][n].calculateGradient(layer[l+1]);
// calc the gradient of the bias at the input layer
if (train_bias)
layer.front()[layer.front().size()-1].calculateGradient(layer[1]);
// update weights in the entire network
for (int l = layer.size() - 1; l >= 1; l--)
for (int n = 0; n < layer[l].size() - not train_bias; n++)
layer[l][n].actualize(layer[l-1], learning_rate, inertia);
// calc the gradient of the bias at the input layer
if (train_bias)
layer.front()[layer.front().size()-1].actualize(layer[0],
learning_rate,
inertia);
return avg_err;
}
void NeuralNetwork::draw () const
{
std::cout << "topology:";
for (auto l : layer)
std::cout << " " << (l.size() - 1);
std::cout << "\nactivation functions:";
for (auto l : layer)
std::cout << " " << l[0].activationFunctionName();
std::cout << "\ncost function: " << cost_function->getName();
std::cout << "\nnormalized output: ";
normalize_output ? std::cout << "yes" : std::cout << "no";
std::cout << "\nweights:";
for (int l = 0; l < layer.size(); l++)
{
std::cout << "\n\n *layer: " << l << std::endl;
if (l == 0)
{
for (int n = 0; n < layer[l].size() - 1; n++)
std::cout << " input neuron\n";
std::cout << " ";
layer.front().back().draw();
}
else
{
for (int n = 0; n < layer[l].size() - (l == layer.size() - 1); n++)
{
std::cout << " ";
layer[l][n].draw();
}
}
}
std::cout << std::endl << std::endl;
}
std::pair<double, double> NeuralNetwork::weightInfo () const
{
double sum = 0, qty = 0, max = -1;
for (auto l : layer)
for (auto n : l)
{
std::vector<double> weights = n.getWeights();
for (auto w : weights)
{
double weight = w < 0 ? -w : w;
sum += weight;
++qty;
if (weight > max)
max = weight;
}
}
return std::make_pair(sum/qty, max);
}
unsigned int NeuralNetwork::qtyInputs () const
{
return layer[0].size() - 1;
}
unsigned int NeuralNetwork::qtyOutputs () const
{
return layer.back().size() - 1;
}
CostFunction* NeuralNetwork::initCostFunction (std::string cost_function_name)
{
if (cost_function_name == "mse") return new MSE(normalize_output);
else throw CostFunctionNotFound();
return NULL;
}