snntorch.utils

snntorch.utils contains a handful of utility functions for handling datasets.

snntorch.utils.data_subset(dataset, subset, idx=0)[source]

Partition the dataset by a factor of 1/subset without removing access to data and target attributes.

Example:

from snntorch import utils
from torchvision import datasets

data_path = "path/to/data"
subset = 10

#  Download MNIST training set
mnist_train = datasets.MNIST(data_path, train=True, download=True)
print(len(mnist_train))
>>> 60000

#  Reduce size of MNIST training set
utils.data_subset(mnist_train, subset)
print(len(mnist_train))
>>> 6000
Parameters:
  • dataset (torchvision dataset) – Dataset

  • subset (int) – Factor to reduce dataset by

  • idx (int, optional) – Which subset of the train and test sets to index into, defaults to 0

Returns:

Partitioned dataset

Return type:

list of torch.utils.data

snntorch.utils.reset(net)[source]

Reset the hidden states (and detach them from the computation graph) of every snnTorch neuron contained in net.

Only the neurons that actually belong to net are touched, so calling reset() on one network never clears the hidden states of a separate network that happens to use the same neuron class.

Parameters:

net (torch.nn.Module) – Network (or module) containing snnTorch neuron layers.

snntorch.utils.valid_split(ds_train, ds_val, split, seed=0)[source]

Randomly split a dataset into non-overlapping new datasets of given lengths. Optionally fix the generator for reproducible results. Operates similarly to random_split from torch.utils.data.dataset but retains data and target attributes.

Example

from snntorch import utils
from torchvision import datasets

data_path = "path/to/data"
val_split = 0.1

#  Download MNIST training set into mnist_val and mnist_train
mnist_train = datasets.MNIST(data_path, train=True, download=True)
mnist_val = datasets.MNIST(data_path, train=True, download=True)

print(len(mnist_train))
>>> 60000

print(len(mnist_val))
>>> 60000

#  Validation split
mnist_train, mnist_val = utils.valid_split(mnist_train,
mnist_val, val_split)

print(len(mnist_train))
>>> 54000

print(len(mnist_val))
>>> 6000
Parameters:
  • ds_train (torchvision dataset) – Training set

  • ds_val (torchvision dataset) – Validation set

  • split (Float) – Proportion of samples assigned to the validation set from the training set

  • seed (int, optional) – Fix to generate reproducible results, defaults to 0

Returns:

Randomly split train and validation sets

Return type:

list of torch.utils.data