qualia_plugin_snn.learningmodel.pytorch.SNN module

Contains the base class for spiking neural network models.

class qualia_plugin_snn.learningmodel.pytorch.SNN.SNN[source]

Bases: LearningModelPyTorch, StepModule

Base class for spiking neural network models to inherit from.

is_snn: bool = True

Always True in case of spiking neural networks

__select_neuron(neuron: dict[str, dict[str, RecursiveConfigUnion] | list[RecursiveConfigUnion] | str | int | float | bool]) type[Module]

Select a spiking neuron class from a kind specified in configuration file.

The class will be looked up by its name in the following modules successively, first match is used:

  1. spikingjelly.activation_based.neuron

  2. qualia_plugin_snn.learningmodel.pytorch.layers.CustomNode

  3. qualia_plugin_snn.learningmodel.pytorch.quantized_SNN_layers

Parameters:

neuron (dict[str, dict[str, RecursiveConfigUnion] | list[RecursiveConfigUnion] | str | int | float | bool]) – A spiking neuron configuration dict

Returns:

The class corresponding to the kind specified in the neuron configuration dict

Raises:
  • ValueError – When the 'kind' key cannot be found in the neuron dict

  • TypeError – When the value associated to kind is not a string

  • AttributeError – When the class for kind cannot be looked up

Return type:

type[Module]

__extract_neuron_params(neuron: dict[str, dict[str, RecursiveConfigUnion] | list[RecursiveConfigUnion] | str | int | float | bool]) dict[str, dict[str, dict[str, RecursiveConfigUnion] | list[RecursiveConfigUnion] | str | int | float | bool] | list[dict[str, RecursiveConfigUnion] | list[RecursiveConfigUnion] | str | int | float | bool] | str | int | float | bool | None]

Extract params from the given neuron configuration dict, also convert v_reset.

v_reset is specified as either a float (hard-reset) or false (soft-reset) in the TOML configuration since there is no None equivalent in TOML. However SpikingJelly neurons use None to signify soft-reset, so convert v_reset=false to v_reset=None.

Parameters:

neuron (dict[str, dict[str, RecursiveConfigUnion] | list[RecursiveConfigUnion] | str | int | float | bool]) – The spiking neuron configuration dict

Returns:

The params dict for the given neuron configuration dict with converted v_reset

Raises:

TypeError – When the value associated to params is not a dict

Return type:

dict[str, dict[str, dict[str, RecursiveConfigUnion] | list[RecursiveConfigUnion] | str | int | float | bool] | list[dict[str, RecursiveConfigUnion] | list[RecursiveConfigUnion] | str | int | float | bool] | str | int | float | bool | None]

create_neuron(quant_params: QuantizationConfigDict | None = None) Module[source]

Instanciate a spiking neuron from the kind and params found in neuron of :meth:__init__.

Parameters:

quant_params (QuantizationConfigDict | None) – Optional quantization configuration dict in case of quantized network, see qualia_core.learningmodel.pytorch.Quantizer.Quantizer

Returns:

A spiking neuron instance

Return type:

Module

__init__(input_shape: tuple[int, ...], output_shape: tuple[int, ...], timesteps: int, neuron: dict[str, dict[str, RecursiveConfigUnion] | list[RecursiveConfigUnion] | str | int | float | bool] | None) None[source]

Construct SNN.

step_mode is extracted from the neuron configuration dict neuron. __select_neuron() and __extract_neuron_params() are called to prepare the neuron for instanciation with create_neuron() which should be used in derived classes to instanciate the spiking neurons.

Below is an example of a neuron configuration dict for SpikingJelly’s multi-step spikingjelly.activation_based.neuron.IFNode soft-reset with threshold at 1.0.

  • Python

neuron = {
    'kind': 'IFNode',
    'params': {
        'v_threshold': 1.0,
        'v_reset': False,
        'step_mode': 'm',
        }
}
  • TOML

[[model]]
params.neuron.kind = 'IFNode'
params.neuron.params.v_threshold = 1.0
params.neuron.params.v_reset = false
params.neuron.params.step_mode = 'm'
Parameters:
  • input_shape (tuple[int, ...]) – Input shape passed to qualia_core.learningmodel.pytorch.LearningModel.LearningModel with timesteps prepended

  • output_shape (tuple[int, ...]) – Output shape passed to qualia_core.learningmodel.pytorch.LearningModel.LearningModel

  • timesteps (int) – Number of timesteps

  • neuron (dict[str, dict[str, RecursiveConfigUnion] | list[RecursiveConfigUnion] | str | int | float | bool] | None) – Spiking neuron configuration dict

Raises:
  • ValueError – When neuron is None or empty

  • TypeError – When the value associated to params.step_mode is not a string

Return type:

None

timesteps: int

Number of timesteps