utils#

sinabs.utils.reset_states(model: Module) None[source]#

Helper function to recursively reset all states of spiking layers within the model.

Parameters:

model (Module) – The torch module

Return type:

None

sinabs.utils.zero_grad(model: Module) None[source]#

Helper function to recursively zero the gradients of all spiking layers within the model.

Parameters:

model (Module) – The torch module

Return type:

None

sinabs.utils.get_activations(torchanalog_model, tsrData, name_list=None)[source]#

Return torch analog model activations for the specified layers.

sinabs.utils.get_network_activations(model: Module, inp, name_list: List = None, bRate: bool = False) List[ndarray][source]#

Returns the activity of neurons in each layer of the network.

Parameters:
  • model (Module) – Model for which the activations are to be read out

  • inp – Input to the model

  • bRate (bool) – If true returns the rate, else returns spike count

  • name_list (List) – list of all layers whose activations need to be compared

Return type:

List[ndarray]

sinabs.utils.normalize_weights(ann: Module, sample_data: Tensor, output_layers: List[str], param_layers: List[str], percentile: float = 99)[source]#

Rescale the weights of the network, such that the activity of each specified layer is normalized.

The method implemented here roughly follows the paper: Conversion of Continuous-Valued Deep Networks to Efficient Event-Driven Networks for Image Classification by Rueckauer et al. https://www.frontiersin.org/article/10.3389/fnins.2017.00682

Parameters:
  • ann (Module) – Torch module

  • sample_data (Tensor) – Input data to normalize the network with

  • output_layers (List[str]) – List of layers to verify activity of normalization. Typically this is a relu layer

  • param_layers (List[str]) – List of layers whose parameters preceed output_layers

  • percentile (float) – A number between 0 and 100 to determine activity to be normalized by where a 100 corresponds to the max activity of the network. Defaults to 99.

sinabs.utils.set_batch_size(model: Module, batch_size: int)[source]#

Update any model with sinabs squeeze layers to a given batch size.

Parameters:
  • model (nn.Module) – pytorch model with sinabs Squeeze layers

  • batch_size (int) – The new batch size

sinabs.utils.validate_memory_mapping_speck(input_feature_size: int, output_feature_size: int, kernel_size: Tuple[int, int], stride: Tuple[int, int], padding: Tuple[int, int], input_dimension: Tuple[int, int] = [64, 64], conv_2d: bool = True)[source]#
Helper function to verify if it is possible to map a specific layer on to speck.

This function validates kernel and neuron memories. It doesnt check for all the network layers together. It considers the mapping of a Conv2D layer only.

Parameters:
  • input_feature_size (int) – number of input channels

  • output_feature_size (int) – number of output channels

  • kernel_size (Tuple[int, int]) – 2D kernel size

  • stride (Tuple[int, int]) – 2D stride size

  • padding (Tuple[int, int]) – 2D padding size

  • input_dimension (Tuple[int, int]) – 2D input dimension size. Defaults to [64,64]

  • conv_2d (bool) – if it is mapping a Conv2D layers. Defaults to True. It won’t validate other types of network.

Returns:

Message indicating layer can be mapped with total size of kernel and neuron memories.

Return type:

msg (string)

Raises:

Exception – if neuron or kernel memories are higher than available on chip.