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=1.0, v_reset=0.0, surrogate_function=Sigmoid(alpha=4.0, spiking=True), detach_reset=False, step_mode='s', store_v_seq=False)[源代码]#
基类:
MemoryModule
中文
生产级可微分 SNN 神经元的基类。常规前向由显式状态的 functional 状态转移支持;子类应实现
single_step_functional_forward,并仅在存在 独立序列实现或专用 kernel 时重写multi_step_functional_forward。 若需要通过neuronal_charge修改 Python 神经元方程,请继承SimpleBaseNode。- 参数:
v_threshold (float) -- 神经元的阈值电压
v_reset (Optional[float]) -- 神经元的重置电压。如果不为
None,当神经元释放脉冲后,电压会被重置为v_reset; 如果设置为None,当神经元释放脉冲后,电压会被减去v_thresholdsurrogate_function (SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数
detach_reset (bool) -- 是否将reset过程的计算图分离
step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)
store_v_seq (bool) -- 在使用
step_mode = 'm'时,给与shape = [T, N, *]的输入后,是否保存中间过程的shape = [T, N, *]的各个时间步的电压值self.v_seq。设置为False时计算完成后只保留最后一个时刻的电压,即shape = [N, *]的self.v。 通常设置成False,可以节省内存。在使用step_mode = 's'时,每个时间步结束后的电压会被追加到self.v_seq, 直到调用reset();每一步都会复制整个序列,因此该选项主要用于监控和调试
English
Base class for production differentiable spiking neurons. Regular forward is backed by an explicit-state functional transition. Subclasses should implement
single_step_functional_forwardand overridemulti_step_functional_forwardonly for an independent sequence implementation or specialized kernel. InheritSimpleBaseNodeinstead to modify Python neuron equations throughneuronal_charge.- 参数:
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 (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)
store_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. When usingstep_mode = 's', the voltage after each time-step is appended toself.v_sequntilreset()is called; every step copies the whole sequence, so this option is meant for monitoring and debugging
- detach()[源代码]#
-
中文
从计算图中分离所有有状态变量,行为与
MemoryModule.detach相同。若store_v_seq = True,已累积的self.v_seq也会被分离, 因此在单步模式下逐步记录电压时,functional.detach_net仍可用于实现 TBPTT (Truncated Back Propagation Through Time)。
English
Detach all stateful variables from the computation graph, behaving like
MemoryModule.detach. Whenstore_v_seq = True, the accumulatedself.v_seqis detached as well, sofunctional.detach_netstill implements TBPTT (Truncated Back Propagation Through Time) when the voltage is recorded step by step in single-step mode.
- single_step_functional_forward(inputs, states, **kwargs)[源代码]#
-
中文
使用显式状态执行一个神经元时间步。生产级神经元子类必须实现本方法, 且不得修改模块 memory 或传入的
states。- 参数:
- 返回:
(outputs, updated_states)- 返回类型:
- 抛出:
NotImplementedError -- 子类未实现 functional 状态转移时抛出
English
Run one neuron time step with explicit states. Production neuron subclasses must implement this method without mutating module memories or the supplied
states.- 参数:
- 返回:
(outputs, updated_states)- 返回类型:
- 抛出:
NotImplementedError -- If the subclass does not implement a functional state transition
- single_step_forward(x, *args, **kwargs)[源代码]#
-
中文
执行一个时间步的前向传播,行为与
MemoryModule.single_step_forward相同。若store_v_seq = True,本步重置后的电压self.v会被追加到self.v_seq(shape = [T, N, *],T为自上次reset()以来的步数), 因此被LinearRecurrentContainer、ElementWiseRecurrentContainer或MultiStepContainer以单步模式驱动的神经元也能记录电压序列。若输入的形状、 设备或数据类型发生变化,序列会重新开始;detach()会同时分离已累积的序列, 以便实现 TBPTT。每一步都会复制整个序列,代价为 \(O(T^2)\) ,因此该功能主要用于监控和调试。
English
Run one time-step, behaving like
MemoryModule.single_step_forward. Whenstore_v_seq = True, the post-reset voltageself.vof this step is appended toself.v_seq(shape = [T, N, *], whereTcounts the steps since the lastreset()), so neurons driven in single-step mode byLinearRecurrentContainer,ElementWiseRecurrentContainerorMultiStepContaineralso record their voltage sequence. The sequence restarts when the input shape, device or dtype changes, anddetach()also detaches the accumulated sequence so that TBPTT stays bounded. Every step copies the whole sequence, an \(O(T^2)\) cost, so this is meant for monitoring and debugging.
- class spikingjelly.activation_based.neuron.base_node.NonSpikingBaseNode(decode=None)[源代码]#
-
- 参数:
decode (Optional[str]) -- 解码方式。若不为
None,在forward中将使用该方式对膜电位序列进行解码
- class spikingjelly.activation_based.neuron.base_node.SimpleBaseNode(v_threshold=1.0, v_reset=0.0, surrogate_function=Sigmoid(alpha=4.0, spiking=True), detach_reset=False, step_mode='s')[源代码]#
基类:
MemoryModule
中文
面向教学和神经元动力学自定义的纯 PyTorch 接口。
Simple描述的是接口 目标,并不表示一种神经元数学模型。该类将 SNN 神经元的充电、放电和重置 职责直接表达为neuronal_charge()、neuronal_fire()和neuronal_reset();用户通常只需要重写充电方程。该类不提供原生 functional 状态转移,spikingjelly.activation_based.base.to_functional_forward()会通过通用 状态替换路径转换其实例。- 参数:
v_threshold (float) -- 神经元的阈值电压
v_reset (Optional[float]) -- 神经元的重置电压
surrogate_function (SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数
detach_reset (bool) -- 是否将 reset 过程的计算图分离
step_mode (str) -- 步进模式,可以为
's'(单步) 或'm'(多步)
English
A pure-PyTorch interface for teaching and customizing neuron dynamics.
Simpledescribes the interface goal; it is not a neuron mathematical model. The class exposes the charge, fire, and reset responsibilities of an SNN neuron directly asneuronal_charge(),neuronal_fire(), andneuronal_reset(); users normally only need to override the charge equation. This class does not provide a native functional transition. Instances are converted byspikingjelly.activation_based.base.to_functional_forward()through the general state-substitution path.- 参数:
v_threshold (float) -- threshold of this neurons layer
v_reset (Optional[float]) -- reset voltage of this neurons layer
surrogate_function (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)
- single_step_forward(x)[源代码]#
-
中文
依次执行充电、放电和重置,完成一个时间步的前向传播。
English
Run one time step by applying charge, fire, and reset in order.
- multi_step_forward(x_seq)[源代码]#
-
中文
沿时间维逐步调用
single_step_forward()。
English
Apply
single_step_forward()successively along the time dimension.
- neuronal_charge(x)[源代码]#
-
中文
使用单步输入更新
self.v。继承SimpleBaseNode自定义动力学时, 通常只需要实现本方法。- 参数:
x (Tensor) -- 单步输入张量
- 抛出:
NotImplementedError -- 子类未实现充电方程时抛出
- 返回类型:
None
English
Update
self.vfrom one input step. Subclasses ofSimpleBaseNodenormally only need to implement this method to customize their dynamics.- 参数:
x (Tensor) -- Single-step input tensor
- 抛出:
NotImplementedError -- If the subclass does not implement a charge equation
- 返回类型:
None
Integrate-and-fire (IF) Neurons#
- class spikingjelly.activation_based.neuron.integrate_and_fire.SimpleIFNode(v_threshold=1.0, v_reset=0.0, surrogate_function=Sigmoid(alpha=4.0, spiking=True), detach_reset=False, step_mode='s')[源代码]#
-
中文
基于
SimpleBaseNode充电-放电-重置接口的纯 PyTorch IF 实现。- 参数:
v_threshold (float) -- 神经元阈值电压
v_reset (Optional[float]) -- 神经元重置电压
surrogate_function (SurrogateFunctionBase) -- 替代梯度函数
detach_reset (bool) -- 是否在反向传播时分离 reset 计算图
step_mode (str) -- 步进模式,可为
"s"或"m"
English
A pure-PyTorch IF implementation built on the charge-fire-reset interface of
SimpleBaseNode.- 参数:
v_threshold (float) -- Threshold voltage of the neuron
v_reset (Optional[float]) -- Reset voltage of the neuron
surrogate_function (SurrogateFunctionBase) -- Surrogate gradient function
detach_reset (bool) -- Whether to detach reset graph in backward
step_mode (str) -- Step mode, either
"s"or"m"
- class spikingjelly.activation_based.neuron.integrate_and_fire.IFNode(v_threshold=1.0, v_reset=0.0, surrogate_function=Sigmoid(alpha=4.0, spiking=True), detach_reset=False, step_mode='s', store_v_seq=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 (SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数
detach_reset (bool) -- 是否将 reset 过程的计算图分离
step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)
store_v_seq (bool) -- 在使用
step_mode = 'm'时,给与shape = [T, N, *]的输入后,是否保存中间过程的shape = [T, N, *]的各个时间步的电压值self.v_seq。设置为False时计算完成后只保留最后一个时刻的电压,即shape = [N, *]的self.v。 通常设置成False,可以节省内存。在使用step_mode = 's'时,每个时间步结束后的电压会被追加到self.v_seq, 直到调用reset();每一步都会复制整个序列,因此该选项主要用于监控和调试
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 (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)
store_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. When usingstep_mode = 's', the voltage after each time-step is appended toself.v_sequntilreset()is called; every step copies the whole sequence, so this option is meant for monitoring and debugging
- class spikingjelly.activation_based.neuron.integrate_and_fire.HalfThresholdIFNode(v_threshold=1.0, surrogate_function=Sigmoid(alpha=4.0, spiking=True), detach_reset=False, step_mode='s', store_v_seq=False)[源代码]#
基类:
BaseNode
中文
半阈值初始膜电位的 Integrate-and-Fire 神经元。每次调用
reset()后膜电位会恢复为v_threshold / 2。单步前向中的脉冲后重置仍使用 标准软重置。除此之外,其充电、放电和重置动力学与软重置 IF 神经元一致:\[H[t] = V[t-1] + X[t]\]\[S[t] = \Theta(H[t] - V_{threshold})\]\[V[t] = H[t] - S[t] V_{threshold}\]训练时使用
surrogate_function为脉冲函数提供替代梯度;前向输出仍为 离散脉冲。- 参数:
surrogate_function (SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数
detach_reset (bool) -- 是否在反向传播时分离 reset 计算图
step_mode (str) -- 步进模式,可以为
"s"或"m"store_v_seq (bool) -- 是否将每个时间步的膜电位序列保存到
self.v_seq。在step_mode="s"时膜电位会逐步追加,直到调用reset();每一步都会 复制整个序列,因此该选项主要用于监控和调试
- 抛出:
TypeError -- 当
v_threshold不是实数或张量时抛出ValueError -- 当
v_threshold不是单元素有限正数时抛出
English
An Integrate-and-Fire neuron with half-threshold initial membrane potential. After each explicit
reset(), its membrane potential is restored tov_threshold / 2. The per-step post-spike reset still uses the standard soft reset. Apart from the initial reset value, its charge, fire, and reset dynamics are the same as a soft-reset IF neuron:\[H[t] = V[t-1] + X[t]\]\[S[t] = \Theta(H[t] - V_{threshold})\]\[V[t] = H[t] - S[t] V_{threshold}\]During training,
surrogate_functionprovides surrogate gradients for the spike function; the forward output remains discrete spikes.- 参数:
v_threshold (float or Tensor) -- Threshold voltage of the neuron, which must be a finite positive real number or a scalar tensor
surrogate_function (SurrogateFunctionBase) -- Surrogate gradient function for the spike function in backward propagation
detach_reset (bool) -- Whether to detach the reset computation graph in backward propagation
step_mode (str) -- Step mode, either
"s"or"m"store_v_seq (bool) -- Whether to store the membrane potentials at every time step in
self.v_seq. Whenstep_mode="s"the voltage is appended step by step untilreset()is called and every step copies the whole sequence, so this option is meant for monitoring and debugging
- 抛出:
TypeError -- Raised when
v_thresholdis not a real number or tensorValueError -- Raised when
v_thresholdis not scalar finite positive
- class spikingjelly.activation_based.neuron.integrate_and_fire.ActivationAwareIFNode(v_threshold=1.0, v_offset=0.0, channel_dim=-1, v_reset=None, surrogate_function=Sigmoid(alpha=4.0, spiking=True), detach_reset=False, step_mode='s', store_v_seq=False)[源代码]#
基类:
MemoryModule
中文
Activation-aware IF 神经元,用于 ANN2SNN 中 Activation-Aware Redistribution (AAR) 风格的最小垂直切片。该神经元 支持标量或 1D channel-wise 的发放阈值
v_threshold和膜电位偏移v_offset。当v_threshold或v_offset为 1D 张量时,会沿channel_dim广播到输入张量。它不继承
BaseNode,也不改变现有IFNode/BaseNode的标量v_threshold约定。该实现用于研究和转换,不表示默认 ANN2SNN 路径支持多元素阈值。单步动力学为:
\[H[t] = V[t-1] + X[t]\]\[S[t] = \Theta(H[t] + O - V_{th})\]其中
O为v_offset。软复位时:\[V[t] = H[t] - S[t] V_{th}\]硬复位时:
\[V[t] = S[t] V_{reset} + (1 - S[t]) H[t]\]- 参数:
channel_dim (int) -- 1D
v_threshold/v_offset对应的输入通道维。v_reset (Optional[float]) -- 硬复位电压。
None表示软复位。 若不为None,reset()会将膜电位v恢复为v_reset, 与BaseNode的硬复位语义一致。surrogate_function (SurrogateFunctionBase) -- 反向传播时使用的替代函数。
detach_reset (bool) -- 是否在反向传播时分离 reset 计算图。
step_mode (str) -- 步进模式,
"s"为单步,"m"为多步。store_v_seq (bool) -- 多步模式下是否保存每个时间步的膜电位。本类仅在多步 模式下保存
v_seq,单步模式下不会累积。
- 抛出:
ValueError -- 当 step_mode、channel_dim、threshold、offset、多步输入形状 或逐通道参数长度非法时抛出。
English
Activation-aware IF neuron for an ANN2SNN Activation-Aware Redistribution (AAR) style minimal vertical slice. This neuron supports scalar or 1D channel-wise firing threshold
v_thresholdand membrane offsetv_offset. A 1Dv_thresholdorv_offsetis broadcast to the input tensor alongchannel_dim.It does not inherit from
BaseNodeand does not change the scalarv_thresholdconvention of existingIFNode/BaseNode. It is intended for research and conversion workloads; the default ANN2SNN path still does not support multi-element thresholds.The single-step dynamics are:
\[H[t] = V[t-1] + X[t]\]\[S[t] = \Theta(H[t] + O - V_{th})\]where
Oisv_offset. With soft reset:\[V[t] = H[t] - S[t] V_{th}\]With hard reset:
\[V[t] = S[t] V_{reset} + (1 - S[t]) H[t]\]- 参数:
v_threshold (float or Tensor) -- Firing threshold. It must be a finite positive scalar or a finite positive 1D tensor.
v_offset (float or Tensor) -- Membrane offset. It must be a finite scalar or a finite 1D tensor.
channel_dim (int) -- Input channel dimension for 1D
v_threshold/v_offset.v_reset (Optional[float]) -- Hard-reset voltage.
Nonemeans soft reset. If it is notNone,reset()restores membrane voltagevtov_reset, matching the hard-reset semantics ofBaseNode.surrogate_function (SurrogateFunctionBase) -- Surrogate function used in backward.
detach_reset (bool) -- Whether to detach the reset graph during backward.
step_mode (str) -- Step mode,
"s"for single-step and"m"for multi-step.store_v_seq (bool) -- Whether to store membrane voltage at each time step in multi-step mode. This class stores
v_seqonly in multi-step mode and does not accumulate it in single-step mode.
- 抛出:
ValueError -- If step_mode, channel_dim, threshold, offset, multi-step input shape, or channel-wise parameter length is invalid.
- property store_v_seq: bool#
-
中文
返回多步前向后是否保存完整膜电位序列。将该属性从
True设为False会立即释放之前由self.v_seq引用的序列张量。- 返回:
是否保存完整膜电位序列。
- 返回类型:
English
Return whether the full membrane-voltage sequence is stored after a multi-step forward. Changing this property from
TruetoFalseimmediately releases the sequence tensor previously referenced byself.v_seq.- 返回:
Whether to store the full membrane-voltage sequence.
- 返回类型:
- single_step_functional_forward(inputs, states, **kwargs)[源代码]#
-
中文
使用显式膜电位执行一个 activation-aware IF 时间步。 本方法不修改模块状态。
- 参数:
- 返回:
((spike,), updated_states)。- 返回类型:
English
Run one activation-aware IF time step with explicit membrane voltage. This method does not mutate module state.
- multi_step_functional_forward(inputs, states, **kwargs)[源代码]#
-
中文
使用显式状态执行 activation-aware IF 多步前向。可由注册算子执行的 推理输入会依据张量 device 自动分发;训练和其他不兼容输入保留 Torch 参考状态转移。
- 参数:
- 返回:
((spike_seq,), updated_states)。- 返回类型:
- 抛出:
ValueError -- 当输入形状、T 或逐通道参数长度非法时抛出。
English
Run the multi-step activation-aware IF forward pass with explicit state. Eligible inference inputs are dispatched by tensor device; training and other unsupported inputs use the Torch reference transition.
- 参数:
- 返回:
((spike_seq,), updated_states).- 返回类型:
- 抛出:
ValueError -- If the input shape, T, or channel-wise parameter length is invalid.
- class spikingjelly.activation_based.neuron.integrate_and_fire.NonSpikingIFNode(decode=None)[源代码]#
-
中文
不发放脉冲的 IF 节点,输出膜电位(或根据
decode进行解码)。- 参数:
decode (Optional[str]) -- 非脉冲输出解码方式,见
NonSpikingBaseNode
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
Leaky Integrate-and-fire (LIF) Neurons#
- class spikingjelly.activation_based.neuron.lif.SimpleLIFNode(tau, decay_input, v_threshold=1.0, v_reset=0.0, surrogate_function=Sigmoid(alpha=4.0, spiking=True), detach_reset=False, step_mode='s')[源代码]#
-
中文
基于
SimpleBaseNode充电-放电-重置接口的纯 PyTorch LIF 实现。
English
A pure-PyTorch LIF implementation built on the charge-fire-reset interface of
SimpleBaseNode.- 参数:
decay_input (bool) -- 输入是否参与衰减(详见父类)
v_threshold (float) -- 神经元的阈值电压(详见父类)
v_reset (float) -- 神经元的重置电压(详见父类)
surrogate_function (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"
- class spikingjelly.activation_based.neuron.lif.LIFNode(tau=2.0, decay_input=True, v_threshold=1.0, v_reset=0.0, surrogate_function=Sigmoid(alpha=4.0, spiking=True), detach_reset=False, step_mode='s', store_v_seq=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 (SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数
detach_reset (bool) -- 是否将 reset 过程的计算图分离
step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)
store_v_seq (bool) -- 在使用
step_mode = 'm'时,给与shape = [T, N, *]的输入后,是否保存中间过程的shape = [T, N, *]的各个时间步的电压值self.v_seq。设置为False时计算完成后只保留最后一个时刻的电压,即shape = [N, *]的self.v。 通常设置成False,可以节省内存。在使用step_mode = 's'时,每个时间步结束后的电压会被追加到self.v_seq, 直到调用reset();每一步都会复制整个序列,因此该选项主要用于监控和调试
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 (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)
store_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. When usingstep_mode = 's', the voltage after each time-step is appended toself.v_sequntilreset()is called; every step copies the whole sequence, so this option is meant for monitoring and debugging
Parametric Leaky Integrate-and-fire (PLIF) Neurons#
- class spikingjelly.activation_based.neuron.plif.ParametricLIFNode(init_tau=2.0, decay_input=True, v_threshold=1.0, v_reset=0.0, surrogate_function=Sigmoid(alpha=4.0, spiking=True), detach_reset=False, step_mode='s', store_v_seq=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 (SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数
detach_reset (bool) -- 是否将 reset 过程的计算图分离
step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)
store_v_seq (bool) -- 在使用
step_mode = 'm'时,给与shape = [T, N, *]的输入后,是否保存中间过程的shape = [T, N, *]的各个时间步的电压值self.v_seq。设置为False时计算完成后只保留最后一个时刻的电压,即shape = [N, *]的self.v。 通常设置成False,可以节省内存。在使用step_mode = 's'时,每个时间步结束后的电压会被追加到self.v_seq, 直到调用reset();每一步都会复制整个序列,因此该选项主要用于监控和调试
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 (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)
store_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. When usingstep_mode = 's', the voltage after each time-step is appended toself.v_sequntilreset()is called; every step copies the whole sequence, so this option is meant for monitoring and debugging
Parallel Spiking Neuron Family#
- class spikingjelly.activation_based.neuron.psn.PSN(T, surrogate_function=ATan(alpha=2.0, spiking=True))[源代码]#
-
中文
并行脉冲神经元(Parallel Spiking Neuron,PSN),由 Parallel Spiking Neurons with High Efficiency and Long-term Dependencies Learning Ability 提出。神经元动力学定义如下:
\begin{align*} H &= WX, \qquad W \in \mathbb{R}^{T \times T}, X \in \mathbb{R}^{T \times N} \\ S &= \Theta(H - B), \qquad B \in \mathbb{R}^{T}, S \in \{0, 1\}^{T \times N} \end{align*}其中 \(W\) 是可学习的权重矩阵,\(B\) 是可学习的阈值。
注意
PSN 仅支持多步模式。
- 参数:
T (int) -- 时间步数
surrogate_function (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{align*} H &= WX, \qquad W \in \mathbb{R}^{T \times T}, X \in \mathbb{R}^{T \times N} \\ S &= \Theta(H - B), \qquad B \in \mathbb{R}^{T}, S \in \{0, 1\}^{T \times N} \end{align*}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 (SurrogateFunctionBase) -- the function for calculating surrogate gradients of the heaviside step function in backward
- class spikingjelly.activation_based.neuron.psn.MaskedPSN(k, T, lambda_init=0.0, surrogate_function=ATan(alpha=2.0, spiking=True), step_mode='s')[源代码]#
基类:
MemoryModule
中文
Masked Parallel Spiking Neuron,由 Parallel Spiking Neurons with High Efficiency and Long-term Dependencies Learning Ability 提出。神经元动力学定义如下:
\begin{align*} H &= (W \cdot {M}_{k})X, \qquad 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), \qquad B \in \mathbb{R}^{T}, S \in \{0, 1\}^{T \times N} \end{align*}其中 \(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 (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{align*} H &= (W \cdot {M}_{k})X, \qquad 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), \qquad B \in \mathbb{R}^{T}, S \in \{0, 1\}^{T \times N} \end{align*}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 (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)
- single_step_forward(x)[源代码]#
-
中文
执行一次单步状态转移并返回脉冲。超过
T时保留旧行为:先将输入推进queue,再抛出异常;time_step不增加。- 参数:
x (Tensor) -- 当前输入张量,形状为
[N, *]。- 返回:
与
x同形状的脉冲张量。- 返回类型:
- 抛出:
ValueError --
lambda_ < 1时。OverflowError -- 调用次数超过
T时。
English
Run one state transition and return spikes. When the call exceeds
T, the input is first advanced intoqueuebefore the error;time_stepdoes not increase.- 参数:
x (Tensor) -- Current input tensor shaped
[N, *].- 返回:
Spike tensor with the same shape as
x.- 返回类型:
- 抛出:
ValueError -- If
lambda_ < 1.OverflowError -- If the call exceeds
T.
- property lambda_#
- class spikingjelly.activation_based.neuron.psn.SlidingPSN(k, exp_init=True, surrogate_function=ATan(alpha=2.0, spiking=True), step_mode='s')[源代码]#
基类:
MemoryModule
中文
Sliding Parallel Spiking Neuron,由 Parallel Spiking Neurons with High Efficiency and Long-term Dependencies Learning Ability 提出。神经元动力学定义如下:
\begin{align*} H[t] &= \sum_{i=0}^{k-1} W_i \cdot X[t - k + 1 + i] \\ S[t] &= \Theta(H[t] - B) \end{align*}其中 \(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 (SurrogateFunctionBase) -- 反向传播时用来计算脉冲函数梯度的替代函数
step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)
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{align*} H[t] &= \sum_{i=0}^{k-1} W_i \cdot X[t - k + 1 + i] \\ S[t] &= \Theta(H[t] - B) \end{align*}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 (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)
FlexSN#
FlexSN automatically uses its Torch implementation on CPU. On CUDA it selects the fused Triton implementation for supported cores and uses the Torch/HOP path for supported compositions that cannot be fused. Its constructor has no backend parameter.