Skip to content

Commit 593d8dc

Browse files
Hananel-Hazanclaude
andcommitted
fix: bernoulli_loader honors max_prob kwarg (#743)
bernoulli_loader read kwargs.get("dt") instead of kwargs.get("max_prob"). Since dt is a named parameter it never lands in **kwargs, so max_prob was silently ignored and the spike rate stayed at 1.0 regardless of the argument. Adds test_bernoulli_loader_max_prob, which asserts the empirical spike rate tracks max_prob (the existing test only checked output shape, so the bug went undetected). Fix mirrors PR #743. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 406cce3 commit 593d8dc

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎bindsnet/encoding/loaders.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def bernoulli_loader(
2626
:param float max_prob: Maximum probability of spike per Bernoulli trial.
2727
"""
2828
# Setting kwargs.
29-
max_prob = kwargs.get("dt", 1.0)
29+
max_prob = kwargs.get("max_prob", 1.0)
3030

3131
for i in range(len(data)):
3232
# Encode datum as Bernoulli spike trains.

‎test/encoding/test_encoding.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@ def test_bernoulli_loader(self):
3737
for i, spikes in enumerate(spike_loader):
3838
assert spikes.size() == torch.Size((t, n))
3939

40+
def test_bernoulli_loader_max_prob(self):
41+
# Regression test (PR #743): bernoulli_loader must honor the ``max_prob``
42+
# keyword argument. Previously it read ``kwargs.get("dt")``, which never
43+
# exists in kwargs (``dt`` is a named parameter), so max_prob was silently
44+
# ignored and the spike rate stayed at 1.0 regardless of the argument.
45+
torch.manual_seed(0)
46+
47+
# All-ones input over many trials: the empirical spike rate should track
48+
# max_prob, not the dt default of 1.0.
49+
data = torch.ones(1, 20000)
50+
for m in [0.0, 0.25, 0.5, 0.75]:
51+
spikes = next(bernoulli_loader(data, time=1, max_prob=m))
52+
assert abs(spikes.float().mean().item() - m) < 0.02
53+
54+
# dt must not leak into max_prob: with dt set but max_prob explicit,
55+
# the rate follows max_prob.
56+
spikes = next(bernoulli_loader(data, time=1, dt=0.5, max_prob=0.3))
57+
assert abs(spikes.float().mean().item() - 0.3) < 0.02
58+
59+
# Back-compat: omitting max_prob defaults to 1.0.
60+
spikes = next(bernoulli_loader(data, time=1))
61+
assert spikes.float().mean().item() == 1.0
62+
4063
def test_poisson(self):
4164
for n in [1, 100]: # number of nodes in layer
4265
for t in [1000]: # number of timesteps

0 commit comments

Comments
 (0)