spikingjelly.activation_based.layer package#

备注

API稳定性说明

SpikingJelly 0.0.0.1.0layer 模块的内部实现进行了重构:原来的 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.

MultiStepContainer

Container for multi-step forward pass.

SeqToANNContainer

Container that merges the time and batch dimensions for forward pass.

TLastMultiStepContainer

Multi-step container whose time dimension is placed at the last.

TLastSeqToANNContainer

SeqToANNContainer whose time dimension is placed at the last.

StepModeContainer

Container that sets the step mode for all contained modules.

ElementWiseRecurrentContainer

Container for element-wise recurrent connections.

LinearRecurrentContainer

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#

Conv1d

1D convolution layer with step mode support.

Conv2d

2D convolution layer with step mode support.

Conv3d

3D convolution layer with step mode support.

Convolutional Transpose Layers#

ConvTranspose1d

1D transposed convolution layer with step mode support.

ConvTranspose2d

2D transposed convolution layer with step mode support.

ConvTranspose3d

3D transposed convolution layer with step mode support.

Upsampling Layers#

Upsample

Upsampling layer with configurable interpolation methods.

Pooling Layers#

MaxPool1d

1D max pooling layer with step mode support.

MaxPool2d

2D max pooling layer with step mode support.

MaxPool3d

3D max pooling layer with step mode support.

MaxUnpool1d

1D max unpooling layer with step mode support.

MaxUnpool2d

2D max unpooling layer with step mode support.

MaxUnpool3d

3D max unpooling layer with step mode support.

AvgPool1d

1D average pooling layer with step mode support.

AvgPool2d

2D average pooling layer with step mode support.

AvgPool3d

3D average pooling layer with step mode support.

AdaptiveAvgPool1d

1D adaptive average pooling layer with step mode support.

AdaptiveAvgPool2d

2D adaptive average pooling layer with step mode support.

AdaptiveAvgPool3d

3D adaptive average pooling layer with step mode support.

Linear Layers#

Linear

Linear transformation layer with step mode support.

Flatten

Flatten layer with step mode support.

Layers with Weight Standardization#

WSConv2d

Weight Standardization 2D convolution layer with step mode support.

WSLinear

Weight Standardization linear layer with step mode support.

Group Normalization Layers#

GroupNorm

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#

TemporalWiseAttention

Temporal-wise attention.

MultiDimensionalAttention

Multi-dimensional attention.

Attention for Spiking Transformers#

SpikingSelfAttention

Spiking self-attention for Spikformer.

QKAttention

Query-Key attention for QKFormer.

TokenQKAttention

Token-wise Query-Key attention for QKFormer.

ChannelQKAttention

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#

BatchNorm1d

1D batch normalization layer with step mode support.

BatchNorm2d

2D batch normalization layer with step mode support.

BatchNorm3d

3D batch normalization layer with step mode support.

NeuNorm#

NeuNorm

Neural Normalization layer.

Threshold-Dependent Batch Normalization#

ThresholdDependentBatchNorm1d

1D threshold-dependent batch normalization layer.

ThresholdDependentBatchNorm2d

2D threshold-dependent batch normalization layer.

ThresholdDependentBatchNorm3d

3D threshold-dependent batch normalization layer.

Temporal Effective Batch Normalization#

TemporalEffectiveBatchNorm1d

1D temporal effective batch normalization layer.

TemporalEffectiveBatchNorm2d

2D temporal effective batch normalization layer.

TemporalEffectiveBatchNorm3d

3D temporal effective batch normalization layer.

Batch Normalization Through Time#

BatchNormThroughTime1d

1D batch normalization through time.

BatchNormThroughTime2d

2D batch normalization through time.

BatchNormThroughTime3d

3D batch normalization through time.

Dropout Variants#

SpikingJelly 提供了适用于 SNN 的 Dropout 实现,支持步进模式。


SpikingJelly provides dropout implementations suitable for SNNs with step mode support.

Dropout

Dropout layer with step mode support.

Dropout2d

2D dropout layer with step mode support.

DropConnectLinear

DropConnect linear layer with step mode support.

Online Learning Modules#

SpikingJelly 的 在线学习模块 提供了用于在线学习的辅助类和操作。


SpikingJelly's online learning modules provide auxiliary classes and operations for online learning.

ReplaceforGrad

AutoGrad function for gradient replacement.

GradwithTrace

Computing gradients using neuron traces.

SpikeTraceOp

Operation for spike and trace handling in online training.

OTTTSequential

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.

SpikeCountToBinary

Expand each integer spike count into consecutive binary event slots.

TemporalBinSum

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),
)

警告

SpikeCountToBinaryTemporalBinSum 必须使用相同的 num_slotsalways_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.

SynapseFilter

Synapse filter layer with exponential decay.

PrintShapeModule

Module for printing input shapes during forward pass.

VotingLayer

Voting layer for multi-class classification.

Delay

Temporal delay operation.