-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselector_module.py
347 lines (300 loc) · 12.4 KB
/
selector_module.py
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
import torch
import network_utils
import plot_utils
import matplotlib.pyplot as plt
import os
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if not os.path.exists("logs"):
os.mkdir("logs")
class Selector_module(object):
def __init__(
self,
params,
):
"""
Initialize Disinhibitory_module class
params: Dictionnary including all network params. More details in
results/Set_mFasNet_params.ipynb
"""
# Network properties
self.n_memory = params["n_memory"]
self.msp_neurons = params["msp_neurons"]
self.input_shape = params["input_shape"]
self.batch_size = params['batch_size']
self.plot = params["plot"]
# Neuron model
self.len_epsc = params["l"]
self.decay = 0.9
self.tau = params["tau"]
self.wta_speed = params["a2"]
self.lr_msp = params["lr_msp"]
# WTA parameters
self.beta = torch.FloatTensor([params["beta"]]).to(device)
self.r = params["a1"]
self.self_inhib = params["a3"]
self.module_inhib = params["a4"]
self.alpha_p = params["alpha_p"]
# Layer initialisation
self.init_layer()
# Weight initialisation
self.FF_scaling = self.input_shape
(
self.FF_msp_weights,
self.FB_msp_weights,
self.WTA_matrix,
self.commitment_matrix,
) = self.initiate_msp_weights()
self.commitment_matrix_fix = self.commitment_matrix.clone()
self.FF_msp_weights_fix = self.FF_msp_weights.clone()
self.max_commitment = params["Wmax"]
self.initiate_info()
def initiate_msp_weights(self):
"""
Initialise Weights between EI mismatch to memory selector, It also implements between the neurons in the WTA network
"""
bloc_FF_msp_weights = torch.ones(
(self.batch_size, self.input_shape, self.msp_neurons))
bloc_shape = bloc_FF_msp_weights.shape
bloc_WTA_matrix = torch.ones(
(self.batch_size, self.msp_neurons, self.msp_neurons))
FF_msp_weights = torch.zeros(
(self.batch_size,
self.n_memory *
self.input_shape,
self.n_memory *
self.msp_neurons))
WTA_matrix = torch.zeros(
(self.batch_size,
self.n_memory *
self.msp_neurons,
self.n_memory *
self.msp_neurons))
idxx = 0
idxy = 0
for memory in range(self.n_memory):
bloc_shape = bloc_FF_msp_weights[0].shape
idxx = bloc_shape[0] * memory
idxy = bloc_shape[1] * memory
FF_msp_weights[:, idxx:idxx + bloc_shape[0],
idxy:idxy + bloc_shape[1]] = bloc_FF_msp_weights
bloc_shape = bloc_WTA_matrix[0].shape
idxx = bloc_shape[0] * memory
idxy = bloc_shape[1] * memory
WTA_matrix[:, idxx:idxx +
bloc_shape[0], idxy:idxy +
bloc_shape[1]] = bloc_WTA_matrix
FB_msp_weights = 1 - FF_msp_weights.clone()
commitment_matrix = WTA_matrix.clone() / self.msp_neurons
WTA_matrix = (1 - WTA_matrix) .clone() / self.msp_neurons
return FF_msp_weights.to(device), FB_msp_weights.to(
device), WTA_matrix.to(device), commitment_matrix.to(device)
def initiate_info(self):
"""
Dic info is a dictionnary saving intersting / to plot data of a simulation
"""
self.info = {}
self.info["msp_input"] = []
self.info["eta"] = []
self.info["msp"] = []
self.info["msp_spikes"] = []
self.info["inhib_spikes"] = []
self.info["WTA"] = []
def init_layer(self):
"""
Initialisation to 0 of all neurons potential etc.. of the network
"""
self.msp_input_potential = torch.zeros(
self.batch_size, self.n_memory * self.msp_neurons).to(device)
self.inhibitory_eta_msp = torch.zeros_like(
self.msp_input_potential).to(device)
self.refractoriness = torch.zeros_like(
self.msp_input_potential).to(device)
self.inhibitory_msp_input_potential = torch.zeros_like(
self.msp_input_potential).to(device)
self.msp_spikes = torch.zeros(
self.batch_size,
self.n_memory *
self.msp_neurons).to(device)
self.inhibitory_msp_spikes = torch.zeros(
self.batch_size, self.n_memory * self.msp_neurons).to(device)
self.EPSC_msp_decay = torch.zeros(
self.batch_size,
self.n_memory *
self.msp_neurons).to(device)
self.EPSC_inhibitory_msp_decay = torch.zeros(
self.batch_size, self.n_memory * self.msp_neurons).to(device)
self.active_memory_activity = torch.zeros(
self.batch_size, self.n_memory).to(device)
self.EPSC_msp_decay = torch.zeros(
self.batch_size,
self.n_memory *
self.msp_neurons).to(device)
self.wta_output = torch.zeros(
self.batch_size,
self.n_memory *
self.msp_neurons).to(device)
self.filtered_inhibition = torch.zeros(
self.batch_size,
self.n_memory).to(device)
def phi(self, x):
"""
error neuron activation function
param x: neuron membrane potential
return $$f(x)$$
"""
return (x > 0).float() * torch.tanh(x)
def update_pot(self, h, I):
"""
Layer update
param h: input potential
param I: input current
return: Integrated potential
"""
h += 1 / self.tau * (-h + I)
return h
def wta_update_module(self, x, msp_spikes, wta_input, meanput):
"""
Runs one step of the WTA dynamic on the module.
param x: neuron self input potential
param msp_spikes: Inhibition coming from self memory (More inhibition implies more error in SpikeSumNet)
param wta_input: Inhibition coming from other memories (More inhibition implies other memory active)
return: Updated input potential
"""
step = self.wta_speed * (
- x
- self.self_inhib *
torch.einsum("bkj,bj->bk", self.commitment_matrix, msp_spikes)
- self.module_inhib * wta_input
+ meanput.repeat(wta_input.shape[-1],1).T
)
self.input = self.self_inhib * \
torch.einsum("bkj,bj->bk", self.commitment_matrix, msp_spikes).detach().clone()
x += step
return x.detach()
def update_layer(self, input_potential, EPSC_decay, refractoriness):
"""
Full update of the error layer
Param input_potential: Input potential receive by the neurons
Param EPSC_decay: Estimation of time since last spike of error neurons
refractoriness: refractoriness value (inhibition due to recent spike)
returns: new spikes index, active EPSC, estimation of time since last spike, refractoriness
"""
spikes = 0 * input_potential
membrane_potential = input_potential - refractoriness
idx = self.phi(membrane_potential) > torch.Tensor(
self.batch_size, self.msp_neurons * self.n_memory).uniform_().to(device)
spikes[idx] = 1.0
membrane_potential[idx] = 0
EPSC, EPSC_decay = network_utils.square_EPSC(EPSC_decay, self.len_epsc, spikes)
refractoriness *= self.decay
refractoriness[idx] = 1
return spikes, EPSC, EPSC_decay, refractoriness
def clear_spike_train(self):
"""
allow suppressing all saved spike times
"""
self.info["msp_spikes"] = []
self.info["inhib_spikes"] = []
def forward(self, input_, commitement_modulation, learning=True):
"""
Disinhibitory module step
param input_: Spiking activity coming from SpikeSumNet
param commitement_modulation: Modulation information receive from SpikeSumNet
param learning: switch learning off (debugging only)
"""
msp_input = self.r * \
(torch.einsum("bjk,bj->bk", self.FF_msp_weights, input_ - self.beta)).detach()
self.meanput = 20 * torch.mean(input_, dim=1).detach()
wta_input = self.wta_output.detach().clone()
self.msp_input_potential = self.update_pot(
self.msp_input_potential,
msp_input,
)
(
self.msp_spikes,
EPSC_msp,
self.EPSC_msp_decay,
self.refractoriness,
) = self.update_layer(
self.msp_input_potential, self.EPSC_msp_decay, self.refractoriness
)
self.inhibitory_msp_input_potential = self.wta_update_module(
self.inhibitory_msp_input_potential,
EPSC_msp,
wta_input,
self.meanput
)
(
self.inhibitory_msp_spikes,
EPSC_inhibitory_msp,
self.EPSC_inhibitory_msp_decay,
self.inhibitory_eta_msp,
) = self.update_layer(
self.inhibitory_msp_input_potential,
self.EPSC_inhibitory_msp_decay,
self.inhibitory_eta_msp,
)
self.active_memory_activity += (1 / self.tau * (-self.active_memory_activity + torch.sum(
self.inhibitory_msp_spikes.reshape(self.batch_size, self.n_memory, -1), axis=2))).detach()
if learning:
commitement_modulation = commitement_modulation.repeat_interleave(self.msp_neurons, 1).detach()
self.commitment_matrix += (self.lr_msp *
torch.einsum("bj,bk->bjk", self.msp_spikes, commitement_modulation *
self.inhibitory_msp_spikes) -
(self.commitment_matrix -
self.commitment_matrix_fix) *
self.alpha_p *
0).detach()
self.commitment_matrix *= 1 * (self.commitment_matrix_fix > 0)
self.commitment_matrix[self.commitment_matrix <
self.commitment_matrix_fix] = self.commitment_matrix_fix[self.commitment_matrix < self.commitment_matrix_fix].detach()
self.commitment_matrix[self.commitment_matrix > self.max_commitment] = self.max_commitment# move
self.memory_output = torch.einsum(
"bjk,bk->bj",
self.FB_msp_weights,
EPSC_inhibitory_msp).unsqueeze(1).repeat(1, 2, 1).detach()
self.wta_output = torch.einsum(
"bjk,bk->bj",
self.WTA_matrix,
EPSC_inhibitory_msp).detach()
self.info["msp_spikes"] += [self.msp_spikes]
self.info["inhib_spikes"] += [self.inhibitory_msp_spikes]
def plot_network(self):
"""
Plotting few properties of the network; Spike train of the two neuronal population and the commitment matrix.
"""
if self.plot:
print('---------Selector Network---------')
print('---First layer spikes----')
plot_utils.plot_spike_train(
torch.transpose(
torch.cat(
self.info["msp_spikes"]).view(
-1,
self.msp_neurons * self.n_memory),
0,
1).cpu().detach(),
self.msp_neurons,
1,
title='memory selector spike train layer 1',
directory="{0}_spikes_plot".format(0),
)
print('---Second layer spikes----')
plot_utils.plot_spike_train(
torch.transpose(
torch.cat(
self.info["inhib_spikes"]).view(
-1,
self.msp_neurons * self.n_memory),
0,
1).cpu().detach(),
self.msp_neurons,
1,
title='memory selector spike train layer 2',
directory="{0}_spikes_plot".format(0),
)
print('---Commitment matrix----')
for context in range(self.commitment_matrix.shape[0]):
plt.imshow(self.commitment_matrix.cpu()[context].detach())
plt.colorbar()
plt.show()