discretize#

This module defines methods for discretizing convoluitional torch layers and SINABS spiking layers.

determine_discretization_scale(obj: Tensor, bit_precision: int) float[source]#

Determine a scale for discretization

Determine how much the values of a torch tensor can be scaled in order to fit the given precision

Parameters:
  • obj (torch.Tensor) – Tensor that is to be scaled

  • bit_precision (int) – The precision in bits

Returns:

float – The scaling factor

discretize_conv(layer: Conv2d, spk_thr: float, spk_thr_low: float, spk_state: Tensor | None = None, to_int: bool = True)[source]#

Discretize convolutional layer.

This function takes a 2D convolutional layer and parameters of a subsequent spiking layer to return a discretized copy of the convolutional layer.

Parameters:
  • layer (nn.Conv2d) – Convolutional layer

  • spk_thr (float) – Upper threshold of subsequent spiking layer

  • spk_thr_low (float) – Lower threshold of subsequent spiking layer

  • spk_state (torch.Tensor or None) – State of spiking layer.

  • to_int (bool) – Use integer types for discretized parameter

Returns:

nn.Conv2d – Discretized copy of convolutional layer

discretize_conv_(layer: Conv2d, spk_thr: float, spk_thr_low: float, spk_state: Tensor | None = None, to_int: bool = True)[source]#

Discretize convolutional layer, in-place.

This function discretizes a 2D convolutional layer in-place, based on parameters of a subsequent spiking layer.

Parameters:
  • layer (nn.Conv2d) – Convolutional layer

  • spk_thr (float) – Upper threshold of subsequent spiking layer

  • spk_thr_low (float) – Lower threshold of subsequent spiking layer

  • spk_state (torch.Tensor or None) – State of spiking layer.

  • to_int (bool) – Use integer types for discretized parameter

Returns:

nn.Conv2d – Discretized convolutional layer

discretize_conv_spike(conv_lyr: Conv2d, spike_lyr: IAF, to_int: bool = True) Tuple[Conv2d, IAF][source]#

Discretize convolutional and spiking layers together.

This function takes a 2D convolutional and a spiking layer and returns a copy of each, with discretized weights, bias and threshold.

Parameters:
  • conv_lyr (nn.Conv2d) – Convolutional layer

  • spike_lyr (sl.IAF) – Spiking layer

  • to_int (bool) – Use integer types for discretized parameter

Returns:

  • nn.Conv2d – Discretized copy of convolutional layer

  • sl.IAF – Discretized copy of spiking layer

discretize_conv_spike_(conv_lyr: Conv2d, spike_lyr: IAF, to_int: bool = True) Tuple[Conv2d, IAF][source]#

Discretize convolutional and spiking layers together, in-place.

This function takes a 2D convolutional and a spiking layer and discretizes weights, bias and threshold in-place.

Parameters:
  • conv_lyr (nn.Conv2d) – Convolutional layer

  • spike_lyr (sl.IAF) – Spiking layer

  • to_int (bool) – Use integer types for discretized parameter

Returns:

  • nn.Conv2d – Discretized convolutional layer

  • sl.IAF – Discretized spiking layer

discretize_scalar(obj: float, scaling: float) int[source]#

Scale a float and cast it to discrete integer values

Parameters:
  • obj (float) – Value that is to be discretized

  • scaling (float) – Scaling factor to be applied before discretization

Returns:

int – Scaled and discretized copy of obj.

discretize_spk(layer: IAF, conv_weight: Tensor, conv_bias: Tensor | None = None, to_int: bool = True)[source]#

Discretize spiking layer.

This function takes a spiking layer and parameters of a preceding convolutional layer to return a discretized copy of the spiking layer.

Parameters:
  • layer (sl.IAF) – Spiking layer

  • conv_weight (torch.Tensor) – Weight tensor of preceding convolutional layer

  • conv_bias (torch.Tensor or None) – Bias of preceding convolutional layer

  • to_int (bool) – Use integer types for discretized parameter

Returns:

sl.IAF – Discretized copy of spiking layer

discretize_spk_(layer: IAF, conv_weight: Tensor, conv_bias: Tensor | None = None, to_int: bool = True)[source]#

Discretize spiking layer in-place.

This function discretizes a spiking layer in-place, based on parameters of a preceding convolutional layer.

Parameters:
  • layer (sl.IAF) – Spiking layer

  • conv_weight (torch.Tensor) – Weight tensor of preceding convolutional layer

  • conv_bias (torch.Tensor or None) – Bias of preceding convolutional layer

  • to_int (bool) – Use integer types for discretized parameter

Returns:

sl.IAF – Discretized spiking

discretize_tensor(obj: Tensor, scaling: float, to_int: bool = True) Tensor[source]#

Scale a torch.Tensor and cast it to discrete integer values

Parameters:
  • obj (torch.Tensor) – Tensor that is to be discretized

  • scaling (float) – Scaling factor to be applied before discretization

  • to_int (bool) – If False, round the values, but don’t cast to Int. (Default True).

Returns:

torch.Tensor – Scaled and discretized copy of obj.