-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
375 lines (267 loc) · 18.5 KB
/
main.cpp
File metadata and controls
375 lines (267 loc) · 18.5 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//
// main.cpp
// exp2
//
// Created by Dan the Destroyer on 02/05/2017.
// Copyright © 2017 OFTNAI. All rights reserved.
//
#include <iostream>
#include <iomanip>
#include "Population.hpp"
#include "engine.h"
#include <math.h>
#include <iomanip>
#define BUFSIZE 256
template <typename T>
std::vector<T> linspace(T begin, T end, size_t N);
void testNeuronResponseAgainstEyePositionsInItsPreferredRetinalLocation(int neuron_index, std::vector<float> testing_eye_positions, Neuron *chosen_neuron);
int main(int argc, const char * argv[]) {
/******************/
float r1 = -10, r2 = 10;
float e1 = -35, e2 = 35;
float sparseness = 1;
// create linearly spaced retinal locations [r1,r2] and eye positions intervals [e1.e2]
std::vector<float> preferred_retinal_locations = linspace(r1, r2, (r2-r1+1));
std::vector<float> preferred_eye_positions = linspace(e1, e2, (e2-e1+1));
// || \\ || // || \\ || // || \\ || // || \\ || // || \\ || // || \\ // || \\ || // || \\ || // || \\ // || \\ || // || \\ || // ||
// Print all retinal locations
// for (int i = 0; i < preferred_retinal_locations.size(); i++) {
// if (preferred_retinal_locations.at(i) == 0) std::cout << std::endl << "there you go: " << i << std::endl << std::endl;
// std::cout << preferred_retinal_locations.at(i) << ", ";
//}
//std::cout << std::endl << std::endl;
// Print all retinal locations
// || \\ || // || \\ || // || \\ || // || \\ || // || \\ || // || \\ // || \\ || // || \\ || // || \\ // || \\ || // || \\ || // ||
// (i) creates our population of neurons
Population outputPopulation = *new Population(preferred_retinal_locations, preferred_eye_positions, sparseness);
// END OF (i)
// (ii) creates testing intervals (same as preferred for the moment)
e1= -35; e2= 35;
std::vector<float> testing_eye_positions = linspace(e1, e2, (e2-e1+1)); //(r2-r1+1));
r1 = -10; r2 = 10;
std::vector<float> testing_retinal_locations = linspace(r1, r2, (r2-r1+1));
// END OF (ii)
// save lists of firing rates that are exported as csv files
std::vector<float> firing_rate_list(71);
// (iii) calculates internal activation for all output neurons (optionally prints data for neuron at index 'neuron_index'
int neuron_index = 1489;
Neuron *chosen_neuron = &outputPopulation.neurons.at(neuron_index);
float preferred_retinal_location = -5; //chosen_neuron->GetPreferredRetinalLocation();
std::vector<float>::iterator r;
std::vector<float>::iterator e;
std::vector<Neuron>::iterator neuron;
std::vector<Neuron>::iterator neuron_i;
std::vector<Neuron>::iterator neuron_j;
float sigmoidGain = .48;
float alpha = 5.125;
// (neuron_index, testing_eye_positions, chosen_neuron);
//std::cout << "Hey Dan: " << outputPopulation.getNeuronAtIndex(neuron_index).GetInternalActivation() << std::endl;
std::cout << "\t\t\t\t\t\t\t\t\t\t\t.:: Neuron #" << outputPopulation.neurons.at(neuron_index).GetNeuronID() << " e=" << outputPopulation.neurons.at(neuron_index).GetPreferredEyePosition() << " r=" << outputPopulation.neurons.at(neuron_index).GetPreferredRetinalLocation() << " ::." << std::endl; // print
for (neuron_i = outputPopulation.neurons.begin(); neuron_i != outputPopulation.neurons.end(); neuron_i++) {
for (e = testing_eye_positions.begin(); e != testing_eye_positions.end(); e++) {
neuron_i->ComputeInternalActivation(*e, neuron_i->GetPreferredRetinalLocation());
neuron_i->SetFiringRate((1 / (1 + exp(-sigmoidGain*(neuron_i->GetInternalActivation() + - alpha)))));
//if (neuron_i->GetNeuronID() == neuron_index) std::cout << "Firing rate of neuron #" << neuron_i->GetNeuronID() << " for eye-position " << *e << " and visual target location " << neuron_i->GetPreferredRetinalLocation() << ": " << neuron_i->GetFiringRate() << std::endl;
std::cout << neuron_i->GetFiringRate() << ' ';
}
std::cout << ";" << std::endl;
}
std::cout << std::endl << "Hello Dan" << std::endl;
// code from 'exp2' in this same directory. See README for details.
/*for (e = testing_eye_positions.begin(); e != testing_eye_positions.end(); e++) {
//std::cout << ";" << std::endl;
//std::cout << *e << ' ';
// (i) internal activation of all output neurons (depends only on preferred and current eye-positions/retinal locations)
for (neuron = outputPopulation.neurons.begin(); neuron != outputPopulation.neurons.end(); neuron++) {
neuron->ComputeInternalActivation(*e, neuron->GetPreferredRetinalLocation());
}
// (ii) firing rate of all output neurons
float sum_Wij_times_rj = 0;
int initial_condition = 0; // until sets on a stable state (currently doing it twice)
for (int i=initial_condition; i<5; i++) {
for (neuron_i = outputPopulation.neurons.begin(); neuron_i != outputPopulation.neurons.end(); neuron_i++) {
for (neuron_j = outputPopulation.neurons.begin(); neuron_j != outputPopulation.neurons.end(); neuron_j++) {
// multiply each individual dist_Wij by correspondent rj (based on neuronID == index) <===l;'
sum_Wij_times_rj += (neuron_i->GetWijArrayAtIndex(neuron_j->GetNeuronID())*neuron_j->GetFiringRate());
}
neuron_i->SetFiringRate((1 / (1 + exp(-sigmoidGain*(neuron_i->GetInternalActivation() + 0*sum_Wij_times_rj - alpha)))));
// prints firing rates of chosen_neuron for all testing_eye_positions
if (i == 4 && neuron_i->GetNeuronID() == neuron_index) {
//std::cout << "Firing rate for e = " << *e << ": " << neuron_i->GetFiringRate() << std::endl;
std::cout << neuron_i->GetFiringRate() << ' ';
//firing_rate_list.push_back(neuron_i->GetFiringRate());
}
/ * prints firing rates for preferred eye-position and r == preferred_retinal_location
if (i == 4 && neuron_i->GetPreferredRetinalLocation() == preferred_retinal_location && neuron_i->GetPreferredEyePosition() == (int) *e) {
std::cout << "Neuron ID " << neuron_i->GetNeuronID() << " with preferred e = " << neuron_i->GetPreferredEyePosition() << " and r = " << neuron_i->GetPreferredRetinalLocation() << " has firing rate of " << neuron_i->GetFiringRate() << std::endl;
//std::cout << neuron_i->GetFiringRate() << ' ';
} * /
}
}
//std::cout << "r = " << preferred_retinal_location << " and e = " << *e << " firing rate = " << chosen_neuron->GetFiringRate() << std::endl;
//std::cout << chosen_neuron->GetFiringRate() << ' ';
} */
// uncomment for (e = testing_eye_positions.begin(); e != testing_eye_positions.end(); e++) {
// uncomment for (r = testing_retinal_locations.begin(); r != testing_retinal_locations.end(); r++) {
// (i) internal activation of chosen output neuron
//chosen_neuron->ComputeInternalActivation(*e, *r);
// (ii) internal activation of all output neurons
// uncomment for (neuron = outputPopulation.neurons.begin(); neuron != outputPopulation.neurons.end(); neuron++) {
// uncomment neuron->ComputeInternalActivation(*e, *r);
// uncomment }
// (iii) firing rate of all output neurons
// uncomment float sum_Wij_times_rj = 0;
// uncomment int initial_condition = 4;
// uncomment for (int i=initial_condition; i<5; i++) {
// uncomment for (neuron_i = outputPopulation.neurons.begin(); neuron_i != outputPopulation.neurons.end(); neuron_i++) {
// uncomment for (neuron_j = outputPopulation.neurons.begin(); neuron_j != outputPopulation.neurons.end(); neuron_j++) {
// multiply each individual dist_Wij by correspondent rj (based on neuronID == index) <===l;'
// uncomment sum_Wij_times_rj += (neuron_i->GetWijArrayAtIndex(neuron_j->GetNeuronID())*neuron_j->GetFiringRate());
// uncomment }
//std::cout << "1 + " << (neuron_i->GetInternalActivation() + sum_Wij_times_rj - alpha) << std::endl;
// uncomment neuron_i->SetFiringRate((1 / (1 + exp(-sigmoidGain*(neuron_i->GetInternalActivation() + sum_Wij_times_rj - alpha)))));
//std::cout << std::endl << std::endl << " here --> " << sum_Wij_times_rj << " <-- " << std::endl << std::endl << std::endl;
//std::cout << i << " -> firing rate of neuron #" << neuron_i->GetNeuronID() << "=(" << neuron_i->GetFiringRate() << ") and Wij*rj=(" << sum_Wij_times_rj << ") " << std::endl;
// uncomment }
// uncomment }
// (iv) firing rate of chosen ouput neuron for current (r,e) <==== HERE
// sum_Wij_times_rj = 0;
// //for (neuron_i = outputPopulation.neurons.begin(); neuron_i != outputPopulation.neurons.end(); neuron_i++) {
// for (neuron_j = outputPopulation.neurons.begin(); neuron_j != outputPopulation.neurons.end(); neuron_j++) {
// // multiply each individual dist_Wij by correspondent rj (based on neuronID == index) <===l;'
// sum_Wij_times_rj += chosen_neuron->GetWijArrayAtIndex(neuron_j->GetNeuronID())*neuron_j->GetFiringRate();
// }
// chosen_neuron->SetFiringRate(1 / (1 + exp(-.002*4*(chosen_neuron->GetInternalActivation() + sum_Wij_times_rj - 0))));
// uncomment std::cout << "\t" << chosen_neuron->GetFiringRate(); // << "(" << *r << "," << *e << ") "; // print
//std::cout << " -> firing rate of chosen neuron #" << chosen_neuron->GetNeuronID() << "=(" << chosen_neuron->GetFiringRate() << ") and Wij*rj=(" << sum_Wij_times_rj << ") " << std::endl;
//std::cout << i << " -> firing rate of neuron #" << neuron_i->GetNeuronID() << "=(" << neuron_i->GetFiringRate() << ") and Wij*rj=(" << sum_Wij_times_rj << ") " << std::endl;
//}
// uncomment }
// uncomment std::cout << ";" << std::endl;
// uncomment }
// uncomment std::cout << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl;
// for (neuron = outputPopulation.neurons.begin(); neuron != outputPopulation.neurons.end(); neuron++) {
// //if (neuron->GetNeuronID() == neuron_index) std::cout << "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t.:: Neuron #" << outputPopulation.neurons.at(neuron_index).GetNeuronID() << " e=" << outputPopulation.neurons.at(neuron_index).GetPreferredEyePosition() << " r=" << outputPopulation.neurons.at(neuron_index).GetPreferredRetinalLocation() << " ::." << std::endl; // print
// for (n = testing_eye_positions.begin(); n != testing_eye_positions.end(); n++) {
//
//
// //std::cout << "Eye-position: " << *n << std::endl << std::endl << std::endl;
// for (r = testing_retinal_locations.begin(); r != testing_retinal_locations.end(); r++) {
// //std::cout << std::endl << std::endl << std::endl << "r=" << *r << ", e=" << *n << std::endl << std::endl << std::endl;
//
// neuron->ComputeInternalActivation(*n, *r);
//
// /****/
// float sum_Wij_times_rj = 0;
//
// int initial_condition = 4;
// for (int i=initial_condition; i<5; i++) {
// for (neuron_i = outputPopulation.neurons.begin(); neuron_i != outputPopulation.neurons.end(); neuron_i++) {
//
// for (neuron_j = outputPopulation.neurons.begin(); neuron_j != outputPopulation.neurons.end(); neuron_j++) {
// // multiply each individual dist_Wij by correspondent rj (based on neuronID == index) <===l;'
// sum_Wij_times_rj += neuron_i->GetWijArrayAtIndex(neuron_j->GetNeuronID())*neuron_j->GetFiringRate();
// }
// neuron_i->SetFiringRate(1 / (1 + exp(-.002*4*(neuron_i->GetInternalActivation() + sum_Wij_times_rj - 0))));
// //std::cout << i << " -> firing rate of neuron #" << neuron_i->GetNeuronID() << "=(" << neuron_i->GetFiringRate() << ") and Wij*rj=(" << sum_Wij_times_rj << ") " << std::endl;
// }
// }
//
// if (neuron->GetNeuronID() == neuron_index) std::cout << "\t\t" << neuron->GetFiringRate();// << "(" << *r << "," << *n << ") "; // print
//
// //for (neuron_i = outputPopulation.neurons.begin(); neuron_i != outputPopulation.neurons.end(); neuron_i++) {
// // std::cout << "\t\t" << neuron_i->GetFiringRate();
// //}
// /****/
//
//
// //if (neuron->GetNeuronID() == neuron_index) std::cout << "\t\t" << neuron->GetInternalActivation();// << "(" << *r << "," << *n << ") "; // print
// }
// if (neuron->GetNeuronID() == neuron_index) std::cout << ";" << std::endl; // print
// }
// if (neuron->GetNeuronID() == neuron_index) std::cout << std::endl; // print
// }
// uncomment std::cout << std::endl; // print
// END OF (iii)
// (iv) calculates firing rate
//std::vector<Neuron>::iterator neuron_i;
//std::vector<Neuron>::iterator neuron_j;
// END OF (iv)
// (v) print final firing rates:
//std::cout << std::endl << std::endl << "FIRING RATES" << std::endl << "Oooops, not yet! ;) " << std::endl << std::endl << std::endl << std::endl;
// END OF (v)
// std::vector<float> temp = outputPopulation.neurons.at(0).GetArrayWij();
//
// for (int i =0; i<temp.size(); i++) {
// std::cout << temp.at(i) << " ";
// }
std::cout << std::endl << "Population size: " << outputPopulation.getPopulationSize() << std::endl;
return 0;
}
/**
* This method does what it says it does. Seriously though:
* TODO: write a better method description :P
*
*/
void testNeuronResponseAgainstEyePositionsInItsPreferredRetinalLocation(int neuron_index, std::vector<float> testing_eye_positions, Neuron *chosen_neuron) {
std::cout << "Hello Dan." << std::endl << "It seems that you chose neuron #" << neuron_index << ". Good choice!" << std::endl;
std::cout << "Log: Preferred retinal location: " << chosen_neuron->GetPreferredRetinalLocation() << ". " << std::endl;
std::cout << "Log: Range of testing eye-positions: [" << testing_eye_positions[0] << ", " << testing_eye_positions.back() << "]." << std::endl;
for (std::vector<float>::iterator it = testing_eye_positions.begin(); it != testing_eye_positions.end(); ++it) {
chosen_neuron->ComputeInternalActivation(*it, chosen_neuron->GetPreferredRetinalLocation());
}
}
/**
* My version of MATLAB's 'linspace' :)
*
*/
template <typename T = float>
std::vector<T> linspace(T begin, T end, size_t N) {
T h = (end - begin) / static_cast<T>(N-1);
std::vector<T> xs(N);
typename std::vector<T>::iterator x;
T val;
for (x = xs.begin(), val = begin; x != xs.end(); ++x, val +=h)
*x= val;
return xs;
}
/* Backup line 77 to 123 (?)
for (e = testing_eye_positions.begin(); e != testing_eye_positions.end(); e++) {
for (r = testing_retinal_locations.begin(); r != testing_retinal_locations.end(); r++) {
// (i) internal activation of chosen output neuron
//chosen_neuron->ComputeInternalActivation(*e, *r);
// (ii) internal activation of all output neurons
for (neuron = outputPopulation.neurons.begin(); neuron != outputPopulation.neurons.end(); neuron++) {
neuron->ComputeInternalActivation(*e, *r);
}
// (iii) firing rate of all output neurons
float sum_Wij_times_rj = 0;
int initial_condition = 4;
for (int i=initial_condition; i<5; i++) {
for (neuron_i = outputPopulation.neurons.begin(); neuron_i != outputPopulation.neurons.end(); neuron_i++) {
for (neuron_j = outputPopulation.neurons.begin(); neuron_j != outputPopulation.neurons.end(); neuron_j++) {
// multiply each individual dist_Wij by correspondent rj (based on neuronID == index) <===l;'
sum_Wij_times_rj += (neuron_i->GetWijArrayAtIndex(neuron_j->GetNeuronID())*neuron_j->GetFiringRate());
}
//std::cout << "1 + " << (neuron_i->GetInternalActivation() + sum_Wij_times_rj - alpha) << std::endl;
neuron_i->SetFiringRate((1 / (1 + exp(-sigmoidGain*(neuron_i->GetInternalActivation() + sum_Wij_times_rj - alpha)))));
//std::cout << std::endl << std::endl << " here --> " << sum_Wij_times_rj << " <-- " << std::endl << std::endl << std::endl;
//std::cout << i << " -> firing rate of neuron #" << neuron_i->GetNeuronID() << "=(" << neuron_i->GetFiringRate() << ") and Wij*rj=(" << sum_Wij_times_rj << ") " << std::endl;
}
}
// (iv) firing rate of chosen ouput neuron for current (r,e) <==== HERE
// sum_Wij_times_rj = 0;
// //for (neuron_i = outputPopulation.neurons.begin(); neuron_i != outputPopulation.neurons.end(); neuron_i++) {
// for (neuron_j = outputPopulation.neurons.begin(); neuron_j != outputPopulation.neurons.end(); neuron_j++) {
// // multiply each individual dist_Wij by correspondent rj (based on neuronID == index) <===l;'
// sum_Wij_times_rj += chosen_neuron->GetWijArrayAtIndex(neuron_j->GetNeuronID())*neuron_j->GetFiringRate();
// }
// chosen_neuron->SetFiringRate(1 / (1 + exp(-.002*4*(chosen_neuron->GetInternalActivation() + sum_Wij_times_rj - 0))));
std::cout << "\t" << chosen_neuron->GetFiringRate(); // << "(" << *r << "," << *e << ") "; // print
//std::cout << " -> firing rate of chosen neuron #" << chosen_neuron->GetNeuronID() << "=(" << chosen_neuron->GetFiringRate() << ") and Wij*rj=(" << sum_Wij_times_rj << ") " << std::endl;
//std::cout << i << " -> firing rate of neuron #" << neuron_i->GetNeuronID() << "=(" << neuron_i->GetFiringRate() << ") and Wij*rj=(" << sum_Wij_times_rj << ") " << std::endl;
//}
}
std::cout << ";" << std::endl;
}
*/