Skip to content

fix: SpikingNeuron.zeros() now clears hidden states in-place - #424

Open
xyaz1313 wants to merge 1 commit into
jeshraghian:masterfrom
xyaz1313:fix/spikingneuron-zeros-inplace
Open

fix: SpikingNeuron.zeros() now clears hidden states in-place#424
xyaz1313 wants to merge 1 commit into
jeshraghian:masterfrom
xyaz1313:fix/spikingneuron-zeros-inplace

Conversation

@xyaz1313

Copy link
Copy Markdown

Bug Fix: SpikingNeuron.zeros() now clears hidden states in-place

Root Cause

The SpikingNeuron.zeros() static method used state = torch.zeros_like(state) which creates a new tensor and rebinds the local variable. The caller's original tensor remains unchanged, making the method effectively a no-op.

Fix

Changed to state.zero_() which modifies the tensor in-place, consistent with the detach() method directly above it which already uses state.detach_().

Testing

Added tests/test_snntorch/test_zeros.py with two test cases:

  1. test_zeros_clears_hidden_state — verifies hidden state is cleared after running a Leaky neuron
  2. test_zeros_multiple_args — verifies multiple tensors are zeroed correctly

Fixes #423

The previous implementation used `state = torch.zeros_like(state)` which
only rebinds the local variable without modifying the caller's tensor.
Changed to `state.zero_()` for in-place modification, consistent with
the `detach()` method directly above it.

Fixes jeshraghian#423
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SpikingNeuron.zeros does not clear hidden states

1 participant