-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathmnist_mb_classifier.py
345 lines (293 loc) · 12.3 KB
/
mnist_mb_classifier.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
"""
MNIST classification using an insect-inspired mushroom body model
=================================================================
This example learns MNIST digits using STDP and an insect-inspired mushroom body model
This example can be used as follows:
.. argparse::
:filename: ../userproject/mnist_mb_classifier.py
:func: get_parser
:prog: mnist_mb_classifier
"""
import mnist
import numpy as np
from copy import copy
from argparse import ArgumentParser
from pygenn import (create_current_source_model, create_custom_update_model,
create_neuron_model, create_out_post_var_ref,
create_spike_time_var_ref, create_var_ref,
create_weight_update_model, init_sparse_connectivity,
init_postsynaptic, init_weight_update, GeNNModel)
from tqdm.auto import tqdm
# ----------------------------------------------------------------------------
# Parameters
# ----------------------------------------------------------------------------
# Simulation time step
DT = 0.1
# Scaling factor for converting normalised image pixels to input currents (nA)
INPUT_SCALE = 80.0
# Size of current to use to stimulate correct MBON when training (nA)
MBON_STIMULUS_CURRENT = 5.0
# Number of Projection Neurons in model (should match image size)
NUM_PN = 784
# Number of Kenyon Cells in model (defines memory capacity)
NUM_KC = 20000
# Number of output neurons in model
NUM_MBON = 10
# How long to present each image to model
PRESENT_TIME_MS = 20.0
# Standard LIF neurons parameters
LIF_PARAMS = {
"C": 0.2,
"TauM": 20.0,
"Vrest": -60.0,
"Vreset": -60.0,
"Vthresh": -50.0,
"Ioffset": 0.0,
"TauRefrac": 2.0}
# We only want PNs to spike once
PN_PARAMS = copy(LIF_PARAMS)
PN_PARAMS["TauRefrac"] = 100.0
# Weight of each synaptic connection
PN_KC_WEIGHT = 0.2
# Time constant of synaptic integration
PN_KC_TAU_SYN = 3.0
# How many projection neurons should be connected to each Kenyon Cell
PN_KC_FAN_IN = 20
# We will use weights of 1.0 for KC->GGN connections and
# want the GGN to inhibit the KCs after 200 spikes
GGN_PARAMS = {"Vthresh": 200.0}
KC_MBON_TAU_SYN = 3.0
KC_MBON_PARAMS = {"tau": 15.0,
"rho": 0.01,
"eta": 0.00002,
"wMin": 0.0,
"wMax": 0.0233}
# ----------------------------------------------------------------------------
# Custom models
# ----------------------------------------------------------------------------
# Current source model, allowing current to be injected into neuron from variable
cs_model = create_current_source_model(
"cs_model",
vars=[("magnitude", "scalar")],
injection_code="injectCurrent(magnitude);")
# Minimal integrate and fire neuron model
if_model = create_neuron_model(
"IF",
params=["Vthresh"],
vars=[("V", "scalar")],
sim_code=
"""
V += Isyn;
""",
threshold_condition_code=
"""
V >= Vthresh
""",
reset_code=
"""
V= 0.0;
""")
# Symmetric STDP learning rule
symmetric_stdp = create_weight_update_model(
"symmetric_stdp",
params=["tau", "rho", "eta", "wMin", "wMax"],
vars=[("g", "scalar")],
pre_spike_syn_code=
"""
const scalar dt = t - st_post;
const scalar timing = exp(-dt / tau) - rho;
const scalar newWeight = g + (eta * timing);
g = fmin(wMax, fmax(wMin, newWeight));
""",
post_spike_syn_code=
"""
const scalar dt = t - st_pre;
const scalar timing = fmax(exp(-dt / tau) - rho, -0.1 * rho);
const scalar newWeight = g + (eta * timing);
g = fmin(wMax, fmax(wMin, newWeight));
""")
# Custom update for resetting neuron state
pn_reset = create_custom_update_model(
"pn_reset",
params=["Vreset"],
var_refs=[("V", "scalar"), ("RefracTime", "scalar")],
update_code=
"""
V = Vreset;
RefracTime = 0.0;
""")
kc_reset = create_custom_update_model(
"kc_reset",
params=["Vreset"],
var_refs=[("PNOutPost", "scalar"), ("GGNOutPost", "scalar"),
("V", "scalar"), ("RefracTime", "scalar")],
update_code=
"""
PNOutPost = 0.0;
GGNOutPost = 0.0;
V = Vreset;
RefracTime = 0.0;
""")
ggn_reset = create_custom_update_model(
"ggn_reset",
params=["Vreset"],
var_refs=[("V", "scalar")],
update_code=
"""
V = Vreset;
""")
mbon_reset = create_custom_update_model(
"mbon_reset",
params=["Vreset"],
var_refs=[("OutPost", "scalar"), ("V", "scalar"),
("RefracTime", "scalar")],
update_code=
"""
OutPost = 0.0;
V = Vreset;
RefracTime = 0.0;
""")
# Custom update for resetting spike times
reset_st = create_custom_update_model(
"reset_st",
var_refs=[("SpikeTimes", "scalar")],
update_code=
f"""
SpikeTimes = {-np.finfo(np.float32).max};
""")
# ----------------------------------------------------------------------------
# CLI
# ----------------------------------------------------------------------------
def get_parser():
parser = ArgumentParser()
parser.add_argument("--test", action="store_true", help="Load saved weights (rather than training)")
parser.add_argument("--plot-weight-distribution", action="store_true", help="Plot weight distribution after training")
return parser
if __name__ == "__main__":
args = get_parser().parse_args()
# Set the download url for the MNIST dataset
mnist.datasets_url = "https://storage.googleapis.com/cvdf-datasets/mnist/"
# Reshape and normalise data
images = mnist.test_images() if args.test else mnist.train_images()
images = np.reshape(images, (images.shape[0], -1)).astype(np.float32)
images /= np.sum(images, axis=1)[:, np.newaxis]
labels = mnist.test_labels() if args.test else mnist.train_labels()
# Create model
model = GeNNModel("float", "mnist_mb")
model.dt = DT
# Create neuron populations
lif_init = {"V": PN_PARAMS["Vreset"], "RefracTime": 0.0}
if_init = {"V": 0.0}
pn = model.add_neuron_population("pn", NUM_PN, "LIF", PN_PARAMS, lif_init)
kc = model.add_neuron_population("kc", NUM_KC, "LIF", LIF_PARAMS, lif_init)
ggn = model.add_neuron_population("ggn", 1, if_model, GGN_PARAMS, if_init)
mbon = model.add_neuron_population("mbon", NUM_MBON, "LIF", LIF_PARAMS, lif_init)
# Turn on spike recording
pn.spike_recording_enabled = True
kc.spike_recording_enabled = True
mbon.spike_recording_enabled = True
# Create current sources to deliver input to network
pn_input = model.add_current_source("pn_input", cs_model, pn , {}, {"magnitude": 0.0})
# Create current sources to deliver input and supervision to network
if not args.test:
mbon_input = model.add_current_source("mbon_input", cs_model, mbon , {}, {"magnitude": 0.0})
# Create synapse populations
pn_kc_connectivity = None if args.test else init_sparse_connectivity("FixedNumberPreWithReplacement", {"num": PN_KC_FAN_IN})
pn_kc = model.add_synapse_population("pn_kc", "SPARSE",
pn, kc,
init_weight_update("StaticPulseConstantWeight", {"g": PN_KC_WEIGHT}),
init_postsynaptic("ExpCurr", {"tau": PN_KC_TAU_SYN}),
pn_kc_connectivity)
# Load saved connectivity if testing
if args.test:
pn_kc_ind = np.load("pn_kc_ind.npy")
pn_kc.set_sparse_connections(pn_kc_ind[0], pn_kc_ind[1])
kc_ggn = model.add_synapse_population("kc_ggn", "DENSE",
kc, ggn,
init_weight_update("StaticPulseConstantWeight", {"g": 1.0}),
init_postsynaptic("DeltaCurr"))
ggn_kc = model.add_synapse_population("ggn_kc", "DENSE",
ggn, kc,
init_weight_update("StaticPulseConstantWeight", {"g": -5.0}),
init_postsynaptic("ExpCurr", {"tau": 5.0}))
kc_mbon_weight_update = (init_weight_update("StaticPulse", {}, {"g": np.load("kc_mbon_g.npy")}) if args.test
else init_weight_update(symmetric_stdp, KC_MBON_PARAMS, {"g": 0.0}))
kc_mbon = model.add_synapse_population("kc_mbon", "DENSE",
kc, mbon,
kc_mbon_weight_update,
init_postsynaptic("ExpCurr", {"tau": KC_MBON_TAU_SYN}))
# Add custom updates to reset model state between examples
model.add_custom_update("pn_reset", "Reset", pn_reset,
{"Vreset": LIF_PARAMS["Vreset"]},
var_refs={"V": create_var_ref(pn, "V"),
"RefracTime": create_var_ref(pn, "RefracTime")})
model.add_custom_update("kc_reset", "Reset", kc_reset,
{"Vreset": LIF_PARAMS["Vreset"]},
var_refs={"V": create_var_ref(kc, "V"),
"RefracTime": create_var_ref(kc, "RefracTime"),
"PNOutPost": create_out_post_var_ref(pn_kc),
"GGNOutPost": create_out_post_var_ref(ggn_kc)})
model.add_custom_update("ggn_reset", "Reset", ggn_reset,
{"Vreset": 0.0},
var_refs={"V": create_var_ref(ggn, "V")})
model.add_custom_update("mbon_reset", "Reset", mbon_reset,
{"Vreset": LIF_PARAMS["Vreset"]},
var_refs={"V": create_var_ref(mbon, "V"),
"RefracTime": create_var_ref(mbon, "RefracTime"),
"OutPost": create_out_post_var_ref(kc_mbon)})
if not args.test:
model.add_custom_update("kc_reset_st", "ResetST", reset_st,
var_refs={"SpikeTimes": create_spike_time_var_ref(kc)})
model.add_custom_update("mbon_reset_st", "ResetST", reset_st,
var_refs={"SpikeTimes": create_spike_time_var_ref(mbon)})
# Convert present time into timesteps
present_timesteps = int(round(PRESENT_TIME_MS / DT))
# Build model and load it
model.build()
model.load(num_recording_timesteps=present_timesteps)
# Present images
num_correct = 0
for s in tqdm(range(images.shape[0])):
# Set training image
pn_input.vars["magnitude"].view[:] = images[s] * INPUT_SCALE
pn_input.vars["magnitude"].push_to_device()
# Turn on correct output neuron
if not args.test:
mbon_input.vars["magnitude"].view[:] = 0
mbon_input.vars["magnitude"].view[labels[s]] = MBON_STIMULUS_CURRENT
mbon_input.vars["magnitude"].push_to_device()
# Simulate present timesteps
for i in range(present_timesteps):
model.step_time()
# Reset neuron state
model.custom_update("Reset")
# Reset spike times
if not args.test:
model.custom_update("ResetST")
if args.test:
# Download spikes from GPU
model.pull_recording_buffers_from_device()
# Determine the classification and count correct
mbon_spike_times, mbon_spike_ids = mbon.spike_recording_data[0]
if len(mbon_spike_times) > 0:
if mbon_spike_ids[np.argmin(mbon_spike_times)] == labels[s]:
num_correct += 1
if args.test:
print(f"\n{num_correct}/{images.shape[0]} correct ({(num_correct * 100.0) / images.shape[0]} %)")
else:
pn_kc.pull_connectivity_from_device()
kc_mbon.vars["g"].pull_from_device()
# Save weighs and connectivity
kc_mbon_g_view = kc_mbon.vars["g"].view
np.save("kc_mbon_g.npy", kc_mbon_g_view)
np.save("pn_kc_ind.npy", np.vstack((pn_kc.get_sparse_pre_inds(),
pn_kc.get_sparse_post_inds())))
# Plot weight distribution
if args.plot_weight_distribution:
from matplotlib import pyplot as plt
fig, axis = plt.subplots(figsize=(10, 5))
axis.hist(kc_mbon_g_view, bins=100)
axis.axvline(np.average(kc_mbon_g_view), linestyle="--")
axis.set_xlabel("Weight [nA]")
axis.set_ylabel("Count");
plt.show()