Skip to content

SpikingNeuron.zeros does not clear hidden states #423

Description

@GraceDyer

Issue type

  • Bug Report
  • Feature Request
  • Help wanted
  • Other

snnTorch version

0.9.4

Description

I found a reproducible bug in the public helper SpikingNeuron.zeros.

The helper appears intended to clear hidden-state tensors to zero, but in the current implementation it does not actually modify the passed state tensor.

From source inspection, the implementation is effectively:

@staticmethod
def zeros(*args):
    for state in args:
        state = torch.zeros_like(state)

This only rebinds the local variable state and does not mutate the caller-visible tensor, so the original hidden state remains unchanged.

Because of this, calling SpikingNeuron.zeros(...) is currently a no-op in the tested path.

Minimal code to reproduce the error/bug

import torch
import snntorch as snn
from snntorch._neurons.neurons import SpikingNeuron

lif = snn.Leaky(beta=0.5, init_hidden=True)

_ = lif(torch.ones(2, 4))
before = lif.mem.clone()

SpikingNeuron.zeros(lif.mem)

after = lif.mem.clone()

print("before_nonzero =", int((before != 0).sum().item()))
print("after_nonzero =", int((after != 0).sum().item()))
print("all_zero_after =", bool(torch.equal(after, torch.zeros_like(after))))

What is expected

Calling SpikingNeuron.zeros(state) should clear the provided hidden state tensor to zero.

What actually happens

The tensor is unchanged.

Example output in my reproduction:

before_nonzero = 8
after_nonzero = 8
all_zero_after = False

This looks like an implementation bug rather than an environment issue, because the helper currently reassigns a local variable instead of modifying the provided tensor.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions