Skip to content

Commit

Permalink
Merge pull request #478 from BindsNET/hananel
Browse files Browse the repository at this point in the history
Update BindsNET to PyTorch 1.8.1
  • Loading branch information
Hananel-Hazan committed Apr 25, 2021
2 parents 9a945a3 + f8a56f2 commit ead5521
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 34 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
# Important:

If you are using BindsNET for your research, please consider answering a short survey on the value of a standardized format that could be used for the exchange of computational models in neuroscience, cognitive science, and computer science.

[Survey link](https://docs.google.com/forms/d/e/1FAIpQLSfOZSyoWL4J_MfHTIq5Jx0dnJSVHNFH7UP3AHG_x7ddlMvBPA/viewform?usp=sf_link)

Data from this survey will be used exclusively for our own internal purposes, and will not be shared beyond our small group other than summary form for any purpose. The survey is anonymous unless you chose to add an email address for follow-up correspondence.


<p align="center"><img width="25%" src="docs/logo.png"/></p>

A Python package used for simulating spiking neural networks (SNNs) on CPUs or GPUs using [PyTorch](http://pytorch.org/) `Tensor` functionality.
Expand Down
16 changes: 11 additions & 5 deletions bindsnet/analysis/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def plot_weights(
save = a[0] + a[1]

plt.savefig(save, bbox_inches="tight")
plt.savefig(a[0] + ".png", bbox_inches="tight")

plt.close(fig)
plt.ion()
Expand Down Expand Up @@ -407,6 +408,13 @@ def plot_assignments(
if save is not None:
plt.ioff()

a = save.split(".")
if len(a) == 2:
save = a[0] + ".1." + a[1]
else:
a[1] = "." + str(1 + int(a[1])) + ".png"
save = a[0] + a[1]

fig, ax = plt.subplots(figsize=figsize)
ax.set_title("Categorical assignments")

Expand Down Expand Up @@ -440,7 +448,7 @@ def plot_assignments(
plt.close()

plt.ion()
return im
return im, save
else:
if not im:
fig, ax = plt.subplots(figsize=figsize)
Expand Down Expand Up @@ -749,8 +757,7 @@ def plot_voltages(
v[1]
.cpu()
.numpy()[
time[0] : time[1],
n_neurons[v[0]][0] : n_neurons[v[0]][1],
time[0] : time[1], n_neurons[v[0]][0] : n_neurons[v[0]][1]
]
)
if thresholds is not None and thresholds[v[0]].size() == torch.Size(
Expand All @@ -764,8 +771,7 @@ def plot_voltages(
v[1]
.cpu()
.numpy()[
time[0] : time[1],
n_neurons[v[0]][0] : n_neurons[v[0]][1],
time[0] : time[1], n_neurons[v[0]][0] : n_neurons[v[0]][1]
]
.T,
cmap=cmap,
Expand Down
2 changes: 1 addition & 1 deletion bindsnet/encoding/encodings.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def poisson(

# Create Poisson distribution and sample inter-spike intervals
# (incrementing by 1 to avoid zero intervals).
dist = torch.distributions.Poisson(rate=rate)
dist = torch.distributions.Poisson(rate=rate, validate_args=False)
intervals = dist.sample(sample_shape=torch.Size([time + 1]))
intervals[:, datum != 0] += (intervals[:, datum != 0] == 0).float()

Expand Down
4 changes: 2 additions & 2 deletions bindsnet/evaluation/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def ngram(

predictions.append(torch.argmax(score))

return torch.tensor(predictions).long()
return torch.tensor(predictions, device=spikes.device).long()


def update_ngram_scores(
Expand Down Expand Up @@ -246,7 +246,7 @@ def update_ngram_scores(
for order in zip(*(fire_order[k:] for k in range(n))):
for sequence in product(*order):
if sequence not in ngram_scores:
ngram_scores[sequence] = torch.zeros(n_labels)
ngram_scores[sequence] = torch.zeros(n_labels, device=spikes.device)

ngram_scores[sequence][int(labels[i])] += 1

Expand Down
2 changes: 1 addition & 1 deletion bindsnet/network/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def forward(self, x: torch.Tensor) -> None:
if self.traces_additive:
self.x += self.trace_scale * self.s.float()
else:
self.x.masked_fill_(self.s, 1)
self.x.masked_fill_(self.s.bool(), 1)

if self.sum_input:
# Add current input to running sum.
Expand Down
4 changes: 2 additions & 2 deletions bindsnet/network/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,12 +794,12 @@ def __init__(
)
if self.wmin == -np.inf or self.wmax == np.inf:
v = torch.clamp(
torch.rand(*source.shape, *target.shape)[i.byte()],
torch.rand(*source.shape, *target.shape)[i.bool()],
self.wmin,
self.wmax,
)
else:
v = self.wmin + torch.rand(*source.shape, *target.shape)[i.byte()] * (
v = self.wmin + torch.rand(*source.shape, *target.shape)[i.bool()] * (
self.wmax - self.wmin
)
w = torch.sparse.FloatTensor(i.nonzero().t(), v)
Expand Down
2 changes: 1 addition & 1 deletion examples/mnist/SOM_LM-SNNs.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@

# Determines number of workers to use
if n_workers == -1:
n_workers = torch.cuda.is_available() * 4 * torch.cuda.device_count()
n_workers = 0 # torch.cuda.is_available() * 4 * torch.cuda.device_count()

n_sqrt = int(np.ceil(np.sqrt(n_neurons)))
start_intensity = intensity
Expand Down
6 changes: 3 additions & 3 deletions examples/mnist/batch_eth_mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
parser.add_argument("--test", dest="train", action="store_false")
parser.add_argument("--plot", dest="plot", action="store_true")
parser.add_argument("--gpu", dest="gpu", action="store_true")
parser.set_defaults(plot=True, gpu=False)
parser.set_defaults(plot=True, gpu=True)

args = parser.parse_args()

Expand Down Expand Up @@ -90,7 +90,7 @@

# Determines number of workers to use
if n_workers == -1:
n_workers = gpu * 4 * torch.cuda.device_count()
n_workers = 0 # gpu * 1 * torch.cuda.device_count()

n_sqrt = int(np.ceil(np.sqrt(n_neurons)))
start_intensity = intensity
Expand All @@ -116,7 +116,7 @@
dataset = MNIST(
PoissonEncoder(time=time, dt=dt),
None,
root=os.path.join(ROOT_DIR, "data", "MNIST"),
"../../data/MNIST",
download=True,
transform=transforms.Compose(
[transforms.ToTensor(), transforms.Lambda(lambda x: x * intensity)]
Expand Down
4 changes: 2 additions & 2 deletions examples/mnist/conv_mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
parser.add_argument("--test", dest="train", action="store_false")
parser.add_argument("--plot", dest="plot", action="store_true")
parser.add_argument("--gpu", dest="gpu", action="store_true")
parser.set_defaults(plot=True, gpu=False, train=True)
parser.set_defaults(plot=True, gpu=True, train=True)

args = parser.parse_args()

Expand Down Expand Up @@ -164,7 +164,7 @@
start = t()

train_dataloader = torch.utils.data.DataLoader(
train_dataset, batch_size=1, shuffle=True, num_workers=4, pin_memory=gpu
train_dataset, batch_size=1, shuffle=True, num_workers=0, pin_memory=gpu
)

for step, batch in enumerate(tqdm(train_dataloader)):
Expand Down
4 changes: 2 additions & 2 deletions examples/mnist/eth_mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
parser.add_argument("--test", dest="train", action="store_false")
parser.add_argument("--plot", dest="plot", action="store_true")
parser.add_argument("--gpu", dest="gpu", action="store_true")
parser.set_defaults(plot=True, gpu=False)
parser.set_defaults(plot=True, gpu=True)

args = parser.parse_args()

Expand Down Expand Up @@ -85,7 +85,7 @@

# Determines number of workers to use
if n_workers == -1:
n_workers = gpu * 4 * torch.cuda.device_count()
n_workers = 0 # gpu * 4 * torch.cuda.device_count()

if not train:
update_interval = n_test
Expand Down
2 changes: 1 addition & 1 deletion examples/mnist/supervised_mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
parser.add_argument("--plot", dest="plot", action="store_true")
parser.add_argument("--gpu", dest="gpu", action="store_true")
parser.add_argument("--device_id", type=int, default=0)
parser.set_defaults(plot=True, gpu=False, train=True)
parser.set_defaults(plot=True, gpu=True, train=True)

args = parser.parse_args()

Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ foolbox
scipy>=1.1.0
numpy>=1.14.2
cython>=0.28.5
torch==1.8.1
torchvision==0.9.1
torch>=1.5.1
torchvision>=0.6.1
tqdm>=4.19.9
setuptools>=39.0.1
matplotlib>=2.1.0
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="BindsNET",
version="0.2.8",
version="0.2.9",
description="Spiking neural networks for ML in Python",
license="AGPL-3.0",
long_description=long_description,
Expand All @@ -17,8 +17,8 @@
zip_safe=False,
install_requires=[
"numpy>=1.14.2",
"torch==1.8.1",
"torchvision==0.9.1",
"torch>=1.5.1",
"torchvision>=0.6.1",
"tensorboardX>=1.7",
"tqdm>=4.19.9",
"matplotlib>=2.1.0",
Expand Down

0 comments on commit ead5521

Please sign in to comment.