forked from fmi-basel/stork
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayers.py
344 lines (289 loc) · 10.3 KB
/
layers.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
# Layers module
# Julian, October 2021
from . import connections
from . import nodes
from . import utils
from . import constraints
from typing import Iterable
class AbstractLayer:
"""
Abstract base class for layer object
"""
def __init__(self, name, model, recurrent, dalian=False) -> None:
super().__init__()
self.model = model
self.name = name
self.recurrent = recurrent
self.dalian = dalian
# Lists that hold neurons and connections in this layer
self.neurons = []
self.connections = []
def add_connection(self, connection, recurrent="detect", inhibitory=False):
"""
Adds a connection to the layer
:param source: source group
:param destination: target group
:param connection_class: connection class to use
:param recurrent: whether connection is considered recurrent or not
:param inhibitory: whether the neurons are inhibitory or not (for dalian layers)
"""
# Assert that the target neuron group is in the layer
assert (
connection.dst in self.neurons
), "Target neuron group is not in this Layer"
# Detect whether connection is recurrent or not
if recurrent == "detect":
recurrent = True if connection.src == connection.dst else False
else:
assert isinstance(recurrent, bool)
# Add is_recurrent and is_inhibitory boolean to connection
# This is used by some initializer classes to calculate optimal weight initializations
connection.is_recurrent = recurrent
connection.is_inhibitory = inhibitory
# Add pointers to the connection to the model, layer and neuron object
self.model.add_connection(connection)
connection.dst.afferents.append(connection)
self.connections.append(connection)
def add_neurons(self, neurons, inhibitory=False):
"""
Adds a neuron group to the layer
:param neurons: neuron group
"""
# Add afferents list to neuron group
neurons.afferents = []
# Flag inhibitory
neurons.is_inhibitory = inhibitory
# Add pointers to self and model
self.neurons.append(neurons)
self.model.add_group(neurons)
class Layer(AbstractLayer):
"""
Implements a 'Layer' class that wraps multiple 'nodes' and 'connection' objects
and adds them to an instance of an nn.Module.
The 'Layer' class fulfills the following purpose:
1. Provide an easy-to-use and easy-to-modify constructor for each layer of a neural network
2. Enable layer-wise initialization strategies. Some initializer classes (in the 'initializers.py') module
take a 'Layer' object as input and initialize all connections in the layer.
The 'Layer' class is only a constructor and does NOT inherit from `nn.Module`, nor does it
add a pointer to itself to the model. This could be differently implemented in the future
"""
def __init__(
self,
name,
model,
size,
input_group,
recurrent=True,
regs=None,
w_regs=None,
connection_class=connections.Connection,
neuron_class=nodes.LIFGroup,
flatten_input_layer=True,
neuron_kwargs={},
connection_kwargs={},
) -> None:
super().__init__(name, model, recurrent)
# Make neuron group
nodes = neuron_class(size, name=self.name, regularizers=regs, **neuron_kwargs)
self.add_neurons(nodes)
# Make afferent connection
con = connection_class(
input_group,
nodes,
regularizers=w_regs,
flatten_input=flatten_input_layer,
**connection_kwargs
)
self.add_connection(con)
# Make recurrent connection
if recurrent:
con = connection_class(
nodes, nodes, regularizers=w_regs, **connection_kwargs
)
self.add_connection(con)
self.output_group = nodes
class ConvLayer(AbstractLayer):
"""
Implements a spiking Convolutional Layer
"""
def __init__(
self,
name,
model,
input_group,
kernel_size,
stride,
padding=0,
nb_filters=16,
shape="auto",
recurrent=True,
regs=None,
w_regs=None,
connection_class=connections.ConvConnection,
neuron_class=nodes.LIFGroup,
neuron_kwargs={},
connection_kwargs={},
recurrent_connection_kwargs={},
) -> None:
super().__init__(name, model, recurrent)
# Calculate size of Convolutional Layer
# Must provide either the exact `shape` or a `nb_filters` parameter
if shape == "auto":
assert isinstance(
nb_filters, int
), "Must provide nb_filters to calculate ConvLayer shape"
shape = utils.convlayer_size(
nb_inputs=input_group.shape[1:],
kernel_size=kernel_size,
padding=padding,
stride=stride,
)
shape_dim = len(input_group.shape) - 1
if shape_dim == 1:
shape = tuple([nb_filters, int(shape[0])])
else:
shape = tuple([nb_filters] + [int(i) for i in shape])
else:
assert isinstance(
shape, tuple
), "`shape` must be 'auto' or a tuple of integers"
# Make neuron group
nodes = neuron_class(shape, name=self.name, regularizers=regs, **neuron_kwargs)
self.add_neurons(nodes)
# Make afferent connection
con = connection_class(
input_group,
nodes,
regularizers=w_regs,
kernel_size=kernel_size,
stride=stride,
padding=padding,
**connection_kwargs
)
self.add_connection(con)
# Make recurrent connection
if recurrent:
rec_kernel_size = recurrent_connection_kwargs.pop("kernel_size", 5)
rec_stride = recurrent_connection_kwargs.pop("stride", 1)
rec_padding = recurrent_connection_kwargs.pop("padding", 2)
con = connection_class(
nodes,
nodes,
regularizers=w_regs,
kernel_size=rec_kernel_size,
stride=rec_stride,
padding=rec_padding,
**recurrent_connection_kwargs
)
self.add_connection(con)
self.output_group = nodes
class DalianLayer(AbstractLayer):
"""
Implements a fully connected layer following Dale's law.
Consists of one Excitatory and one Inhibitory population
"""
def __init__(
self,
name,
model,
size,
input_group,
ei_ratio=4,
recurrent=True,
regs=None,
w_regs=None,
connection_class=connections.Connection,
neuron_class=nodes.ExcInhLIFGroup,
flatten_input_layer=True,
exc_neuron_kwargs={},
inh_neuron_kwargs={},
ff_connection_kwargs={},
rec_inh_connection_kwargs={},
rec_exc_connection_kwargs={},
) -> None:
super().__init__(name, model, recurrent=recurrent, dalian=True)
# Add Dalian constraint
pos_constraint = constraints.MinMaxConstraint(min=0.0)
# Compute inhibitory layer size
if isinstance(size, Iterable):
# For conv layer
size_inh = (int(tuple(size)[0] / ei_ratio),) + tuple(size)[1:]
else:
# For normal layer
size_inh = int(size / ei_ratio)
size = tuple(size) if isinstance(size, Iterable) else size
# Make Exc neuron group
nodes_exc = neuron_class(
size, name=self.name + " exc", regularizers=regs, **exc_neuron_kwargs
)
self.add_neurons(nodes_exc)
# Make Inh neuron group
nodes_inh = neuron_class(
size_inh, name=self.name + " inh", regularizers=regs, **inh_neuron_kwargs
)
self.add_neurons(nodes_inh, inhibitory=True)
# Make afferent connections
con_XE = connection_class(
input_group,
nodes_exc,
name="XE",
regularizers=w_regs,
constraints=pos_constraint,
**ff_connection_kwargs,
flatten_input=flatten_input_layer
)
self.add_connection(con_XE, recurrent=False, inhibitory=False)
con_XI = connection_class(
input_group,
nodes_inh,
name="XI",
regularizers=w_regs,
constraints=pos_constraint,
**ff_connection_kwargs,
flatten_input=flatten_input_layer
)
self.add_connection(con_XI, recurrent=False, inhibitory=False)
# RECURRENT CONNECTIONS: INHIBITORY
# # # # # # # # # # # # # #
con_II = connection_class(
nodes_inh,
nodes_inh,
target="inh",
name="II",
regularizers=w_regs,
constraints=pos_constraint,
**rec_inh_connection_kwargs
)
self.add_connection(con_II, recurrent=False, inhibitory=True)
con_IE = connection_class(
nodes_inh,
nodes_exc,
target="inh",
name="IE",
regularizers=w_regs,
constraints=pos_constraint,
**rec_inh_connection_kwargs
)
self.add_connection(con_IE, recurrent=False, inhibitory=True)
# RECURRENT CONNECTIONS: EXCITATORY
# # # # # # # # # # # # # #
if recurrent:
con_EI = connection_class(
nodes_exc,
nodes_inh,
name="EI",
regularizers=w_regs,
constraints=pos_constraint,
**rec_exc_connection_kwargs
)
self.add_connection(con_EI, recurrent=True, inhibitory=False)
con_EE = connection_class(
nodes_exc,
nodes_exc,
name="EE",
regularizers=w_regs,
constraints=pos_constraint,
**rec_exc_connection_kwargs
)
self.add_connection(con_EE, recurrent=True, inhibitory=False)
self.output_group = nodes_exc