-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHiddenNeuron.cpp
More file actions
355 lines (265 loc) · 12.9 KB
/
HiddenNeuron.cpp
File metadata and controls
355 lines (265 loc) · 12.9 KB
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
/*
* HiddenNeuron.cpp
*
* Copyright 2018 OFTNAI. All rights reserved.
*
*/
#include "HiddenNeuron.h"
#include "Param.h"
#include "Synapse.h"
#include "Region.h"
#include "HiddenRegion.h"
#include "InputRegion.h"
#include "InputNeuron.h"
#include "BinaryWrite.h"
#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;
using std::cerr;
void HiddenNeuron::init(HiddenRegion * region,
u_short depth,
u_short row,
u_short col,
float * activationHistory,
float * inhibitedActivationHistory,
float * firingRateHistory,
float * traceHistory,
float * stimulationHistory,
float * effectiveTraceHistory,
bool saveNeuronHistory,
bool saveSynapseHistory,
u_short desiredFanIn,
float weightVectorLength,
int fixedBufferWeightHistorySize) {
// Call base constructor
Neuron::init(region, depth, row, col);
// Set vars
this->neuronHistoryCounter = 0;
this->synapseHistoryCounter = 0;
this->timeStep = 0; // dnavarro2015 Implementing anti-Hebbian learning rule 10 (Rolls and Stringer, 2001)
this->saveNeuronHistory = saveNeuronHistory;
this->saveSynapseHistory = saveSynapseHistory;
this->desiredFanIn = desiredFanIn;
this->weightVectorLength = weightVectorLength;
// Setup buffer pointers
this->activationHistory = activationHistory;
this->inhibitedActivationHistory = inhibitedActivationHistory;
this->firingRateHistory = firingRateHistory;
this->traceHistory = traceHistory;
this->effectiveTraceHistory = effectiveTraceHistory;
this->stimulationHistory = stimulationHistory;
// Allocate buffer space.
this->fixedBufferWeightHistorySize = fixedBufferWeightHistorySize;
this->fixedBufferTraceHistory = new float[fixedBufferWeightHistorySize];
this->lastTraceBufferElement = 0; // init buffer indicator
// Reserve, so only capacity changes, not size
this->afferentSynapses.reserve(desiredFanIn);
// Initialize all state variables to zero
clearState(true);
}
HiddenNeuron::~HiddenNeuron() {
afferentSynapses.clear();
}
void HiddenNeuron::addAfferentSynapse(const Neuron * preSynapticNeuron, float weight) {
float * buffer;
if (saveSynapseHistory) {
if(desiredFanIn == afferentSynapses.size()) {
cerr << "Attempting to add more synapses then there is space for in neuron buffer, blanknetwork does not match!" << endl;
exit(EXIT_FAILURE);
}
// Ask region for history buffer
//if(saveSynapseHistory) {
//
// cout << ">> Allocating for synapse buffer for neuron (" << row << "," << col << ")" << endl;
//}
buffer = (static_cast<HiddenRegion *>(region))->getSynapseHistorySlot();
}
else
buffer = NULL;
// Add synapse to synapse lisr
afferentSynapses.push_back(Synapse(weight, preSynapticNeuron, this, buffer));//, fixedBufferWeightHistorySize)); // historyLength
}
void HiddenNeuron::setupAfferentSynapses(Region & preSynapticRegion, CONNECTIVITY connectivity, INITIALWEIGHT initialWeight, gsl_rng * rngController) {
if(connectivity == FULL) {
for(int d = 0;d < preSynapticRegion.depth;d++)
for(int i = 0;i < preSynapticRegion.verDimension;i++)
for(int j = 0;j < preSynapticRegion.horDimension;j++) {
float weight = initialWeight != ZERO ? static_cast<float>(gsl_rng_uniform(rngController)) : 0;
addAfferentSynapse(preSynapticRegion.getNeuron(d, i, j), weight);
}
} else if (connectivity == SPARSE) {
for(int d = 0;d < preSynapticRegion.depth;d++) {
u_short connectionsMade = 0;
while(connectionsMade < desiredFanIn) {
// Sample location
unsigned long int rowSource = gsl_rng_uniform_int(rngController, preSynapticRegion.verDimension);
unsigned long int colSource = gsl_rng_uniform_int(rngController, preSynapticRegion.horDimension);
// Grab neuron
Neuron * preSynapticNeuron = preSynapticRegion.getNeuron(d, rowSource, colSource);
// Make sure we don't reconnect
if(!areYouConnectedTo(preSynapticNeuron)) {
float weight = initialWeight != ZERO ? static_cast<float>(gsl_rng_uniform(rngController)) : 0;
addAfferentSynapse(preSynapticNeuron, weight);
connectionsMade++;
}
}
}
} else if (connectivity == SPARSE_BIASED) {
cerr << "BIASED BIASED BIASED" << endl;
u_short connectionsMade = 0;
// Sample row
unsigned long int rowSource = gsl_rng_uniform_int(rngController, preSynapticRegion.verDimension);
// In the future just set int rowSourcMean to something, and use it to set int rowSource = Gauss(rowSourcMean) inside loop
while(connectionsMade < desiredFanIn) {
// Sample location
unsigned long int colSource = gsl_rng_uniform_int(rngController, preSynapticRegion.horDimension);
for(int d = 0;d < preSynapticRegion.depth;d++) {
// Grab neuron
Neuron * preSynapticNeuron = preSynapticRegion.getNeuron(d, rowSource, colSource);
// Make sure we don't reconnect - NOT NECASsARY necessary
//if(areYouConnectedTo(preSynapticNeuron)) {
//
// cerr << "Tried to reconnect, failure!" << endl;
// isTraining(isTraining_FAILURE);
//}
//
float weight = initialWeight != ZERO ? static_cast<float>(gsl_rng_uniform(rngController)) : 0;
addAfferentSynapse(preSynapticNeuron, weight);
connectionsMade++;
}
}
} else {
cerr << "Incorrect connectivity parameter!" << endl;
exit(EXIT_FAILURE);
}
}
bool HiddenNeuron::areYouConnectedTo(const Neuron * n) {
for(u_short s = 0;s < afferentSynapses.size();s++)
if(afferentSynapses[s].preSynapticNeuron == n)
return true;
return false;
}
// dnavarro2016 convergence
unsigned long int HiddenNeuron::getTotalNumberAfferentSynapses() {
return afferentSynapses.size();
}
void HiddenNeuron::output(BinaryWrite & file, DATA data) {
if(data == FIRING_RATE)
output(file, firingRateHistory);
else if(data == ACTIVATION)
output(file, activationHistory);
else if(data == INHIBITED_ACTIVATION)
output(file, inhibitedActivationHistory);
else if(data == TRACE)
output(file, traceHistory);
else if(data == STIMULATION)
output(file, stimulationHistory);
else if(data == EFFECTIVE_TRACE)
output(file, effectiveTraceHistory);
else if(data == FAN_IN_COUNT)
file << static_cast<u_short>(afferentSynapses.size());
else if(data == WEIGHTS_FINAL) {
// Iterate afferent synapses
for(std::vector<Synapse>::iterator s = afferentSynapses.begin(); s != afferentSynapses.end();s++) {
const Neuron * n = (*s).preSynapticNeuron;
file << n->region->regionNr << n->depth << n->row << n->col << (*s).weight;
}
} else if(data == WEIGHT_HISTORY) {
// Iterate afferent synapses
for(std::vector<Synapse>::iterator s = afferentSynapses.begin(); s != afferentSynapses.end();s++) {
// Output presynaptic neuron description
const Neuron * n = (*s).preSynapticNeuron;
file << n->region->regionNr << n->depth << n->row << n->col;
// Output weight history for this synapse
for(unsigned long t = 0;t < synapseHistoryCounter;t++)
file << (*s).weightHistory[t];
}
} else if(data == WEIGHT_AND_NEURON_HISTORY) {
// Output neuron description
file << region->regionNr << depth << row << col << static_cast<u_short>(afferentSynapses.size());
// Output neuron history
output(file, firingRateHistory);
output(file, activationHistory);
output(file, inhibitedActivationHistory);
output(file, traceHistory);
output(file, stimulationHistory);
output(file, effectiveTraceHistory);
// Dump synapse descriptins afferent synapses
for(std::vector<Synapse>::iterator s = afferentSynapses.begin(); s != afferentSynapses.end();s++) {
const Neuron * n = (*s).preSynapticNeuron;
file << n->region->regionNr << n->depth << n->row << n->col; // region, depth, row, col
}
// Dump afferent synapses histories
for(std::vector<Synapse>::iterator s = afferentSynapses.begin(); s != afferentSynapses.end();s++) {
// Output weight history for this synapse
for(unsigned long t = 0;t < synapseHistoryCounter;t++)
file << (*s).weightHistory[t];
}
}
}
void HiddenNeuron::output(BinaryWrite & file, const float * buffer) {
for(unsigned long t = 0;t < neuronHistoryCounter;t++)
file << buffer[t];
}
//////////////////////////////////////////////////////////////////////////
// Trace buffer
//////////////////////////////////////////////////////////////////////////
float HiddenNeuron::getDelayedTrace() {
// The elemnt in the buffer directly behind (lower index, with wrap arround) the oldest, is the newest
return fixedBufferTraceHistory[lastTraceBufferElement];
}
void HiddenNeuron::addNewTraceValueToTraceBuffer() {
// Save new addition in the position of the oldest
fixedBufferTraceHistory[lastTraceBufferElement] = newTrace;
// move position of the oldest along
lastTraceBufferElement = (lastTraceBufferElement == fixedBufferWeightHistorySize-1) ? 0 : lastTraceBufferElement+1;
}
// dnavarro2015 Implementing anti-Hebbian learning rule 11 (Rolls and Stringer, 2001)
void HiddenNeuron::addMyDelayedFiringRate(float delayedFiringRate) {
myFiringRateHistory.push_back(delayedFiringRate);
}
// dnavarro2015 Implementing anti-Hebbian learning rule 11 (Rolls and Stringer, 2001)
float HiddenNeuron::getMyDelayedFiringRate(int timeStep, int delayedTime) {
/*if (delayedTraceTime > myTraceHistory.size()) { // not likely to get this exception
cerr << "Array Index Out of Bounds Exception:\n Required " << delayedTraceTime << " but we only have: " << myTraceHistory.size();
exit(EXIT_FAILURE);
}*/
if ((timeStep - delayedTime) < 0) {
return 0; // if there is still no trace time for the current neuron, then the trace value is zero
}
return myFiringRateHistory.at((timeStep - delayedTime)); //(delayedTraceTime >= 0 ? myTraceHistory[delayedTraceTime] : 0);
}
// dnavarro2015 Implementing anti-Hebbian learning rule 10 (Rolls and Stringer, 2001)
/**
* TODO indexing vector by 'n', include trace time value, test it!! ---> define "neuronID"
*/
void HiddenNeuron::addMyDelayedTrace(float delayedTraceTime) {
myTraceHistory.push_back(delayedTraceTime);
}
// dnavarro2015 Implementing anti-Hebbian learning rule 10 (Rolls and Stringer, 2001)
/**
* TODO Handle vector indexed by 'n' with trace history vectors
*/
float HiddenNeuron::getMyDelayedTrace(int timeStep, int delayedTraceTime) {
if ((timeStep - delayedTraceTime) < 0) {
return 0; // if there is still no trace value for the current neuron at the present time, then the trace value is zero
}
return myTraceHistory.at((timeStep - delayedTraceTime)); //(delayedTraceTime >= 0 ? myTraceHistory[delayedTraceTime] : 0);
}
// dnavarro2016 convergenceTake2
void HiddenNeuron::addMySquareDiffValue(int timeStep, int delayedConvergenceTime, float currenWeight) {
float auxConvValue;
// if (((timeStep - delayedConvergenceTime) >= 0)) { // if we already do have some synaptic weight to compare with, then
auxConvValue = getMyDelayedTrace(timeStep, delayedConvergenceTime);
mySquareDiffValue.push_back((auxConvValue-currenWeight)*(auxConvValue-currenWeight));
// }
}
float HiddenNeuron::getMyRMSConvergenceValue(int timeStep, int delayedTraceTime) {
float rms = 0;
for (std::vector<float>::iterator it = mySquareDiffValue.begin() ; it != mySquareDiffValue.end(); ++it) {
rms += mySquareDiffValue.at(*it);
}
//return rms/mySquareDiffValue.size(); //(delayedTraceTime >= 0 ? myTraceHistory[delayedTraceTime] : 0);
return mySquareDiffValue.at(timeStep);
}