-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmec.cc
403 lines (372 loc) · 15.6 KB
/
mec.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Navigating with grid and place cells in cluttered environments
// Edvardsen et al. (2020). Hippocampus, 30(3), 220-232.
//
// Licensed under the EUPL-1.2-or-later.
// Copyright (c) 2019 NTNU - Norwegian University of Science and Technology.
// Author: Vegard Edvardsen (https://github.com/evegard).
#include "mec.h"
#include <cmath>
#include <cstdlib>
NeuralSheetNetwork::NeuralSheetNetwork(real gain)
: Network(MEC_SIZE * MEC_SIZE),
bump_tracker_initialized(false)
{
this->gain = gain;
this->lambda = MEC_SIZE * 15.0 / 40.0; // "Periodicity"
this->beta = 3.0 / (this->lambda * this->lambda);
this->gamma = 1.05 * this->beta;
}
void NeuralSheetNetwork::initialize_bump_tracker()
{
// Initialize the bump tracker to the maximally activated neuron
real max_activation = -1;
for (int y = 0; y < MEC_SIZE; y++) {
for (int x = 0; x < MEC_SIZE; x++) {
int neuron_index = this->coords_to_neuron_index(x, y);
real activation = this->neurons[current_activity]->values[neuron_index];
if (activation > max_activation) {
max_activation = activation;
this->bump_x = x;
this->bump_y = y;
}
}
}
// Run an initial update of the bump tracker to iteratively move the
// bump location to the center of mass that maximizes disc mass, and
// afterwards reset the (dx, dy) values back to (0, 0). This whole
// step does not really appear to be necessary, as the (dx, dy)
// values seem to stay at (0, 0). However, there is no guarantee for
// this to always happen, and we therefore retain this step
this->bump_tracker_initialized = true;
this->update_bump_tracker();
this->bump_total_dx = 0;
this->bump_total_dy = 0;
}
void NeuralSheetNetwork::update_bump_tracker()
{
if (!this->bump_tracker_initialized) {
return;
}
while (true) {
// Calculate the mass under the disc centered at the current bump
// location, as well as the displacement to the center of mass
real current_mass;
int center_of_mass_dx, center_of_mass_dy;
std::tie(current_mass, center_of_mass_dx, center_of_mass_dy) =
this->calculate_disc_mass(this->bump_x, this->bump_y);
// Calculate the (wrapped) coordinates for the center of mass, as our
// candidate for the new bump location
int center_of_mass_x = Periodic::modulo(this->bump_x + center_of_mass_dx, MEC_SIZE);
int center_of_mass_y = Periodic::modulo(this->bump_y + center_of_mass_dy, MEC_SIZE);
// Calculate the mass at the new potential bump location
real new_mass;
std::tie(new_mass, std::ignore, std::ignore) =
this->calculate_disc_mass(center_of_mass_x, center_of_mass_y);
// If the mass is increased by moving to the new bump location, update
// the bump location as well as the counters for total bump displacement
if (new_mass > current_mass) {
this->bump_x = center_of_mass_x;
this->bump_y = center_of_mass_y;
this->bump_total_dx += center_of_mass_dx;
this->bump_total_dy += center_of_mass_dy;
} else {
break;
}
}
}
std::tuple<real, int, int> NeuralSheetNetwork::calculate_disc_mass(int center_x, int center_y)
{
real mass = 0.0, weighted_dx = 0.0, weighted_dy = 0;
for (int dy = -BUMP_TRACKER_RADIUS; dy < BUMP_TRACKER_RADIUS + 1; dy++) {
for (int dx = -BUMP_TRACKER_RADIUS; dx < BUMP_TRACKER_RADIUS + 1; dx++) {
if (dx * dx + dy * dy > BUMP_TRACKER_RADIUS * BUMP_TRACKER_RADIUS) {
continue;
}
int x = Periodic::modulo(center_x + dx, MEC_SIZE);
int y = Periodic::modulo(center_y + dy, MEC_SIZE);
int neuron_index = this->coords_to_neuron_index(x, y);
real activation = this->neurons[current_activity]->values[neuron_index];
mass += activation;
weighted_dx += dx * activation;
weighted_dy += dy * activation;
}
}
int center_of_mass_dx = round(weighted_dx / mass);
int center_of_mass_dy = round(weighted_dy / mass);
return std::make_tuple(mass, center_of_mass_dx, center_of_mass_dy);
}
MecNetwork::MecNetwork(real gain, MecGainMode gain_mode)
: NeuralSheetNetwork(gain), gain_mode(gain_mode),
activation_probability(gain / MAX_MEC_GAIN)
{
this->add_input(new MecRecurrentInput(this));
}
void MecNetwork::update()
{
for (int i = 0; i < this->size; i++) {
if (this->gain_mode == gain_mode_velocity) {
this->neurons_enabled[i] = true;
} else if (this->gain_mode == gain_mode_poisson_neuron) {
this->neurons_enabled[i] = (Random::uniform() < this->activation_probability);
}
}
Network::update();
}
bool MecNetwork::should_update_neuron(int neuron_index)
{
return this->neurons_enabled[neuron_index];
}
void MecNetwork::update_neuron_values()
{
for (int i = 0; i < this->size; i++) {
if (this->neurons_enabled[i]) {
real input = 1.0 + this->neuron_inputs->values[i];
if (input < 0.0) {
input = 0.0;
}
real output_change = (input - this->neurons[current_activity]->values[i]);
output_change *= 0.1;
this->neurons[next_activity]->values[i] =
this->neurons[current_activity]->values[i] + output_change;
} else {
this->neurons[next_activity]->values[i] =
this->neurons[current_activity]->values[i];
}
}
}
ConvolvedMecNetwork::ConvolvedMecNetwork(MecNetwork *afferent)
: NeuralSheetNetwork(afferent->gain)
{
this->add_input(new MecConvolveInput(this, afferent));
}
void ConvolvedMecNetwork::update_neuron_values()
{
for (int neuron_index = 0; neuron_index < this->size; neuron_index++) {
this->neurons[next_activity]->values[neuron_index] =
this->neuron_inputs->values[neuron_index];
}
}
MecConvolveInput::MecConvolveInput(
ConvolvedMecNetwork *efferent, MecNetwork *afferent)
: Input(efferent), efferent(efferent), afferent(afferent)
{
}
void MecConvolveInput::add_inputs()
{
for (int y = 0; y < MEC_SIZE; y++) {
for (int x = 0; x < MEC_SIZE; x++) {
int afferent_index = this->afferent->coords_to_neuron_index(x, y);
real afferent_value = this->afferent->neurons[current_activity]->values[afferent_index];
for (int dy = 0; dy < 2; dy++) {
for (int dx = 0; dx < 2; dx++) {
int efferent_x = (x + dx) % MEC_SIZE;
int efferent_y = (y + dy) % MEC_SIZE;
int efferent_index = this->efferent->coords_to_neuron_index(efferent_x, efferent_y);
this->efferent->neuron_inputs->values[efferent_index] += 0.25 * afferent_value;
}
}
}
}
}
MecShiftedMaskInput::MecShiftedMaskInput(
Network *efferent, NeuralSheetNetwork *afferent)
: Input(efferent), afferent(afferent)
{
}
void MecShiftedMaskInput::initialize()
{
int w = MEC_SIZE;
int h = MEC_SIZE;
this->weights = new Matrix(2 * w, 2 * h);
for (int y = 0; y < MEC_SIZE; y++) {
for (int x = 0; x < MEC_SIZE; x++) {
real weight = this->get_weight(x, y);
this->weights->values[y + 0][x + 0] = weight;
this->weights->values[y + 0][x + w] = weight;
this->weights->values[y + h][x + 0] = weight;
this->weights->values[y + h][x + w] = weight;
}
}
this->shifts = new std::pair<int, int>[this->efferent->size];
for (int neuron_index = 0; neuron_index < this->efferent->size; neuron_index++) {
this->shifts[neuron_index] = this->get_shift(neuron_index);
}
}
void MecShiftedMaskInput::add_inputs()
{
// Each efferent neuron can separately specify its desired shift of the
// connectivity profile. For MEC neurons this will be their own locations
// in the neural sheet plus their directional-preference-offset, while for
// MEC-diff neurons this depends on their sampled (x, y, direction) tuple.
// Because multiple efferent neurons can share the same origin, we cache
// the sum for each given origin. This cached sum is only considered valid
// during the current execution of this method, therefore we reset the
// cache validity here at the beginning of the method.
for (int y = 0; y < MEC_SIZE; y++) {
for (int x = 0; x < MEC_SIZE; x++) {
this->cached_sum_valid[y][x] = false;
}
}
for (int efferent_neuron = 0; efferent_neuron < this->efferent->size; efferent_neuron++) {
if (!this->efferent->should_update_neuron(efferent_neuron)) {
continue;
}
std::pair<int, int> shift = this->shifts[efferent_neuron];
if (this->cached_sum_valid[shift.second][shift.first]) {
this->efferent->neuron_inputs->values[efferent_neuron] +=
this->cached_sums[shift.second][shift.first];
continue;
}
int shift_x = MEC_SIZE - shift.first;
int shift_y = MEC_SIZE - shift.second;
real sum = 0.0;
aligned_real *neurons = this->afferent->neurons[current_activity]->values;
real *weights = &this->weights->values[shift_y][shift_x];
for (int y = 0; y < MEC_SIZE; y++) {
for (int x = 0; x < MEC_SIZE; x++) {
sum += neurons[x] * weights[x];
}
neurons += MEC_SIZE;
weights += MEC_SIZE * 2;
}
this->efferent->neuron_inputs->values[efferent_neuron] += sum;
this->cached_sums[shift.second][shift.first] = sum;
this->cached_sum_valid[shift.second][shift.first] = true;
}
}
MecRecurrentInput::MecRecurrentInput(MecNetwork *network)
: MecShiftedMaskInput(network, network), afferent(network)
{
}
real MecRecurrentInput::get_weight(int x, int y)
{
if (x > MEC_SIZE / 2) {
x = MEC_SIZE - x;
}
if (y > MEC_SIZE / 2) {
y = MEC_SIZE - y;
}
real distance_squared = x * x + y * y;
return exp(-this->afferent->gamma * distance_squared)
- exp(-this->afferent->beta * distance_squared);
}
std::pair<int, int> MecRecurrentInput::get_shift(int neuron_index)
{
int x = this->afferent->neuron_index_to_x(neuron_index);
int y = this->afferent->neuron_index_to_y(neuron_index);
switch (this->afferent->directionality(x, y)) {
case north: y--; break;
case south: y++; break;
case east: x--; break;
case west: x++; break;
}
x = Periodic::modulo(x, MEC_SIZE);
y = Periodic::modulo(y, MEC_SIZE);
return std::pair<int, int>(x, y);
}
MecNetworkPlot::MecNetworkPlot(NeuralSheetNetwork *network, int number)
: network(network)
{
std::string plot_range = "[-0.5:" + std::to_string(MEC_SIZE - 1) + ".5]";
this->set("xrange", plot_range.c_str());
this->set("yrange", plot_range.c_str());
this->set("size", "square");
this->unset("xtics");
this->unset("ytics");
this->set("cbrange", "[0:0.6]");
this->unset("colorbox");
this->set("border", "");
this->set("margins", "0,5,1,5");
std::string title_string =
"\"{/:Bold=14 Grid module " + std::to_string(number) +
"}\\n@g_{&{g}" + std::to_string(number) +
"} = " + std::to_string(network->gain) + "\"";
this->set("title", title_string.c_str());
int object_number = 5;
for (auto blob : std::vector<std::tuple<double, int, int>> {
std::make_tuple(0.04, 6, -1),
std::make_tuple(0.04, 4, number),
std::make_tuple(0.02, 6, number) }) {
std::string blob_key = "object " + std::to_string(object_number++);
std::string blob_value = "circle at graph -0.025,1.25 size graph " +
std::to_string(std::get<0>(blob)) + " noclip fill solid border lt " +
std::to_string(std::get<2>(blob)) + " lw " +
std::to_string(std::get<1>(blob));
this->set(blob_key.c_str(), blob_value.c_str());
}
}
void MecNetworkPlot::dump_plot_commands(std::ostream &stream)
{
int origin_bump_x = Periodic::modulo(
this->network->bump_x - this->network->bump_total_dx, MEC_SIZE);
int origin_bump_y = Periodic::modulo(
this->network->bump_y - this->network->bump_total_dy, MEC_SIZE);
int bump_radius = BUMP_TRACKER_RADIUS;
stream << "set object 1 circle at " << origin_bump_x << "," << origin_bump_y
<< " size " << bump_radius << " noclip front lw 6 fc rgb 'black';" << std::endl;
stream << "set object 2 circle at " << origin_bump_x << "," << origin_bump_y
<< " size " << bump_radius << " noclip front lw 4 fc rgb 'green';" << std::endl;
stream << "set object 3 circle at " << this->network->bump_x << "," << this->network->bump_y
<< " size " << bump_radius << " noclip front lw 6 fc rgb 'black';" << std::endl;
stream << "set object 4 circle at " << this->network->bump_x << "," << this->network->bump_y
<< " size " << bump_radius << " noclip front lw 4 fc rgb 'yellow';" << std::endl;
stream << "plot '-' matrix with image notitle, "
<< "'-' with vectors nohead lc rgb 'black' lw 4 notitle, "
<< "'-' with vectors nohead lc rgb 'white' lw 2 notitle;" << std::endl;
for (int y = 0; y < MEC_SIZE; y++) {
for (int x = 0; x < MEC_SIZE; x++) {
int neuron_index = this->network->coords_to_neuron_index(x, y);
stream << this->network->neurons[current_activity]->values[neuron_index] << " ";
}
stream << std::endl;
}
stream << std::endl << "e" << std::endl;
for (int i = 0; i < 2; i++) {
double x = this->network->bump_x;
double y = this->network->bump_y;
double direction = atan2(
-this->network->bump_total_dy,
-this->network->bump_total_dx);
double length = sqrt(
pow(this->network->bump_total_dx, 2) +
pow(this->network->bump_total_dy, 2));
do {
double dx = length * cos(direction);
double dy = length * sin(direction);
double t[] = {
dx >= 0 ? HUGE_VAL : (- 0.5 - x) / dx, // Left
dx <= 0 ? HUGE_VAL : (MEC_SIZE - 0.5 - x) / dx, // Right
dy >= 0 ? HUGE_VAL : (- 0.5 - y) / dy, // Bottom
dy <= 0 ? HUGE_VAL : (MEC_SIZE - 0.5 - y) / dy, // Top
};
double min_t = HUGE_VAL;
double segment_length = length;
for (int t_index = 0; t_index < 4; t_index++) {
if (t[t_index] >= 0 && t[t_index] < 1 && t[t_index] < min_t) {
min_t = t[t_index];
segment_length = t[t_index] * length;
}
}
double segment_dx = segment_length * cos(direction);
double segment_dy = segment_length * sin(direction);
stream << x << " " << y << " " << segment_dx << " " << segment_dy << std::endl;
length -= segment_length;
x += segment_dx;
y += segment_dy;
for (int t_index = 0; t_index < 4; t_index++) {
if (t[t_index] == min_t) {
switch (t_index) {
case 0: x += MEC_SIZE; break; // Hit left wall, jump to right wall
case 1: x -= MEC_SIZE; break; // Hit right wall, jump to left wall
case 2: y += MEC_SIZE; break; // Hit bottom wall, jump to top wall
case 3: y -= MEC_SIZE; break; // Hit top wall, jump to bottom wall
}
}
}
} while (length > 0);
stream << "e" << std::endl;
}
for (int i = 0; i < 4; i++) {
stream << "unset object " << (i + 1) << ";" << std::endl;
}
}