Gated Attention Coding for Training High-performance and Efficient
  Spiking Neural Networks
Spiking neural networks (SNNs) are emerging as an energy-efficient
alternative to traditional artificial neural networks (ANNs) due to their
unique spike-based event-driven nature. Coding is crucial in SNNs as it
converts external input stimuli into spatio-temporal feature sequences.
However, most existing deep SNNs rely on direct coding that generates powerless
spike representation and lacks the temporal dynamics inherent in human vision.
Hence, we introduce Gated Attention Coding (GAC), a plug-and-play module that
leverages the multi-dimensional gated attention unit to efficiently encode
inputs into powerful representations before feeding them into the SNN
architecture. GAC functions as a preprocessing layer that does not disrupt the
spike-driven nature of the SNN, making it amenable to efficient neuromorphic
hardware implementation with minimal modifications. Through an observer model
theoretical analysis, we demonstrate GAC's attention mechanism improves
temporal dynamics and coding efficiency. Experiments on CIFAR10/100 and
ImageNet datasets demonstrate that GAC achieves state-of-the-art accuracy with
remarkable efficiency. Notably, we improve top-1 accuracy by 3.10\% on CIFAR100
with only 6-time steps and 1.07\% on ImageNet while reducing energy usage to
66.9\% of the previous works. To our best knowledge, it is the first time to
explore the attention-based dynamic coding scheme in deep SNNs, with
exceptional effectiveness and efficiency on large-scale datasets.The Code is
available at https://github.com/bollossom/GAC.
Xuerui Qiu, Rui-Jie Zhu, Yuhong Chou, Zhaorui Wang, Liang-jian Deng, Guoqi Li
# Gated Attention Coding for Training High-performance and Efficient Spiking Neural Networks

###### Abstract

Spiking neural networks (SNNs) are emerging as an energy-efficient alternative to traditional artificial neural networks (ANNs) due to their unique spike-based event-driven nature. Coding is crucial in SNNs as it converts external input stimuli into spatio-temporal feature sequences. However, most existing deep SNNs rely on direct coding that generates powerless spike representation and lacks the temporal dynamics inherent in human vision. Hence, we introduce Gated Attention Coding (GAC), a plug-and-play module that leverages the multi-dimensional gated attention unit to efficiently encode inputs into powerful representations before feeding them into the SNN architecture. GAC functions as a preprocessing layer that does not disrupt the spike-driven nature of the SNN, making it amenable to efficient neuromorphic hardware implementation with minimal modifications. Through an observer model theoretical analysis, we demonstrate GAC's attention mechanism improves temporal dynamics and coding efficiency. Experiments on CIFAR10/100 and ImageNet datasets demonstrate that GAC achieves state-of-the-art accuracy with remarkable efficiency. Notably, we improve top-1 accuracy by 3.10% on CIFAR100 with only 6-time steps and 1.07% on ImageNet while reducing energy usage to 66.9% of the previous works. To our best knowledge, it is the first time to explore the attention-based dynamic coding scheme in deep SNNs, with exceptional effectiveness and efficiency on large-scale datasets.

## Introduction

Artificial neural networks (ANNs) have garnered remarkable acclaim for their potent representation and astounding triumphs in a plethora of artificial intelligence domains such as computer vision [16], natural language processing [14] and big data applications [20]. Nonetheless, this comes at a significant cost in terms of energy consumption. In contrast, spiking neural networks (SNNs) exhibits heightened biological plausibility [11], spike-driven nature, and low power consumption on neuromorphic hardware, e.g., TrueNorth [15], Loihi [17], Tianjic [21]. Moreover, the versatility of SNNs extends to various tasks, including image classification [13, 14], image reconstruction [22], and language generation [23], although the majority of their applications currently lie within the field of computer vision.

To integrate SNNs into the realm of computer vision, the initial challenge lies in transforming static images into spatio-temporal feature sequences. Various coding schemes have emerged to address this issue, such as rate coding [20], temporal coding [18], and phase coding [19]. Among these, direct coding [20] as shown in Fig. 1-(a), excel in training SNNs on large-scale datasets with minimal simulation time steps.

Figure 1: How our Gated Attention Coding (GAC) differs from existing SNNs’ coding [20] and attention methods [21, 22]. In (a), the solid-colored cube represents the float values, the gray cube denotes the binary spike values, and the cube with the dotted line represents the sparse values. In comparison with direct coding, GAC generates spatio-temporal dynamics output with powerful representation. In (b), compared to other attention methods, GAC only adds the attention module to the encoder without requiring \(N\) Multiply-Accumulation (MAC) blocks for dynamically calculating attention scores in subsequent layers.
Moreover, by adopting direct coding, recent SNN models Li et al. (2021); Shen et al. (2023); Zhou et al. (2023) achieve state-of-the-art performance across various datasets, showcasing the immense potential of coding techniques. However, this approach utilizes a trainable layer to generate float values repetitively at each time step. The repetitive nature of direct coding leads to periodic identical outputs at every time step, generating powerless spike representation and limiting spatio-temporal dynamics. In addition, the repetitive nature of direct coding fails to generate the temporal dynamics inherent in human vision, which serves as the fundamental inspiration for SNN models. Human vision is characterized by its ability to process and perceive dynamic visual stimuli over time. The repetitive nature of direct coding falls short in replicating this crucial aspect, emphasizing the need for alternative coding schemes that can better emulate the temporal dynamics observed in human vision.

Humans can naturally and effectively find salient regions in complex scenes Itti et al. (1998). Motivated by this observation, attention mechanisms have been introduced into deep learning and achieved remarkable success in a wide spectrum of application domains, which is also worth to be explored in SNNs as shown in Fig. 1-(b) Yao et al. (2021); Zhou et al. (2023). However, it has been observed that implementing attention mechanisms to directly modify membrane potential and dynamically compute attention scores for each layer, rather than using static weights, disrupts the fundamental asynchronous spike-driven communication in these methods. Consequently, this approach falls short of providing full support for neuromorphic hardware.

In this paper, we investigate the shortcomings of traditional direct coding and introduce an innovative approach termed Gated Attention Coding (GAC) as depicted in Fig. 1. Instead of producing periodic and powerless results, GAC leverages a multi-dimensional attention mechanism for gating to elegantly generate powerful temporal dynamic encodings from static datasets. As a preprocessing layer, GAC doesn't disrupt the SNNs' spike-driven, enabling efficient neuromorphic hardware implementation with minimal modifications. Experimental results demonstrate that our GAC not only significantly enhances the performance of SNNs, but also notably reduces latency and energy consumption. Moreover, our main contributions can be summarized as follows:

* We propose an observer model to theoretically analyze direct coding limitations and introduce the GAC scheme, a plug-and-play preprocessing layer decoupled from the SNN architecture, preserving its spike-driven nature.
* We evaluate the feasibility of GAC and depict the encoding result under both direct coding and GAC setups to demonstrate the powerful representation of GAC and its advantage in generating spatio-temporal dynamics.
* We demonstrate the effectiveness and efficiency of the proposed method on the CIFAR10/100 and ImageNet datasets with spike-driven nature. Our method outperforms the previous state-of-the-art and shows significant improvements across all test datasets within fewer time steps.

