-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtutorial_1.py
144 lines (117 loc) · 4.88 KB
/
tutorial_1.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
import numpy as np
from os import path
from pygenn.genn_model import (create_custom_neuron_class,
create_custom_current_source_class,
GeNNModel)
from pygenn.genn_wrapper import NO_DELAY
# ----------------------------------------------------------------------------
# Parameters
# ----------------------------------------------------------------------------
IF_PARAMS = {"Vthr": 5.0}
TIMESTEP = 1.0
PRESENT_TIMESTEPS = 100
INPUT_CURRENT_SCALE = 1.0 / 100.0
# ----------------------------------------------------------------------------
# Custom GeNN models
# ----------------------------------------------------------------------------
# Very simple integrate-and-fire neuron model
if_model = create_custom_neuron_class(
"if_model",
param_names=["Vthr"],
var_name_types=[("V", "scalar"), ("SpikeCount", "unsigned int")],
sim_code="$(V) += $(Isyn) * DT;",
reset_code="""
$(V) = 0.0;
$(SpikeCount)++;
""",
threshold_condition_code="$(V) >= $(Vthr)")
# Current source model which injects current with a magnitude specified by a state variable
cs_model = create_custom_current_source_class(
"cs_model",
var_name_types=[("magnitude", "scalar")],
injection_code="$(injectCurrent, $(magnitude));")
# ----------------------------------------------------------------------------
# Build model
# ----------------------------------------------------------------------------
# Create GeNN model
model = GeNNModel("float", "tutorial_1")
model.dT = TIMESTEP
# Load weights
weights = []
while True:
filename = "weights_%u_%u.npy" % (len(weights), len(weights) + 1)
if path.exists(filename):
weights.append(np.load(filename))
else:
break
# Initial values to initialise all neurons to
if_init = {"V": 0.0, "SpikeCount":0}
# Create first neuron layer
neuron_layers = [model.add_neuron_population("neuron0", weights[0].shape[0],
if_model, IF_PARAMS, if_init)]
# Create subsequent neuron layer
for i, w in enumerate(weights):
neuron_layers.append(model.add_neuron_population("neuron%u" % (i + 1),
w.shape[1], if_model,
IF_PARAMS, if_init))
# Create synaptic connections between layers
for i, (pre, post, w) in enumerate(zip(neuron_layers[:-1], neuron_layers[1:], weights)):
model.add_synapse_population(
"synapse%u" % i, "DENSE_INDIVIDUALG", NO_DELAY,
pre, post,
"StaticPulse", {}, {"g": w.flatten()}, {}, {},
"DeltaCurr", {}, {})
# Create current source to deliver input to first layers of neurons
current_input = model.add_current_source("current_input", cs_model,
"neuron0" , {}, {"magnitude": 0.0})
# Build and load our model
model.build()
model.load()
# ----------------------------------------------------------------------------
# Simulate
# ----------------------------------------------------------------------------
# Load testing data
testing_images = np.load("testing_images.npy")
testing_labels = np.load("testing_labels.npy")
# Check dimensions match network
assert testing_images.shape[1] == weights[0].shape[0]
assert np.max(testing_labels) == (weights[1].shape[1] - 1)
# Set current input by scaling first image
current_input.vars["magnitude"].view[:] = testing_images[0] * INPUT_CURRENT_SCALE
# Upload
current_input.push_var_to_device("magnitude")
# Simulate
layer_spikes = [[] for i, _ in enumerate(neuron_layers)]
while model.timestep < PRESENT_TIMESTEPS:
# Advance simulation
model.step_time()
# Loop through neuron layers
for i, l in enumerate(neuron_layers):
# Download spikes
l.pull_current_spikes_from_device()
# Add to data structure
layer_spikes[i].append(np.copy(l.current_spikes))
# ----------------------------------------------------------------------------
# Plotting
# ----------------------------------------------------------------------------
import matplotlib.pyplot as plt
# Create a plot with axes for each
fig, axes = plt.subplots(len(neuron_layers), sharex=True)
# Loop through axes and their corresponding neuron populations
for ax, spk, lyr in zip(axes, layer_spikes, neuron_layers):
spike_ids = np.concatenate(spk)
spike_times = np.concatenate([np.ones_like(s) * i * TIMESTEP
for i, s in enumerate(spk)])
# Plot spikes
ax.scatter(spike_times, spike_ids, s=1)
# Set title, axis labels
ax.set_title(lyr.name)
ax.set_ylabel("Spike number")
ax.set_xlim((0, PRESENT_TIMESTEPS * TIMESTEP))
ax.set_ylim((0, lyr.size))
# Add an x-axis label and translucent line showing the correct label
axes[-1].set_xlabel("Time [ms]")
axes[-1].hlines(testing_labels[0], xmin=0, xmax=PRESENT_TIMESTEPS,
linestyle="--", color="gray", alpha=0.2)
# Show plot
plt.show()