spikingjelly.activation_based.layer package#
备注
API稳定性说明
SpikingJelly 0.0.0.1.0 对 layer 模块的内部实现进行了重构:原来的 layer.py 文件已被拆分并重组为 layer/ 包,以提升代码的模块化程度和可维护性。
该改动不会影响对外公开的 API。 我们强烈建议用户仍然通过 layer 这一顶层命名空间来访问相关功能,而不是从具体的内部子模块中进行导入。 layer 层级下的导入路径被视为稳定的公共接口;更深层的子模块仅作为内部实现细节,未来可能发生变化。
from spikingjelly.activation_based.layer import SeqToANNContainer # 推荐 ✅
from spikingjelly.activation_based.layer.container import SeqToANNContainer # 不推荐 ❌
API Stability Notice
We have refactored the internal implementation of the layer module. The original layer.py file has been reorganized into a package ( layer/ ) for better modularity and maintainability.
This change does not affect the public API. Users are strongly encouraged to continue accessing layers directly from the layer namespace, rather than importing from specific internal submodules. Import paths under layer are considered part of the stable public API, while deeper submodule paths are treated as implementation details and may change in future releases.
from spikingjelly.activation_based.layer import SeqToANNContainer # recommended ✅
from spikingjelly.activation_based.layer.container import SeqToANNContainer # not recommended ❌
Containers#
SpikingJelly 的 容器 封装了常见的网络结构,并支持单步(s)和多步(m)步进模式。
SpikingJelly's containers wrap common network architectures and support both single-step (s) and multi-step (m) step modes.
|
Container for multi-step forward pass. |
|
Container that merges the time and batch dimensions for forward pass. |
|
Multi-step container whose time dimension is placed at the last. |
|
SeqToANNContainer whose time dimension is placed at the last. |
|
Container that sets the step mode for all contained modules. |
|
Container for element-wise recurrent connections. |
|
Container for linear recurrent connections. |
Wrappers for Stateless Layers#
SpikingJelly 的 无状态层包装器 封装了 PyTorch 的标准层,并支持脉冲神经网络的时间步进模式。
SpikingJelly's stateless layer wrappers wrap PyTorch's standard layers and support the step mode of Spiking Neural Networks.
Convolutional Layers#
|
1D convolution layer with step mode support. |
|
2D convolution layer with step mode support. |
|
3D convolution layer with step mode support. |
Convolutional Transpose Layers#
|
1D transposed convolution layer with step mode support. |
|
2D transposed convolution layer with step mode support. |
|
3D transposed convolution layer with step mode support. |
Upsampling Layers#
|
Upsampling layer with configurable interpolation methods. |
Pooling Layers#
|
1D max pooling layer with step mode support. |
|
2D max pooling layer with step mode support. |
|
3D max pooling layer with step mode support. |
|
1D max unpooling layer with step mode support. |
|
2D max unpooling layer with step mode support. |
|
3D max unpooling layer with step mode support. |
|
1D average pooling layer with step mode support. |
|
2D average pooling layer with step mode support. |
|
3D average pooling layer with step mode support. |
|
1D adaptive average pooling layer with step mode support. |
|
2D adaptive average pooling layer with step mode support. |
|
3D adaptive average pooling layer with step mode support. |
Linear Layers#
|
Linear transformation layer with step mode support. |
|
Flatten layer with step mode support. |
Layers with Weight Standardization#
|
Weight Standardization 2D convolution layer with step mode support. |
|
Weight Standardization linear layer with step mode support. |
Group Normalization Layers#
|
Group normalization layer with step mode support. |
Attention Layers#
SpikingJelly 的 注意力层 提供了用于深度脉冲神经网络的注意力机制实现,包括用于卷积 SNN 的注意力层和用于脉冲 Transformers 的注意力层。
SpikingJelly's attention layers provide attention mechanisms for deep Spiking Neural Networks, including attention layers for convolutional SNNs and Spiking Transformers.
Attention for Convolutional SNNs#
|
Temporal-wise attention. |
|
Multi-dimensional attention. |
Attention for Spiking Transformers#
|
Spiking self-attention for Spikformer. |
|
Query-Key attention for QKFormer. |
|
Token-wise Query-Key attention for QKFormer. |
|
Channel-wise Query-Key attention for QKFormer. |
Batch Normalization Variants#
SpikingJelly 提供了多种适用于深度 SNN 的 批归一化层变体 。
SpikingJelly provides multiple batch normalization variants that are optimized for deep SNNs.
Standard Batch Normalization#
|
1D batch normalization layer with step mode support. |
|
2D batch normalization layer with step mode support. |
|
3D batch normalization layer with step mode support. |
NeuNorm#
|
Neural Normalization layer. |
Threshold-Dependent Batch Normalization#
|
1D threshold-dependent batch normalization layer. |
|
2D threshold-dependent batch normalization layer. |
|
3D threshold-dependent batch normalization layer. |
Temporal Effective Batch Normalization#
|
1D temporal effective batch normalization layer. |
|
2D temporal effective batch normalization layer. |
|
3D temporal effective batch normalization layer. |
Batch Normalization Through Time#
|
1D batch normalization through time. |
|
2D batch normalization through time. |
|
3D batch normalization through time. |
Dropout Variants#
SpikingJelly 提供了适用于 SNN 的 Dropout 实现,支持步进模式。
SpikingJelly provides dropout implementations suitable for SNNs with step mode support.
|
Dropout layer with step mode support. |
|
2D dropout layer with step mode support. |
|
DropConnect linear layer with step mode support. |
Online Learning Modules#
SpikingJelly 的 在线学习模块 提供了用于在线学习的辅助类和操作。
SpikingJelly's online learning modules provide auxiliary classes and operations for online learning.
|
AutoGrad function for gradient replacement. |
|
Computing gradients using neuron traces. |
|
Operation for spike and trace handling in online training. |
|
Sequential container for online training through time. |
Integer-Valued Spike Conversion#
以下多步层将 I-LIF 的整数脉冲计数展开为二值事件槽,并在无 bias 权重运算后 恢复逻辑时间维。默认配置在训练模式保持整数路径,在 eval 模式执行二值转换。
The following multi-step layers expand integer I-LIF spike counts into binary event slots and restore the logical time dimension after a bias-free weight operation. Their default configuration keeps the integer path during training and performs binary conversion in evaluation mode.
|
Expand each integer spike count into consecutive binary event slots. |
|
Sum consecutive physical event slots back into logical timesteps. |
import torch.nn as nn
from spikingjelly.activation_based import layer, neuron
num_slots = 4
block = nn.Sequential(
neuron.ILIFNode(step_mode="m"),
layer.SpikeCountToBinary(num_slots),
layer.Linear(16, 32, bias=False, step_mode="m"),
layer.TemporalBinSum(num_slots),
)
警告
SpikeCountToBinary 与 TemporalBinSum 必须使用相同的 num_slots
和 always_convert。连接 ILIFNode 时,num_slots 不得小于
surrogate_function.max_spike_count。两者之间的权重层必须禁用 bias;
bias、归一化和非线性应在 TemporalBinSum 之后执行一次。
Both modules must use the same num_slots and always_convert. When
connected to ILIFNode, num_slots must be no smaller than
surrogate_function.max_spike_count. The intervening weight layer must
disable bias; apply bias, normalization, and nonlinear operations once after
TemporalBinSum.
这些层提供二值事件语义和对应的运算计数,不会把稠密 PyTorch 权重算子替换为 稀疏硬件 kernel,也不单独构成延迟或能耗提升的证据。
These layers provide binary-event semantics and corresponding operation accounting. They do not replace dense PyTorch weight operations with sparse hardware kernels and do not by themselves demonstrate latency or energy gains.
Miscellaneous#
SpikingJelly 的 杂项模块 提供了辅助层和其他实用工具。
SpikingJelly's miscellaneous module provides auxiliary layers and other utilities.
|
Synapse filter layer with exponential decay. |
|
Module for printing input shapes during forward pass. |
|
Voting layer for multi-class classification. |
|
Temporal delay operation. |