## Related Works

### Bio-inspired Spiking Neural Networks

Spiking Neural Networks (SNNs) offer a promising approach to achieving energy-efficient intelligence. These networks aim to replicate the behavior of biological neurons by employing binary spiking signals, where a value of 0 indicates no activity and a value of 1 represents a spiking event. The spike-driven communication paradigm in SNNs is inspired by the functionality of biological neurons and holds the potential for enabling energy-efficient computational systems Roy et al. (2019). By incorporating advanced deep learning and neuroscience knowledge, SNNs can offer significant benefits for a wide range of applications Jin et al. (2022); Qiu et al. (2023); Wu et al. (2023). Recently, there exist two primary methods of training high-performance SNNs. One way is to discretize ANN into spike form through neuron equivalence Li et al. (2021); Hao et al. (2023), i.e., ANN-to-SNN conversion, but this requires a long simulation time step and boosts the energy consumption. We employ the direct training method Wu et al. (2018) and apply surrogate gradient training.

### SNN Coding Schemes

Numerous coding schemes are proposed for image classification tasks. Phase coding Kim et al. (2018) used a weighted spike to encode each pixel and temporal coding Park et al. (2020); Comsa et al. (2020); Zhou et al. (2021) represents information with the firing time of the first neuron spike. These methods have been successfully applied to simple datasets with shallow networks, but achieving high performance becomes more challenging as datasets and networks become larger and more complex. To address this issue, rating coding Van Rullen and Thorpe (2001), which encodes each pixel using spike firing frequency, has been suggested. However, it suffers from long time steps to remain high performance, while small time steps result in lower representation resolution. To overcome these limitations, Wu et al. (2019) proposed the direct coding, in which input is given straight to the network without conversion to spikes and image-spike encoding is done by the first \(\{\textit{Conv-BN}\}\) layer. Then repeat this procedure at each time step and feed the results to spiking neurons. Finally, these encoded spikes will be sent to the SNNs' architecture for feature extraction. However, the limited powerless spike representation in SNNs using direct coding leads to parameter sensitivity and subpar performance. The repetition operation fails to generate dynamic output and neglects redundant data, thus underutilizing the spatio-temporal extraction ability of subsequent SNN architectures and increasing energy consumption in neuromorphic hardware.

### Attention Mechanism

Initially introduced to enhance the performance of sequence-to-sequence tasks Bahdanau et al. (2014), the attention mechanism is a powerful technique that enables improved processing of pertinent information Ioffe and Szegedy (2015). By effectively filtering out distracting noise, the attention mechanism facilitates more focused and efficient data processing, leading to enhanced performance in various applications. Yao et al. (2021) attach the squeeze-and-excitation Hu et al. (2018) attention block to the temporal-wise input of SNN, assessing the significance
over different frames during training and discarding irrelevant frames during inferencing. However, this method only gets better performance on small datasets with shallow networks. Yao et al. (2023) switch CBAM attention (Woo et al., 2018) to multi-dimension attention and inject it in SNNs architecture, revealing deep SNNs' potential as a general architecture to support various applications. Currently, integrating attention blocks into SNN architectures using sparse addition neuromorphic hardware poses challenges, as it necessitates creating numerous multiplication blocks in subsequent layers to dynamically compute attention scores, which could impede the spike-driven nature of SNNs. A potential solution to address this issue involves confining the application of attention mechanisms solely to the encoder, i.e., the first layer of the SNNs. By limiting the attention modifications to the initial stage, the subsequent layers can still maintain the essential spike-driven communication. This approach holds promise in enabling a more feasible implementation of SNNs on neuromorphic hardware, as it mitigates the incompatibility arising from dynamic attention mechanisms throughout the architecture.

## Method

In this section, we first introduce the iterative spiking neuron model. Then we proposed the Gated Attention Coding (GAC) and Gated Attention Unit (GAU) as the basic block of it. Next, we provide the overall framework for training GAC-SNNs. Moreover, we conduct a comprehensive analysis of the direct coding scheme and explain why our GAC outperforms in generating spatio-temporal dynamics encoding results.

### Iterative Spiking Neuron Model

We adopt the Leaky Integrate-and-Fire (LIF) spiking neuron model and translate it to an iterative expression with the Euler method (Wu et al., 2018; Yao et al., 2021). Mathematically, the LIF-SNN layer can be described as an explicitly iterable version for better computational traceability:

\[\begin{cases}\mathbf{U}^{t,n}=\mathbf{H}^{t-1,n}+f(\mathbf{W^{n}},\mathbf{X}^{t,n-1})\\ \mathbf{S}^{t,n}=\Theta(\mathbf{U}^{t,n}-\mathbf{V}_{th})\\ \mathbf{H}^{t,n}=\tau\mathbf{U}^{t,n}\cdot(1-\mathbf{S}^{t,n})+\mathbf{V}_{reset}\mathbf{S}^{t,n}, \end{cases} \tag{1}\]

where \(\tau\) is the time constant, \(t\) and \(n\) respectively represent the indices of the time step and the \(n\)-th layer, \(\mathbf{W}\) denotes synaptic weight matrix between two adjacent layers, \(f(\cdot)\) is the function operation stands for convolution or fully connected, \(\mathbf{X}\) is the input, and \(\mathbf{\Theta(\cdot)}\) denotes the Heaviside step function. When the membrane potential \(\mathbf{U}\) exceeds the firing threshold \(\mathbf{V}_{th}\), the LIF neuron will trigger a spike \(\mathbf{S}\). Moreover, \(\mathbf{H}\) represents the membrane potential after the trigger event which equals to \(\tau\mathbf{U}\) if no spike is generated and otherwise equals to the reset potential \(\mathbf{V}_{reset}\).

### Gated Attention Unit (GAU)

**Temporal Attention.** To establish temporal-wise relationships between SNNs' input, we first perform the squeezing step on the spatial-channel feature map of the repeated input \(\mathbf{X}\in\mathbb{R}^{T\times C\times H\times W}\), where \(T\) is the simulation time step and \(C\) is the channel size. Then we use Avgpool and Maxpool \(\in\mathbb{R}^{T\times 1\times 1\times 1}\) to calculate the maximum and average of the input in the last three dimensions. Additionally, we use a shared MLP network to turn both average-pooled and max-pooled features into a temporal weight vector \(\mathbf{M}\in\mathbb{R}^{T}\), i.e.,

