forked from BindsNET/bindsnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupervised_mnist.py
More file actions
334 lines (283 loc) · 10.8 KB
/
supervised_mnist.py
File metadata and controls
334 lines (283 loc) · 10.8 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
from bindsnet.network.nodes import Nodes
import os
import torch
import torchvision
import numpy as np
import argparse
import matplotlib.pyplot as plt
from torchvision import transforms
from tqdm import tqdm
from bindsnet.datasets import MNIST
from bindsnet.encoding import PoissonEncoder
from bindsnet.network import Network
from bindsnet.network.nodes import Input, LIFNodes
from bindsnet.network.topology import LocalConnection, Connection
from bindsnet.network.monitors import Monitor
from bindsnet.utils import get_square_assignments, get_square_weights
from bindsnet.evaluation import all_activity, proportion_weighting, assign_labels
from bindsnet.analysis.plotting import (
plot_input,
plot_assignments,
plot_performance,
plot_weights,
plot_spikes,
plot_voltages,
)
parser = argparse.ArgumentParser()
parser.add_argument("--seed", type=int, default=0)
parser.add_argument("--n_neurons", type=int, default=100)
parser.add_argument("--n_train", type=int, default=250)
parser.add_argument("--n_test", type=int, default=100)
parser.add_argument("--n_clamp", type=int, default=1)
parser.add_argument("--exc", type=float, default=22.5)
parser.add_argument("--inh", type=float, default=120)
parser.add_argument("--theta_plus", type=float, default=0.05)
parser.add_argument("--time", type=int, default=250)
parser.add_argument("--dt", type=int, default=1.0)
parser.add_argument("--intensity", type=float, default=32)
parser.add_argument("--progress_interval", type=int, default=10)
parser.add_argument("--update_interval", type=int, default=250)
parser.add_argument("--train", dest="train", action="store_true")
parser.add_argument("--test", dest="train", action="store_false")
parser.add_argument("--plot", dest="plot", action="store_true")
parser.add_argument("--gpu", dest="gpu", action="store_true")
parser.add_argument("--device_id", type=int, default=0)
parser.set_defaults(plot=False, gpu=True, train=True)
args = parser.parse_args()
seed = args.seed
n_neurons = args.n_neurons
n_train = args.n_train
n_test = args.n_test
n_clamp = args.n_clamp
exc = args.exc
inh = args.inh
theta_plus = args.theta_plus
time = args.time
dt = args.dt
intensity = args.intensity
progress_interval = args.progress_interval
update_interval = args.update_interval
train = args.train
plot = args.plot
gpu = args.gpu
device_id = args.device_id
gpu=True
# Sets up Gpu use
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
device = 'cuda'
print(torch.cuda.is_available())
if gpu and torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
else:
torch.manual_seed(seed)
device = "cpu"
if gpu:
gpu = False
torch.set_num_threads(os.cpu_count() - 1)
print("Running on Device = ", device)
if not train:
update_interval = n_test
n_classes = 10
n_sqrt = int(np.ceil(np.sqrt(n_neurons)))
start_intensity = intensity
per_class = int(n_neurons / n_classes)
# Design network
C = 1
K = 3
S = 1
compute_size = lambda inp_size, k, s: int((inp_size-k)/s) + 1
network = Network()
# nodes
inp = Input(shape= [1,28,28])
main = LIFNodes(shape= [C, compute_size(28, K, S), compute_size(28, K, S)])
# TODO: Diehl & Cook 2015 (v2)
out = LIFNodes(n= 100)
# connections
LC = LocalConnection(inp, main, K, S, C, [1e-2, 1e-4], )
main_out = Connection(main, out, [1e-2, 1e-4])
#
network.add_layer(main, "main")
network.add_layer(inp, "input")
network.add_layer(out, "output")
network.add_connection(LC, "input", "main")
network.add_connection(main_out, "main", "output")
# Directs network to GPU
if gpu:
network.to("cuda")
# Voltage recording for excitatory and inhibitory layers.
main_monitor = Monitor(network.layers["main"], ["v"], time=time, device=device)
output_monitor = Monitor(network.layers["output"], ["v"], time=time, device=device)
network.add_monitor(main_monitor, name="main")
network.add_monitor(output_monitor, name="output")
# Load MNIST data.
dataset = MNIST(
PoissonEncoder(time=time, dt=dt),
None,
root=os.path.join("..", "..", "data", "MNIST"),
download=True,
transform=transforms.Compose(
[transforms.ToTensor(), transforms.Lambda(lambda x: x * intensity)]
),
)
# Create a dataloader to iterate and batch data
dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=True)
# Record spikes during the simulation.
spike_record = torch.zeros(update_interval, time, n_neurons, device=device)
# Neuron assignments and spike proportions.
assignments = -torch.ones_like(torch.Tensor(n_neurons), device=device)
proportions = torch.zeros_like(torch.Tensor(n_neurons, n_classes), device=device)
rates = torch.zeros_like(torch.Tensor(n_neurons, n_classes), device=device)
# Sequence of accuracy estimates.
accuracy = {"all": [], "proportion": []}
# Labels to determine neuron assignments and spike proportions and estimate accuracy
labels = torch.empty(update_interval, device=device)
spikes = {}
for layer in set(network.layers):
spikes[layer] = Monitor(network.layers[layer], state_vars=["s"], time=time)
network.add_monitor(spikes[layer], name="%s_spikes" % layer)
# Train the network.
print("Begin training.\n")
inpt_axes = None
inpt_ims = None
spike_axes = None
spike_ims = None
weights_im = None
assigns_im = None
perf_ax = None
voltage_axes = None
voltage_ims = None
pbar = tqdm(total=n_train)
for (i, datum) in enumerate(dataloader):
if i > n_train:
break
image = datum["encoded_image"]
label = datum["label"]
if i % update_interval == 0 and i > 0:
# Get network predictions.
all_activity_pred = all_activity(spike_record, assignments, n_classes)
proportion_pred = proportion_weighting(
spike_record, assignments, proportions, n_classes
)
# Compute network accuracy according to available classification strategies.
accuracy["all"].append(
100 * torch.sum(labels.long() == all_activity_pred).item() / update_interval
)
accuracy["proportion"].append(
100 * torch.sum(labels.long() == proportion_pred).item() / update_interval
)
print(
"\nAll activity accuracy: %.2f (last), %.2f (average), %.2f (best)"
% (accuracy["all"][-1], np.mean(accuracy["all"]), np.max(accuracy["all"]))
)
print(
"Proportion weighting accuracy: %.2f (last), %.2f (average), %.2f (best)\n"
% (
accuracy["proportion"][-1],
np.mean(accuracy["proportion"]),
np.max(accuracy["proportion"]),
)
)
# Assign labels to excitatory layer neurons.
assignments, proportions, rates = assign_labels(
spike_record, labels, n_classes, rates
)
# Add the current label to the list of labels for this update_interval
labels[i % update_interval] = label[0]
# Run the network on the input.
choice = np.random.choice(int(n_neurons / n_classes), size=n_clamp, replace=False)
clamp = {"output": per_class * label.long() + torch.Tensor(choice).long()}
if gpu:
inputs = {"X": image.cuda().view(time, 1, 1, 28, 28)}
else:
inputs = {"X": image.view(time, 1, 1, 28, 28)}
network.run(inputs=inputs, time=time, clamp=clamp)
# Get voltage recording.
exc_voltages = main_monitor.get("v")
inh_voltages = output_monitor.get("v")
# Add to spikes recording.
spike_record[i % update_interval] = spikes["output"].get("s").view(time, n_neurons)
# Optionally plot various simulation information.
if plot:
inpt = inputs["X"].view(time, 784).sum(0).view(28, 28)
input_exc_weights = network.connections[("input", "main")].w
square_weights = get_square_weights(
input_exc_weights.view(784, n_neurons), n_sqrt, 28
)
square_assignments = get_square_assignments(assignments, n_sqrt)
voltages = {"main": exc_voltages, "output": inh_voltages}
inpt_axes, inpt_ims = plot_input(
image.sum(1).view(28, 28), inpt, label=label, axes=inpt_axes, ims=inpt_ims
)
spike_ims, spike_axes = plot_spikes(
{layer: spikes[layer].get("s").view(time, 1, -1) for layer in spikes},
ims=spike_ims,
axes=spike_axes,
)
weights_im = plot_weights(square_weights, im=weights_im)
assigns_im = plot_assignments(square_assignments, im=assigns_im)
perf_ax = plot_performance(accuracy, x_scale=update_interval, ax=perf_ax)
voltage_ims, voltage_axes = plot_voltages(
voltages, ims=voltage_ims, axes=voltage_axes
)
plt.pause(1e-8)
network.reset_state_variables() # Reset state variables.
pbar.set_description_str("Train progress: ")
pbar.update()
print("Progress: %d / %d \n" % (n_train, n_train))
print("Training complete.\n")
print("Testing....\n")
# Load MNIST data.
test_dataset = MNIST(
PoissonEncoder(time=time, dt=dt),
None,
root=os.path.join("..", "..", "data", "MNIST"),
download=True,
train=False,
transform=transforms.Compose(
[transforms.ToTensor(), transforms.Lambda(lambda x: x * intensity)]
),
)
# Sequence of accuracy estimates.
accuracy = {"all": 0, "proportion": 0}
# Record spikes during the simulation.
spike_record = torch.zeros(1, int(time / dt), n_neurons, device=device)
# Train the network.
print("\nBegin testing\n")
network.train(mode=False)
pbar = tqdm(total=n_test)
for step, batch in enumerate(test_dataset):
if step > n_test:
break
# Get next input sample.
inputs = {"X": batch["encoded_image"].view(int(time / dt), 1, 1, 28, 28)}
if gpu:
inputs = {k: v.cuda() for k, v in inputs.items()}
# Run the network on the input.
network.run(inputs=inputs, time=time, input_time_dim=1)
# Add to spikes recording.
spike_record[0] = spikes["output"].get("s").squeeze()
# Convert the array of labels into a tensor
label_tensor = torch.tensor(batch["label"], device=device)
# Get network predictions.
all_activity_pred = all_activity(
spikes=spike_record, assignments=assignments, n_labels=n_classes
)
proportion_pred = proportion_weighting(
spikes=spike_record,
assignments=assignments,
proportions=proportions,
n_labels=n_classes,
)
# Compute network accuracy according to available classification strategies.
accuracy["all"] += float(torch.sum(label_tensor.long() == all_activity_pred).item())
accuracy["proportion"] += float(
torch.sum(label_tensor.long() == proportion_pred).item()
)
network.reset_state_variables() # Reset state variables.
pbar.set_description_str(
f"Accuracy: {(max(accuracy['all'] ,accuracy['proportion'] ) / (step+1)):.3}"
)
pbar.update()
print("\nAll activity accuracy: %.2f" % (accuracy["all"] / n_test))
print("Proportion weighting accuracy: %.2f \n" % (accuracy["proportion"] / n_test))
print("Testing complete.\n")