Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ data/*
.claude/*
.claude
.vscode/*
.venv/*
10 changes: 5 additions & 5 deletions bindsnet/encoding/encodings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def single(
quantile = torch.quantile(datum, 1 - sparsity)
s = torch.zeros([time, *shape], device=device)
s[0] = torch.where(datum > quantile, torch.ones(shape), torch.zeros(shape))
return torch.Tensor(s).byte()
return s.bool()


def repeat(datum: torch.Tensor, time: int, dt: float = 1.0, **kwargs) -> torch.Tensor:
Expand Down Expand Up @@ -93,7 +93,7 @@ def bernoulli(
spikes = torch.bernoulli(max_prob * datum.repeat([time, 1]))
spikes = spikes.view(time, *shape)

return spikes.byte()
return spikes.bool()


def poisson(
Expand Down Expand Up @@ -131,7 +131,7 @@ def poisson(
x = torch.pow(x, (datum * 0.11 + 5) / 50)
y = torch.tensor(x < 0.6, dtype=torch.bool, device=device)

return y.view(time, *shape).byte()
return y.view(time, *shape).bool()
else:
# Compute firing rates in seconds as function of data intensity,
# accounting for simulation time step.
Expand All @@ -149,7 +149,7 @@ def poisson(
times[times >= time + 1] = 0

# Create tensor of spikes.
spikes = torch.zeros(time + 1, size, device=device).byte()
spikes = torch.zeros(time + 1, size, device=device, dtype=torch.bool)
spikes[times, torch.arange(size)] = 1
spikes = spikes[1:]

Expand Down Expand Up @@ -184,7 +184,7 @@ def rank_order(

# Create spike times tensor (one spike per neuron whose time lies in
# ``(0, time)``; vectorised form of the per-neuron loop).
spikes = torch.zeros(time, size, device=device).byte()
spikes = torch.zeros(time, size, device=device, dtype=torch.bool)
fire = (times > 0) & (times < time)
idx = fire.nonzero(as_tuple=False).squeeze(1)
spikes[(times[fire] - 1).to(device), idx.to(device)] = 1
Expand Down
14 changes: 6 additions & 8 deletions bindsnet/network/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(
self.traces_additive = (
traces_additive # Whether to record spike traces additively.
)
self.register_buffer("s", torch.ByteTensor()) # Spike occurrences.
self.register_buffer("s", torch.BoolTensor()) # Spike occurrences.

self.sum_input = sum_input # Whether to sum all inputs.

Expand Down Expand Up @@ -232,7 +232,7 @@ def forward(self, x: torch.Tensor) -> None:
:param x: Inputs to the layer.
"""
# Set spike occurrences to input values.
self.s = x
self.s = x.bool()

super().forward(x)

Expand Down Expand Up @@ -1239,7 +1239,7 @@ def __init__(
self.c = -65.0 + 15 * (self.r**2)
self.d = 8 - 6 * (self.r**2)
self.S = 0.5 * torch.rand(n, n)
self.excitatory = torch.ones(n).byte()
self.excitatory = torch.ones(n, dtype=torch.bool)

elif excitatory == 0:
self.r = torch.rand(n)
Expand All @@ -1249,10 +1249,10 @@ def __init__(
self.d = 2 * torch.ones(n)
self.S = -torch.rand(n, n)

self.excitatory = torch.zeros(n).byte()
self.excitatory = torch.zeros(n, dtype=torch.bool)

else:
self.excitatory = torch.zeros(n).byte()
self.excitatory = torch.zeros(n, dtype=torch.bool)

ex = int(n * excitatory)
inh = n - ex
Expand Down Expand Up @@ -1430,9 +1430,7 @@ def __init__(
) # Set in compute_decays.

self.register_buffer("v", torch.FloatTensor()) # Neuron voltages.
self.register_buffer(
"last_spikes", torch.ByteTensor()
) # Previous spikes occurrences in time window
self.register_buffer("last_spikes", torch.BoolTensor())
self.register_buffer("theta", torch.zeros(*self.shape)) # Adaptive thresholds.
self.lbound = lbound # Lower bound of voltage.

Expand Down
2 changes: 1 addition & 1 deletion bindsnet/network/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def update(self, **kwargs) -> None:
Keyword arguments:

:param bool learning: Whether to allow connection updates.
:param ByteTensor mask: Boolean mask determining which weights to clamp to zero.
:param BoolTensor mask: Boolean mask determining which weights to clamp to zero.
"""
learning = kwargs.get("learning", True)

Expand Down
3 changes: 1 addition & 2 deletions docs/source/guide/guide_part_i.rst
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ two-layer, input-output spiking neural network.
network.add_monitor(monitor=target_monitor, name="B")

# Create input spike data, where each spike is distributed according to Bernoulli(0.1).
input_data = torch.bernoulli(0.1 * torch.ones(time, source_layer.n)).byte()
input_data = torch.bernoulli(0.1 * torch.ones(time, source_layer.n)).bool()
inputs = {"A": input_data}

# Simulate network on input data.
Expand All @@ -417,7 +417,6 @@ two-layer, input-output spiking neural network.
"A": source_monitor.get("s"), "B": target_monitor.get("s")
}
voltages = {"B": target_monitor.get("v")}

plt.ioff()
plot_spikes(spikes)
plot_voltages(voltages, plot_type="line")
Expand Down
5 changes: 4 additions & 1 deletion test/encoding/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from bindsnet.encoding import *


class TestEncodings:
"""
Tests all stable encoding functions and generators.
Expand All @@ -16,6 +15,7 @@ def test_bernoulli(self):
spikes = bernoulli(datum, time=t, max_prob=m)

assert spikes.size() == torch.Size((t, n))
assert spikes.dtype == torch.bool

def test_multidim_bernoulli(self):
for shape in [[5, 5], [10, 10], [25, 25]]: # shape of nodes in layer
Expand All @@ -25,6 +25,7 @@ def test_multidim_bernoulli(self):
spikes = bernoulli(datum, time=t, max_prob=m)

assert spikes.size() == torch.Size((t, *shape))
assert spikes.dtype == torch.bool

def test_bernoulli_loader(self):
for s in [1, 100]: # number of data samples
Expand Down Expand Up @@ -67,6 +68,7 @@ def test_poisson(self):
spikes = poisson(datum, time=t) # Encode as spikes.

assert spikes.size() == torch.Size((t, n))
assert spikes.dtype == torch.bool

def test_poisson_loader(self):
for s in [1, 10]: # number of data samples
Expand All @@ -77,3 +79,4 @@ def test_poisson_loader(self):

for i, spikes in enumerate(spike_loader):
assert spikes.size() == torch.Size((t, n))
assert spikes.dtype == torch.bool
2 changes: 1 addition & 1 deletion test/network/test_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def check_weights(self, conn_type, shape_a, shape_b, shape_w, *args, **kwargs):

### Run network ###
network.run(
inputs={"input": torch.bernoulli(torch.rand(time, 100)).byte()},
inputs={"input": torch.bernoulli(torch.rand(time, 100)).bool()},
time=time,
reward=1,
)
Expand Down
26 changes: 13 additions & 13 deletions test/network/test_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_hebbian(self):
target="output",
)
network.run(
inputs={"input": torch.bernoulli(torch.rand(250, 100)).byte()}, time=250
inputs={"input": torch.bernoulli(torch.rand(250, 100)).bool()}, time=250
)

# Conv2dConnection test
Expand All @@ -60,7 +60,7 @@ def test_hebbian(self):
)
# shape is [time, batch, channels, height, width]
network.run(
inputs={"input": torch.bernoulli(torch.rand(250, 1, 1, 10, 10)).byte()},
inputs={"input": torch.bernoulli(torch.rand(250, 1, 1, 10, 10)).bool()},
time=250,
)

Expand All @@ -80,7 +80,7 @@ def test_post_pre(self):
target="output",
)
network.run(
inputs={"input": torch.bernoulli(torch.rand(250, 100)).byte()}, time=250
inputs={"input": torch.bernoulli(torch.rand(250, 100)).bool()}, time=250
)

network2 = Network(dt=1.0)
Expand All @@ -97,7 +97,7 @@ def test_post_pre(self):
target="output",
)
network2.run(
inputs={"input": torch.bernoulli(torch.rand(250, 100)).byte()}, time=250
inputs={"input": torch.bernoulli(torch.rand(250, 100)).bool()}, time=250
)

# Conv2dConnection test
Expand All @@ -117,7 +117,7 @@ def test_post_pre(self):
target="output",
)
network.run(
inputs={"input": torch.bernoulli(torch.rand(250, 1, 1, 10, 10)).byte()},
inputs={"input": torch.bernoulli(torch.rand(250, 1, 1, 10, 10)).bool()},
time=250,
)

Expand All @@ -139,7 +139,7 @@ def test_weight_dependent_post_pre(self):
target="output",
)
network.run(
inputs={"input": torch.bernoulli(torch.rand(250, 100)).byte()}, time=250
inputs={"input": torch.bernoulli(torch.rand(250, 100)).bool()}, time=250
)

# Conv2dConnection test
Expand All @@ -161,7 +161,7 @@ def test_weight_dependent_post_pre(self):
target="output",
)
network.run(
inputs={"input": torch.bernoulli(torch.rand(250, 1, 1, 10, 10)).byte()},
inputs={"input": torch.bernoulli(torch.rand(250, 1, 1, 10, 10)).bool()},
time=250,
)

Expand All @@ -181,7 +181,7 @@ def test_mstdp(self):
target="output",
)
network.run(
inputs={"input": torch.bernoulli(torch.rand(250, 100)).byte()},
inputs={"input": torch.bernoulli(torch.rand(250, 100)).bool()},
time=250,
reward=1.0,
)
Expand All @@ -204,7 +204,7 @@ def test_mstdp(self):
)

network.run(
inputs={"input": torch.bernoulli(torch.rand(250, 1, 1, 10, 10)).byte()},
inputs={"input": torch.bernoulli(torch.rand(250, 1, 1, 10, 10)).bool()},
time=250,
reward=1.0,
)
Expand All @@ -225,7 +225,7 @@ def test_mstdpet(self):
target="output",
)
network.run(
inputs={"input": torch.bernoulli(torch.rand(250, 100)).byte()},
inputs={"input": torch.bernoulli(torch.rand(250, 100)).bool()},
time=250,
reward=1.0,
)
Expand All @@ -248,7 +248,7 @@ def test_mstdpet(self):
)

network.run(
inputs={"input": torch.bernoulli(torch.rand(250, 1, 1, 10, 10)).byte()},
inputs={"input": torch.bernoulli(torch.rand(250, 1, 1, 10, 10)).bool()},
time=250,
reward=1.0,
)
Expand All @@ -269,7 +269,7 @@ def test_rmax(self):
target="output",
)
network.run(
inputs={"input": torch.bernoulli(torch.rand(250, 100)).byte()},
inputs={"input": torch.bernoulli(torch.rand(250, 100)).bool()},
time=250,
reward=1.0,
)
Expand Down Expand Up @@ -313,7 +313,7 @@ def _build(rule, n=8, **rule_kwargs):
def _drive(network, n=8, time=100, seed=0):
torch.manual_seed(seed)
network.run(
inputs={"input": torch.bernoulli(torch.rand(time, n)).byte()},
inputs={"input": torch.bernoulli(torch.rand(time, n)).bool()},
time=time,
reward=1.0,
)
Expand Down
Loading