\[\begin{split}\mathcal{F}_{T}(\mathbf{X})=&(\mathbf{W}_{m}( \mathrm{ReLU}(\mathbf{W}_{n}(\mathrm{AvgPool}(\mathbf{X}))))\\ &+\mathbf{W}_{m}(\mathrm{ReLU}(\mathbf{W}_{n}(\mathrm{MaxPool}(\mathbf{X}))) ),\end{split} \tag{2}\]

where \(\mathcal{F}_{T}(\cdot)\) is the functional operation of temporal attention. And \(\mathbf{W}_{m}\in\mathbb{R}^{T\times\frac{T}{r}}\), \(\mathbf{W}_{n}\in\mathbb{R}^{\frac{T}{r}\times T}\) are the weights of two shared dense layers. Moreover, \(r\) is the temporal dimension reduction factor used to manage its computing overhead.

**Spatial Channel Attention.** To generate the spatial channel dynamics for encoding result, we use a shared 2-D convolution

Figure 2: The GAC-SNN framework consists of two main components: an encoder and an architecture. In (a), we introduce the encoder, i.e., the GAC module. (b) focuses on the GAU, which acts as the fundamental building block of the GAC layer. It comprises Temporal Attention, Spatial Channel Attention, and Gating sub-modules. (c) Common SNN ResNet architectures. The Conv layer in SEW-ResNet uses a multiply-accumulate operator, not spike computations. Spiking-ResNet retains its spike-driven nature via direct coding, while GAC disrupts it. More details can be seen in discussions. MS-ResNet avoids floating-point multiplications, preserving its spike-driven nature. Hence, we use the MS-ResNet to benefit from neuromorphic implementations.
operation at each time step to get the spatial channel matrix \(\mathbf{N}=[\mathbf{N}^{1},\mathbf{N}^{2},\cdots,\mathbf{N}^{t}]\in\mathbb{R}^{T\times C\times H \times W}\), i.e.,

\[\mathcal{F}_{SC}(\mathbf{X})=\sum_{i=0}^{K-1}\sum_{j=0}^{K-1}\mathbf{W}_{i,j}\cdot\mathbf{X} _{i,j}^{t}, \tag{3}\]

where \(\mathcal{F}_{SC}(\cdot)\) is the functional operation of spatial channel attention, \(\mathbf{W}_{i,j}\) is the learnable parameter and \(K\) represents the size of the 2-D convolution kernel size.

**Gating.** After the above two operations, we get the temporal vector \(\mathbf{M}\) and spatial channel matrix \(\mathbf{N}\). Then to extract SNNs' input \(\mathbf{X}\) temporal-spatial-channel fused dynamics features, we first broadcast the temporal vector to \(\mathbb{R}^{T\times 1\times 1\times 1}\) and gating the above result by :

\[\mathcal{F}_{G}(\mathbf{X})=\sigma(\mathbf{M}\odot\mathbf{N})=\sigma(\mathcal{F}_{T}(\mathbf{ X})\odot\mathcal{F}_{SC}(\mathbf{X})), \tag{4}\]

where \(\sigma(\cdot)\) and \(\odot\) are the Sigmoid function and Hadamard Product. By the above three sub-modules, we can get the functional operation \(\mathcal{F}_{G}(\cdot)\) of GAU, which is the basic unit of the next novel coding.

### Gated Attention Coding (GAC)

Compared with the previous direct coding [23, 24], we introduce a novel encoding called Gated Attention Coding (GAC). And Fig. 2 describes the specific process. Given that the input \(\mathbf{X}\in\mathbb{R}^{C\times H\times W}\) of static datasets, we assign the first layer as an encoding layer. And we first use the convolution layer to generate features, then repeat this procedure after each time step and feed the results to the LIF model and the GAU module respectively. Finally, gating the output of the above two modules. So to this end, the whole GAC process can be described as:

\[\mathbf{O}=\mathcal{F}_{G}(f^{k\times k}(\mathbf{X}))\odot\mathcal{SN}(f^{k\times k} (\mathbf{X})), \tag{5}\]

where \(\mathbf{X}\) and \(\mathbf{O}\) is the GAC-SNN's input and output, \(f^{k\times k}(\cdot)\) is a shared 2-D convolution operation with the filter size of \(k\times k\), and \(\mathcal{SN}(\cdot)\) is the spiking neuron model. Moreover, \(\odot\) is the Hadamard Product, and \(\mathcal{F}_{G}(\cdot)\) is the functional operation of GAU, which can fuse temporal-spatial-channel information for better encoding feature expression capabilities.

### Overall Training Framework

We give the overall training algorithm of GAC for training deep SNNs from scratch with our GAC and spatio-temporal backpropagation (STBP) [23]. In the error backpropagation, we suppose the last layer as the decoding layer, and the final output \(\mathbf{K}\) can be determined by: \(\mathbf{K}=\frac{1}{T}\sum_{t=1}^{T}\mathbf{O}^{t}\), where \(\mathbf{O}^{t}\) is the SNNs' output of the last layer and \(T\) is the time steps. Then we calculate the cross-entropy loss function [11] between output and label, which can be described as:

\[q_{i}=\frac{e^{k_{i}}}{\sum_{j=1}^{n}e^{k_{j}}}, \tag{6}\]

\[\mathcal{L}=-\sum_{i=1}^{n}y_{i}log(q_{i}), \tag{7}\]

where \(\mathbf{K}\) = \((k_{1},k_{2},\cdots,k_{n})\) and \(\mathbf{Y}\) = \((y_{1},y_{2},\cdots,y_{n})\) are the output vector and label vector. Moreover, the codes of the overall training algorithm can be found in **Supplementary Material A.**

### Theoretical Analysis

To understand the highlights of our proposed method and the role of SNN encoders, we introduce the observer model for measuring SNN coding. Encoders are used to convert static images into feature sequences, incorporating temporal information into SNNs at each time step. Some encoders are embedded within the SNN as part of it (e.g., the first Conv-based spiking neuron layer for direct coding), while others are not included in the SNN models, e.g., rate coding. The embedded encoders can be easily distinguished from the rest of the network since they use actual values for linear transformations, unlike spikes or quantized data. Functionally, encoders convert static data into the temporal dimension. This definition helps us understand what SNN encoders are.

**Definition 1**.: _Encoder. An encoder in SNNs for image classification tasks is used to translate static input \(\mathbf{X}\in\mathbb{R}^{C_{in}\times H\times W}\) into dynamics feature sequences \(\mathbf{A}\in\mathbb{R}^{T\times C_{out}\times H\times W}\)._

Moreover, two points should be noted in **Definition** 1. Firstly, \(\mathbf{A}\) is used to indicate the encoders' output no matter spikes or real values. And Membrane Shortcut (MS) ResNet architecture [23] is considered to use sequential real values as input after the encoder. Secondly, although two dimensions (\(C_{out},T\)) are changed, the spatial one \(\bar{C}_{out}\) is similar to the operation in ANNs, which means \(T\) is unique for SNN encoders. In other words, the time step \(T\) is the secret of the time information, and the SNN encoder is designed to generate this added dimension. The discussion above implies that to understand and metric an encoder, we should focus on its temporal dimension and find a proper granularity.

**Definition 2**.: _Neuron Granularity. Considering \(\mathbf{A}\in\mathbb{R}^{T\times C_{out}\times H\times W}\) is output feature sequences of the encoder, given a fixed position \(c,h,w\) for the output \(\mathbf{A}\), so that we get a vector along temporal axis \(a=\mathbf{A}_{\cdot,c,h,w}=\left[a^{1},a^{2},\cdots,a^{t}\right]\)._

Here the encoding feature vector \(a\) is not subscripted in **Definition** 2 because the encoder is usually symmetric, and the choice of position for analysis does not affect its generality. To measure the vector \(a\), we introduce the observer model and information entropy [10]. Assuming an observer is positioned right behind the encoder, recording and predicting the elements of vector \(a\) in a time-ordered manner. Hence, the observer model can be formally established as follows:

* The observer notices the elements of encoded feature sequences \(a\in\mathbb{R}^{T}\) or \(\{0,1\}^{T}\) in time step order.
* At any time step \(t\), the observer remembers all elements from time \(1\) to time \(t-1\), and it is guessing the element \(a^{t}\) of encoded feature sequences \(a\in\mathbb{R}^{T}\).
* The observer is aware of the mechanism or the encoder structure, but not its specific parameters.

Moreover, at time step \(t\), guessing \(a^{t}\), the observer should answer it with probability. The probability can be described as:

\[p^{t}(a^{t})=p(a^{t}|a^{1},\cdots,a^{t-1}), \tag{8}\]

And we use the information entropy to meter the quantity of information gotten by the observer, i.e.,

\[\mathcal{H}(\mathbf{V}^{t})=\sum_{t}p^{t}(a^{t})\log(p^{t}(a^{t})), \tag{9}\]
where \(\mathbf{V}^{t}\) is used to indicate the random variable version of \(a^{t}\). Specifically, when \(p^{t}(a^{t})=0\) or 1, \(p^{t}(a^{t})\log(p^{t}(a^{t}))=0\) it means that a deterministic event contribute \(0\) to information entropy. Moreover, for the observer model, when the element \(a^{t}\) is deterministic, there is no additional information that deserves observing at time \(t\).

To better understand the concept of information entropy in this context, it is crucial to consider the role of an encoder whose task is to convert information into tensors that generate temporal dynamics. Ideally, the encoder should utilize as numerous time steps as possible to code information, resulting in a positive information entropy along the time axis. The positive entropy indicates the presence of information, which is crucial for spiking neural networks. While it is difficult to assign a precise value to the entropy, it is possible to measure the duration of positive entropy. In this way, longer-lasting positive entropy can be considered a more effective use of the temporal dimension.

**Definition 3**.: _Dynamics Loss & Dynamics Duration. Considering a specific position with encoded feature vector \(a=[a^{1},a^{2},\cdots,a^{t}]\) alone temporal dimension, if there exists \(t_{e}\) for all \(t\), \(t>t_{e}\), the observer mentioned above have an entropy \(\mathcal{H}(\mathbf{V}^{t})=0\). Then we call the moment \(t\) after \(t_{e}\) is Dynamics Loss. And for \(\mathbf{T}_{e}=\inf\left(t_{e}\right)\), we call it Dynamics Duration._

**Definition 3** delineates the encoder's effective encoding range. Dynamics Duration indicates when coding entropy \(\mathcal{H}(\mathbf{V}^{t})\geq 0\). At Dynamics Loss time steps, the entropy \(\mathcal{H}(\mathbf{V}^{t})\) drops to 0, rendering encoding unnecessary. Moreover, to metric the encoder, the key is to find and compare the dynamics duration time step \(\mathbf{T}_{e}\).

**Proposition 1**.: _Given same \(\{\textit{Conv-BN}\}\) parameters, denoting the dynamic duration of GAC as \(\mathbf{T}_{g}\) and direct coding's as \(\mathbf{T}_{d}\), and \(\mathbf{T}_{g}\geq\mathbf{T}_{d}\)_

Proof.: Denoted that \(\mathbf{X}\in\mathbb{R}^{T\times C\times H\times W}\) is the repetitive output after \(\{\textit{Conv-BN}\}\) module, \(a=[a^{1},a^{2},\cdots,a^{t}]\) is the encoded feature vector.

For direct coding, it sends the repetitive output \(\mathbf{X}\) to the spiking neuron for coding, resulting in the encoded feature vector \(a\) being powerless and periodic with 0 or 1. Moreover, the period \(\mathbf{T}_{p}\) is:

\[\mathbf{T}_{p}=\lceil\log_{\tau}(1-\frac{\mathbf{V}_{th}(1-\tau)}{x_{i,j}})\rceil, \tag{10}\]

where \(\tau\) is the time constant, \(\mathbf{V}_{th}\) is the firing threshold and \(x_{i,j}\) is the pixel of the input \(\mathbf{X}\) after \(\{\textit{Conv-BN}\}\) module. Hence, the subsequent output is predictable when the observer has found the first spike. Thus, the direct coding's dynamic duration \(\mathbf{T}_{d}=\mathbf{T}_{p}\). Moreover, the derivation of \(\mathbf{T}_{p}\) and analysis of other coding schemes' dynamics can be found in **Supplementary Material B**.

For GAC, we multiplied the output of direct coding and GAU to expand the dynamic duration of the encoding results. Thus, the GAC's dynamic duration \(\mathbf{T}_{g}=\lfloor\frac{\mathbf{T}}{\mathbf{T}_{d}}\rfloor\mathbf{T}_{d}\). It can be seen that \(\mathbf{T}_{g}\geq\mathbf{T}_{d}\). 

According to **Proposition** 1, GAC lasts its dynamics longer than direct coding. Moreover, this reflects the superiority of GAC in generating dynamic encoding results. As depicted in Fig. 3, GAC's encoding results on static datasets vary significantly at each time step, i.e., temporal dynamics.

### Theoretical Energy Consumption Calculation

The GAC-SNN architecture can transform matrix multiplication into sparse addition, which can be implemented as addressable addition on neuromorphic chips. In the encoding layer, convolution operations serve as MAC operations that convert analog inputs into spikes, similar to direct coding-based SNNs [22]. Conversely, in SNN's architecture, the convolution (Conv) or fully connected (FC) layer transmits spikes and performs AC operations to accumulate weights for postsynaptic neurons. Additionally, the inference energy cost of GAC-SNN can be expressed as follows:

\[\begin{split} E_{total}=E_{MAC}\cdot FL_{conv}^{1}+\\ E_{AC}\cdot T\cdot(\sum_{n=2}^{N}FL_{conv}^{n}\cdot fr^{n}+ \sum_{m=1}^{M}FL_{fc}^{m}\cdot fr^{m}),\end{split} \tag{11}\]

where \(N\) and \(M\) are the total number of Conv and FC layers, \(E_{MAC}\) and \(E_{AC}\) are the energy costs of MAC and AC operations, and \(fr^{m}\), \(fr^{n}\), \(FL_{conv}^{n}\) and \(FL_{fc}^{m}\) are the firing rate and FLOPs of the \(n\)-th Conv and \(m\)-th FC layer. Previous SNN works [16, 17] assume 32-bit floating-point implementation in 45nm technology, where \(E_{MAC}\) = 4.6pJ and \(E_{AC}\) = 0.9pJ for various operations.

## Experiments

In this section, we evaluate the classification performance of GAC-SNN on static datasets, e.g., CIFAR10, CIFAR100, ImageNet [15, 14]. To verify the effectiveness and efficiency of the proposed coding, we

Figure 3: Visualization results. (a) Original image. (b)(c)(d)(e) Encoding results of the direct coding (top) and GAC (bottom) at different time steps. Compared to direct coding, GAC enhances dynamics by introducing variations at each time step.
integrate the GAC module into the Membrane Shortcut (MS) ResNet [14], to see if the integrated architecture can generate significant improvement when compared with previous state-of-the-art works. Specifically, the details of the architecture are shown in Fig. 2-(c), and why we use it is illustrated in the discussions. More details of the training details, datasets, hyper-parameter settings, convergence analysis, and trainable parameter analysis can be found in **Supplementary Material B**.

**GAC Can Produce Powerful and Dynamics Results**

We evaluated GAC's effectiveness in reducing redundant temporal information and improving encoding results for static datasets. By training MS-ResNet-34 on ImageNet with and without GAC, we generated the encoding output shown in Fig. 3. And it can be seen that our GAC can help SNNs to capture more texture information. Hence, our approach enhances SNNs' representation ability and temporal dynamics by introducing significant variations in GAC results at each time step, compared to the periodic output of direct coding.

**GAC Can Get Effective and Efficient SNNs**

**Effectiveness.** The GAC-SNN demonstrate remarkable performance enhancement compared to existing state-of-the-art works (Tab. 1-2). On CIFAR10 and CIFAR100, GAC achieves higher accuracy than previous methods using only 2-time steps. With the same time steps, GAC improves 1.43% and 3.10% on CIFAR10 and CIFAR100 over GLIF [23]. Moreover, compared to the baseline MS-ResNet [14], our method outperforms it on CIFAR10 and CIFAR100 by 1.54% and 4.04% with 6-time steps. For the larger and more challenging ImageNet dataset, compared with the baseline MS-ResNet [14], we apply our GAC to MS-ResNet-18 and can significantly increase the accuracy (65.14% v.s. 63.10%). Compared with other advanced works [19, 23], GAC-based MS-ResNet-34 achieves 70.42% top-1 accuracy and surpasses all previous directly-trained SNNs with the same depth.

**Efficiency.** Compared with prior works, the GAC-SNN shine in energy consumption (Tab. 2). We first make an intuitive comparison of energy consumption in the SNN field. Specifically, GAC-SNN (This work) vs. SEW-ResNet-34 at 4-time steps: Power, 2.20mJ vs. 4.04mJ. That is, our model has +3.22% higher accuracy than SEW-ResNet with only the previous 54.5% power. And GAC-SNN (This work) vs. MS-ResNet-34 vs. Att-MS-ResNet-34 at 6-time steps: Power, 2.34mJ vs. 4.29mJ vs. 4.11mJ. That is, Our model has the lowest power under the same structured and time steps. For instance, as the layers increase from 18 to 34, MS ResNet (baseline) has 1.83x(4.29mJ/2.34mJ) and 1.51x(5.11mJ/3.38mJ) higher energy consumption than our GAC-SNN. At the same time, our task performance on the above same depth network structures has improved by +2.04% and +0.83%, respectively.

### Ablation study

**Comparison between Different SNN Coding Schemes.** To future demonstrate the advantage of GAC, we evaluate the performance of our GAC and other coding schemes e.g., Phase coding [15], Temporal coding [20], Rate coding [20], Direct coding [20]. Tab. 3 displays CIFAR10 test accuracy, where GAC achieves 96.18% top-1 with MS-ResNet-18 in 6-time steps.

**The Effect of Parameter Kernel Size \(K\).** We investigate the impact of the 2D convolution kernel size \(K\) in the Spatial Channel Attention module of our GAC. Specifically, there is a trade-off between performance and latency as kernel size increases. It is almost probable that when kernel size increases, the receptive region of the local attention mechanism also does so, improving SNN performance. These benefits do, however, come at a cost of high parameters and significant latency. To this end, we trained the GAC-based MS-ResNet-18 on CIFAR100 with various \(K\) values. As shown in Fig. 4-(b), accuracy rises with increasing \(K\), plateauing after \(K\) exceeds 4. This indicates our GAC maintains strong generalization

\begin{table}
\begin{tabular}{c c c c c c} \hline \hline \multirow{2}{*}{Methods} & \multirow{2}{*}{Architecture} & Params & Time & CIFAR10 & CIFAR100 \\  & & (M) & Steps & Acc.(\%) & Acc.(\%) \\ \hline ANN2SNN [1] & VGG-16 & 33.60/33.64 & 32 & 95.42 & 76.45 \\ tdBN [1]\({}^{AAAI}\) & Spiking-ResNet-19 & 12.63/12.67 & 6 & 93.16 & 71.12 \\ TET [1] & Spiking-ResNet-19 & 12.63/12.67 & 6 & 94.50 & 74.72 \\ GLIF [23]\({}^{NeurIPS}\) & Spiking-ResNet-19 & 12.63/12.67 & 6 & 95.03 & 77.35 \\ \hline MS-ResNet\({}^{\ddagger}\)[14] & MS-ResNet-18 & 12.50/12.54 & 6 & 94.92 & 76.41 \\ \cline{2-6}  & MS-ResNet-18 & 12.63/12.67 & 6 & **96.46**\(\pm\)0.06 & **80.45**\(\pm\)0.27 \\
**GAC-SNN** & MS-ResNet-18 & 12.63/12.67 & 4 & 96.24\(\pm\)0.08 & 79.83\(\pm\)0.15 \\  & MS-ResNet-18 & 12.63/12.67 & 2 & 96.18\(\pm\)0.03 & 78.92\(\pm\)0.10 \\ \hline ANN\({}^{\ddagger}\)[14] & MS-ResNet-18 & 12.50/12.54 & N/A & 96.75 & 80.67 \\ \hline \hline \end{tabular}
\end{table}
Table 1: Comparison between the proposed methods and previous works on CIFAR datasets. \({}^{\ddagger}\) denote self-implementation results with open-source code [14].The "Params” column indicates network parameter size on CIFAR10/100 datasets.

Figure 4: Ablation study on CIFAR100.
despite large \(K\) variations. To maintain excellent performance and efficiency, we consider employing \(K=4\) in our work.

**Comparison of Different Attention.** We conducted ablation studies on the Temporal Attention (TA) and Spatial Channel Attention (SCA) modules to assess their effects. Fig. 4-(a) indicates that the SCA module contributes more to performance improvement due to the most SNNs design that channels outnumber time steps. The SCA module extracts additional significant features compared to the TA module. Notably, regardless of the module we ablate, performance will be affected, which may help you understand our design.

## Discussions

**Analysis of Different GAC-SNN's ResNet Architecture.** Residual connection is a crucial basic operation in deep SNNs' ResNet architecture. And there are three shortcut techniques in existing advanced deep SNNs. Spiking-ResNet [12] performs a shortcut between membrane potential and spike. Spike-Element-Wise (SEW) ResNet [13] employs a shortcut to connect the output spikes in different layers. Membrane Shortcut (MS) ResNet [12], creating a shortcut between membrane potential of spiking neurons in various layers. Specifically, we leverage the membrane shortcut in the proposed GAC for this reason:

_Spike-driven_ describes the capacity of the SNNs' architecture to convert matrix multiplication (i.e., high-power Multiply-Accumulation) between weight and spike tensors into sparse addition (i.e., low-power Accumulation). The spike-driven operations can only be supported by binary spikes. However, as the SEW shortcut creates the addition between binary spikes, the values in the spike tensors are multi-bit (integer) spikes. Additionally, GAC-based Spiking-ResNet is not entirely spike-driven. Because the second layer convolution operation's input changes to a floating-point number when using GAC, the input for the other layers remains a spike tensor. By contrast, as shown in Fig. 2-(c), spiking neurons are followed by the MS shortcut. Hence, each convolution layer in GAC-based MS-ResNet architecture will always get a sparse binary spike tensor as its input.

**Impact of GAC and Other SNNs' Attention Methods on the Spike-driven Nature.** As shown in Fig. 1-(b), other SNN-oriented attention works [23, 20] adding an attention mechanism to the SNNs architecture need to design numerous multiplication blocks and prevent all matrix multiplications related to the spike matrix from being converted into sparse additions, which hinders the implementation of neuromorphic hardware. However, adding an attention mechanism in the encoder doesn't. As the encoder and architecture are decoupled in SNN hardware design [10], our GAC, like direct coding [12], incorporates the multiplication block for analog-to-spike conversion in the encoder without impacting the spike-driven traits of the sparse addition SNN architecture.

## Conclusion

This paper focuses on the SNNs' coding problem, which is described as the inability of direct coding to produce powerful and temporal dynamic outputs. We have observed that this issue manifests as periodic powerless spike representation due to repetitive operations in direct coding. To tackle this issue, we propose Gated Attention Coding (GAC), a spike-driven and neuromorphic hardware-friendly solution that seamlessly integrates with existing Conv-based SNNs. GAC incorporates a multi-dimensional attention mechanism inspired by atten

\begin{table}
\begin{tabular}{c c c c c c c} \hline \hline Methods & Architecture & Spike -driven & Params (M) & Time Steps & Power (mJ) & Top-1 Acc.(\%) \\ \hline ANN2SNN [12]\({}^{AAAI}\) & ResNet-34 & ✓ & 21.79 & 64 & - & 68.61 \\ TET [13]\({}^{ICLR}\) & Spiking-ResNet-34 & ✓ & 21.79 & 6 & - & 64.79 \\ tdBN [13]\({}^{AAAI}\) & Spiking-ResNet-34 & ✓ & 21.79 & 6 & 6.39 & 63.72 \\ SEW-ResNet [13]\({}^{NeurIPS}\) & SEW-ResNet-34 & ✗ & 21.79 & 4 & 4.04 & 67.04 \\ \hline MS-ResNet [12] & MS-ResNet-18 & ✓ & 11.69 & 6 & 4.29 & 63.10 \\  & MS-ResNet-34 & ✓ & 21.80 & 6 & 5.11 & 69.43 \\ \cline{2-6} Att-MS-ResNet [23]\({}^{TPAMI}\) & Att-MS-ResNet-18 & ✗ & 11.96(+0.27) & 6 & 4.11 & 64.15\({}^{*}\) \\ Att-MS-ResNet-34 & ✗ & 22.30(+0.50) & 6 & 5.05 & 69.35\({}^{*}\) \\ \cline{2-6}
**GAC-SNN** & MS-ResNet-18 & ✓ & 11.82(**+0.13**) & 6/4 & 2.34/**1.49** & **65.14**/64.05 \\  & MS-ResNet-34 & ✓ & 21.93(**+0.13**) & 6/4 & 3.38/**2.20** & **70.42**/69.77 \\ \hline ANN [12] & MS-ResNet-18 & ✗ & 11.69 & N/A & 14.26 & 69.76 \\  & MS-ResNet-34 & ✗ & 21.80 & N/A & 16.87 & 73.30 \\ \hline \hline \end{tabular}

* needs a large training time (1000 epochs and 600 batch size) compared to other methods.

\end{table}
Table 2: Comparison between the proposed method and previous works on the ImageNet dataset. Power is the average theoretical energy consumption when predicting a batch of images from the test set, details of which are shown in Eq.5. The "Spike-driven" column indicates if an independent design of the multiplication module is required in the SNNs architecture, which hinders the implementation of neuromorphic hardware.

\begin{table}
\begin{tabular}{c c c c} \hline \hline Architecture & Schemes & Time Steps & Acc.(\%) \\ \hline ResNet-19 & Phase Coding & 8 & 91.40 \\ VGG-16 & Temporal Coding & 100 & 92.68 \\ ResNet-19 & Rate Coding & 6 & 93.16 \\ MS-ResNet-18 & Direct Coding & 6 & 94.92 \\ \hline MS-ResNet-18 & GAC & 6 & **96.46\(\pm\)**0.06 \\ \hline \hline \end{tabular}
\end{table}
Table 3: Comparisons with different coding schemes.
tion mechanism and human dynamics vision in neuroscience. By effectively establishing sptiao-temporal relationships at each moment, GAC acts as a preprocessing layer and efficiently encodes static images into powerful representations with temporal dynamics while minimizing redundancy. Our method has been extensively evaluated through experiments, demonstrating its effectiveness with state-of-the-art results: CIFAR10 (96.46%), CIFAR100 (80.45%), and ImageNet (70.42%). We hope our investigations pave the way for more advanced coding schemes and inspire the design of high-performance and efficient spike-driven SNNs.

## Supplementary Material B

In this supplementary material B, we first present the hyperparameters settings and training details. Then we present the details of our architecture, training algorithm, and the analysis of the GAC's firing rate, the period of direct coding [21], and other coding schemes' dynamics. Moreover, we give additional results to depict the GAC's advantage.

## Hyper-Parameters Settings

In this section, we give the specific hyperparameters of the LIF model and training settings in all experiments, as depicted in Tab. 4 and Tab.5.

## Training Details

For all of our experiments, we use the stochastic gradient descent optimizer with 0.9 momentum and weight decay (e.g., CIFAR10 \(5e-4\) and ImageNet \(1e-4\) for 250 training epochs. The learning rate is set to 0.1 and cosine decay is to 0. All the training and testing codes are implemented using PyTorch [22]. The training is performed on eight NVIDIA V100 GPUs and 32GB memory for each task. The summaries of datasets and augmentation involved in the experiment are listed below.

**CIFAR 10/100** consist of 50k training images and 10k testing images with the size of 32 x 32 [16]. We use ResNet-18 for both CIFAR10 and CIFAR100. Random horizontal flips, crops, and Cutmix are applied to the training images for augmentation.

**ImageNet** contains around 1.3 million 1, 000-class images for training and 50, 000 images for validation [17]. The batch size and total epochs are set to 256. We crop the images to 224x224 and use the standard augmentation for the training data, which is similar to the Att-MS-ResNet[23]. Moreover, we use the label smoothing (7) to avoid gradient vanishing, which is similar to the Att-MS-ResNet [23].

## Architecture Details

The MS-ResNet-series architecture is the primary network architecture used in this work. There are numerous ResNet variations utilized in the field of SNNs, such as MS-ResNet-18, and MS-ResNet-34. They are also used interchangeably in existing literature [11, 24]. To avoid confusion in various ResNet architectures, we will sort out the architecture details in this part. Specifically, MS-ResNet-18 is originally designed for the ImageNet dataset in [11]. To process CIFAR dataset, we remove the max-pooling layer, replace the first 7 \(\times\) 7 convolution layer with a 3 \(\times\) 3 convolution layer, and replace the first and the second 2-stride convolution operations as 1-stride, following the modification philosophy from TET [4].

## Analysis of the Firing Rate

GAC controls membrane potentials and reduces spike firing rates of the post-encoding layer in GAC-SNN. It achieves by transforming matrix multiplication into sparse addition, which can be implemented as addressable addition on neuromorphic chips. Moreover, high sparsity is observed in GAC-based MS-ResNet, as shown in Fig. 5. The firing rate \(r\) is defined as the firing probability of each neuron per timestep and can be estimated by:

\[r=\frac{\#\text{Spike}}{\#\text{Neuron}\cdot T}, \tag{12}\]

Where #Spike denotes the number of spikes during \(T\) timesteps and #Neuron denotes the number of neurons in the network. Specifically, after applying the GAC technique, the average spike firing rate of MS-ResNet-18 decreased from **0.215** to **0.154**. Similarly, the release rate of MS-ResNet-34

\begin{table}
\begin{tabular}{c|c|c|c|c} \hline Stage & Output Size & ResNet-18 & ResNet-34 \\ \hline Conv1 & 112x112 & \multicolumn{2}{c|}{7x, 64, stride=2} \\ \hline Conv2 & 56x56 & \begin{tabular}{c} [3x3, 64 \\ 3x3, 64 \\ \end{tabular} & \(*2\) & \begin{tabular}{c} [3x3, 64 \\ 3x3, 64 \\ \end{tabular} & \(*2\) \\ \hline Conv3 & 28x28 & \begin{tabular}{c} [3x3, 128 \\ 3x3, 128 \\ \end{tabular} & \(*2\) & \begin{tabular}{c} [3x3, 128 \\ \end{tabular} \\ \hline Conv4 & 14x14 & \begin{tabular}{c} [3x3, 256 \\ 3x3, 256 \\ \end{tabular} & \(*2\) & \begin{tabular}{c} [3x3, 256 \\ \end{tabular} \\ \hline Conv5 & 7x7 & \begin{tabular}{c} [3x3, 512 \\ \end{tabular} & \(*2\) & 
\begin{tabular}{c} [3x3, 512 \\ \end{tabular} \\ \hline FC & 1x1 & \multicolumn{2}{c}{AveragePool, FC-1000} \\ \hline \end{tabular}
\end{table}
Table 6: MS-ResNet-series architecture for ImageNet.

\begin{table}
\begin{tabular}{c c} \hline Parameter & Value \\ \hline Threshold \(\mathbf{V}_{th}\) & 0.5 \\ Reset potential \(\mathbf{V}_{reset}\) & 0 \\ Decay factor \(\tau\) & 0.25 \\ Surrogate function’s window size \(a\) & 1 \\ \hline \end{tabular}
\end{table}
Table 4: Hyper-parameter setting on LIF model.

\begin{table}
\begin{tabular}{c c c c} \hline Parameter & CIFAR10 & CIFAR10 & ImageNet \\ \hline Learning Rate & \(5e-4\) & \(5e-4\) & \(1e-4\) \\ Batch Size & 64 & 64 & 256 \\ Time steps & 6/4/2 & 6/4/2 & 6/4 \\ Training Epochs & 250 & 250 & 250 \\ \hline \end{tabular}
\end{table}
Table 5: Hyper-parameter training settings of GAC-SNNs.
reduced from **0.225** to **0.1729**. These reductions indicate that GAC has effectively contributed to reducing the spike firing rates in both MS-ResNet-34 and MS-ResNet-104 models. Lower spike firing rates can be beneficial in terms of reducing computational load, and energy consumption, and potentially improving the overall efficiency of the models.

**Derivation of the direct coding period \(\mathbf{T}_{p}\)**

For direct coding, the real value data repeatedly pass through the {_Conv-BN_} layer for linear transformations and then the LIF model for \(\{0,1\}\) encoding results. Since the input of the LIF model is the same for every time step, it is trivial that the output of the LIF model in the encoding layer is periodical and the information of input (real value) is encoded into the period \(\mathbf{T}_{p}\). Suppose the reset value is \(0\) and the threshold is \(\mathbf{V}_{th}\), the time \(\mathbf{T}_{p}\) is expressed as follow:

\[\mathbf{T}_{p}=\lceil\log_{\tau}(1-\frac{\mathbf{V}_{th}(1-\tau)}{x_{i,j}})\rceil, \tag{13}\]

Here \(\tau\) is the attenuation factor and \(x_{i,j}\) is the pixel of the input image of the LIF model after the {_Conv-BN_} module. \(\mathbf{T}_{p}\) is the first time step of firing and the period of firing for the corresponding LIF model. It is trivial that \(T\) increases monotonically with \(x\) and the resolution of the encoding depends on the value of \(x\). The most important thing is that after the LIF model, the direct coding is period coding, making the \(0,1\) output periodical. Moreover, the derivation of the direct coding's period is as follows: Suppose when \(t=0\), the membrane potential \(\mathbf{U}^{t}=0\), and the neuron fires at time step \(t=\mathbf{T}_{p}\). According to the direct coding, the input of any specific neuron is \(x_{i,j}\) (a constant). Since the threshold and the attenuation factor are separately denoted as \(\mathbf{V}_{th}\) and \(\tau\).According to the iterative formula of the LIF model before the spike fire time, we have:

\[\mathbf{U}^{t}=\tau\mathbf{U}^{t-1}+x_{i,j}=\sum_{k=0}^{t-1}\tau^{k}x_{i,j}=x_{i,j} \sum_{k=0}^{t-1}\tau, \tag{14}\]

Also, we have:

\[\mathbf{U}^{T_{p}-1}\leq\mathbf{V}_{th}\leq\mathbf{U}^{T_{p}}, \tag{15}\]

Bringing 14 to 15, we have:

\[x_{i,j}\sum_{k=0}^{T_{p}-2}\tau\leq\mathbf{V}_{th}\leq x_{i,j}\sum_{ k=0}^{T_{p}-1}, \tag{16}\] \[(1-\tau^{T_{p}-1})\frac{x}{1-\tau}\leq\mathbf{V}_{th}\leq(1-\tau^{T_{ p}})\frac{x_{i,j}}{1-\tau},\] (17) \[\tau^{T_{p}}\leq 1-\frac{\mathbf{V}_{th}(1-\tau)}{x_{i,j}}\leq\tau^{T_{ p}-1},\] (18) \[T_{p}-1\leq\log_{\tau}(1-\frac{\mathbf{V}_{th}(1-\tau)}{x_{i,j}}) \leq T_{p},\] (19) \[T_{p}=\lceil\log_{\tau}(1-\frac{\mathbf{V}_{th}(1-\tau)}{x_{i,j}})\rceil, \tag{20}\]

### Analysis of other Coding Schemes' Dynamics

In this section, we use the observer model to analyze the dynamics of common coding schemes such as rate coding [20] and coding scheme used in MS-ResNet [14]. Hence we give the following two propositions to describe, i.e.,

**Proposition 2**.: _For rate coding with positive value \(x\in(0,1)\), giving the total timestep \(\mathbf{T}\), denoting dynamics duration of rate coding as \(\mathbf{T}_{r}\), then \(\mathbf{T}_{r}=\mathbf{T}\)._

Proof.: For rate coding, the output in each timestep follows the Bernoulli distribution of \(n=1,p=x\), and is independent of each other at different time steps (that is, independent and identically distributed). Therefore, for any timestep \(t\), information entropy \(H(\mathbf{V}^{t})=x\log(x)+(1-x)\log(1-x)>0\). This means that all-time step encoding information is dynamic. 

**Proposition 3**.: _For MS coding, denoting dynamics duration of rate coding as \(\mathbf{T}_{m}\), then \(\mathbf{T}_{m}=\mathbf{1}\)._

Proof.: For MS coding, the information for each timestep is repeated. Thus, it is predictable after the first time of observation. Therefore, \(T_{m}=1\). 

According to **Proposition**1, it is known that rate coding has all-time step dynamics. However, it suffers from long time steps to remain high performance, while small time steps result in lower representation resolution. According to **Proposition**2, MS coding results are the same at every time step. And our GAC-based MS-ResNet improves temporal dynamics and encoding efficiency through an attention mechanism.

## Training Algorithm to Fit Target Output

In this section, we introduce the training process of SNN gradient descent and the parameter update method of STBP [14]. SNNs' parameters can be taught using gradient descent techniques, just like ANNs, after determining the derivative of the spike generation process. Classification, as well as other tasks for both ANNs and SNNs, can be thought of as optimizing network parameters to meet a goal output when given a certain input. Moreover, the accumulated gradients of loss \(\mathcal{L}\) with respect to weights \(\mathbf{W}_{n}^{j}\) at layer \(n\) can be calculated as:

Figure 5: Firing rate advantage on the ImageNet dataset.
\[\begin{cases}\frac{\partial\mathcal{C}}{\partial\mathcal{S}_{i}^{t,n}}=\sum_{j} \frac{\partial\mathcal{C}}{\partial U_{j}^{t,n+1}}\frac{\partial U_{j}^{t,n+1} }{\partial\mathcal{S}_{i}^{t,n}}+\frac{\partial\mathcal{C}}{\partial U_{i}^{t +1,n}}\frac{\partial U_{j}^{t+1,n}}{\partial\mathcal{S}_{i}^{t,n}}\\ \frac{\partial\mathcal{C}}{\partial U_{i}^{t,n}}=\frac{\partial\mathcal{C}}{ \partial\mathcal{S}_{i}^{t,n}}\frac{\partial\mathcal{S}_{j}^{t,n}}{\partial U _{i}^{t,n}}+\frac{\partial\mathcal{C}}{\partial U_{i}^{t+1,n}}\frac{\partial U _{j}^{t+1,n}}{\partial U_{j}^{t,n}}\\ \frac{\partial\mathcal{C}}{\partial\mathcal{W}_{n}^{t}}=\sum_{t=1}^{T}\frac{ \partial\mathcal{C}}{\partial U_{i}^{t,n+1}}\mathcal{S}_{j}^{t,n},\end{cases} \tag{21}\]

where \(\mathcal{S}^{t,n}\) and \(\mathcal{U}_{j}^{t,n}\) represent the binary spike and membrane potential of the neuron in layer \(n\), at time \(t\).

Moreover, notice that \(\frac{\partial\mathcal{S}^{t,n}}{\partial\mathcal{U}^{t,n}}\) is non-differentiable. To overcome this problem, Wu et al. (2018) proposes the surrogate function to make only the neurons whose membrane potentials close to the firing threshold receive nonzero gradients during backpropagation. In this paper, we use the rectangle function, which has been shown to be effective in gradient descent and may be calculated by:

\[\frac{\partial\mathcal{S}^{t,n}}{\partial\mathcal{U}^{t,n}}=\frac{1}{a}\operatorname {sign}\left(\left|\mathcal{U}^{t,n}-\boldsymbol{V}_{\mathrm{th}}\right|<\frac{ a}{2}\right), \tag{22}\]

where \(a\) is a defined coefficient for controlling the width of the gradient window.

## Additional Results

### Convergence Analysis

We empirically demonstrate the convergence of our proposed method. As shown in Fig. 6, the performance of our GAC-based MS-ResNet stabilizes and converges to a higher level compared to MS-ResNet as training epochs increase. Moreover, the GAC-SNN achieves state-of-the-art performance after only 20 epochs, demonstrating its efficacy.

### Trainable Parameter Analysis

We also give the effects of the Temporal Attention (TA), Spatial Channel Attention (SCA), and Gated Attention Unit (GAU) on the increase of model parameters, as shown in Tab. 7. First, with different datasets, the proportion of different modules to the number of model parameters varies. The increase of SCA and GAU to the number of parameters is much higher than that of TA. This phenomenon is consistent with our expectations that given that the channel size in the dataset is much larger than the simulation time step, indicating that SCA has a lot of parameters.

### More Visualization Results on GAC

To further illustrate the advantages of our GAC, we provide the visualization results (i.e., model's GradCAM (Selvaraju et al., 2017)) on MS-ResNet-34 with GAC or direct coding, as shown in Fig. 7 to help understand our design. And it can be seen in GradCAM heat map results that our GAC can help SNNs to capture more texture information.
