Attention Layers#
SpikingJelly 的 注意力层 提供了用于深度脉冲神经网络的注意力机制实现,包括用于卷积 SNN 的注意力层和用于脉冲 Transformers 的注意力层。 有关脉冲 Transformer 的更多信息,见 脉冲Transformer构建、训练和改进 。
SpikingJelly's attention layers provide attention mechanisms for deep Spiking Neural Networks, including attention layers for convolutional SNNs and Spiking Transformers. For more information about Spiking Transformers, see Spiking Transformer Construction, Training, and Improvements .
- class spikingjelly.activation_based.layer.attention.TemporalWiseAttention(T: int, reduction: int = 16, dimension: int = 4)[源代码]#
-
中文
Temporal-Wise Attention Spiking Neural Networks for Event Streams Classification 中提出 的TemporalWiseAttention层。TemporalWiseAttention层必须放在二维卷积层之后脉冲神经元之前,例如:
Conv2d -> TemporalWiseAttention -> LIF输入的尺寸是
[T, N, C, H, W]或者[T, N, L],经过TemporalWiseAttention层,输出为[T, N, C, H, W]或者[T, N, L]。reduction是压缩比,相当于论文中的 \(r\)。- 参数:
English
The TemporalWiseAttention layer is proposed in Temporal-Wise Attention Spiking Neural Networks for Event Streams Classification.
It should be placed after the convolution layer and before the spiking neurons, e.g.,
Conv2d -> TemporalWiseAttention -> LIFThe dimension of the input is
[T, N, C, H, W]or[T, N, L], after the TemporalWiseAttention layer, the output dimension is[T, N, C, H, W]or[T, N, L].reductionis the reduction ratio,which is \(r\) in the paper.- 参数:
- 返回:
None
- 返回类型:
None
- class spikingjelly.activation_based.layer.attention.MultiDimensionalAttention(T: int, C: int, reduction_t: int = 16, reduction_c: int = 16, kernel_size=3)[源代码]#
-
中文
Attention Spiking Neural Networks 中提出 的MA-SNN模型以及MultiStepMultiDimensionalAttention层。
您可以从以下链接中找到MA-SNN的示例项目: - MA-SNN/MA-SNN - ridgerchu/SNN_Attention_VGG
输入的尺寸是
[T, N, C, H, W],经过MultiStepMultiDimensionalAttention层,输出为[T, N, C, H, W]。- 参数:
English
The MA-SNN model and MultiStepMultiDimensionalAttention layer are proposed in Attention Spiking Neural Networks.
You can find the example projects of MA-SNN in the following links: - MA-SNN/MA-SNN - ridgerchu/SNN_Attention_VGG
The dimension of the input is
[T, N, C, H, W], after the MultiStepMultiDimensionalAttention layer, the output dimension is[T, N, C, H, W].- 参数:
- 返回:
None
- 返回类型:
None
- class spikingjelly.activation_based.layer.attention.SpikingSelfAttention(dim, num_heads=8, backend: str = 'torch')[源代码]#
-
中文
Spikformer: When Spiking Neural Network Meets Transformer 中提出的 Spiking Self Attention 层。本模块在 Spikformer源代码 的基础上做了改进,显著提高了运行效率。关于 Spikformer 和本模块实现方式的更多信息, 参见教程 脉冲Transformer构建、训练和改进 。
本模块的输入是尺寸为
[T, N, C, L]的脉冲张量,其中T是时间步数,N是 batch size ,C是 channel 数量,L是 token 数量 (对于视觉任务,L=H*W)。 输出是尺寸为[T, N, C, L]的脉冲张量。- 参数:
English
Spiking Self-Attention layer proposed in Spikformer: When Spiking Neural Network Meets Transformer. This module is implemented based on Spikformer source code with several improvements that significantly enhance efficiency. For more details about Spikformer and the implementation of this module, please refer to the tutorial Spiking Transformer Construction, Training, and Improvements.
The input to this module is a spike tensor of shape
[T, N, C, L], whereTdenotes the number of time steps,Nis the batch size,Cis the number of channels, andLis the number of tokens (for vision tasks,L = H * W). The output is a spiking tensor with the same shape[T, N, C, L].- 参数:
- 返回:
None
- 返回类型:
None
- property backend#
一旦设置,本模块中所有神经元的后端都会被同样地设置。
Once set, the backend of all the neurons in this module will also be changed.
- forward(x_seq: Tensor)[源代码]#
- 参数:
x_seq (torch.Tensor) --
shape=[T, N, C, L]- 返回:
shape=[T, N, C, L]- 返回类型:
- class spikingjelly.activation_based.layer.attention.QKAttention(dim: int, num_heads: int = 8, qka_type: str = 'token', backend: str = 'torch')[源代码]#
-
中文
QKFormer: Hierarchical Spiking Transformer using Q-K Attention 中提出的 Q-K Attention 层。本模块在 QKFormer源代码 的基础上做了改进,显著提高了运行效率;改进思路与 Spikformer 类似,见教程 脉冲Transformer构建、训练和改进 。
本模块的输入是尺寸为
[T, N, C, L]的脉冲张量,其中T是时间步数,N是 batch size ,C是 channel 数量,L是 token 数量 (对于视觉任务,L=H*W)。 输出是尺寸为[T, N, C, L]的脉冲张量。- 参数:
English
Q-K Attention layer proposed in QKFormer: Hierarchical Spiking Transformer using Q-K Attention. This module is implemented based on the QKFormer source code, with several improvements that significantly enhance efficiency. The improvement strategy is similar to that used in Spikformer; see the tutorial Spiking Transformer Construction, Training, and Improvements for details.
The input to this module is a spike tensor of shape
[T, N, C, L], whereTdenotes the number of time steps,Nis the batch size,Cis the number of channels, andLis the number of tokens (for vision tasks,L = H * W). The output is a spiking tensor with the same shape[T, N, C, L].- 参数:
dim (int) -- number of channels.
num_heads (int) -- number of heads in multi-head self-attention. Default:
8.qka_type (str) -- type of QKAttention. Available options are
tokenandchannel. The default istoken, which generates a token-wise mask.backend (str) -- backend used by the internal neurons of this module. Default:
torch.
- 返回:
None
- 返回类型:
None
- property backend#
一旦设置,本模块中所有神经元的后端都会被同样地设置。
Once set, the backend of all the neurons in this module will also be changed.
- property qka_type#
只读。构造时设置,随后不可修改。
Read-only. Set when constructing, and cannot be modified afterwards.
- forward(x_seq)[源代码]#
- 参数:
x_seq (torch.Tensor) --
shape=[T, N, C, L]- 返回:
shape=[T, N, C, L]- 返回类型:
- class spikingjelly.activation_based.layer.attention.TokenQKAttention(dim: int, num_heads: int = 8, backend: str = 'torch')[源代码]#
基类:
QKAttentionQKAttention(..., qka_type="token"). SeeQKAttention.
- class spikingjelly.activation_based.layer.attention.ChannelQKAttention(dim: int, num_heads: int = 8, backend: str = 'torch')[源代码]#
基类:
QKAttentionQKAttention(..., qka_type="channel"). SeeQKAttention.