Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Floating point precision error #580

Open
zechengtang opened this issue Sep 22, 2024 · 0 comments
Open

Floating point precision error #580

zechengtang opened this issue Sep 22, 2024 · 0 comments

Comments

@zechengtang
Copy link

zechengtang commented Sep 22, 2024

There is a floating point precision error when using 0.02 at each time step. This issue can be reproduced with the following code:

from spikingjelly.activation_based import neuron
import torch

if_layer = neuron.IFNode(step_mode='s')

print(' -'* 6)
if_layer.reset()
print(f"init v: {if_layer.v}")
x = torch.FloatTensor([0.05])
print(f"input {x}")
print(' -'* 6)
for i in range(100):
    out = if_layer(x)
    if out == 1:
        print(f"if_layer fires at step {i}")
        
print(' -'* 6)
if_layer.reset()
print(f"init v: {if_layer.v}")
x = torch.FloatTensor([0.02])
print(f"input {x}")
print(' -'* 6)

for i in range(200):
    out = if_layer(x)
    if out == 1:
        print(f"if_layer fires at step {i}")
        
print(' -'* 6)
if_layer.reset()
print(f"init v: {if_layer.v}")
x = torch.FloatTensor([0.02])
print(f"input {x}")
print(' -'* 6)

for i in range(50):
    out = if_layer(x)
print(f"membrane at step 50:   {if_layer.v}")
print(f"membrane - 1.0:        {if_layer.v - 1.0}")

It appears that the error stems from float32 precision limitations rather than any specific code within this project. I recommend adding a parameter eps with a default value of 1e-6 and changing the judging condition from v >= v_threshold to v + eps >= v_threshold. Or you could just adjust the variable x used in your tutorials (https://spikingjelly.readthedocs.io/zh-cn/0.0.0.0.14/activation_based/neuron.html#id4) to avoid any misunderstanding.

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

No branches or pull requests

1 participant