## 3 Methods
### A CNN-BiLSTM Method with Multi-Head Self-Attention
#### 3.4.3. Muti-Head Self-Attention in the Transformer  
The Transformer model [21] is an autoregressive generative model that uses mainly self-attentive mechanisms and sinusoidal location information. Each layer includes a time-self-noticing sublayer, a pre-feedback network sublayer, a residual network sublayer, and a dropout layer.  
Attention essentially assigns a weighting factor to each element in the EEG sequence, and if each element is stored, attention can be calculated as the similarity between \(Q\) and \(K\). The similarity calculated from \(Q\) and \(K\) reflects the importance of the extracted \(V\) values, the weights, which are then weighted and summed to obtain the attention values. The special point of the self-attention mechanism in the \(K\), \(Q\), \(V\) model is that \(Q=K=V\). The scaled dot product attention formula is defined as Equation (10):  
\[Attention(Q,K,V)=softmax(\frac{QK^{T}}{\sqrt{d_{k}}})V \tag{10}\]  
The multi-headed self-attention mechanism obtains different representations of h (i.e., each head) of (\(Q\), \(K\), \(V\)), calculates the self-attention of each representation, and connects the results. This can be expressed in the same notation as in Equations (11) and (12):  
\[head_{i}=Attention(QW_{i}^{Q},KW_{i}^{K},VW_{i}^{V}), \tag{11}\]  
\[MultiHead(Q,K,V)=Contact(head_{i},\dots,head_{h})W^{0}, \tag{12}\]  
where \(W_{i}\) and \(W^{0}\) make the parameter matrix; the structure of the multi-headed self-attentive mechanism is shown in Figure 4.  
The BiLSTM contains two outputs, the output \(O=[O_{1},O_{2},O_{3},\dots,O_{D}]\) for all time steps and the hidden state \(H_{D}\) for the last time step \(D\). Since \(O=[O_{1},O_{2},O_{3},\dots,O_{D}]\) denotes the EEG sentiment feature and \(H_{D}\) denotes the hidden temporal feature, to identify the importance of time on sentiment, we need to establish the self-attention relationship between \(H_{D}\) and \(O_{t}\). That is, to establish the weight of each time step output \(O_{t}\) for \(H_{D}\)  
Figure 4: Multi-Head Self Attention structure.  
Since BiLSTM itself considers the location information, there is no need to set up additional location encoding. The implementation method of the self-attention mechanism in BiLSTM we use is scaled dot product attention [21], which is the self-attention implementation method proposed by the transformer.  
The output \(O_{t}\) of each time step is linearly transformed as \(Key_{t}\) and \(Value_{t}\), while the output \(H_{D}\) of the final time step is multiplied by the matrix \(W_{Q}\) as \(Query\). at time step \(t\), \(Key_{t}\), \(Value_{t}\), \(Query\), score \(e\) and weight a have the following equations:  
\[Query=\omega_{Q}H_{D}, \tag{13}\]  
\[Value_{t}=\omega_{V}O_{t}, \tag{14}\]  
\[Key_{t}=\omega_{K}O_{t}, \tag{15}\]  
\[e_{t}=QueryKey_{t}^{T}/\sqrt{d_{k}}, \tag{16}\]  
\[a_{t}=\frac{exp(e_{t})}{\sum_{t=0}^{n}exp(e_{t})}, \tag{17}\]  
where \(Query\) does not change with time step and \(\omega_{Q}\), \(\omega_{K}\), \(\omega_{V}\) are parameters of the neural network that are modified with backpropagation. The weights \(a_{t}\) and \(Value_{t}\) of each time step are weighted and summed to obtain the emotional feature vector with self-attentiveness:  
\[z(Q,K,V)=\sum_{t}a_{t}Value_{t} \tag{18}\]  
To obtain the multi-headed self-attention, the above equation is performed h times to obtain the multi-headed self-attention feature \(z_{1},\dots,z_{h}\), which is spliced and linearly transformed once as the final output:  
\[MultiHead(Q,K,V)=Concat(z_{1},\dots,z_{h})\omega_{z} \tag{19}\]