-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqsnn_util.py
More file actions
189 lines (143 loc) · 5.72 KB
/
qsnn_util.py
File metadata and controls
189 lines (143 loc) · 5.72 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
import os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import torch
import torch.nn as nn
import torchvision
time_step = 1e-3
# Check whether a GPU is available
if torch.cuda.is_available():
device = torch.device("cuda")
else:
device = torch.device("cpu")
w1 = None
w2 = None
def current2firing_time(x, tau=20, thr=0.2, tmax=1.0, epsilon=1e-7):
""" Computes first firing time latency for a current input x assuming the charge time of a current based LIF neuron.
Args:
x -- The "current" values
Keyword args:
tau -- The membrane time constant of the LIF neuron to be charged
thr -- The firing threshold value
tmax -- The maximum time returned
epsilon -- A generic (small) epsilon > 0
Returns:
Time to first spike for each "current" x
"""
idx = x<thr
x = np.clip(x,thr+epsilon,1e9)
T = tau*np.log(x/(x-thr))
T[idx] = tmax
return T
def sparse_data_generator(X, y, batch_size, nb_steps, nb_units, shuffle=True ):
""" This generator takes datasets in analog format and generates spiking network input as sparse tensors.
Args:
X: The data ( sample x event x 2 ) the last dim holds (time,neuron) tuples
y: The labels
"""
labels_ = np.array(y,dtype=np.int)
number_of_batches = len(X)//batch_size
sample_index = np.arange(len(X))
# compute discrete firing times
tau_eff = 20e-3/time_step
firing_times = np.array(current2firing_time(X, tau=tau_eff, tmax=nb_steps), dtype=np.int)
unit_numbers = np.arange(nb_units)
if shuffle:
np.random.shuffle(sample_index)
total_batch_count = 0
counter = 0
while counter<number_of_batches:
batch_index = sample_index[batch_size*counter:batch_size*(counter+1)]
coo = [ [] for i in range(3) ]
for bc,idx in enumerate(batch_index):
c = firing_times[idx]<nb_steps
times, units = firing_times[idx][c], unit_numbers[c]
batch = [bc for _ in range(len(times))]
coo[0].extend(batch)
coo[1].extend(times)
coo[2].extend(units)
i = torch.LongTensor(coo).to(device)
v = torch.FloatTensor(np.ones(len(coo[0]))).to(device)
X_batch = torch.sparse.FloatTensor(i, v, torch.Size([batch_size,nb_steps,nb_units])).to(device)
y_batch = torch.tensor(labels_[batch_index],device=device)
yield X_batch.to(device=device), y_batch.to(device=device)
counter += 1
def plot_voltage_traces(mem, spk=None, dim=(3,5), spike_height=5):
gs=GridSpec(*dim)
if spk is not None:
dat = (mem+spike_height*spk).detach().cpu().numpy()
else:
dat = mem.detach().cpu().numpy()
for i in range(np.prod(dim)):
if i==0: a0=ax=plt.subplot(gs[i])
else: ax=plt.subplot(gs[i],sharey=a0)
ax.plot(dat[i])
ax.axis("off")
class SuperSpike(torch.autograd.Function):
"""
Here we implement our spiking nonlinearity which also implements
the surrogate gradient. By subclassing torch.autograd.Function,
we will be able to use all of PyTorch's autograd functionality.
Here we use the normalized negative part of a fast sigmoid
as this was done in Zenke & Ganguli (2018).
"""
scale = 100.0 # controls steepness of surrogate gradient
@staticmethod
def forward(ctx, input):
"""
In the forward pass we compute a step function of the input Tensor
and return it. ctx is a context object that we use to stash information which
we need to later backpropagate our error signals. To achieve this we use the
ctx.save_for_backward method.
"""
ctx.save_for_backward(input)
out = torch.zeros_like(input)
out[input > 0] = 1.0
return out
@staticmethod
def backward(ctx, grad_output):
"""
In the backward pass we receive a Tensor we need to compute the
surrogate gradient of the loss with respect to the input.
Here we use the normalized negative part of a fast sigmoid
as this was done in Zenke & Ganguli (2018).
"""
input, = ctx.saved_tensors
grad_input = grad_output.clone()
grad = grad_input/(SuperSpike.scale*torch.abs(input)+1.0)**2
return grad
def sparse_data_generator_DVS(X, y, batch_size, nb_steps, nb_units, shuffle, time_step, device):
""" This generator takes datasets in analog format and generates spiking network input as sparse tensors.
Args:
X: The data ( sample x event x 2 ) the last dim holds (time,neuron) tuples
y: The labels
"""
try:
labels_ = np.array(y.cpu(),dtype=np.int)
except:
labels_ = np.array(y,dtype=np.int)
number_of_batches = len(y)//batch_size
sample_index = np.arange(len(y))
if shuffle:
np.random.shuffle(sample_index)
total_batch_count = 0
counter = 0
while counter<number_of_batches:
batch_index = sample_index[batch_size*counter:batch_size*(counter+1)]
coo = [ [] for i in range(3) ]
for bc,idx in enumerate(batch_index):
temp = X[X['batch'] == idx]
batch = [bc for _ in range(len(temp['ts']))]
coo[0].extend(batch)
coo[1].extend(temp['ts'].tolist())
coo[2].extend(temp['unit'].tolist())
i = torch.LongTensor(coo)#.to(device)
v = torch.FloatTensor(np.ones(len(coo[0])))#.to(device)
X_batch = torch.sparse.FloatTensor(i, v, torch.Size([batch_size,300,128*128]))#.to(device)
y_batch = torch.tensor(labels_[batch_index])
try:
yield X_batch.to(device=device), y_batch.to(device=device)
counter += 1
except StopIteration:
return