discretize#
This module defines methods for discretizing convoluitional torch layers and SINABS spiking layers.
- sinabs.backend.dynapcnn.discretize.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:
The scaling factor
- Return type:
float
- sinabs.backend.dynapcnn.discretize.discretize_conv(layer: Conv2d, spk_thr: float, spk_thr_low: float, spk_state: Tensor | None = None, to_int: bool = True) Conv2d[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) – State of spiking layer. Defaults to None.
to_int (bool) – Use integer types for discretized parameter. Defaults to True.
- Returns:
Discretized copy of convolutional layer
- Return type:
Conv2d
- sinabs.backend.dynapcnn.discretize.discretize_conv_(layer: Conv2d, spk_thr: float, spk_thr_low: float, spk_state: Tensor | None = None, to_int: bool = True) Conv2d[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) – State of spiking layer. Defaults to None.
to_int (bool) – Use integer types for discretized parameter. Defaults to True.
- Returns:
Discretized convolutional layer
- Return type:
Conv2d
- sinabs.backend.dynapcnn.discretize.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:
Tuple containing a discretized copy of convolutional layer and a discretized copy of spiking layer.
- Return type:
Tuple[Conv2d, IAF]
- sinabs.backend.dynapcnn.discretize.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:
A tuple containing a discretized convolutional layer and a discretized spiking layer.
- Return type:
Tuple[Conv2d, IAF]
- sinabs.backend.dynapcnn.discretize.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:
Scaled and discretized copy of obj.
- Return type:
int
- sinabs.backend.dynapcnn.discretize.discretize_spk(layer: IAF, conv_weight: Tensor, conv_bias: Tensor | None = None, to_int: bool = True) IAF[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.
Args: layer (sl.IAF): Spiking layer. conv_weight (torch.Tensor): Weight tensor of preceding convolutional layer. conv_bias (torch.Tensor): Bias of preceding convolutional layer.
Optional argument, defaults to None.
to_int (bool): Use integer types for discretized parameter.
- sinabs.backend.dynapcnn.discretize.discretize_spk_(layer: IAF, conv_weight: Tensor, conv_bias: Tensor | None = None, to_int: bool = True) IAF[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) – Bias of preceding convolutional layer. Optional argument, defaults to None.
to_int (bool) – Use integer types for discretized parameter.
- Returns:
Discretized spiking
- Return type:
- sinabs.backend.dynapcnn.discretize.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. Defaults to True.
- Returns:
Scaled and discretized copy of obj.
- Return type:
Tensor