network#

class sinabs.Network(analog_model=None, spiking_model=None, input_shape: Optional[Union[ndarray, List, Tuple]] = None, synops: bool = False, batch_size: int = 1, num_timesteps: int = 1)[source]#

Class of a spiking neural network.

Parameters:
  • input_shape (Optional[Union[ndarray, List, Tuple]]) –

  • synops (bool) –

  • batch_size (int) –

  • num_timesteps (int) –

spiking_model#

torch.nn.Module, a spiking neural network model

analog_model#

torch.nn.Module, an artifical neural network model

input_shape#

Tuple, size of input

synops#

If True (default: False), register hooks for counting synaptic operations during forward passes, instantiating sinabs.SNNSynOpCounter.

compare_activations(data, name_list: Optional[Union[ndarray, List, Tuple]] = None, compute_rate: bool = False, verbose: bool = False) Tuple[ndarray, ndarray, str][source]#

Compare activations of the analog model and the SNN for a given data sample.

Parameters:
  • data (np.ndarray) – Data to process

  • name_list (List[str]) – list of all layer names (str) whose activations need to be compared

  • compute_rate (bool) – True if you want to compute firing rate. By default spike count is returned

  • verbose (bool) – print debugging logs to the terminal

Returns:

A tuple of lists (ann_activity, snn_activity, name_list)
  • ann_activity: output activity of the ann layers

  • snn_activity: output activity of the snn layers

  • name_list: spiking layers’ name list for plotting comparison

Return type:

tuple

forward(tsrInput) Tensor[source]#

Forward pass for this model.

Return type:

Tensor

get_synops(num_evs_in=None) dict[source]#

Please see docs for sinabs.SNNSynOpCounter.get_synops().

Return type:

dict

plot_comparison(data, name_list: Optional[Union[ndarray, List, Tuple]] = None, compute_rate=False)[source]#

Plots a scatter plot of all the activations.

Parameters:
  • data – Data to be processed

  • name_list (Optional[Union[ndarray, List, Tuple]]) – ArrayLike with names of all the layers of interest to be compared

  • compute_rate – Compare firing rates instead of spike count

Returns:

A tuple of lists (ann_activity, snn_activity)
  • ann_activity: output activity of the ann layers

  • snn_activity: output activity of the snn layers

Return type:

tuple

reset_states(randomize: bool = False, value_ranges: Optional[List[Dict[str, Tuple[float, float]]]] = None)[source]#

Reset all neuron states in the submodules.

Parameters:
  • randomize (Bool) – If true, reset the states between a range provided. Else, the states are reset to zero.

  • value_ranges (Optional[List[Dict[str, Tuple[float, float]]]]) – A list of value_range dictionaries with the same length as the total stateful layers in the module. Each dictionary is a key value pair: buffer_name -> (min, max) for each state that needs to be reset. The states are reset with a uniform distribution between the min and max values specified. Any state with an undefined key in this dictionary will be reset between 0 and 1 This parameter is only used if randomize is set to true.

zero_grad(set_to_none: bool = False) None[source]#

Sets gradients of all model parameters to zero. See similar function under torch.optim.Optimizer for more context.

Parameters:

set_to_none (bool) – instead of setting to zero, set the grads to None. See torch.optim.Optimizer.zero_grad() for details.

Return type:

None