fangwei123456 / spikingjelly

SpikingJelly is an open-source deep learning framework for Spiking Neural Network (SNN) based on PyTorch.
https://spikingjelly.readthedocs.io
Other
1.4k stars 244 forks source link

Membrane value is 0.0 with a burst of spikes #503

Closed JackCaster closed 8 months ago

JackCaster commented 8 months ago

Issue type

SpikingJelly version

0.0.0.0.14

Description

When a LIFNode is supplied with an high enough current to spike continuously, its membrane value remains 0. I wonder if this is the expected behavior.

For example, when the current is at 1.3, I get this image

But when I increase the current to 2.5, I get this image

I would expect the voltage to keep moving between its reset state and the threshold regardless of the value of the input current. My application requires the value of the membrane, not only the spikes.

What do you think?

Minimal code to reproduce the error/bug

from spikingjelly.activation_based import neuron

import matplotlib.pyplot as plt
import numpy as np

torch.manual_seed(2024)

dt = 0.01
ts = torch.arange(0.0, 1.0, dt)
xs = torch.ones_like(ts) * 2.5

lif = neuron.LIFNode(v_reset=0.0)

ys = []
vs = []

for x in xs:
    ys.append(lif(x).unsqueeze(0))
    vs.append(lif.v.unsqueeze(0))

ys = torch.concatenate(ys)
vs = torch.concatenate(vs)

fig, ax = plt.subplots(3, 1, tight_layout=True)
ax[0].plot(ts, xs)
ax[0].set_title("Stimuli")
ax[1].plot(ts, vs)
ax[1].set_title("Membrane potential")
ax[2].vlines(ts[ys.bool()], -0.25, 0.25, color="r")
ax[2].set_title("Spikes")

plt.show()
tmasquelier commented 8 months ago

Hi, I think this behavior is expected. If your input is so strong that you have a spike at every timestep, then you reset at every timestep, and the potential remains at the reset value. I think what you want here is to decrease the duration of the timestep in order to capture the potential increase. In SJ there is no notion of milliseconds, all the time constants are expressed in number of timesteps. So decreasing the timestep duration boils down to increasing the time constants (expressed in timesteps). So try to increase the tau of your LIF. I hope this helps.

JackCaster commented 8 months ago

Thank you. Let's say that my input is sampled with a dt = 0.01 (100 Hz). Does it mean that I should set tau = 1/dt?

tmasquelier commented 8 months ago

No. The sampling rate should be >> 1/tau If you want to remain in the biological range, you have tau ~ 10 ms So you should sample your input at ~ 1000 Hz (or interpolate your 100Hz signal) So dt = 1 ms And tau = 10 dt