Core Neuron Modules#
SpikingJelly 的 核心神经元模块 提供了规范神经元抽象。 这些神经元是 SNN 领域中被广泛接受和使用的模型,旨在作为研究与实际应用中的基础构建单元。
纳入该类别的主要标准包括:
神经元在概念上具有通用性,不依赖于某一特定论文、任务或训练策略。
该神经元可以被推荐用于下游项目中的通用建模需求。
除非明确需要某种特定的研究型行为,否则在构建新模型时,建议用户优先使用核心神经元模块。
SpikingJelly's core neuron modules provide canonical and stable neuron abstractions. These neurons represent widely accepted models in SNN (SNN) literature and are designed to serve as fundamental building blocks for both research and practical applications.
The main criteria for inclusion in this category are:
The neuron is conceptually general and not tied to a specific paper, task, or training strategy.
The neuron can be recommended for general use in downstream projects.
Users are encouraged to preferentially use core neuron modules when building new models, unless a specific research-oriented behavior is explicitly required.
Base Classes#
- class spikingjelly.activation_based.neuron.base_node.BaseNode(v_threshold: float = 1.0, v_reset: float | None = 0.0, surrogate_function: SurrogateFunctionBase = Sigmoid(alpha=4.0, spiking=True), detach_reset: bool = False, step_mode='s', backend='torch', store_v_seq: bool = False)[源代码]#
基类:
MemoryModule
中文
可微分SNN神经元的基类神经元。
- 参数:
v_threshold (float) -- 神经元的阈值电压
v_reset (Optional[float]) -- 神经元的重置电压。如果不为
None,当神经元释放脉冲后,电压会被重置为v_reset; 如果设置为None,当神经元释放脉冲后,电压会被减去v_thresholdsurrogate_function (surrogate.SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数
detach_reset (bool) -- 是否将reset过程的计算图分离
step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)
backend (str) -- 使用哪种后端。不同的
step_mode可能会带有不同的后端。可以通过打印self.supported_backends查看当前 使用的步进模式支持的后端。在支持的情况下,使用'cupy'或'triton'后端速度更快。store_v_seq (bool) -- 在使用
step_mode = 'm'时,给与shape = [T, N, *]的输入后,是否保存中间过程的shape = [T, N, *]的各个时间步的电压值self.v_seq。设置为False时计算完成后只保留最后一个时刻的电压,即shape = [N, *]的self.v。 通常设置成False,可以节省内存
English
This class is the base class of differentiable spiking neurons.
- 参数:
v_threshold (float) -- threshold of this neurons layer
v_reset (Optional[float]) -- reset voltage of this neurons layer. If not
None, the neuron's voltage will be set tov_resetafter firing a spike. IfNone, the neuron's voltage will subtractv_thresholdafter firing a spikesurrogate_function (surrogate.SurrogateFunctionBase) -- the function for calculating surrogate gradients of the heaviside step function in backward
detach_reset (bool) -- whether detach the computation graph of reset in backward
step_mode (str) -- the step mode, which can be s (single-step) or m (multi-step)
backend (str) -- backend fot this neurons layer. Different
step_modemay support for different backends. The user can printself.supported_backendsand check what backends are supported by the currentstep_mode. If supported, using'cupy'or'triton'backend will be fasterstore_v_seq (bool) -- when using
step_mode = 'm'and given input withshape = [T, N, *], this option controls whether storing the voltage at each time-step toself.v_seqwithshape = [T, N, *]. If set toFalse, only the voltage at last time-step will be stored toself.vwithshape = [N, *], which can reduce the memory consumption
- 返回:
None
- 返回类型:
None
- abstractmethod neuronal_charge(x: Tensor)[源代码]#
-
中文
定义神经元的充电差分方程。子类必须实现这个函数。
English
Define the charge difference equation. The sub-class must implement this function.
- neuronal_fire()[源代码]#
-
中文
根据当前神经元的电压、阈值,计算输出脉冲。
English
Calculate out spikes of neurons by their current membrane potential and threshold voltage.
- neuronal_reset(spike)[源代码]#
-
中文
根据当前神经元释放的脉冲,对膜电位进行重置。
English
Reset the membrane potential according to neurons' output spikes.
- single_step_forward(x: Tensor)[源代码]#
-
中文
按照充电、放电、重置的顺序进行前向传播。
- 参数:
x (torch.Tensor) -- 输入到神经元的电压增量
- 返回:
神经元的输出脉冲
- 返回类型:
English
Forward by the order of
neuronal_charge,neuronal_fire, andneuronal_reset.- 参数:
x (torch.Tensor) -- increment of voltage inputted to neurons
- 返回:
out spikes of neurons
- 返回类型:
- class spikingjelly.activation_based.neuron.base_node.NonSpikingBaseNode(decode: str | None = None)[源代码]#
-
- 参数:
decode (Optional[str]) -- 解码方式。若不为
None,在forward中将使用该方式对膜电位序列进行解码- 返回:
None
- 返回类型:
None
- class spikingjelly.activation_based.neuron.base_node.SimpleBaseNode(v_threshold: float = 1.0, v_reset: float | None = 0.0, surrogate_function: SurrogateFunctionBase = Sigmoid(alpha=4.0, spiking=True), detach_reset: bool = False, step_mode='s')[源代码]#
基类:
MemoryModule
中文
BaseNode的简化版,便于用户修改或扩展神经元。- 参数:
v_threshold (float) -- 神经元的阈值电压
v_reset (Optional[float]) -- 神经元的重置电压
surrogate_function (surrogate.SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数
detach_reset (bool) -- 是否将 reset 过程的计算图分离
step_mode (str) -- 步进模式,可以为
's'(单步) 或'm'(多步)
English
A simple version of
BaseNode. Users can modify this neuron easily.- 参数:
v_threshold (float) -- threshold of this neurons layer
v_reset (Optional[float]) -- reset voltage of this neurons layer
surrogate_function (surrogate.SurrogateFunctionBase) -- the function for calculating surrogate gradients of the heaviside step function in backward
detach_reset (bool) -- whether detach the computation graph of reset in backward
step_mode (str) -- the step mode, which can be
's'(single-step) or'm'(multi-step)
- 返回:
None
- 返回类型:
None
Integrate-and-fire (IF) Neurons#
- class spikingjelly.activation_based.neuron.integrate_and_fire.SimpleIFNode(v_threshold: float = 1.0, v_reset: float | None = 0.0, surrogate_function: SurrogateFunctionBase = Sigmoid(alpha=4.0, spiking=True), detach_reset: bool = False, step_mode='s')[源代码]#
-
中文
IFNode的简化版实现。- 参数:
v_threshold (float) -- 神经元阈值电压
v_reset (Optional[float]) -- 神经元重置电压
surrogate_function (surrogate.SurrogateFunctionBase) -- 替代梯度函数
detach_reset (bool) -- 是否在反向传播时分离 reset 计算图
step_mode (str) -- 步进模式,可为
"s"或"m"
English
A simple version of
IFNode.- 参数:
v_threshold (float) -- Threshold voltage of the neuron
v_reset (Optional[float]) -- Reset voltage of the neuron
surrogate_function (surrogate.SurrogateFunctionBase) -- Surrogate gradient function
detach_reset (bool) -- Whether to detach reset graph in backward
step_mode (str) -- Step mode, either
"s"or"m"
- 返回:
None
- 返回类型:
None
- neuronal_charge(x: Tensor)[源代码]#
-
中文
中文
神经元充电的微分方程:
\[H[t] = V[t-1] + X[t]\]- 参数:
x (torch.Tensor) -- 输入电压
- 返回:
None(膜电位更新存储在
self.v中)- 返回类型:
None
English
English
The differential equation for neuronal charge:
\[H[t] = V[t-1] + X[t]\]- 参数:
x (torch.Tensor) -- Input voltage
- 返回:
None (membrane potential is stored in
self.v)- 返回类型:
None
- class spikingjelly.activation_based.neuron.integrate_and_fire.IFNode(v_threshold: float = 1.0, v_reset: float | None = 0.0, surrogate_function: SurrogateFunctionBase = Sigmoid(alpha=4.0, spiking=True), detach_reset: bool = False, step_mode='s', backend='torch', store_v_seq: bool = False)[源代码]#
基类:
BaseNode
中文
Integrate-and-Fire 神经元模型,可以看作理想积分器,无输入时电压保持恒定,不会像 LIF 神经元那样衰减。其阈下神经动力学方程为:
\[H[t] = V[t-1] + X[t]\]- 参数:
v_threshold (float) -- 神经元的阈值电压
v_reset (Optional[float]) -- 神经元的重置电压。如果不为
None,当神经元释放脉冲后,电压会被重置为v_reset; 如果设置为None,当神经元释放脉冲后,电压会被减去v_thresholdsurrogate_function (surrogate.SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数
detach_reset (bool) -- 是否将 reset 过程的计算图分离
step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)
backend (str) -- 使用哪种后端。不同的
step_mode可能会带有不同的后端。可以通过打印self.supported_backends查看当前 使用的步进模式支持的后端。在支持的情况下,使用'cupy'或'triton'后端速度更快。store_v_seq (bool) -- 在使用
step_mode = 'm'时,给与shape = [T, N, *]的输入后,是否保存中间过程的shape = [T, N, *]的各个时间步的电压值self.v_seq。设置为False时计算完成后只保留最后一个时刻的电压,即shape = [N, *]的self.v。 通常设置成False,可以节省内存
English
The Integrate-and-Fire neuron, which can be seen as an ideal integrator. The voltage of the IF neuron will not decay as that of the LIF neuron. The sub-threshold neural dynamics of it is as followed:
\[H[t] = V[t-1] + X[t]\]- 参数:
v_threshold (float) -- threshold of this neurons layer
v_reset (Optional[float]) -- reset voltage of this neurons layer. If not
None, the neuron's voltage will be set tov_resetafter firing a spike. IfNone, the neuron's voltage will subtractv_thresholdafter firing a spikesurrogate_function (surrogate.SurrogateFunctionBase) -- the function for calculating surrogate gradients of the heaviside step function in backward
detach_reset (bool) -- whether detach the computation graph of reset in backward
step_mode (str) -- the step mode, which can be s (single-step) or m (multi-step)
backend (str) -- backend fot this neurons layer. Different
step_modemay support for different backends. The user can printself.supported_backendsand check what backends are supported by the currentstep_mode. If supported, using'cupy'or'triton'backend will be fasterstore_v_seq (bool) -- when using
step_mode = 'm'and given input withshape = [T, N, *], this option controls whether storing the voltage at each time-step toself.v_seqwithshape = [T, N, *]. If set toFalse, only the voltage at last time-step will be stored toself.vwithshape = [N, *], which can reduce the memory consumption
- 返回:
None
- 返回类型:
None
- class spikingjelly.activation_based.neuron.integrate_and_fire.NonSpikingIFNode(decode: str | None = None)[源代码]#
-
中文
中文
不发放脉冲的 IF 节点,输出膜电位(或根据
decode进行解码)。- 参数:
decode (Optional[str]) -- 非脉冲输出解码方式,见
NonSpikingBaseNode
English
English
Non-spiking IF node that outputs membrane potential (or decoded outputs specified by
decode).- 参数:
decode (Optional[str]) -- Decoding mode for non-spiking outputs, see
NonSpikingBaseNode- 返回:
None
- 返回类型:
None
Leaky Integrate-and-fire (LIF) Neurons#
- class spikingjelly.activation_based.neuron.lif.SimpleLIFNode(tau: float, decay_input: bool, v_threshold: float = 1.0, v_reset: float = 0.0, surrogate_function: SurrogateFunctionBase = Sigmoid(alpha=4.0, spiking=True), detach_reset: bool = False, step_mode='s')[源代码]#
-
中文
LIFNode的简化版实现。
English
A simple version of
LIFNode.- 参数:
decay_input (bool) -- 输入是否参与衰减(详见父类)
v_threshold (float) -- 神经元的阈值电压(详见父类)
v_reset (float) -- 神经元的重置电压(详见父类)
surrogate_function (surrogate.SurrogateFunctionBase) -- 替代梯度函数(详见父类)
detach_reset (bool) -- 是否将 reset 过程的计算图分离
step_mode (str) -- 步进模式,可为
"s"或"m"tau -- Membrane time constant (see parent class
LIFNode)decay_input -- Whether input participates in decay (see parent)
v_threshold -- Threshold voltage of the neuron (see parent)
v_reset -- Reset voltage of the neuron (see parent)
surrogate_function -- Surrogate gradient function (see parent)
detach_reset -- Whether to detach reset graph in backward
step_mode -- Step mode, either
"s"or"m"
- 返回:
None
- 返回类型:
None
- class spikingjelly.activation_based.neuron.lif.LIFNode(tau: float = 2.0, decay_input: bool = True, v_threshold: float = 1.0, v_reset: float | None = 0.0, surrogate_function: SurrogateFunctionBase = Sigmoid(alpha=4.0, spiking=True), detach_reset: bool = False, step_mode='s', backend='torch', store_v_seq: bool = False)[源代码]#
基类:
BaseNode
中文
Leaky Integrate-and-Fire 神经元模型,可以看作是带漏电的积分器。其阈下神经动力学方程为:
若
decay_input == True:\[H[t] = V[t-1] + \frac{1}{\tau}(X[t] - (V[t-1] - V_{reset}))\]若
decay_input == False:\[H[t] = V[t-1] - \frac{1}{\tau}(V[t-1] - V_{reset}) + X[t]\]- 参数:
tau (float) -- 膜电位时间常数
decay_input (bool) -- 输入是否也会参与衰减
v_threshold (float) -- 神经元的阈值电压
v_reset (Optional[float]) -- 神经元的重置电压。如果不为
None,当神经元释放脉冲后,电压会被重置为v_reset; 如果设置为None,当神经元释放脉冲后,电压会被减去v_thresholdsurrogate_function (surrogate.SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数
detach_reset (bool) -- 是否将 reset 过程的计算图分离
step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)
backend (str) -- 使用哪种后端。不同的
step_mode可能会带有不同的后端。可以通过打印self.supported_backends查看当前 使用的步进模式支持的后端。在支持的情况下,使用'cupy'或'triton'后端速度更快。store_v_seq (bool) -- 在使用
step_mode = 'm'时,给与shape = [T, N, *]的输入后,是否保存中间过程的shape = [T, N, *]的各个时间步的电压值self.v_seq。设置为False时计算完成后只保留最后一个时刻的电压,即shape = [N, *]的self.v。 通常设置成False,可以节省内存
English
The Leaky Integrate-and-Fire neuron, which can be seen as a leaky integrator. The subthreshold neural dynamics of it is as followed:
If
decay_input == True:\[H[t] = V[t-1] + \frac{1}{\tau}(X[t] - (V[t-1] - V_{reset}))\]If
decay_input == False:\[H[t] = V[t-1] - \frac{1}{\tau}(V[t-1] - V_{reset}) + X[t]\]- 参数:
tau (float) -- membrane time constant
decay_input (bool) -- whether the input will decay
v_threshold (float) -- threshold of this neurons layer
v_reset (Optional[float]) -- reset voltage of this neurons layer. If not
None, the neuron's voltage will be set tov_resetafter firing a spike. IfNone, the neuron's voltage will subtractv_thresholdafter firing a spikesurrogate_function (surrogate.SurrogateFunctionBase) -- the function for calculating surrogate gradients of the heaviside step function in backward
detach_reset (bool) -- whether detach the computation graph of reset in backward
step_mode (str) -- the step mode, which can be s (single-step) or m (multi-step)
backend (str) -- backend fot this neurons layer. Different
step_modemay support for different backends. The user can printself.supported_backendsand check what backends are supported by the currentstep_mode. If supported, using'cupy'or'triton'backend will be fasterstore_v_seq (bool) -- when using
step_mode = 'm'and given input withshape = [T, N, *], this option controls whether storing the voltage at each time-step toself.v_seqwithshape = [T, N, *]. If set toFalse, only the voltage at last time-step will be stored toself.vwithshape = [N, *], which can reduce the memory consumption
- 返回:
None
- 返回类型:
None
- class spikingjelly.activation_based.neuron.lif.NonSpikingLIFNode(tau: float = 2.0, decode: str | None = None)[源代码]#
-
Non-spiking version of
LIFNodethat outputs continuous-valued membrane potentials instead of spikes.See also:
spikingjelly.activation_based.layer.misc.SynapseFilter.- 参数:
- 返回:
None
- 返回类型:
None
Parametric Leaky Integrate-and-fire (PLIF) Neurons#
- class spikingjelly.activation_based.neuron.plif.ParametricLIFNode(init_tau: float = 2.0, decay_input: bool = True, v_threshold: float = 1.0, v_reset: float | None = 0.0, surrogate_function: SurrogateFunctionBase = Sigmoid(alpha=4.0, spiking=True), detach_reset: bool = False, step_mode='s', backend='torch', store_v_seq: bool = False)[源代码]#
基类:
BaseNode
中文
Parametric Leaky Integrate-and-Fire (PLIF) 神经元模型,提出自 Incorporating Learnable Membrane Time Constant to Enhance Learning of Spiking Neural Networks。可以看作是带漏电的积分器。其阈下神经动力学方程为:
若
decay_input == True:\[H[t] = V[t-1] + \frac{1}{\tau}(X[t] - (V[t-1] - V_{reset}))\]若
decay_input == False:\[H[t] = V[t-1] - \frac{1}{\tau}(V[t-1] - V_{reset}) + X[t]\]其中 \(\frac{1}{\tau} = {\rm Sigmoid}(w)\),\(w\) 是可学习的参数。
- 参数:
init_tau (float) -- 膜电位时间常数的初始值
decay_input (bool) -- 输入是否也会参与衰减
v_threshold (float) -- 神经元的阈值电压
v_reset (Optional[float]) -- 神经元的重置电压。如果不为
None,当神经元释放脉冲后,电压会被重置为v_reset; 如果设置为None,当神经元释放脉冲后,电压会被减去v_thresholdsurrogate_function (surrogate.SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数
detach_reset (bool) -- 是否将 reset 过程的计算图分离
step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)
backend (str) -- 使用哪种后端。不同的
step_mode可能会带有不同的后端。可以通过打印self.supported_backends查看当前 使用的步进模式支持的后端。在支持的情况下,使用'cupy'或'triton'后端速度更快。store_v_seq (bool) -- 在使用
step_mode = 'm'时,给与shape = [T, N, *]的输入后,是否保存中间过程的shape = [T, N, *]的各个时间步的电压值self.v_seq。设置为False时计算完成后只保留最后一个时刻的电压,即shape = [N, *]的self.v。 通常设置成False,可以节省内存
English
The Parametric Leaky Integrate-and-Fire (PLIF) neuron, proposed in Incorporating Learnable Membrane Time Constant to Enhance Learning of Spiking Neural Networks, can be seen as a leaky integrator. The subthreshold neural dynamics of it is as followed:
IF
decay_input == True:\[H[t] = V[t-1] + \frac{1}{\tau}(X[t] - (V[t-1] - V_{reset}))\]IF
decay_input == False:\[H[t] = V[t-1] - \frac{1}{\tau}(V[t-1] - V_{reset}) + X[t]\]where \(\frac{1}{\tau} = {\rm Sigmoid}(w)\), \(w\) is a learnable parameter.
- 参数:
init_tau (float) -- the initial value of membrane time constant
decay_input (bool) -- whether the input will decay
v_threshold (float) -- threshold of this neurons layer
v_reset (Optional[float]) -- reset voltage of this neurons layer. If not
None, the neuron's voltage will be set tov_resetafter firing a spike. IfNone, the neuron's voltage will subtractv_thresholdafter firing a spikesurrogate_function (surrogate.SurrogateFunctionBase) -- the function for calculating surrogate gradients of the heaviside step function in backward
detach_reset (bool) -- whether detach the computation graph of reset in backward
step_mode (str) -- the step mode, which can be s (single-step) or m (multi-step)
backend (str) -- backend for this neurons layer. Different
step_modemay support for different backends. The user can printself.supported_backendsand check what backends are supported by the currentstep_mode. If supported, using'cupy'or'triton'backend will have the fastest training speedstore_v_seq (bool) -- when using
step_mode = 'm'and given input withshape = [T, N, *], this option controls whether storing the voltage at each time-step toself.v_seqwithshape = [T, N, *]. If set toFalse, only the voltage at last time-step will be stored toself.vwithshape = [N, *], which can reduce the memory consumption
- 返回:
None
- 返回类型:
None
Parallel Spiking Neuron Family#
- class spikingjelly.activation_based.neuron.psn.PSN(T: int, surrogate_function: SurrogateFunctionBase = ATan(alpha=2.0, spiking=True))[源代码]#
-
中文
并行脉冲神经元(Parallel Spiking Neuron,PSN),由 Parallel Spiking Neurons with High Efficiency and Long-term Dependencies Learning Ability 提出。神经元动力学定义如下:
\[\begin{split}H &= WX, ~~~~~~~~~~~~~~~W \in \mathbb{R}^{T \times T}, X \in \mathbb{R}^{T \times N}\\ S &= \Theta(H - B), ~~~~~B \in \mathbb{R}^{T}, S\in \{0, 1\}^{T \times N}\end{split}\]其中 \(W\) 是可学习的权重矩阵,\(B\) 是可学习的阈值。
注意
PSN 仅支持多步模式。
- 参数:
T (int) -- 时间步数
surrogate_function (surrogate.SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数
English
The Parallel Spiking Neuron (PSN), proposed in Parallel Spiking Neurons with High Efficiency and Long-term Dependencies Learning Ability. The neuronal dynamics are defined as:
\[\begin{split}H &= WX, ~~~~~~~~~~~~~~~W \in \mathbb{R}^{T \times T}, X \in \mathbb{R}^{T \times N}\\ S &= \Theta(H - B), ~~~~~B \in \mathbb{R}^{T}, S\in \{0, 1\}^{T \times N}\end{split}\]where \(W\) is the learnable weight matrix, and \(B\) is the learnable threshold.
Note
The PSN only supports the multi-step mode.
- 参数:
T (int) -- the number of time-steps
surrogate_function (surrogate.SurrogateFunctionBase) -- the function for calculating surrogate gradients of the heaviside step function in backward
- 返回:
None
- 返回类型:
None
- class spikingjelly.activation_based.neuron.psn.MaskedPSN(k: int, T: int, lambda_init: float = 0.0, surrogate_function: SurrogateFunctionBase = ATan(alpha=2.0, spiking=True), step_mode: str = 's')[源代码]#
基类:
MemoryModule
中文
Masked Parallel Spiking Neuron,由 Parallel Spiking Neurons with High Efficiency and Long-term Dependencies Learning Ability 提出。神经元动力学定义如下:
\[\begin{split}H &= (W \cdot {M}_{k})X, ~~~~~~~~~~~~~~~W \in \mathbb{R}^{T \times T}, {M}_{k} \in \mathbb{R}^{T \times T}, X \in \mathbb{R}^{T \times N} \\ S &= \Theta(H - B), ~~~~~B \in \mathbb{R}^{T}, S\in \{0, 1\}^{T \times N}\end{split}\]其中 \(W\) 是可学习权重矩阵,\(B\) 是可学习阈值,\({M}_{k}\) 定义为:
\[\begin{split}{M}_{k}[i][j] = \begin{cases} 1, ~~ j \leq i \leq j + k - 1 \\ 0, \mathrm{otherwise} \end{cases}.\end{split}\]\(\lambda\) 用于调节逐步掩码过程:
\[M_{k}(\lambda) = \lambda \cdot M_{k} + (1 - \lambda) \cdot J,\]其中 \(J\) 为全 1 矩阵。用户可以在训练中通过
self.lambda_ = ...设置 \(\lambda\)。注意
Masked PSN 支持单步模式和多步模式,但多步模式比单步模式快得多。
- 参数:
k (int) -- Masked PSN 的阶数
T (int) -- 时间步数
lambda_init (float) -- \(\lambda\) 的初始值,用于调节逐步掩码过程
surrogate_function (surrogate.SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数
step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)
English
Masked Parallel Spiking Neuron (Masked PSN), proposed in Parallel Spiking Neurons with High Efficiency and Long-term Dependencies Learning Ability. The neuronal dynamics are defined as:
\[\begin{split}H &= (W \cdot {M}_{k})X, ~~~~~~~~~~~~~~~W \in \mathbb{R}^{T \times T}, {M}_{k} \in \mathbb{R}^{T \times T}, X \in \mathbb{R}^{T \times N} \\ S &= \Theta(H - B), ~~~~~B \in \mathbb{R}^{T}, S\in \{0, 1\}^{T \times N}\end{split}\]where \(W\) is the learnable weight matrix, \(B\) is the learnable threshold, and \({M}_{k}\) is defined as:
\[\begin{split}{M}_{k}[i][j] = \begin{cases} 1, ~~ j \leq i \leq j + k - 1 \\ 0, \mathrm{otherwise} \end{cases}.\end{split}\]\(\lambda\) is used to adjust the progressive masking process:
\[M_{k}(\lambda) = \lambda \cdot M_{k} + (1 - \lambda) \cdot J,\]where \(J\) is an all-one matrix. Users can set \(\lambda\) during training by calling
self.lambda_ = ....Note
The masked PSN supports both single-step and multi-step mode. Multi-step mode is much faster than single-step mode.
- 参数:
k (int) -- the order of the Masked PSN
T (int) -- the number of time-steps
lambda_init (float) -- the initial value of \(\lambda\) to adjust the progressive masking process
surrogate_function (surrogate.SurrogateFunctionBase) -- the function for calculating surrogate gradients of the heaviside step function in backward
step_mode (str) -- the step mode, which can be s (single-step) or m (multi-step)
- 返回:
None
- 返回类型:
None
- property lambda_#
- class spikingjelly.activation_based.neuron.psn.SlidingPSN(k: int, exp_init: bool = True, surrogate_function: SurrogateFunctionBase = ATan(alpha=2.0, spiking=True), step_mode: str = 's', backend: str = 'gemm')[源代码]#
基类:
MemoryModule
中文
Sliding Parallel Spiking Neuron,由 Parallel Spiking Neurons with High Efficiency and Long-term Dependencies Learning Ability 提出。神经元动力学定义如下:
\[\begin{split}H[t] &= \sum_{i=0}^{k-1} W_i \cdot X[t - k + 1 + i], \\ S[t] &= \Theta(H[t] - B),\end{split}\]其中 \(W = [W_0, W_1, ..., W_{k-1}] \in \mathbb{R}^{T}\) 是可学习权重,\(B\) 是可学习阈值。
注意
Sliding PSN 支持单步模式和多步模式,但多步模式比单步模式快得多。
- 参数:
k (int) -- Sliding PSN 的阶数
exp_init (bool) -- 如果为
True,权重初始化为(..., 1/4, 1/2, 1);如果为False,权重使用 Kaiming uniform 初始化surrogate_function (surrogate.SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数
step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)
backend (str) -- 神经元层使用的后端,可以为 "gemm" 或 "conv"。此选项仅在多步模式下生效
English
Sliding Parallel Spiking Neuron (Sliding PSN), proposed in Parallel Spiking Neurons with High Efficiency and Long-term Dependencies Learning Ability. The neuronal dynamics are defined as:
\[\begin{split}H[t] &= \sum_{i=0}^{k-1} W_i \cdot X[t - k + 1 + i], \\ S[t] &= \Theta(H[t] - B),\end{split}\]where \(W = [W_0, W_1, ..., W_{k-1}] \in \mathbb{R}^{T}\) is the learnable weight, and \(B\) is the learnable threshold.
Note
Sliding PSN supports both single-step and multi-step mode. Multi-step mode is much faster than single-step mode.
- 参数:
k (int) -- the order of the Sliding PSN
exp_init (bool) -- if
True, the weight will be initialized as(..., 1/4, 1/2, 1); ifFalse, the weight will be initialized by Kaiming uniformsurrogate_function (surrogate.SurrogateFunctionBase) -- the function for calculating surrogate gradients of the heaviside step function in backward
step_mode (str) -- the step mode, which can be s (single-step) or m (multi-step)
backend (str) -- backend for this neuron layer, which can be "gemm" or "conv". This option only works for multi-step mode
- 返回:
None
- 返回类型:
None
FlexSN#
- class spikingjelly.activation_based.neuron.flexsn.FlexSNKernel(core: Callable, num_inputs: int, num_states: int, num_outputs: int, example_inputs: Tuple[Tensor] | None = None, requires_grad: Tuple[bool] | None = None)[源代码]#
基类:
object
中文
FlexSNKernel可以根据自定义的 PyTorch 单步函数core生成 Triton 多步脉冲神经元核。 不同于FlexSN,FlexSNKernel是对底层 custom op 调度的轻量Callable封装。实例化后,
FlexSNKernel对象接受的输入参数为[*input_seqs, *states],其中input_seqs是num_inputs个输入序列,states是num_states个初始状态;返回值为[*output_seqs, *state_seqs], 其中output_seqs是num_outputs个输出序列,state_seqs是num_states个状态序列。- 参数:
core (Callable) -- 描述单步前向推理的函数,签名应为
[*inputs, *states] -> [*outputs, *updated_states],其中输入、输出和状态均为张量。num_inputs (int) -- 输入序列的数量。
num_states (int) -- 初始状态张量的数量,同时也应与返回的更新后状态数量一致。
num_outputs (int) -- 输出序列的数量。
example_inputs (Optional[Tuple[torch.Tensor]]) -- 传给
core的示例张量,形式为[*inputs, *states],用于辅助构建推理与训练 kernel。 若为None,则由底层构建器使用默认示例张量。requires_grad (Optional[Tuple[bool]]) -- 指示
core各个输入参数是否需要梯度的布尔元组,仅用于训练 kernel 的构建。 若为None,则由底层构建器采用默认行为。
- 抛出:
RuntimeError -- 当前环境未启用 CUDA 时抛出,因为
FlexSNKernel仅支持运行在 CUDA 设备上的 Triton 内核。
English
FlexSNKernelcan generate Triton multi-step spiking neuron kernels from a customized PyTorch single-step functioncorevia FlexSN's triton / inductor backend. It is a lightweightCallablewrapper over the underlying FlexSN custom-op dispatch path.The input arguments of a
FlexSNKernelobject is[*input_seqs, *states], whereinput_seqsis a list of input sequences,statesis a list of initial states; the return value is[*output_seqs, *state_seqs], whereoutput_seqsis a list of output sequences, andstate_seqsis a list of state sequences.- 参数:
core (Callable) -- function describing the single-step inference dynamics with signature
[*inputs, *states] -> [*outputs, *updated_states]. Inputs, outputs, and states should all be tensors.num_inputs (int) -- number of input sequences.
num_states (int) -- number of initial state tensors, which should also match the number of updated states returned by
core.num_outputs (int) -- number of output sequences.
example_inputs (Optional[Tuple[torch.Tensor]]) -- example tensors passed to
corein the form[*inputs, *states]. They are used to help build inference and training kernels. IfNone, the backend builders use their default example tensors.requires_grad (Optional[Tuple[bool]]) -- tuple indicating whether each argument of
corerequires gradients. It is only used when building the training kernels. IfNone, the backend builders use their default behavior.
- 抛出:
RuntimeError -- raised when CUDA is unavailable, because
FlexSNKernelonly supports the Triton kernels on CUDA devices.- 返回:
None
- 返回类型:
None
- class spikingjelly.activation_based.neuron.flexsn.FlexSN(core: Callable, num_inputs: int, num_states: int, num_outputs: int, example_inputs: Tuple[Tensor] | None = None, requires_grad: Tuple[bool] | None = None, step_mode: str = 'm', backend: str = 'triton', store_state_seqs: bool = False, example_outputs: Tuple[Tensor] | None = None)[源代码]#
基类:
MemoryModule
中文
FlexSN可以根据自定义的 PyTorch 单步函数core生成 Triton 多步脉冲神经元。FlexSN在FlexSNKernel的基础上,进一步实现了其他 SpikingJelly 神经元的功能。 实例化后,FlexSN对象输入和输出的语义与其他 SpikingJelly 神经元一致,取决于步进模式step_mode。- 参数:
core (Callable) -- 描述单步前向推理的函数,签名应为
[*inputs, *states] -> [*outputs, *updated_states], 其中输入与输出均为张量。“inputs”和“outputs”的数量任意,需用num_inputs和num_outputs指明。 “states”的数量任意,与“updated_states”数量一致,且需用num_states指明。num_inputs (int) -- 输入的数量,应严格对应
core参数及example_inputs中“inputs”的数量。num_states (int) -- 状态的数量,应严格对应
core的参数、返回值及example_inputs中的“states”的数量。num_outputs (int) -- 输出的数量,应严格对应
core返回值中“outputs”的数量。example_inputs (Optional[Tuple[torch.Tensor]]) -- 提供给
core的示例输入,形式为[*inputs, *states]。 这些张量都会自动被放置到cuda设备上。若为None,则自动生成num_inputs + num_states个仅含一个元素的张量。默认为None。requires_grad (Optional[Tuple[bool]]) -- 指示
core的参数 (即[*inputs, *states]) 是否需要梯度。 用于生成前向和反向计算图。长度应与core的参数及example_inputs对应。 若为None,则所有参数均需梯度。默认None。step_mode (str) -- 步进模式。
"triton"和"inductor"内核仅在"m"模式下可用。默认"m"。backend (str) -- 使用的后端。
"triton"、"inductor"和"hop"仅在step_mode="m"时可用;"torch"始终可用。"triton"与"inductor"在 FlexSN 中是并列且等价的 Triton 类标签, 当前共享同一条维护中的 Triton 执行路径。默认"triton"。store_state_seqs (bool) -- 是否保存状态序列。如果为
True,用户可以通过state_seqs属性访问。state_seqs是个列表,每个元素是形状为[T, ...]的张量。默认False。example_outputs (Optional[Tuple[torch.Tensor]]) --
core的单步输出模板,形式为tuple([*outputs])。 当backend="torch"且输入为空序列T == 0时, 需要用它来构造输出张量的 形状和 dtype, 从而避免为了推断输出而执行core。对于"triton"与"inductor"这两个等价的后端, 若提供该参数, 每个模板张量都必须与第一个example_inputs张量的单步形状和 dtype 相匹配。"hop"后端会保留任意 输出模板, 并在空序列/HOP 路径中按运行时输入设备物化它们, 不执行上述形状/dtype 校验。若不需要空序列模板, 则可以为None。默认None。
English
FlexSNcan generate Triton multi-step spiking neuron from a customized PyTorch single-step functioncore.FlexSNis built uponFlexSNKerneland further implements other features of SpikingJelly neurons. The input / output semantics of aFlexSNobject is similar to those of other SpikingJelly neurons, depending onstep_mode.- 参数:
core (Callable) -- a function describing the single-step inference dynamics of the spiking neuron. Its signature should be
[*inputs, *states] -> [*outputs, *updated_states], and the arguments and return values should all be tensors. There can be arbitrary number of inputs and outputs (specified bynum_inputsandnum_outputs). There can be arbitrary number of states (specified bynum_states), and the number of updated states should match the number of states.num_inputs (int) -- number of inputs. It should strictly match the number of inputs" in
core's arguments andexample_inputs.num_states (int) -- number of states. It should strictly match the number of "states" in
core's arguments,core's return values, andexample_inputs.num_outputs (int) -- number of outputs. It should strictly match the number of "outputs" in
core's return values.example_inputs (Optional[Tuple[torch.Tensor]]) -- example inputs to
corewith the form of[*inputs, *states]. These tensors will be moved tocudadevice. If None,example_inputswill benum_inputs + num_statestensors with single element. Defaults toNone.requires_grad (Optional[Tuple[bool]]) -- whether the core's arguments (i.e.
[*inputs, *states]) requires gradients. This info is used to generate the forward and backward graphs. Its length should match the number ofcore's arguments and the length ofexample_inputs. If None, all argument tensors require grad. Defaults toNone.step_mode (str) -- step mode.
"triton"and"inductor"backends are available only in "m" mode. Defaults to"m".backend (str) -- backend to use.
"triton","inductor", and"hop"are available only whenstep_mode="m"."torch"is always available. In FlexSN,"triton"and"inductor"are peer labels and currently dispatch the same maintained Triton execution path. Defaults to"triton".store_state_seqs (bool) -- whether to store the state sequences. If
True, users can access the state sequences viastate_seqsproperty.state_seqsis a list of tensors with shape[T, ...]. Defaults toFalse.example_outputs (Optional[Tuple[torch.Tensor]]) -- per-step output templates for
corewith the form oftuple([*outputs]). Whenbackend="torch"and the input sequence is empty (T == 0), these templates are required to materialize output shapes and dtypes without executingcore. For the equivalent"triton"/"inductor"backends, each provided template must match the firstexample_inputstensor's per-step shape and dtype. The"hop"backend intentionally allows arbitrary output templates and materializes them on the runtime input device for empty-sequence/HOP paths, so it does not enforce that scan-backend shape/dtype check. Defaults toNonewhen empty-sequence output templates are not needed.
- 返回:
None
- 返回类型:
None
- property kernel#
- property backend#
- static init_states(num_states: int, step_mode: str, *args) List[Tensor][源代码]#
-
中文
初始化神经元的状态张量。用户可以通过重写此方法来自定义状态初始化规则。默认情况下,所有 状态均被初始化为零张量。
- 参数:
num_states (int) -- 状态变量的数量。应与
core的“states”部分中的张量数量一致。step_mode (str) -- 本模块当前所处的步进模式。可选值为
"s"或"m"。args (Sequence[torch.Tensor]) --
forward的输入,即[*inputs]。用户应根据args和FlexSN的step_mode等信息来决定状态张量的初始化方式。
- 返回:
初始化后的状态张量列表,顺序对应了
core参数的“states”部分。- 返回类型:
List[torch.Tensor]
English
Initialize the neuron state tensors. Users can override this method to customize the state initialization rules. By default, all state tensors are initialized to zero.
The state tensors are stored in the
statesattribute, which is a list of tensors, whose order corresponds to the "states" part ofcore.- 参数:
num_states (int) -- number of states. It should strictly match the number of "states" in
core's return values.step_mode (str) -- the current step mode of this module. It can be
"s"or"m".args (Sequence[torch.Tensor]) -- the input of
forward, i.e.,[*inputs]. Users should initialize state tensors based onargsandstep_mode.
- 返回:
the list of initialized state tensors, whose order corresponds to the "states" part of
core.- 返回类型:
List[torch.Tensor]