-
Notifications
You must be signed in to change notification settings - Fork 0
/
v1-attributes.py
264 lines (186 loc) · 6.37 KB
/
v1-attributes.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
"""Attempt #1 at organizing neuron models
- We specify types of neurons using subclasses of Neuron
- This includes things like LIF vs HH and also Float vs Fixed, Rate vs Spiking
- We build a NeuronPool object which actually has code for running neurons
- We keep a list of known Neuron types around so if we're asked for just
a Rate neuron, we can pick the first on on the list that matches
"""
import numpy as np
"""
Neuron type specifications
"""
class Neuron(object):
pass
class LIF(Neuron):
def __init__(self, tau_rc=0.02, tau_ref=0.002):
self.tau_rc = tau_rc
self.tau_ref = tau_ref
class Rate(Neuron):
pass
class Spiking(Neuron):
pass
class Fixed(Neuron):
pass
class Izhikevich(Neuron):
def __init__(self, a=0.02, b=0.2, c=-65, d=8):
self.a = a
self.b = b
self.c = c
self.d = d
"""
Base class for neuron pools
Pass in a list of neuron_types to set parameters
"""
class NeuronPool:
def __init__(self, n_neurons, neuron_types=None):
if neuron_types is None:
neuron_types = self.neuron_types
for n in neuron_types:
for key, value in n.__dict__.items():
if not key.startswith('_'):
setattr(self, key, value)
self.make(n_neurons)
def make(self, n_neurons):
raise NotImplementedError('NeuronPools must provide "make"')
def step(self, dt, J):
raise NotImplementedError('NeuronPools must provide "step"')
"""
Various neuron models
"""
class LIFRatePool(NeuronPool):
neuron_type = [LIF, Rate]
def make(self, n_neurons):
pass
def step(self, dt, J):
old = np.seterr(divide='ignore', invalid='ignore')
try:
r = 1.0 / (self.tau_ref + self.tau_rc * np.log1p(1.0 / (J-1)))
r[J <= 1] = 0
finally:
np.seterr(**old)
return r * dt # multiply by dt to do rate per timestep
class LIFSpikingPool(NeuronPool):
neuron_type = [LIF, Spiking]
def make(self, n_neurons):
self.voltage = np.zeros(n_neurons)
self.refractory_time = np.zeros(n_neurons)
def step(self, dt, J):
dv = (dt / self.tau_rc) * (J - self.voltage)
self.voltage += dv
self.voltage[self.voltage < 0] = 0
self.refractory_time -= dt
self.voltage *= (1-self.refractory_time / dt).clip(0, 1)
spiked = self.voltage > 1
overshoot = (self.voltage[spiked > 0] - 1) / dv[spiked > 0]
spiketime = dt * (1 - overshoot)
self.voltage[spiked > 0] = 0
self.refractory_time[spiked > 0] = self.tau_ref + spiketime
return spiked
class LIFFixedPool(NeuronPool):
neuron_type = [LIF, Spiking, Fixed]
def make(self, n_neurons):
self.voltage = np.zeros(n_neurons, dtype='i32')
self.refractory_time = np.zeros(n_neurons, dtype='u8')
self.dt = None
self.lfsr = 1
def step(self, dt, J):
if self.dt != dt:
self.dt = dt
self.dt_over_tau_rc = int(dt * 0x10000 / self.tau_rc)
self.ref_steps = int(self.tau_ref / dt)
J = np.asarray(J * 0x10000, dtype='i32')
dv = ((J - self.voltage) * self.dt_over_tau_rc) >> 16
dv[self.refractory_time > 0] = 0
self.refractory_time[self.refractory_time > 0] -= 1
self.voltage += dv
self.voltage[self.voltage < 0] = 0
spiked = self.voltage > 0x10000
self.refractory_time[spiked > 0] = self.ref_steps
# randomly adjust the refractory period to account for overshoot
for i in np.where(spiked > 0)[0]:
p = ((self.voltage[i] - 0x10000) << 16) / dv[i]
if self.lfsr < p:
self.refractory_time[i] -= 1
self.lfsr = (self.lfsr >> 1) ^ (-(self.lfsr & 0x1) & 0xB400)
self.voltage[spiked > 0] = 0
return spiked
class IzhikevichPool(NeuronPool):
neuron_type = [Izhikevich, Spiking]
def make(self, n_neurons):
self.v = np.zeros(n_neurons) + self.c
self.u = self.b * self.v
def step(self, dt, J):
dv = (0.04 * self.v ** 2 + 5 * self.v + 140 - self.u + J) * 1000
du = (self.a * (self.b * self.v - self.u)) * 1000
self.v += dv * dt
self.u += du * dt
spiked = self.v >= 30
self.v[spiked > 0] = self.c
self.u[spiked > 0] = self.u[spiked > 0] + self.d
return spiked
"""
List of known neuron models, in order of preference
"""
neuron_models = [
LIFSpikingPool,
LIFRatePool,
LIFFixedPool,
IzhikevichPool,
]
"""
Create a pool of neurons, given the required type specifications
"""
import inspect
def create(n_neurons, neuron_type):
# make sure it's a list
try:
len(neuron_type)
except TypeError:
neuron_type = [neuron_type]
# make sure elements in the list are instances, not classes
for i, type in enumerate(neuron_type):
if inspect.isclass(type):
neuron_type[i] = type()
# look through the list of neuron models to see if we can
# find a match
for model in neuron_models:
for type in neuron_type:
if type.__class__ not in model.neuron_type:
break
else:
return model(n_neurons, neuron_type)
raise Exception('Could not find suitable neuron model')
if __name__ == '__main__':
spiking = create(100, [LIF, Spiking])
rate = create(100, [LIF, Rate])
fixed = create(100, [LIF, Fixed])
iz = create(100, [Izhikevich])
#iz = create(100, [Izhikevich(a=0.02, b=0.2, c=-50, d=2)])
J = np.linspace(-2, 10, 100)
dt = 0.001
T = 1
spiking_data = []
rate_data = []
iz_data = []
fixed_data = []
v = []
for i in range(int(T/dt)):
spiking_data.append(spiking.step(dt, J))
rate_data.append(rate.step(dt, J))
iz_data.append(iz.step(dt, J))
fixed_data.append(fixed.step(dt, J))
v.append(fixed.voltage[-1])
rate_tuning = np.sum(rate_data, axis=0)/T
spiking_tuning = np.sum(spiking_data, axis=0)/T
iz_tuning = np.sum(iz_data, axis=0)/T
fixed_tuning = np.sum(fixed_data, axis=0)/T
import pylab
pylab.subplot(2, 1, 1)
pylab.plot(J, rate_tuning)
pylab.plot(J, spiking_tuning)
pylab.plot(J, iz_tuning)
pylab.plot(J, fixed_tuning, linewidth=4)
pylab.subplot(2, 1, 2)
pylab.plot(v)
#pylab.plot(np.array(fixed_data)[:,-1])
pylab.show()