-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.cpp
561 lines (455 loc) · 16.5 KB
/
Network.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#include "Network.h"
#include "Utils.h"
#include "ProblemInfo.h"
#include <stdlib.h>
#include <QtCore/QPair>
#include <QtCore/QDebug>
#include <iostream>
#include <cmath>
Network::Network(int id)
: m_id(id)
, maxNeuronId(0)
, m_averageError(0.0)
, m_sparsity(0.0)
{
int numNeurons = INPUT_SIZE + OUTPUT_SIZE + HIDDEN_SIZE;
/*
* This is a fake neuron to represent the predecessor neuron for biases link
* (see below). The layer is meaningless, while the ID cannot be taken by any
* other neuron in the network.
*/
Neuron* dummy = new SigmoidNeuron(-1, Neuron::InputLayer);
m_neurons.insert(-1, dummy);
for (int i = 1; i <= numNeurons; i++) {
Neuron::Layer layer = Neuron::HiddenLayer;
if (i <= INPUT_SIZE) {
Neuron* neuron = new SigmoidNeuron(i, Neuron::InputLayer);
/*
* This is a fake connection, always with weight 1, used to set the input attribute to this neuron.
*/
neuron->addInConnection( new Link(1.0f, NULL, neuron) );
/*
* Adds a fake link to represent biases. This is easier than setting biases in the neurons,
* because we can mutate and train them as we do with weights.
*
* The output is set now to 1.0 so the neuron will get the bias as input, and will never be
* changed.
*/
createBiasLink(dummy, neuron);
m_inputNeurons.append(neuron);
m_neurons.insert(i, neuron);
continue;
}
Neuron* neuron = NULL;
/*
* Please note the output neurons have IDs smaller than the hidden neurons'. This is because we may need to add/remove
* hidden neurons later, due to mutations, while the input and output neurons never change.
*/
if (i <= INPUT_SIZE + OUTPUT_SIZE) {
layer = Neuron::OutputLayer;
neuron = new TangentNeuron(i, layer);
} else {
neuron = new SigmoidNeuron(i, layer);
}
createBiasLink(dummy, neuron);
m_neurons.insert(i, neuron);
if (layer == Neuron::OutputLayer) {
m_outputNeurons.append(neuron);
} else {
m_hiddenNeurons.append(neuron);
}
}
maxNeuronId = numNeurons;
/*
* No connections between hidden neurons are allowed
*/
for (int i = 1; i <= INPUT_SIZE; i++) {
for (int j = (INPUT_SIZE + OUTPUT_SIZE + 1); j <= numNeurons; j++) {
createRandomLink(i, j);
}
}
for (int i = INPUT_SIZE + OUTPUT_SIZE + 1; i <= numNeurons; i++) {
for (int j = (INPUT_SIZE + 1); j <= INPUT_SIZE + OUTPUT_SIZE; j++) {
createRandomLink(i, j);
}
}
}
/*
* Makes an hard copy of this network. Used during breeding for the genetic algorithm.
*/
Network::Network(const Network* other, int id)
: m_id(id)
, maxNeuronId(0)
, m_sparsity(0)
{
Q_FOREACH (Neuron* otherNeuron, other->m_neurons.values()) {
int neuronId = otherNeuron->id();
if (neuronId == -1) {
m_neurons.insert(-1, new SigmoidNeuron(-1, Neuron::InputLayer));
continue;
}
switch ( otherNeuron->layer() ) {
case Neuron::InputLayer: {
Neuron* newNeuron = new SigmoidNeuron(neuronId, otherNeuron);
newNeuron->addInConnection( new Link( 1.0f, 0, newNeuron) );
m_inputNeurons.append(newNeuron);
m_neurons.insert(neuronId, newNeuron);
break;
}
case Neuron::HiddenLayer: {
Neuron* newNeuron = new SigmoidNeuron(neuronId, otherNeuron);
m_neurons.insert(neuronId, newNeuron);
m_hiddenNeurons.append(newNeuron);
break;
}
case Neuron::OutputLayer: {
Neuron* newNeuron = new TangentNeuron(neuronId, otherNeuron);
m_neurons.insert(neuronId, newNeuron);
m_outputNeurons.append(newNeuron);
break;
}
}
}
maxNeuronId = other->maxNeuronId;
Q_FOREACH (Link* link, other->m_connectivity.links()) {
int in = link->predecessor()->id();
int out = link->successor()->id();
Link* newLink = new Link(link->weight(), m_neurons[in], m_neurons[out]);
newLink->setOutput( link->output() );
m_neurons[in]->addOutConnection(newLink);
m_neurons[out]->addInConnection(newLink);
m_connectivity.addLink(in, out, newLink);
}
}
Network::~Network()
{
qDeleteAll(m_neurons);
m_neurons.clear();
}
/*
* Creates a link between i and j with 50% probability.
*/
void Network::createRandomLink(int i, int j)
{
double r = randomDouble(0.0, 1.0);
if (r <= 0.5) {
Link* link = new Link(randomWeight(), m_neurons[i], m_neurons[j]);
m_neurons[i]->addOutConnection(link);
m_neurons[j]->addInConnection(link);
m_connectivity.addLink(i, j, link);
}
}
/*
* Creates a fake link to represent a bias
*/
void Network::createBiasLink(Neuron* dummy, Neuron* neuron)
{
Link* biasLink = new Link(randomBias(), dummy, neuron);
biasLink->setOutput(1.0);
neuron->addInConnection(biasLink);
m_connectivity.addLink(-1, neuron->id(), biasLink);
}
double Network::randomBias()
{
return randomDouble(-6.0, 1.0);
}
double Network::randomWeight()
{
return randomDouble(-0.2, 0.2);
}
Neuron* Network::getNeuron(int id) const
{
return m_neurons[id];
}
Link* Network::getLink(int n1, int n2) const
{
return m_connectivity.link(n1, n2);
}
bool Network::operator==(const Network& other)
{
return m_id == other.id();
}
void Network::setId(int i)
{
m_id = i;
}
int Network::id() const
{
return m_id;
}
void Network::applyInput(double input[], int expectedClass)
{
for (int i = 0; i < m_inputNeurons.size(); i++) {
Neuron* neuron = m_inputNeurons[i];
Link* inLink = neuron->inConnections().first();
inLink->setOutput( input[i] );
neuron->computeOutput();
}
Q_FOREACH (Neuron* hidden, m_hiddenNeurons) {
hidden->computeOutput();
}
Neuron* outNeuron = m_outputNeurons.first();
outNeuron->computeOutput();
m_lastOutput = (outNeuron->output() > 0.0) ? 1.0 : 0.0;
m_oldError = m_lastError;
m_lastError = (expectedClass == m_lastOutput) ? 0.0 : 1.0; /* simple classification error */
computeGradients(expectedClass);
}
void Network::computeGradients(int expectedClass)
{
double target = (expectedClass == 0) ? -1 : 1;
double out = m_outputNeurons.first()->output();
double oGradient = (1 - out) * out * (target - out);
Q_FOREACH (Link* inLink, m_outputNeurons.first()->inConnections()) {
inLink->setGradient(oGradient);
}
for (int i = 0; i < m_hiddenNeurons.size(); i++) {
Neuron* hidden = m_hiddenNeurons[i];
double out = hidden->output();
double derivative = (1 - out) * out;
double sum = 0.0;
Q_FOREACH (Link* outGoing, hidden->outConnections()) {
sum += oGradient * outGoing->weight();
}
Q_FOREACH (Link* inLink, hidden->inConnections()) {
inLink->setGradient(derivative * sum);
}
return;
}
}
void Network::mutate(MutationOperator op)
{
switch (op) {
case RemoveLink: {
if (m_connectivity.complexity() < LINK_SIZE_MIN) { /* avoid removing too many links, and mutate the weights instead */
applyGaussianMutation();
break;
}
int in = 0;
int out = 0;
int linkType = randomInteger(1, 2);
int inMin, inMax, outMin, outMax;
inMin = inMax = outMin = outMax = 0;
/*
* Determines in which range the IDs should be randomly chosen, depending on whether we're removing a link
* between input and hidden, or between hidden and output (no other kind is allowed).
*/
switch (linkType) {
case 1: { /* Link between input and hidden neuron */
inMin = 1;
inMax = INPUT_SIZE;
outMin = INPUT_SIZE + OUTPUT_SIZE + 1;
outMax = maxNeuronId;
break;
}
case 2: { /* Between hidden and output */
inMin = INPUT_SIZE + OUTPUT_SIZE + 1;
inMax = maxNeuronId;
outMin = INPUT_SIZE + 1;
outMax = INPUT_SIZE + OUTPUT_SIZE;
break;
}
default:
break;
}
int attempt = 0;
do {
in = randomInteger(inMin, inMax);
out = randomInteger(outMin, outMax);
if (++attempt > 20) {
return;
}
} while (!m_connectivity.link(in, out));
Link* link = m_connectivity.link(in, out);
link->predecessor()->removeOutConnection(link);
link->successor()->removeInConnection(link);
m_connectivity.removeLink(in, out);
delete link;
link = 0;
break;
}
case AddLink: {
int in = 0;
int out = 0;
bool found = false;
int linkType = randomInteger(1, 2);
int inMin, inMax, outMin, outMax;
inMin = inMax = outMin = outMax = 0;
/*
* Again determines the accepted range (see RemoveLink case).
*/
switch (linkType) {
case 1: { /* Link between input and hidden neuron */
inMin = 1;
inMax = INPUT_SIZE;
outMin = INPUT_SIZE + OUTPUT_SIZE + 1;
outMax = maxNeuronId;
break;
}
case 2: { /* Link between hidden and output neuron */
inMin = INPUT_SIZE + OUTPUT_SIZE + 1;
inMax = maxNeuronId;
outMin = INPUT_SIZE + 1;
outMax = INPUT_SIZE + OUTPUT_SIZE;
break;
}
}
/*
* Check all the pairs of neurons, and pick the first pair that isn't connected by a link.
*/
for (in = inMin; in <= inMax; in++) {
for (out = outMin; out <= outMax; out++) {
if (!m_connectivity.link(in, out) && !m_connectivity.link(out, in)) {
found = true;
Link* link = new Link(randomWeight(), m_neurons[in], m_neurons[out]);
link->predecessor()->addOutConnection(link);
link->successor()->addInConnection(link);
m_connectivity.addLink(in, out, link);
break;
}
}
if (found) {
break;
}
}
break;
}
/*
* NOTE: when removing a neuron, the last one in the list, i.e. the one with the biggest ID, gets the ID of the removed
* one (if they don't coincide). This simplifies a lot of loops in the network.
*/
case RemoveNeuron: {
if (m_hiddenNeurons.size() <= HIDDEN_SIZE_MIN) { /* avoid removing all neurons, and mutate weights instead */
applyGaussianMutation();
break;
}
int neuronId = randomInteger(INPUT_SIZE + OUTPUT_SIZE + 1, maxNeuronId);
Neuron* neuron = m_neurons[neuronId];
m_hiddenNeurons.removeOne(neuron);
m_neurons.remove(neuronId);
m_connectivity.removeAllLinks(neuronId, maxNeuronId, m_neurons);
if (neuronId != maxNeuronId) {
Neuron* latestNeuron = m_neurons[maxNeuronId];
int oldId = latestNeuron->id();
latestNeuron->setId(neuronId);
m_neurons.remove(oldId);
m_neurons.insert(neuronId, latestNeuron);
m_connectivity.changeId(oldId, neuronId);
}
maxNeuronId--;
delete neuron;
neuron = 0;
break;
}
/*
* We always add an hidden neuron.
*/
case AddNeuron: {
if (m_hiddenNeurons.size() >= HIDDEN_SIZE_MAX) {
applyGaussianMutation();
break;
}
int neuronId = ++maxNeuronId;
Neuron* neuron = new SigmoidNeuron(neuronId, Neuron::HiddenLayer);
createBiasLink(m_neurons[-1], neuron);
m_neurons.insert(neuronId, neuron);
m_hiddenNeurons.append(neuron);
/*
* Randomly connects with the input layer
*/
Q_FOREACH (Neuron* inputNeuron, m_inputNeurons) {
int choice = randomInteger(1, 2);
if (choice == 1) {
Link* link = new Link(randomWeight(), inputNeuron, neuron);
inputNeuron->addOutConnection(link);
neuron->addInConnection(link);
m_connectivity.addLink(inputNeuron->id(), neuronId, link);
}
}
/*
* Since this is a binary classification problem, there is only one output neuron, and we always connect this one
* to it (the link may be removed by further mutations).
*/
Q_FOREACH (Neuron* outputNeuron, m_outputNeurons) {
Link* link = new Link(randomWeight(), neuron, outputNeuron);
neuron->addOutConnection(link);
outputNeuron->addInConnection(link);
m_connectivity.addLink(neuronId, outputNeuron->id(), link);
}
break;
}
case WeightMutation: {
applyGaussianMutation();
break;
}
default:
break;
}
}
void Network::applyGaussianMutation()
{
Q_FOREACH (Link* link, m_connectivity.links()) {
double newWeight = gaussianMutation( link->weight(), 0, 0.05 );
link->setWeight(newWeight + link->weight());
}
}
double Network::error() const
{
return m_lastError;
}
double Network::output() const
{
return m_lastOutput;
}
int Network::complexity() const
{
return m_connectivity.complexity();
}
double Network::sparsity() const
{
return m_sparsity;
}
void Network::setSparsity(double s)
{
m_sparsity = s;
}
void Network::addSparsity(double s)
{
m_sparsity += s;
}
void Network::updateByRProp()
{
/* Train weights */
Q_FOREACH (Link* link, m_connectivity.links()) {
double gradient = link->gradient();
double signChange = gradient * link->previousGradient();
// qDebug() << "Gradients:" << gradient << link->previousGradient();
double delta = 0.0f;
double oldDelta = link->delta();
double weightChange = 0.0;
if (signChange > 0) {
delta = minimum(oldDelta * POSITIVE_ETA, MAX_STEP);
weightChange = (gradient > 0) ? -delta : +delta; /* sign function */
}
else if (signChange < 0) {
delta = max(oldDelta * NEGATIVE_ETA, MIN_STEP);
if (m_lastError > m_oldError) { /* Rprop+ condition */
weightChange = (gradient > 0) ? +delta : -delta;
}
link->setGradient(0);
} else {
delta = oldDelta;
weightChange = (gradient > 0) ? -oldDelta : +oldDelta;
}
link->setWeight( link->weight() + weightChange );
link->setDelta(delta);
}
}
double Network::averageError() const
{
return m_averageError;
}
void Network::setAverageError(double a)
{
m_averageError = a;
}