-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNN_NeuralNetwork.hpp
390 lines (339 loc) · 13.2 KB
/
RNN_NeuralNetwork.hpp
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
/*
* RNN_NeuralNetwork.hpp
*
* Revision: November 2018
* Author: Thassyo Pinto - [email protected]
*/
#ifndef RNN_NEURALNETWORK_HPP_
#define RNN_NEURALNETWORK_HPP_
// Standard libraries
#include <algorithm>
#include <fstream>
#include <vector>
// Local libraries
#include "Misc_Random.hpp"
#include "RNN_Neuron.hpp"
#include "RNN_Connection.hpp"
// An artificial neural network class.
template<typename Neuron_t = Neuron, typename Connection_t = Connection>
class NeuralNetwork{
public:
// Builds a recurrent neural network with the supplied number of input and output neurons.
NeuralNetwork(size_t nbOfInputs = 4, size_t nbOfOutputs = 8){
this->setInputs(nbOfInputs);
this->setOutputs(nbOfOutputs);
size_t numberOfNeurons = _nbOfInputs + _nbOfOutputs;
// Creates neurons.
for(int i = 0; i < numberOfNeurons; i++){
this->addNeuron();
}
// Creates connections.
for(int i = 0; i < _nbOfInputs; i++){
for(int j = 0; j < _nbOfOutputs; j++){
this->addConnection(i, j + _nbOfInputs, 0.0);
}
}
}
// Adds a neuron to this network.
void addNeuron(){
_neurons.push_back(Neuron());
}
// Adds a connection between the two indicated neurons.
void addConnection(size_t sourceIndex, size_t targetIndex, double weight = 0.0){
_connections.push_back(Connection(sourceIndex, targetIndex, weight));
this->addIncoming(targetIndex, _connections.size() - 1);
this->addOutgoing(sourceIndex, _connections.size() - 1);
}
// Removes a connection between the two indicated neurons.
void removeConnection(size_t connectionIndex, size_t sourceIndex, size_t targetIndex){
_connections.erase(_connections.begin() + connectionIndex);
this->updateIndices(_neurons[sourceIndex].getOutgoingIndices(), connectionIndex);
this->updateIndices(_neurons[targetIndex].getIncomingIndices(), connectionIndex);
}
// Updates the vector of connection indices after removing an existing connection.
void updateIndices(std::vector<size_t> vect, size_t value){
vect.erase(std::remove(vect.begin(), vect.end(), value), vect.end());
for(int i = 0; i < vect.size(); i++){
if(vect[i] > value){
vect[i] = vect[i]-1;
}
}
}
// Sets the number of inputs of this network.
void setInputs(size_t nbOfInputs){
_nbOfInputs = nbOfInputs;
}
// Sets the number of outputs of this network.
void setOutputs(size_t nbOfOutputs){
_nbOfOutputs = nbOfOutputs;
}
// Returns the number of inputs of this network.
size_t getInputs(){
return _nbOfInputs;
}
// Returns the number of outputs of this network.
size_t getOutputs(){
return _nbOfOutputs;
}
// Sets the minimum weight that a connection may get due to randomization or mutation.
void setMinWeight(double minWeight){
_minWeight = minWeight;
}
// Sets the maximum weight that a connection may get due to randomization or mutation.
void setMaxWeight(double maxWeight){
_maxWeight = maxWeight;
}
// Sets the weight mutation rate.
void setWeightMutRate(double weightMutRate){
_weightMutRate = weightMutRate;
}
// Sets the neuron mutation rate.
void setNeuronMutRate(double neuronMutRate){
_neuronMutRate = neuronMutRate;
}
// Sets the add neuron mutation rate.
void setAddNeuronMutRate(double addNeuronMutRate){
_addNeuronMutRate = addNeuronMutRate;
}
// Sets the add connection mutation rate.
void setAddConnectionMutRate(double addConnectionMutRate){
_addConnectionMutRate = addConnectionMutRate;
}
// Returns a reference to the vector of neurons of this network.
std::vector<Neuron_t>& getNeurons(){
return _neurons;
}
// Returns a reference to the vector of connections of this network.
std::vector<Connection_t>& getConnections(){
return _connections;
}
// Sets the activation value of the indicated neuron.
void setValue(size_t neuronIndex, double value){
_neurons[neuronIndex].setValue(value);
}
// Returns the activation value of the indicated neuron.
double getValue(size_t neuronIndex){
if(neuronIndex >= _neurons.size()){
std::cerr << "Index out of bounds! Index: " << neuronIndex << " size: " << _neurons.size() << std::endl;
}
return _neurons[neuronIndex].getValue();
}
// Sets the bias of the indicated neuron.
void setBias(size_t neuronIndex, double bias){
_neurons[neuronIndex].setBias(bias);
}
// Returns the bias of the indicated neuron.
double getBias(size_t neuronIndex){
if(neuronIndex >= _neurons.size()){
std::cerr << "Index out of bounds! Index: " << neuronIndex << " size: " << _neurons.size() << std::endl;
}
return _neurons[neuronIndex].getBias();
}
// Returns the activation value of the indicated output neuron.
double getOutputValue(size_t neuronIndex){
return this->getValue(neuronIndex + _nbOfInputs);
}
// Sets the amount of incoming potential of the indicated neuron.
void setIncoming(size_t neuronIndex, double incoming){
_neurons[neuronIndex].setIncoming(incoming);
}
// Updates the current incoming potential of the indicated neuron.
void updateIncoming(size_t neuronIndex, double update){
_neurons[neuronIndex].updateIncoming(update);
}
// Updates the activation value of the indicated neuron.
void propagateNeuron(size_t neuronIndex){
_neurons[neuronIndex].propagate();
}
// Resets the state of the indicated neuron.
void resetNeuron(size_t neuronIndex){
_neurons[neuronIndex].reset();
}
// Adds the index of an incoming connection to the indicated neuron.
void addIncoming(size_t neuronIndex, size_t incomingIndex){
_neurons[neuronIndex].addIncoming(incomingIndex);
}
// Adds the index of an outgoing connection to the indicated neuron.
void addOutgoing(size_t neuronIndex, size_t outgoingIndex){
_neurons[neuronIndex].addOutgoing(outgoingIndex);
}
// Sets the weight of the indicated connection.
void setWeight(size_t connectionIndex, double weight){
_connections[connectionIndex].setWeight(weight);
}
// Returns the weight of the indicated connection.
double getWeight(size_t connectionIndex){
if(connectionIndex >= _connections.size()){
std::cerr << "Index out of bounds! Index: " << connectionIndex << " size: " << _connections.size() << std::endl;
}
return _connections[connectionIndex].getWeight();
}
// Returns the source of the indicated connection.
size_t getSource(size_t connectionIndex){
if(connectionIndex >= _connections.size()){
std::cerr << "Index out of bounds! Index: " << connectionIndex << " size: " << _connections.size() << std::endl;
}
return _connections[connectionIndex].getSource();
}
// Returns the target of the indicated connection.
size_t getTarget(size_t connectionIndex){
if(connectionIndex >= _connections.size()){
std::cerr << "Index out of bounds! Index: " << connectionIndex << " size: " << _connections.size() << std::endl;
}
return _connections[connectionIndex].getTarget();
}
// Performs one update of network activation.
void update(){
// Resets the incoming values of neurons to 0.
for(int i = 0; i < _neurons.size(); i++){
this->setIncoming(i, 0.0);
}
// Updates the incoming values of all neurons by iterating over all their incoming connections.
for(int i = 0; i < _neurons.size(); i++){
std::vector<size_t> incomingConnections = _neurons[i].getIncomingIndices();
for(int j = 0; j < incomingConnections.size(); j++){
double updateValue = this->getValue(this->getSource(incomingConnections[j])) * this->getWeight(incomingConnections[j]);
this->updateIncoming(i, updateValue);
}
}
// Propagates the incoming value to become the current activation of that neuron.
for(int i = 0; i < _neurons.size(); i++){
this->propagateNeuron(i);
}
}
// Randomizes the network.
void randomize(){
// Assigns each neuron, uniform randomly, a bias in [_minWeight, _maxWeight]
for(int i = 0; i < _neurons.size(); i++){
this->setBias(i, randDouble(_minWeight, _maxWeight));
}
// Assigns each connection, uniform randomly, a weight in [_minWeight, _maxWeight]
for(int i = 0; i < _connections.size(); i++){
this->setWeight(i, randDouble(_minWeight, _maxWeight));
}
}
// Mutates the neural network.
void mutate(){
// Probability of neuron-i bias to be mutated.
for(int i = 0; i < _neurons.size(); i++){
if(randDouble() <= _neuronMutRate){
double randBias = randGaussian();
if(randBias < _minWeight) this->setBias(i, _minWeight);
else if(randBias > _maxWeight) this->setBias(i, _maxWeight);
else this->setBias(i, randBias);
}
}
// Probability of connection-i weight to be mutated.
for(int i = 0; i < _connections.size(); i++){
if(randDouble() <= _weightMutRate){
double randWeight = randGaussian();
if(randWeight < _minWeight) this->setWeight(i, _minWeight);
else if(randWeight > _maxWeight) this->setWeight(i, _maxWeight);
else this->setWeight(i, randWeight);
}
}
// Probability of adding a new neuron.
if(randDouble() <= _addNeuronMutRate){
// Adds a new neuron.
this->addNeuron();
// Randomly selects an existing connection.
size_t randConnection = randDouble(_connections.size() - 1);
size_t randSource = this->getSource(randConnection);
size_t randTarget = this->getTarget(randConnection);
double randWeight = this->getWeight(randConnection);
// Removes random connection and update indices.
this->removeConnection(randConnection, randSource, randTarget);
// Adds connections of the new neuron.
addConnection(randSource, _neurons.size()-1, 1.0);
addConnection(_neurons.size()-1, randTarget, randWeight);
}
// Probability of adding a new connection.
if(randDouble() <= _addConnectionMutRate){
size_t nodeIndex = 0;
size_t newSource = randIndex(_neurons.size());
std::vector<size_t> nodeSet;
nodeSet.clear();
// Remove input neurons from node set.
for(int i = _nbOfInputs; i < _neurons.size(); i++){
nodeSet.push_back(i);
}
// Adds new connection to available node.
if(!nodeSet.empty()){
size_t newTarget = nodeSet[randIndex(nodeSet.size())];
this->addConnection(newSource, newTarget, randDouble(_minWeight, _maxWeight));
}
}
}
// Initializes the network with specific values and weights.
void initialize(double initValue, double initWeight){
for(int i = 0; i < _neurons.size(); i++){
this->setValue(i, initValue);
}
for(int i = 0; i < _connections.size(); i++){
this->setWeight(i, initWeight);
}
}
// Resets all neurons in the network.
void reset(){
for(int i = 0; i < _neurons.size(); i++){
this->resetNeuron(i);
}
}
// Writes the current activation of the network to the output stream.
void logActivation(std::ofstream& activationFile){
if(activationFile.is_open()){
activationFile << this->getValue(0);
for(int i = 1; i < _neurons.size(); i++){
activationFile << " " << this->getValue(i);
}
activationFile << "\n";
}
}
// Run the neural network for a given number of updates.
void run(size_t numberOfUpdates, std::string activationFileName = ""){
std::ofstream actFile;
if(activationFileName != "") actFile.open(activationFileName);
for(int i = 0; i < numberOfUpdates; i++){
this->update();
this->logActivation(actFile);
this->mutate();
}
actFile.close();
}
protected:
//Vectors containing neurons and connections.
std::vector<Neuron_t> _neurons;
std::vector<Connection_t> _connections;
// Number of inputs and outputs.
size_t _nbOfInputs;
size_t _nbOfOutputs;
// Weight constraints.
double _minWeight;
double _maxWeight;
//Mutation rates
double _weightMutRate;
double _neuronMutRate;
double _addNeuronMutRate;
double _addConnectionMutRate;
};
// Convenience function for writing network connections to a file-stream.
std::ostream& operator<<(std::ostream& is, NeuralNetwork<Neuron, Connection>& obj){
std::vector<Neuron> neurons = obj.getNeurons();
std::vector<Connection> connections = obj.getConnections();
size_t nbOfInputs = obj.getInputs();
size_t nbOfOutputs = obj.getOutputs();
is << nbOfInputs << " ";
is << nbOfOutputs << " ";
is << neurons.size() << " ";
is << connections.size() << " ";
// Write neurons to file.
for(size_t i=0; i<neurons.size(); ++i){
is << neurons[i].getBias() << " ";
}
// Write connections to file.
for(size_t i=0; i<connections.size(); ++i){
is << connections[i].getSource() << " " << connections[i].getTarget() << " "<< connections[i].getWeight()<< " ";
}
return is;
}
#endif /* RNN_NEURALNETWORK_HPP_ */