qualia_plugin_snn.learningmodel.pytorch.SResNet module
Contains the template for a residual spiking neural network.
- class qualia_plugin_snn.learningmodel.pytorch.SResNet.BasicBlockBuilder[source]
Bases:
Protocol
Signature for basicblockbuilder.
Used to bind hyperparameters constant across all the ResNet blocks.
- __call__(in_planes: int, planes: int, kernel_size: int, stride: int, padding: int) BasicBlock [source]
Build a
BasicBlock
.- Parameters:
in_planes (int) – Number of input channels
planes (int) – Number of filters (i.e., output channels) in the main branch Conv layers
kernel_size (int) –
kernel_size
for the main branch Conv layersstride (int) –
kernel_size
for the MaxPool layers, no MaxPool layer added if 1padding (int) – Padding for the main branch Conv layers
- Returns:
- Return type:
- __init__(*args, **kwargs)
- class qualia_plugin_snn.learningmodel.pytorch.SResNet.BasicBlock[source]
Bases:
Module
A single ResNetv1 block.
Structure is:
| / \ | | Conv | | | BatchNorm | | | MaxPool Conv | | IF BatchNorm | | Conv MaxPool | | BatchNorm IF | | IF | | | \ / | Add
Main (left) branch Conv use
kernel_size=kernel_size
, while residual (right) branch Conv usekernel_size=1
.BatchNorm layers will be absent if
batch_norm == False
MaxPool layer will be absent if
stride == 1
.Residual (right) branch Conv layer will be asbent if
in_planes==planes
, except ifforce_projection_with_stride==True
andstride != 1
.- __init__(sjlayers_t: ModuleType, in_planes: int, planes: int, kernel_size: int, stride: int, padding: int, batch_norm: bool, bn_momentum: float, force_projection_with_stride: bool, create_neuron: Callable[[], Module], step_mode: str) None [source]
Construct
BasicBlock
.- Parameters:
sjlayers_t (ModuleType) – Module containing the aliased layers to use (1D or 2D)
in_planes (int) – Number of input channels
planes (int) – Number of filters (i.e., output channels) in the main branch Conv layers
kernel_size (int) –
kernel_size
for the main branch Conv layersstride (int) –
kernel_size
for the MaxPool layers, no MaxPool layer added if 1padding (int) – Padding for the main branch Conv layers
batch_norm (bool) – If
True
, add BatchNorm layer after each Conv layerbn_momentum (float) – BatchNorm layer
momentum
force_projection_with_stride (bool) – If
True
, residual Conv layer is kept whenstride != 1
even ifin_planes == planes
create_neuron (Callable[[], Module]) –
qualia_plugin_snn.learningmodel.pytorch.SNN.SNN.create_neuron()
method to instantiate a spiking neuronstep_mode (str) – SpikingJelly
step_mode
fromqualia_plugin_snn.learningmodel.pytorch.SNN.SNN.step_mode
- Return type:
None
- class qualia_plugin_snn.learningmodel.pytorch.SResNet.SResNet[source]
Bases:
SNN
Residual spiking neural network template.
Similar to
qualia_core.learningmodel.pytorch.ResNet.ResNet
but with spiking neuron activation layers (e.g., IF) instead oftorch.nn.ReLU
.Example TOML configuration for a 2D ResNetv1-18 over 4 timesteps with soft-reset multi-step IF based on the SResNet template:
[[model]] name = "SResNetv1-18" params.filters = [64, 64, 128, 256, 512] params.kernel_sizes = [ 7, 3, 3, 3, 3] params.paddings = [ 3, 1, 1, 1, 1] params.strides = [ 2, 1, 1, 1, 1] params.num_blocks = [ 2, 2, 2, 2] params.prepool = 1 params.postpool = 'max' params.batch_norm = true params.dims = 2 params.timesteps = 4 params.neuron.kind = 'IFNode' params.neuron.params.v_reset = false # Soft reset params.neuron.params.v_threshold = 1.0 params.neuron.params.detach_reset = true params.neuron.params.step_mode = 'm' # Multi-step mode, make sure to use SpikingJellyMultiStep learningframework params.neuron.params.backend = 'torch'
- __init__(input_shape: tuple[int, ...], output_shape: tuple[int, ...], filters: list[int], kernel_sizes: list[int], num_blocks: list[int], strides: list[int], paddings: list[int], prepool: int = 1, postpool: str = 'max', batch_norm: bool = False, bn_momentum: float = 0.1, force_projection_with_stride: bool = True, neuron: RecursiveConfigDict | None = None, timesteps: int = 2, dims: int = 1, basicblockbuilder: BasicBlockBuilder | None = None) None [source]
Construct
SResNet
.Structure is:
Input | AvgPool | Conv | BatchNorm | IF | BasicBlock | … | BasicBlock | GlobalPool | Flatten | Linear
- Parameters:
filters (list[int]) – List of
out_channels
for Conv layers inside eachBasicBlock
group, must be of the same size asnum_blocks
, first element is for the first Conv layer at the beginning of the networkkernel_sizes (list[int]) – List of
kernel_size
for Conv layers inside eachBasicBlock
group, must of the same size asnum_blocks
, first element is for the first Conv layer at the beginning of the networknum_blocks (list[int]) – List of number of
BasicBlock
in each group, also defines the number ofBasicBlock
groups inside the networkstrides (list[int]) – List of
kernel_size
for MaxPool layers inside eachBasicBlock
group, must of the same size asnum_blocks
,stride
is applied only to the firstBasicBlock
of the group, nextBasicBlock
in the group use astride
of1
, first element is the stride of the first Conv layer at the beginning of the networkpaddings (list[int]) – List of
padding
for Conv layer inside eachBasicBlock
group, must of the same size asnum_blocks
, first element is for the first Conv layer at the beginning of the networkprepool (int) – AvgPool layer
kernel_size
to add at the beginning of the network, no layer added if 0postpool (str) – Global pooling layer type after all
BasicBlock
, either max for MaxPool or avg for AvgPoolbatch_norm (bool) – If
True
, add a BatchNorm layer after each Conv layer, otherwise no layer addedbn_momentum (float) – BatchNorm
momentum
force_projection_with_stride (bool) – If
True
, residual Conv layer is kept whenstride != 1
even ifin_planes == planes
inside aBasicBlock
neuron (RecursiveConfigDict | None) – Spiking neuron configuration, see
qualia_plugin_snn.learningmodel.pytorch.SNN.SNN.__init__()
timesteps (int) – Number of timesteps
dims (int) – Either 1 or 2 for 1D or 2D convolutional network.
basicblockbuilder (BasicBlockBuilder | None) – Optional function with
BasicBlockBuilder.__call__()
signature to build a basic block after binding constants common across all basic blocks
- Return type:
None