## 2 Proposed Methods
### Gated Self-Attention Block  
After detecting local spatiotemporal patterns from 3D EEG cuboid representations of each frequency band using the 3D Conv block, a gated self-attention block is applied to obtain salient EEG features related to stress response and to capture both long-range dependencies and global information using nonlocal operations. Typically, the attention mechanism [25] is used for identifying important patterns in the input using a weight matrix. Long short-term memory (LSTM) and GRU have been used, but recently, they are being replaced by transformers. In deep learning, the self-attention mechanism is a key component of the transformer that can be applied to an entire sequence. This helps generate better sequence representations by learning task-specific relationships between different elements of a specific sequence. The self-attention module computes attention weights by generating three linear projections (Key, Value, and Query) of an input sequence, and mapping those weights to the input sequence [26]. Key and Value are the characteristics of the 3D representation extracted from each convolution block, whereas Query determines the value to be focused on during the learning process. In this study, Key, Query, and Value are converted into vectors using a \(1\times 1\times 1\) convolution filter and are expressed as \(k(x)\), \(q(x)\), and \(v(x)\), respectively. \[Key:\quad k(x) =W_{k}x, \tag{1}\] \[Query:\;\;q(x) =W_{q}x,\] (2) \[Value:\;\;v(x) =W_{0}x, \tag{3}\]  
where \(x\in R^{(U\times N)}\) denotes the feature of the previous layer, \(U\) indicates the number of channels, and \(N\) represents the number of feature positions in the previous layer; \(W_{k}\), \(W_{q}\), and \(W_{q}\) denote the \(1\times 1\times 1\) convolution filters. Based on the similarity between Key and Query, the self-attention map \(a_{ij}\) representing the attention weight is calculated as  
\[a_{i,j}=\frac{\exp(k(x_{i})^{T}q(x_{j}))}{\sum_{i=1}^{n}\exp(k(x_{i})^{T}q(x_{j}) )}, \tag{4}\]  
where \(a_{i,j}\) denotes the weight of the week representing the relative interest between each domain \(i\) and all other domains, \(j\) indicates the index of the output position, and \(c\in\left(c_{1},\,c_{2},\cdots,\,c_{j}\right)\in R^{(I\times N)}\) represents the output of the attention layer, computed as:  
\[c_{j}=W_{c}(\sum_{i=1}^{N}a_{i,j}v(x_{i})), \tag{5}\]  
In Equation (5), a 1 \(\times\) 1 \(\times\) 1 convolution filter (\(W_{c}\)) is used to keep the number of channels identical to that of the original input and ensure memory efficiency, thereby reducing the number of channels in the final output. However, this attention mechanism suppresses unnecessary information to learn and focus on important areas from the entire 3D representation, which results in total information loss. To address this problem, a residual connection is added. Typically, residual connections solve the gradient decay/explosion problem by easing the propagation of the gradient as the network becomes deeper. The residual network [27] is built out of modules called residual blocks, which can be formulated as  
\[y=F^{b}+R\left(F^{b}\right), \tag{6}\]  
where \(y\) denotes the output of the residual module, \(F^{b}\) indicates the input, and \(R(F^{b})\) represents the output obtained by applying residual concatenation to the input sequence \(F^{b}\). This module is comprised of two 3 DConvBs with a 3 \(\times\) 3 Conv3D layer, 3D batch normalization, and ReLU. At the end of this original residual module, a self-attention layer is introduced to ensure that global information can be efficiently captured without being limited to local residual learning. Accordingly, the output of the residual attention (RA) block can be expressed as  
\[y=F^{b}+R\left(F^{b}\right)+\gamma A\left(R\left(F^{b}\right)\right), \tag{7}\]  
where \(A(R(F^{b}))\) indicates the output of the self-attention map, and \(\gamma\) denotes a learnable parameter. By default, \(\gamma\) is set to 0 to ensure that the network can only rely on signals from local neighbors.