Skip to content

Commit 3283308

Browse files
authored
Merge pull request BindsNET#8 from WeihaoTan/master
Fix bugs to make CNN converted to SNN successfully
2 parents c1609b7 + 2795d47 commit 3283308

3 files changed

Lines changed: 18 additions & 20 deletions

File tree

bindsnet/conversion/conversion.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ def set_requires_grad(module, value):
140140
):
141141
prev_module = module2
142142

143+
if isinstance(module2, nn.Linear):
144+
if prev_module is not None:
145+
scale_factor = np.percentile(activations.cpu(), percentile)
146+
prev_module.weight *= prev_factor / scale_factor
147+
prev_module.bias /= scale_factor
148+
prev_factor = scale_factor
149+
143150
else:
144151
activations = all_activations[name]
145152
if isinstance(module, nn.ReLU):
@@ -188,20 +195,16 @@ def _ann_to_snn_helper(prev, current, node_type, last=False, **kwargs):
188195
)
189196

190197
elif isinstance(current, nn.Conv2d):
191-
input_height, input_width = prev.shape[2], prev.shape[3]
192-
out_channels, output_height, output_width = (
193-
current.out_channels,
194-
prev.shape[2],
195-
prev.shape[3],
196-
)
198+
input_height, input_width = prev.shape[1], prev.shape[2]
199+
out_channels = current.out_channels
197200

198201
width = (
199202
input_height - current.kernel_size[0] + 2 * current.padding[0]
200203
) / current.stride[0] + 1
201204
height = (
202205
input_width - current.kernel_size[1] + 2 * current.padding[1]
203206
) / current.stride[1] + 1
204-
shape = (1, out_channels, int(width), int(height))
207+
shape = (out_channels, int(width), int(height))
205208

206209
layer = node_type(
207210
shape=shape, reset=0, thresh=1, refrac=0, sum_input=last, **kwargs
@@ -223,7 +226,7 @@ def _ann_to_snn_helper(prev, current, node_type, last=False, **kwargs):
223226
)
224227

225228
elif isinstance(current, nn.MaxPool2d):
226-
input_height, input_width = prev.shape[2], prev.shape[3]
229+
input_height, input_width = prev.shape[1], prev.shape[2]
227230
current.kernel_size = _pair(current.kernel_size)
228231
current.padding = _pair(current.padding)
229232
current.stride = _pair(current.stride)
@@ -234,7 +237,7 @@ def _ann_to_snn_helper(prev, current, node_type, last=False, **kwargs):
234237
height = (
235238
input_width - current.kernel_size[1] + 2 * current.padding[1]
236239
) / current.stride[1] + 1
237-
shape = (1, prev.shape[1], int(width), int(height))
240+
shape = (prev.shape[0], int(width), int(height))
238241

239242
layer = PassThroughNodes(shape=shape)
240243
connection = topology.MaxPool2dConnection(
@@ -253,7 +256,6 @@ def _ann_to_snn_helper(prev, current, node_type, last=False, **kwargs):
253256
prev.shape[current.dims[0]],
254257
prev.shape[current.dims[1]],
255258
prev.shape[current.dims[2]],
256-
prev.shape[current.dims[3]],
257259
]
258260
)
259261

@@ -265,9 +267,8 @@ def _ann_to_snn_helper(prev, current, node_type, last=False, **kwargs):
265267
layer = PassThroughNodes(
266268
shape=[
267269
prev.shape[0],
268-
prev.shape[1],
269-
current.padding[0] + current.padding[1] + prev.shape[2],
270-
current.padding[2] + current.padding[3] + prev.shape[3],
270+
current.padding[0] + current.padding[1] + prev.shape[1],
271+
current.padding[2] + current.padding[3] + prev.shape[2],
271272
]
272273
)
273274

bindsnet/network/network.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,6 @@ def run(
392392
mask=masks.get(c, None), learning=self.learning, **kwargs
393393
)
394394

395-
# Get input to all layers.
396-
current_inputs.update(self._get_inputs())
397-
398395
# Record state variables of interest.
399396
for m in self.monitors:
400397
self.monitors[m].record()

bindsnet/network/topology.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def __init__(
285285
"(input_height - filter_height + 2 * padding_height) / stride_height + 1,"
286286
"(input_width - filter_width + 2 * padding_width) / stride_width + 1"
287287
)
288-
288+
289289
assert (
290290
target.shape[0] == shape[1]
291291
and target.shape[1] == shape[2]
@@ -405,7 +405,7 @@ def __init__(
405405
self.padding = _pair(padding)
406406
self.dilation = _pair(dilation)
407407

408-
self.register_buffer("firing_rates", torch.zeros(source.shape))
408+
self.register_buffer("firing_rates", torch.zeros(source.s.shape))
409409

410410
def compute(self, s: torch.Tensor) -> torch.Tensor:
411411
# language=rst
@@ -429,7 +429,7 @@ def compute(self, s: torch.Tensor) -> torch.Tensor:
429429
return_indices=True,
430430
)
431431

432-
return s.take(indices).float()
432+
return s.flatten(2).gather(2, indices.flatten(2)).view_as(indices).float()
433433

434434
def update(self, **kwargs) -> None:
435435
# language=rst
@@ -452,7 +452,7 @@ def reset_state_variables(self) -> None:
452452
"""
453453
super().reset_state_variables()
454454

455-
self.firing_rates = torch.zeros(self.source.shape)
455+
self.firing_rates = torch.zeros(self.source.s.shape)
456456

457457

458458
class LocalConnection(AbstractConnection):

0 commit comments

Comments
 (0)