Motor imagery electroencephalogram signal recognition algorithm based on mirror image contrast loss and sliding window Transform
Through the combination of mirror contrast loss and sliding window Transformer model, the problem of deep learning model locating event-related desynchronization/synchronization phenomenon in motor imagery EEG signal recognition is solved, the recognition performance is improved and the computational complexity is reduced, achieving Efficient extraction of EEG signal features.
Patent Information
- Application Number
- CN202410863374.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-29
- Publication Date
- 2024-10-11
AI Technical Summary
It is difficult for existing deep learning models to accurately locate event-related desynchronization/synchronization phenomena in motor imagery EEG signal recognition, and the computational complexity of the global multi-head self-attention mechanism of the Transformer model increases with the length of the input sequence, limiting feature extraction. time resolution.
A motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer is used to generate mirror signals by exchanging the EEG channels of the left and right hemispheres of the brain. The model is enhanced by combining mirror contrast loss and a sliding time window multi-head self-attention mechanism. Ability to localize event-related desynchronization/synchronization phenomena and reduce computational complexity.
It improves the localization ability of event-related desynchronization and synchronization phenomena in EEG signals, solves the problem of sequence length limitation in the Transformer model, and improves the performance of motor imagery EEG signal recognition and the robustness of the model.
Smart Images

Figure CN118760925A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of motor imagery EEG signal recognition, and relates to a motor imagery EEG signal recognition algorithm based on mirror contrast loss and a sliding window Transformer. Background Art
[0002] Brain-computer interface technology promotes the integration of biological intelligence and artificial intelligence by establishing a direct communication channel between the brain and the computer. This technology allows users to interact directly with external devices through cognitive processes, thus providing broad prospects for applications such as motor function rehabilitation and assistive device control. Among various brain-computer interface paradigms, motor imagery brain-computer interface has become a research focus due to its non-invasiveness and operational flexibility. Motor imagery EEG signals reflect non-invasive endogenous brain activity by capturing the EEG signals of users when performing motor imagery tasks.
[0003] At present, motor imagery EEG signal recognition algorithms are mainly focused on single-subject BCI systems, which require personalized modeling for each target subject, involving the acquisition and model adjustment of a large number of individual EEG signals. This personalized calibration process increases the complexity and calibration time of the system, and reduces the practicality and convenience of the system. In contrast, subject-independent BCI systems aim to solve the generalization problem across subjects, so that BCI systems can better adapt to multiple subjects and expand the application scope of BCI technology. However, the spatial differences in event-related desynchronization / synchronization phenomena between different subjects pose a huge challenge to research. In EEG signals, imagining left or right hand movements can induce event-related desynchronization phenomena in the sensorimotor area on the contralateral side of the brain and event-related synchronization phenomena on the ipsilateral side. Accurately identifying and locating event-related desynchronization / synchronization is an important criterion for motor imagery classification. However, existing deep learning models are trained and used as black boxes, which makes it difficult to accurately locate event-related desynchronization / synchronization phenomena, resulting in unsatisfactory motor imagery recognition results. Therefore, enhancing the model's event-related desynchronization / synchronization localization ability is crucial to improving the performance of subject-independent motor imagery EEG signal recognition.
[0004] In recent years, deep learning models have been widely used in motor imagery EEG signal recognition, but their "black box" nature makes it difficult for the model to accurately locate event-related desynchronization / synchronization phenomena. In addition, although Transformer-based models perform well in extracting discriminative features of EEG signals, the computational complexity of their global multi-head self-attention mechanism grows quadratically with the increase in the length of the input sequence. Existing Transformer models usually use short input sequences, which limits the temporal resolution of feature extraction. Summary of the invention
[0005] The purpose of the present invention is to provide a motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer. The method combines mirror contrast loss and time sliding window Transformer model, aims to improve the localization ability of event-related desynchronization and event-related synchronization phenomena in EEG signals and solve the problem of sequence length limitation of EEG signals in Transformer model.
[0006] The technical solution adopted by the present invention is a motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer, specifically:
[0007] Step 1: Dataset division: divide the selected dataset into new subject settings;
[0008] Step 2: Data preprocessing: Select channels, perform bandpass filtering, remove artifacts, and normalize the EEG signals in the new subject setting divided in step 1;
[0009] Step 3: Generate mirror EEG signals; generate mirror EEG signals by flipping the left and right cerebral hemisphere channels of the EEG signals preprocessed in step 2, and swap the left and right hand movement imagination labels corresponding to the generated mirror EEG signals;
[0010] Step 4: Build a sliding window Transformer model and train the model using the EEG signals preprocessed in step 2 and the mirror EEG signals generated in step 3;
[0011] Step 5: Calculate the image contrast loss;
[0012] Step 6: Calculate the classification loss, calculate the cross entropy classification loss and combine it with the mirror contrast loss obtained in step 5 to jointly train the sliding window Transformer EEG signal recognition model constructed in step 4.
[0013] The present invention is also characterized in that:
[0014] In step 1: the selected data sets are Brain-Computer Interface Competition IV Dataset 2a and Brain-Computer Interface Competition IV Dataset 2b;
[0015] In step 1: there are two ways to divide the BCI Competition IV dataset 2a and BCI Competition IV dataset 2b into new subject settings:
[0016] The first new subject setup is:
[0017] When the training sessions of all 9 subjects in dataset 2a were used as the training set, the 4th and 5th test sessions of all 9 subjects in dataset 2b were used as the test set;
[0018] The second new subject setting is:
[0019] While the 3rd training session of all 9 subjects in dataset 2b was used as the training set, the testing session of all 9 subjects in dataset 2a was used as the testing set.
[0020] Step 2 is implemented as follows:
[0021] Select channels C3, C4 and Cz of the motor imagery area to ensure that the channels of the training data and the test data in the new subject setting divided in step 1 are consistent; perform 4-38Hz or 0-38Hz bandpass filtering on the EEG signals of the selected channels to remove EEG signal components outside the range of 4-38Hz or 0-38Hz; use a 50Hz notch filter to remove artifacts and noise from the EEG signals after bandpass filtering; then perform channel-level logarithmic sliding normalization on the EEG signals after notch filtering.
[0022] Step 3 is implemented as follows:
[0023] 3.1 Exchange channel;
[0024] The data of the C3 EEG channel of the EEG signal after the data preprocessing in step 2 is converted to the C4 EEG channel, and the data of the C4 EEG channel is converted to the C3 EEG channel, thereby generating a mirror EEG signal;
[0025] 3.2 Exchange labels;
[0026] The left and right hand movement imagery labels corresponding to the mirror EEG signals are changed to the opposite labels of the EEG signals before the exchange, so as to obtain the mirror EEG signals after the left and right hand movement imagery labels are exchanged.
[0027] In step 4, the overall architecture of the sliding window Transformer model consists of three parts in sequence: a feature extraction module based on a convolutional neural network, a multi-head self-attention module based on a sliding time window, and a classification module.
[0028] Step 4 is as follows:
[0029] Step 4.1: Use the feature extraction module based on convolutional neural network to extract the spatiotemporal features of EEG signals; this module is composed of the time domain convolution layer C t , spatial convolution layer C s And the batch normalization layer BN, the specific work of each submodule is:
[0030] First, the EEG signal after data preprocessing in step 2 and the mirror EEG signal generated in step 3 are input into the time domain convolution layer, using the time domain convolution layer C with a kernel size of T×1. tProcessing time information, where the time convolution kernel T is set to 25; secondly, the time domain convolution layer C t The output result is input to the spatial convolution layer C s , apply a spatial convolution layer C with a kernel size of 1×C s To fuse the information of each EEG signal channel, the spatial convolution kernel C is set to 3; finally, the output of the spatial convolution layer is input to the batch normalization layer to normalize the distribution of batch data;
[0031] The operation flow of the feature extraction module based on convolutional neural network is expressed as follows:
[0032] F=BN(C s (C t (X))) (1)
[0033] Among them, F represents the output of the feature extraction module based on the convolutional neural network, BN represents the batch normalization layer, and C s represents the spatial convolutional layer, C t represents the time domain convolutional layer, X represents the EEG signal after data preprocessing in step 2 or the mirror EEG signal after the left and right hand movement imagination labels are swapped generated in step 3;
[0034] 4.2. Use the multi-head self-attention module based on sliding time window to establish a multi-head self-attention layer along the time dimension to capture the discriminative features of EEG signals and control the computational load at the same time; the multi-head self-attention module based on sliding time window includes a multi-head self-attention module based on time domain window and a multi-head self-attention module based on sliding time domain window in turn. The specific work of each sub-module is as follows:
[0035] 4.2.1. A multi-head self-attention module based on a time domain window is used to calculate the local time domain attention score; the multi-head self-attention module based on a time domain window is composed of a first normalization layer, a time domain window multi-head self-attention layer integrated in a residual network structure, a second normalization layer, and a multi-layer perceptron layer integrated in a residual network structure. The following describes in detail the working content of each layer in the multi-head self-attention module based on a time domain window:
[0036] 1) The first normalization layer: perform layer normalization on the output result F of the feature extraction module based on the convolutional neural network in step 4.1 to normalize the value of the feature dimension of each EEG signal;
[0037] 2) Time-domain window multi-head self-attention layer: It is used to extract the local time-domain attention score of the input vector in the time dimension and calculate the attention score in a single window. Specifically, it takes the output features of the first normalization layer as input and then divides it into a set of non-overlapping local windows of size M; performs self-attention score calculation in each local window to enhance the model's perception of local information and improve computational efficiency, such as Figure 3 As shown; the window size M of the time domain window multi-head self-attention layer is set to 8;
[0038] The specific calculation steps for performing self-attention score calculation in each local window are as follows: First, the output features of the first normalization layer are linearly transformed to obtain three representations: query Q, key K and value V, as shown in formula (2); then, attention calculation is performed on query Q, key K and value V. The attention calculation is to obtain the attention score by calculating the dot product similarity between the query and the key, and then obtain the attention weight by performing a Softmax operation. The value vector is then weighted and summed using the attention weight to obtain the self-attention representation of each position, as shown in formula (3); finally, the self-attention representation of each attention head is weightedly concatenated and residually connected with the output result F of the feature extraction module based on the convolutional neural network in step 4.1 to obtain the output result O, as shown in formula (4);
[0039] Among them, the expressions of formula (2) to formula (4) are as follows:
[0040]
[0041] In formula (2), 1) represents the output features of the first normalization layer, LN represents the layer normalization layer, represents the weight matrix corresponding to the i-th attention head query Q, represents the weight matrix corresponding to the i-th attention head key K, represents the weight matrix corresponding to the i-th attention head value V, Q i represents the query of the ith attention head, K i represents the key of the i-th attention head, V i Represents the value of the i-th attention head;
[0042]
[0043] In formula (3), h i represents the self-attention representation of the i-th attention head, Attention represents the self-attention calculation, Q i ,K i ,V i Consistent with formula (2), Softmax represents the Softmax operation, dk represents the dimension of the key (K), and T represents the transpose operation;
[0044] O=concat(h1,…,h i ,…,h H )W o +F (4)
[0045] In formula (4), h i represents the self-attention representation of the i-th attention head, i ranges from 1 to H, H represents the number of attention heads, W o represents the linear transformation matrix, F represents the output result of the feature extraction module based on convolutional neural network in 4.1;
[0046] 3) The second normalization layer: perform layer normalization on the output result O of the time domain window multi-head self-attention layer, normalize the value of the feature dimension of each EEG signal, and obtain the output result of the second normalization layer
[0047] 4) Multilayer perceptron layer: It is composed of a fully connected layer, a GELU activation function layer, and another fully connected layer in sequence; the output result of the second normalization layer is calculated, and the output result of the second normalization layer is added to the output result of the multilayer perceptron layer to generate the final output A of the multi-head self-attention module based on the time domain window. The specific operation process is expressed as follows:
[0048]
[0049] In formula (5), A represents the final output of the multi-head self-attention module based on the time domain window, O represents the output result of the multi-head self-attention layer of the time domain window, FC represents the fully connected layer, GELU represents the GELU activation function layer, Represents the output result of the second normalization layer;
[0050] 4.2.2. Use a multi-head self-attention module based on a sliding time domain window to promote information exchange between adjacent local windows; the multi-head self-attention module based on a sliding time domain window is composed of a third normalization layer, a sliding time domain window multi-head self-attention layer integrated in a residual network structure, a fourth normalization layer, and a multi-layer perceptron layer integrated in a residual network structure; the following describes in detail the working content of each layer in the multi-head self-attention module based on a sliding time domain window:
[0051] 1) The third normalization layer: the final output A of the multi-head self-attention module based on the time domain window is normalized to normalize the value of the feature dimension of each EEG signal;
[0052] 2) Sliding time-domain window multi-head self-attention layer: Calculate the attention score in the cyclically shifted local window for information interaction; The sliding time-domain window multi-head self-attention layer shifts the time-domain window to the right with a step size of M / 2 to obtain a new local window, and calculates the self-attention score in the new local window;
[0053] The specific calculation steps for performing self-attention score calculation in each new local window are as follows: First, the output features of the third normalization layer are linearly transformed to obtain three representations: query Q, key K and value V; then, attention calculation is performed on query Q, key K and value V. The attention calculation is to obtain the attention score by calculating the dot product similarity between the query and the key, and then obtain the attention weight through Softmax operation. The value vector is weighted and summed using the attention weight to obtain the self-attention representation of each position; finally, the self-attention representation of each attention head is weightedly spliced, and the output result A of the multi-head self-attention module based on the time domain window in step 4.2.1 is residually connected to obtain the final output result of the sliding time domain window multi-head self-attention layer;
[0054] 3) The fourth normalization layer: normalizes the output results of the sliding time domain window multi-head self-attention layer, and normalizes the value of each EEG signal on the feature dimension;
[0055] 4) Multilayer perceptron layer: It consists of a fully connected layer, a GELU activation function layer, and another fully connected layer in sequence. It calculates the output of the fourth normalization layer and adds the output of the fourth normalization layer to the output of the multilayer perceptron layer to generate the output of the multi-head self-attention module based on the sliding time window.
[0056] 4.3. The output results of the multi-head self-attention module based on the sliding time window are input into the classification module to calculate the predicted probability of the motor imagery task; the classification module includes a square activation function layer, an average pooling layer, a log activation layer and a fully connected layer in sequence, and the specific work of each submodule is as follows;
[0057] First, the output results of the multi-head self-attention module based on the sliding time window A square activation function layer is used for nonlinear transformation; secondly, an average pooling layer is used to reduce the dimension of the feature; then, a log activation layer is used for nonlinear transformation; finally, the feature is passed to a fully connected layer and the final prediction probability of the model is calculated by Softmax. The calculation process of the classification module is expressed as follows:
[0058]
[0059] In formula (8), P represents the predicted probability, Softmax represents the Softmax operation, FC represents the fully connected layer, Log represents the logarithmic activation layer, AvgPool represents the average pooling layer, and Square represents the square activation function layer. Represents the output of the multi-head self-attention module based on the sliding time window.
[0060] Step 5 is as follows:
[0061] The output features of the multi-head self-attention module based on the sliding time window in step 4 Input into calculation formula (9) to calculate the Euclidean distance between every two sample pairs in a batch; then use calculation formula (10) to perform weighted summation of the Euclidean distances of all sample pairs to obtain the final image comparison loss;
[0062] The calculation formula of Euclidean distance is as follows:
[0063]
[0064] In formula (9), D ij represents the Euclidean distance between sample pairs (i, j), represents the output feature of the multi-head self-attention module based on the sliding time window of the i-th sample, represents the output feature of the multi-head self-attention module based on the sliding time window of the j-th sample, and n represents the n-th coordinate;
[0065] The calculation formula of image contrast loss is as follows:
[0066] L d =w o ∑ i,j∈OE g ij D ij +w m ∑ i∈ME,j∈OE g ij D ij (10)
[0067] In formula (10), L d represents the image contrast loss, w o represents the weight of the contrast loss of the original EEG signal, w m represents the weight of the contrast loss of the mirror EEG signal, w o and w m It is set to 0.2 and 0.3 according to experience, i, j represents two sample pairs from the original EEG signal set or the mirror EEG signal set, OE represents the original EEG signal set, ME represents the mirror EEG signal set, D ijRepresents the Euclidean distance between sample pairs (i, j); when i and j are positive sample pairs, the variable g ij The value is 1; when i, j is a negative sample pair, the variable g ij The value is -1.
[0068] In step 6, the classification loss calculation process is:
[0069] The final output prediction probability p of the sliding window Transformer model in step 4 for each motor imagery EEG trial i The actual label is input into the classification loss for calculation, and the mirror contrast loss generated in step 5 is added to obtain the final loss function of the model training. The specific calculation method is as follows:
[0070]
[0071] Among them, L c represents the classification loss, N is the total number of motor imagery EEG trials, i represents the i-th motor imagery EEG trial, y i represents the true label of the ith motor imagery EEG trial, P = {p1,…,p i ,…,p N}, P represents the final output prediction probability of the sliding window Transformer model, p i represents the predicted probability of the ith motor imagery EEG trial, and ln represents logarithmic calculation;
[0072] In step 6, the final loss function of the confirmed model training is the sum of the image contrast loss and the classification loss:
[0073] L f =L c +L d (12)
[0074] In formula (12), L f Represents the final loss function of model training, L c represents the classification loss obtained in step 6, L d represents the mirror contrast loss obtained in step 5.
[0075] The beneficial effects of the present invention are:
[0076] The method of the present invention aims at the problem of spatial localization ability of event-related desynchronization / synchronization phenomenon in motor imagery EEG classification. By exchanging the EEG channels of the left and right hemispheres of the brain to generate mirror signals of the original EEG signals, a mirror contrast loss is proposed. By comparing the deep features of the original EEG signals and the mirror EEG signals, the model's ability to locate event-related desynchronization / synchronization phenomena is enhanced. In addition, this patent designs a sliding window Transformer model, which extracts the temporal features of EEG signals through a time sliding window multi-head self-attention mechanism, specifically including multi-head self-attention calculations within local time domain windows and multi-head self-attention calculations within sliding time domain windows. Combining these two parts enhances the interaction between local windows, improves the modeling ability of long-distance time dependencies, and solves the problem of sequence length limitation of motor imagery EEG signals in the Transformer model. BRIEF DESCRIPTION OF THE DRAWINGS
[0077] Figure 1 It is a schematic diagram of the training framework of the motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer of the present invention;
[0078] Figure 2 It is a schematic diagram of exchanging the left and right EEG channels to construct a mirrored EEG signal;
[0079] Figure 3 This is a schematic diagram of the overall architecture of the sliding window Transformer model. DETAILED DESCRIPTION
[0080] The present invention is described in detail below with reference to the accompanying drawings and specific embodiments.
[0081] The present invention provides a motor imagery EEG signal recognition algorithm (MCL-WST) based on mirror contrast loss and sliding window Transformer, and its training framework is as follows Figure 1 The algorithm specifically includes the following steps:
[0082] Step 1: Dataset division: Divide the Brain-Computer Interface Competition IV Dataset 2a (hereinafter referred to as Dataset 2a) and the Brain-Computer Interface Competition IV Dataset 2b (hereinafter referred to as Dataset 2b) into new subject settings.
[0083] In step 1: there are two ways to divide the brain-computer interface competition IV dataset 2a (hereinafter referred to as dataset 2a) and the brain-computer interface competition IV dataset 2b (hereinafter referred to as dataset 2b) into new subject settings:
[0084] The first new subject setup is:
[0085] When the training sessions of all 9 subjects in dataset 2a were used as the training set, the 4th and 5th test sessions of all 9 subjects in dataset 2b were used as the test set;
[0086] The second new subject setting is:
[0087] While the 3rd training session of all 9 subjects in dataset 2b was used as the training set, the testing session of all 9 subjects in dataset 2a was used as the testing set.
[0088] In order to study the classification performance of the subject's independent motor imagery EEG signals, Dataset 2a and Dataset 2b are divided into the above two new subject settings. Since the experimental data used for testing and training come from completely different subjects, they are called new subject settings. Since the two new subject settings are consistent in the subsequent processing steps, no distinction is made during steps 2 to 6, and the experimental results of the two new subject settings are only evaluated in the experimental verification part.
[0089] Step 2: Data preprocessing: The EEG signals in the new subject setting divided in step 1 are subjected to channel selection, bandpass filtering, artifact removal and normalization.
[0090] Step 2 is implemented as follows:
[0091] Select channels C3, C4 and Cz of the motor imagery area to ensure that the channels of the training data and the test data in the new subject setting divided in step 1 are consistent; perform 4-38Hz or 0-38Hz bandpass filtering on the EEG signals of the selected channels to remove EEG signal components outside the range of 4-38Hz or 0-38Hz; use a 50Hz notch filter to remove artifacts and noise from the EEG signals after bandpass filtering to purify the EEG signals and improve the quality of the EEG signals; then perform channel-level logarithmic sliding normalization on the EEG signals after notch filtering.
[0092] Step 3: Mirror EEG signal generation: The EEG signal preprocessed in step 2 is used to generate a mirror EEG signal by flipping the left and right hemisphere channels, and the left and right hand movement imagination labels corresponding to the generated mirror EEG signal are swapped, such as Figure 2 shown.
[0093] Step 3 is implemented as follows:
[0094] 3.1 Exchange channel;
[0095] Specifically, the data of the C3 EEG channel of the EEG signal after the data preprocessing in step 2 is converted to the C4 EEG channel, and the data of the C4 EEG channel is converted to the C3 EEG channel, thereby generating a mirror EEG signal;
[0096] Since the channel position of the EEG signal after the exchange is symmetrical with the channel position of the EEG signal before the exchange, it is called a "mirror EEG signal".
[0097] 3.2 Exchange labels;
[0098] The labels of the left and right hand motor imagination corresponding to the mirror EEG signal are changed to the opposite labels of the EEG signal before the exchange, and the mirror EEG signal after the labels of the left and right hand motor imagination are exchanged is obtained; because the event-related desynchronization / synchronization phenomenon in the mirror EEG signal appears on the opposite side relative to the EEG signal before the exchange. For example, if the label of the EEG signal before the exchange is the left hand, the label of the mirror EEG signal is the right hand; if the label of the EEG signal before the exchange is the right hand, the label of the mirror EEG signal is the left hand.
[0099] In the training phase, both the mirror EEG signals and the EEG signals before the channel swapping are used for model training. By adopting this reasonable data augmentation technique, the number of training samples is doubled, thus facilitating the model training process.
[0100] Step 4: Build a sliding window Transformer model (SWT) and train the model using the EEG signals preprocessed in step 2 and the mirror EEG signals after the left and right hand movement imagery labels are swapped generated in step 3;
[0101] In step 4, the overall architecture of the sliding window Transformer model consists of three parts in sequence: a feature extraction module based on a convolutional neural network, a multi-head self-attention module based on a sliding time window, and a classification module, such as Figure 3 shown.
[0102] Step 4 is as follows:
[0103] Step 4.1: Use the feature extraction module based on convolutional neural network to extract the spatiotemporal features of EEG signals; this module is composed of the time domain convolution layer C t , spatial convolution layer C s And the batch normalization layer BN, the specific work of each submodule is:
[0104] First, the EEG signal after data preprocessing in step 2 and the mirror EEG signal generated in step 3 are input into the time domain convolution layer, using the time domain convolution layer C with a kernel size of T×1. t Processing time information, where the time convolution kernel T is set to 25; secondly, the time domain convolution layer C t The output result is input to the spatial convolution layer C s , apply a spatial convolution layer C with a kernel size of 1×C sTo fuse the information of each EEG signal channel, the spatial convolution kernel C is set to 3; finally, the output of the spatial convolution layer is input to the batch normalization layer to normalize the distribution of batch data;
[0105] The operation flow of the feature extraction module based on convolutional neural network is expressed as follows:
[0106] F=BN(C s (C t (X))) (1)
[0107] Among them, F represents the output of the feature extraction module based on the convolutional neural network, BN represents the batch normalization layer, and C s represents the spatial convolutional layer, C t represents the time domain convolutional layer, and X represents the EEG signal after data preprocessing in step 2 or the mirror EEG signal after the left and right hand movement imagination labels are swapped generated in step 3.
[0108] 4.2. Use the multi-head self-attention module based on sliding time window to establish a multi-head self-attention layer along the time dimension to capture the discriminative features of EEG signals and control the computational load at the same time; the multi-head self-attention module based on sliding time window includes a multi-head self-attention module based on time domain window and a multi-head self-attention module based on sliding time domain window in turn. The specific work of each sub-module is as follows:
[0109] 4.2.1. A multi-head self-attention module based on a time domain window is used to calculate the local time domain attention score; the multi-head self-attention module based on a time domain window is composed of a first normalization layer, a time domain window multi-head self-attention layer integrated in a residual network structure, a second normalization layer, and a multi-layer perceptron layer integrated in a residual network structure. The following describes in detail the working content of each layer in the multi-head self-attention module based on a time domain window:
[0110] 1) The first normalization layer: perform layer normalization on the output result F of the feature extraction module based on the convolutional neural network in step 4.1 to normalize the value of the feature dimension of each EEG signal;
[0111] 2) Time-domain window multi-head self-attention layer: It is used to extract the local time-domain attention score of the input vector in the time dimension and calculate the attention score in a single window. Specifically, it takes the output features of the first normalization layer as input and then divides it into a set of non-overlapping local windows of size M; performs self-attention score calculation in each local window to enhance the model's perception of local information and improve computational efficiency, such as Figure 3 As shown; the window size M of the time domain window multi-head self-attention layer is set to 8;
[0112] The specific calculation steps for performing self-attention score calculation in each local window are as follows: First, the output features of the first normalization layer are linearly transformed to obtain three representations: query Q, key K and value V, as shown in formula (2); then, attention calculation is performed on query Q, key K and value V. The attention calculation is to obtain the attention score by calculating the dot product similarity between the query and the key, and then obtain the attention weight by performing a Softmax operation. The value vector is then weighted and summed using the attention weight to obtain the self-attention representation of each position, as shown in formula (3); finally, the self-attention representation of each attention head is weightedly concatenated and residually connected with the output result F of the feature extraction module based on the convolutional neural network in step 4.1 to obtain the output result O, as shown in formula (4);
[0113] Among them, the expressions of formula (2) to formula (4) are as follows:
[0114]
[0115] In formula (2), 1) represents the output features of the first normalization layer, LN represents the layer normalization layer, represents the weight matrix corresponding to the i-th attention head query Q, represents the weight matrix corresponding to the i-th attention head key K, represents the weight matrix corresponding to the i-th attention head value V, Q i represents the query of the ith attention head, K i represents the key of the i-th attention head, V i Represents the value of the i-th attention head;
[0116]
[0117] In formula (3), h i represents the self-attention representation of the i-th attention head, Attention represents the self-attention calculation, Q i ,K i ,V i Consistent with formula (2), Softmax represents the Softmax operation, d k represents the dimension of the key (K), and T represents the transpose operation;
[0118] O=concat(h1,…,h i ,…,h H )W o +F (4)
[0119] In formula (4), h i represents the self-attention representation of the i-th attention head, i ranges from 1 to H, H represents the number of attention heads, Wo represents the linear transformation matrix, F represents the output result of the feature extraction module based on convolutional neural network in 4.1;
[0120] 3) The second normalization layer: perform layer normalization on the output result O of the time domain window multi-head self-attention layer, normalize the value of the feature dimension of each EEG signal, and obtain the output result of the second normalization layer
[0121] 4) Multilayer perceptron layer: It is composed of a fully connected layer, a GELU activation function layer, and another fully connected layer in sequence; the output result of the second normalization layer is calculated, and the output result of the second normalization layer is added to the output result of the multilayer perceptron layer to generate the final output A of the multi-head self-attention module based on the time domain window. The specific operation process is expressed as follows:
[0122]
[0123] In formula (5), A represents the final output of the multi-head self-attention module based on the time domain window, O represents the output result of the multi-head self-attention layer of the time domain window, FC represents the fully connected layer, GELU represents the GELU activation function layer, Represents the output result of the second normalization layer.
[0124] 4.2.2. Use a multi-head self-attention module based on a sliding time domain window to promote information exchange between adjacent local windows; the multi-head self-attention module based on a sliding time domain window is composed of a third normalization layer, a sliding time domain window multi-head self-attention layer integrated in a residual network structure, a fourth normalization layer, and a multi-layer perceptron layer integrated in a residual network structure; the following describes in detail the working content of each layer in the multi-head self-attention module based on a sliding time domain window:
[0125] 1) The third normalization layer: the final output A of the multi-head self-attention module based on the time domain window is normalized to normalize the value of the feature dimension of each EEG signal;
[0126] 2) Multi-head self-attention layer with sliding time domain window: Attention scores are calculated in the local window of cyclic translation for information interaction; Figure 3 As shown, the sliding time domain window multi-head self-attention layer shifts the time domain window to the right with a step size of M / 2 to obtain a new local window, and calculates the self-attention score in the new local window;
[0127] The specific calculation steps for performing self-attention score calculation in each new local window are as follows: First, the output features of the third normalization layer are linearly transformed to obtain three representations: query Q, key K and value V; then, attention calculation is performed on the query Q, key K and value V. The attention calculation is to obtain the attention score by calculating the dot product similarity between the query and the key, and then obtain the attention weight through the Softmax operation. The value vector is weighted and summed up using the attention weight to obtain the self-attention representation of each position; finally, the self-attention representation of each attention head is weightedly concatenated and residually connected with the output result A of the multi-head self-attention module based on the time domain window in step 4.2.1 to obtain the final output result of the sliding time domain window multi-head self-attention layer.
[0128] The computational complexity of the global multi-head self-attention mechanism is proportional to the square of the input sequence length, but the computational complexity of the multi-head self-attention module based on the sliding time window is linearly related to the length of the input vector. Therefore, it reduces the computational complexity of the Transformer model and solves the problem of sequence length limitation of motor imagery EEG signals in the Transformer model. The computational complexity of the global multi-head self-attention mechanism is as shown in formula (6);
[0129] Ω(MSA)=4LD 2 +2L 2 D (6)
[0130] In formula (6), Ω(MSA) represents the computational complexity of the global multi-head self-attention mechanism, MSA represents the global multi-head self-attention mechanism, L represents the length of the input vector, and D represents the input dimension.
[0131] The computational complexity of the multi-head self-attention module based on sliding time window is shown in formula (7);
[0132] Ω(TW-MSA)=4LD 2 +2M 2 LD (7)
[0133] In formula (7), Ω(TW-MSA) represents the computational complexity of the multi-head self-attention module based on the sliding time window, TW-MSA represents the multi-head self-attention module based on the sliding time window, L represents the length of the input vector, D represents the input dimension, and M represents the window size.
[0134] 3) The fourth normalization layer: normalizes the output results of the sliding time domain window multi-head self-attention layer, and normalizes the value of each EEG signal on the feature dimension;
[0135] 4) Multilayer perceptron layer: It consists of a fully connected layer, a GELU activation function layer, and another fully connected layer in sequence. It calculates the output of the fourth normalization layer and adds the output of the fourth normalization layer to the output of the multilayer perceptron layer to generate the output of the multi-head self-attention module based on the sliding time window.
[0136] 4.3. The output results of the multi-head self-attention module based on the sliding time window are input into the classification module to calculate the predicted probability of the motor imagery task; the classification module includes a square activation function layer, an average pooling layer, a log activation layer and a fully connected layer in sequence, and the specific work of each submodule is as follows;
[0137] First, the output results of the multi-head self-attention module based on the sliding time window A square activation function layer is used for nonlinear transformation; secondly, an average pooling layer is used to reduce the dimension of the feature; then, a log activation layer is used for nonlinear transformation; finally, the feature is passed to a fully connected layer and the final prediction probability of the model is calculated by Softmax. The calculation process of the classification module is expressed as follows:
[0138]
[0139] In formula (8), P represents the predicted probability, Softmax represents the Softmax operation, FC represents the fully connected layer, Log represents the logarithmic activation layer, AvgPool represents the average pooling layer, and Square represents the square activation function layer. Represents the output of the multi-head self-attention module based on the sliding time window.
[0140] Step 5: Calculate the mirror contrast loss, specifically: the output features of the multi-head self-attention module based on the sliding time window in step 4 The input is used in the calculation formula (9) to calculate the Euclidean distance between every two sample pairs in a batch; and the Euclidean distances of all sample pairs are weighted summed up by the calculation formula (10) to obtain the final image comparison loss.
[0141] In the process of calculating the mirror contrast loss, samples belonging to the same motor imagery task category are used as positive sample pairs, while samples belonging to different motor imagery task categories are used as negative sample pairs. By setting the EEG signal after data preprocessing in step 2 and its mirror EEG signal as negative sample pairs, they are used to assist the training of the sliding window Transformer model in step 4 in the contrast learning cost to improve the spatial localization ability of event-related desynchronization phenomena. The mirror contrast loss is applied to the sample set of EEG signals after data preprocessing in step 2 (hereinafter referred to as the original EEG signal set), as well as the sample set of EEG signals after data preprocessing in step 2 and their mirror EEG signals (hereinafter referred to as the mirror EEG signal set). The calculation formula of the Euclidean distance is as follows:
[0142]
[0143] In formula (9), D ij represents the Euclidean distance between sample pairs (i, j), represents the output feature of the multi-head self-attention module based on the sliding time window of the i-th sample, represents the output feature of the j-th sample based on the multi-head self-attention module of the sliding time window, and n represents the n-th coordinate.
[0144] The calculation formula of image contrast loss is as follows:
[0145] L d =w o ∑ i,j∈OE g ij D ij +w m v i∈ME,j∈OE g ij D ij (10)
[0146] In formula (10), L d represents the image contrast loss, w o represents the weight of the contrast loss of the original EEG signal, w m represents the weight of the contrast loss of the mirror EEG signal, w o and w m It is set to 0.2 and 0.3 according to experience, i, j represents two sample pairs from the original EEG signal set or the mirror EEG signal set, OE represents the original EEG signal set, ME represents the mirror EEG signal set, D ij Represents the Euclidean distance between sample pairs (i, j). When i and j are positive sample pairs, the variable g ij The value is 1; when i, j is a negative sample pair, the variable g ij The value is -1.
[0147] Because the labels of the original EEG signal and the mirror EEG signal are opposite, they can be combined to form a negative sample pair and applied in the contrast loss. By comparing the deep features of the original EEG signal and the mirror EEG signal, the model's ability to locate event-related desynchronization / synchronization phenomena is enhanced. Contrastive loss optimizes the feature distribution in model training by encouraging the model to bring the features of positive sample pairs closer together and push the features of negative sample pairs farther away. Successfully constructing positive and negative sample pairs is a key factor in contrast loss. Although contrast loss has been successfully applied to deep learning, a large number of negative samples are usually required to effectively train the model and distinguish the differences between samples.
[0148] This patent proposes a mirror contrast loss, which enhances the model's sensitivity to the location of event-related desynchronization / synchronization by comparing the original EEG signal and its mirror EEG signal. It aims to improve the model's spatial perception of the location where event-related desynchronization / synchronization occurs. At the same time, it suppresses features that are not related to the difference in EEG signals between the left and right sides of the brain. In addition, this method effectively increases the number of negative sample pairs, thereby enhancing the robustness of the model.
[0149] Step 6: Calculate the classification loss, calculate the cross entropy classification loss and combine it with the mirror contrast loss obtained in step 5 to jointly train the sliding window Transformer EEG signal recognition model constructed in step 4.
[0150] In step 6, the classification loss is obtained by: the final output prediction probability p of the sliding window Transformer model in step 4 for each motor imagery EEG trial is i The actual label is input into the classification loss for calculation, and the mirror contrast loss generated in step 5 is added to obtain the final loss function of the model training. The specific calculation method is as follows:
[0151]
[0152] Among them, L c represents the classification loss, N is the total number of motor imagery EEG trials, i represents the i-th motor imagery EEG trial, y i represents the true label of the ith motor imagery EEG trial, P = {p1,…,p i ,…,p N}, P represents the final output prediction probability of the sliding window Transformer model, p i represents the predicted probability of the ith motor imagery EEG trial, and ln represents logarithmic calculation.
[0153] In step 6, the final loss function of the confirmed model training is the sum of the image contrast loss and the classification loss:
[0154] L f =L c +Ld (12)
[0155] In formula (12), L f Represents the final loss function of model training, L c represents the classification loss obtained in step 6, L d represents the mirror contrast loss obtained in step 5.
[0156] Experimental verification:
[0157] 1. Experimental parameter setting. Since the training usually converges within 500 epochs, the maximum number of epochs for training is set to 500 to ensure model convergence. During model training, the Adam optimizer is used as the optimization method, and the weight decay parameter is set to 0.05. In order to divide the input feature vector of length L into an integer number of non-overlapping windows, a 4.48-second EEG signal segment containing 1120 sampling points is used. From 0.5 seconds before the start of the motor imagery prompt to 3.98 seconds after the start of the motor imagery prompt. Since the batch size is set to 100, the input data dimension will be 100×1×1120×3.
[0158] 2. Experimental results: In order to verify the effectiveness of the motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer model, multiple groups of experiments were conducted. The experimental results show that the method of the present invention performs well in the motor imagery EEG signal recognition task independent of the subjects.
[0159] Since the subjects in dataset 2a are completely different from those in dataset 2b, the subjects in the test set are new. Since significant randomness in a particular epoch can affect the test accuracy, the evaluation metrics used in the experiment include: (1) the maximum test accuracy within 500 epochs (Maximum Accuracy); (2) the average test accuracy from epoch 401 to 500 (Average Accuracy); (3) the test accuracy at the epoch with the lowest training loss (Accuracy). Table 2 presents the results in the form of “Accuracy / Kappa Coefficient”, with the maximum result highlighted in bold. The first row indicates that the encoding format is “dataset-bandpass filter”. For example, “2a-0Hz” means that the model is trained on dataset 2b and tested on dataset 2a after preprocessing with a 0-38Hz bandpass filter. Five state-of-the-art models are compared in the experiment, including Shallow ConvNet, Deep ConvNet, EEGNet, FBCNet, and ATCNet. In addition, ablation experiments are performed. SWT and MCL-SWT represent SWT models without MCL and with MCL, respectively. Experimental results for two new subject settings under different bandpass filters are presented.
[0160] Table 1 Comparison of classification performance (accuracy and Kappa coefficient values) on the new subject setting
[0161]
[0162]
[0163] The experimental results in Table 1 lead to the following conclusions: (1) The accuracy of MCL-SWT reaches 66.48% and 75.62%, respectively, which exceeds the state-of-the-art models by 2.82% and 2.17%, respectively. (2) The proposed temporal MCL-SWT model shows excellent performance on different datasets and bandpass filters. (3) The recognition accuracy and Kappa value of MCL for SWT are further improved. (4) The proposed SWT model shows resistance to overfitting, which can be demonstrated by the small gap between the maximum accuracy / Kappa and the average accuracy / Kappa of SWT. (5) The experimental results show that MCL-SWT has better inter-subject generalization ability.
[0164] Based on the above conclusions, it can be seen that the motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer model improves the ability to locate event-related desynchronization and event-related synchronization phenomena in EEG signals. The local window mechanism of the multi-head self-attention module based on the sliding time window in the sliding window Transformer model solves the problem of sequence length limitation of motor imagery EEG signals in the Transformer model.
[0165] Example 1
[0166] The motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer is as follows:
[0167] Step 1: Dataset division: divide the selected dataset into new subject settings;
[0168] Step 2: Data preprocessing: Select channels, perform bandpass filtering, remove artifacts, and normalize the EEG signals in the new subject setting divided in step 1;
[0169] Step 3: Generate mirror EEG signals; generate mirror EEG signals by flipping the left and right cerebral hemisphere channels of the EEG signals preprocessed in step 2, and swap the left and right hand movement imagination labels corresponding to the generated mirror EEG signals;
[0170] Step 4: Build a sliding window Transformer model and train the model using the EEG signals preprocessed in step 2 and the mirror EEG signals generated in step 3;
[0171] Step 5: Calculate the image contrast loss;
[0172] Step 6: Calculate the classification loss, calculate the cross entropy classification loss and combine it with the mirror contrast loss obtained in step 5 to jointly train the sliding window Transformer EEG signal recognition model constructed in step 4.
[0173] Example 2
[0174] The motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer is as follows:
[0175] Step 1: Dataset division: divide the selected dataset into new subject settings;
[0176] In step 1: the selected data sets are Brain-Computer Interface Competition IV Dataset 2a and Brain-Computer Interface Competition IV Dataset 2b;
[0177] In step 1: there are two ways to divide the BCI Competition IV dataset 2a and BCI Competition IV dataset 2b into new subject settings:
[0178] The first new subject setup is:
[0179] When the training sessions of all 9 subjects in dataset 2a were used as the training set, the 4th and 5th test sessions of all 9 subjects in dataset 2b were used as the test set;
[0180] The second new subject setting is:
[0181] While the 3rd training session of all 9 subjects in dataset 2b was used as the training set, the testing session of all 9 subjects in dataset 2a was used as the testing set.
[0182] Step 2: Data preprocessing: Select channels, perform bandpass filtering, remove artifacts, and normalize the EEG signals in the new subject setting divided in step 1;
[0183] Step 3: Generate mirror EEG signals; generate mirror EEG signals by flipping the left and right cerebral hemisphere channels of the EEG signals preprocessed in step 2, and swap the left and right hand movement imagination labels corresponding to the generated mirror EEG signals;
[0184] Step 4: Build a sliding window Transformer model and train the model using the EEG signals preprocessed in step 2 and the mirror EEG signals generated in step 3;
[0185] Step 5: Calculate the image contrast loss;
[0186] Step 6: Calculate the classification loss, calculate the cross entropy classification loss and combine it with the mirror contrast loss obtained in step 5 to jointly train the sliding window Transformer EEG signal recognition model constructed in step 4.
[0187] Example 3
[0188] The motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer is as follows:
[0189] Step 1: Dataset division: divide the selected dataset into new subject settings;
[0190] In step 1: the selected data sets are Brain-Computer Interface Competition IV Dataset 2a and Brain-Computer Interface Competition IV Dataset 2b.
[0191] In step 1: there are two ways to divide the BCI Competition IV dataset 2a and BCI Competition IV dataset 2b into new subject settings:
[0192] The first new subject setup is:
[0193] When the training sessions of all 9 subjects in dataset 2a were used as the training set, the 4th and 5th test sessions of all 9 subjects in dataset 2b were used as the test set;
[0194] The second new subject setting is:
[0195] While the 3rd training session of all 9 subjects in dataset 2b was used as the training set, the testing session of all 9 subjects in dataset 2a was used as the testing set.
[0196] Step 2: Data preprocessing: Select channels, perform bandpass filtering, remove artifacts, and normalize the EEG signals in the new subject setting divided in step 1;
[0197] Step 2 is implemented as follows:
[0198] Select channels C3, C4 and Cz of the motor imagery area to ensure that the channels of the training data and the test data in the new subject setting divided in step 1 are consistent; perform 4-38Hz or 0-38Hz bandpass filtering on the EEG signals of the selected channels to remove EEG signal components outside the range of 4-38Hz or 0-38Hz; use a 50Hz notch filter to remove artifacts and noise from the EEG signals after bandpass filtering; then perform channel-level logarithmic sliding normalization on the EEG signals after notch filtering.
[0199] Step 3: Generate mirror EEG signals; generate mirror EEG signals by flipping the left and right cerebral hemisphere channels of the EEG signals preprocessed in step 2, and swap the left and right hand movement imagination labels corresponding to the generated mirror EEG signals;
[0200] Step 4: Build a sliding window Transformer model and train the model using the EEG signals preprocessed in step 2 and the mirror EEG signals generated in step 3;
[0201] Step 5: Calculate the image contrast loss;
[0202] Step 6: Calculate the classification loss, calculate the cross entropy classification loss and combine it with the mirror contrast loss obtained in step 5 to jointly train the sliding window Transformer EEG signal recognition model constructed in step 4.
Claims
1. A motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer, characterized in that: Specifically: Step 1: Dataset division: divide the selected dataset into new subject settings; Step 2: Data preprocessing: Select channels, perform bandpass filtering, remove artifacts, and normalize the EEG signals in the new subject setting divided in step 1; Step 3: Mirror EEG signal generation; The EEG signals after data preprocessing in step 2 are generated into mirror EEG signals by flipping the left and right cerebral hemisphere channels, and the left and right hand movement imagination labels corresponding to the generated mirror EEG signals are swapped; Step 4: Build a sliding window Transformer model and train the model using the EEG signals preprocessed in step 2 and the mirror EEG signals generated in step 3; Step 5: Calculate the image contrast loss; Step 6: Calculate the classification loss, calculate the cross entropy classification loss and combine it with the mirror contrast loss obtained in step 5 to jointly train the sliding window Transformer EEG signal recognition model constructed in step 4.
2. The motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer according to claim 1 is characterized in that: In step 1: the selected data sets are Brain-Computer Interface Competition IV Dataset 2a and Brain-Computer Interface Competition IV Dataset 2b; In step 1: there are two ways to divide the BCI Competition IV dataset 2a and BCI Competition IV dataset 2b into new subject settings: The first new subject setup is: When the training sessions of all 9 subjects in dataset 2a were used as the training set, the 4th and 5th test sessions of all 9 subjects in dataset 2b were used as the test set; The second new subject setting is: While the 3rd training session of all 9 subjects in dataset 2b was used as the training set, the testing session of all 9 subjects in dataset 2a was used as the testing set.
3. The motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer according to claim 2 is characterized in that: Step 2 is implemented as follows: Select channels C3, C4 and Cz of the motor imagery area to ensure that the channels of the training data and the test data in the new subject setting divided in step 1 are consistent; perform 4-38Hz or 0-38Hz bandpass filtering on the EEG signals of the selected channels to remove EEG signal components outside the range of 4-38Hz or 0-38Hz; use a 50Hz notch filter to remove artifacts and noise from the EEG signals after bandpass filtering; then perform channel-level logarithmic sliding normalization on the EEG signals after notch filtering.
4. The motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer according to claim 3 is characterized in that: Step 3 is implemented as follows: 3.1 Exchange channel; The data of the C3 EEG channel of the EEG signal after the data preprocessing in step 2 is converted to the C4 EEG channel, and the data of the C4 EEG channel is converted to the C3 EEG channel, thereby generating a mirror EEG signal; 3.2 Exchange labels; The left and right hand movement imagery labels corresponding to the mirror EEG signals are changed to the opposite labels of the EEG signals before the exchange, so as to obtain the mirror EEG signals after the left and right hand movement imagery labels are exchanged.
5. The motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer according to claim 3 is characterized in that: In step 4, the overall architecture of the sliding window Transformer model consists of three parts in sequence: a feature extraction module based on a convolutional neural network, a multi-head self-attention module based on a sliding time window, and a classification module.
6. The motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer according to claim 5, characterized in that: Step 4 is as follows: Step 4.1: Use the feature extraction module based on convolutional neural network to extract the spatiotemporal features of EEG signals; this module is composed of the time domain convolution layer C t , spatial convolution layer C s And the batch normalization layer BN, the specific work of each submodule is: First, the EEG signal after data preprocessing in step 2 and the mirror EEG signal generated in step 3 are input into the time domain convolution layer, using the time domain convolution layer C with a kernel size of T×1. t Processing time information, where the time convolution kernel T is set to 25; secondly, the time domain convolution layer C t The output result is input to the spatial convolution layer C s , apply a spatial convolution layer C with a kernel size of 1×C s To fuse the information of each EEG signal channel, the spatial convolution kernel C is set to 3; finally, the output of the spatial convolution layer is input to the batch normalization layer to normalize the distribution of batch data; The operation flow of the feature extraction module based on convolutional neural network is expressed as follows: F=BN(C s (C t (X))) (1) Among them, F represents the output of the feature extraction module based on the convolutional neural network, BN represents the batch normalization layer, and C s represents the spatial convolutional layer, C t represents the time domain convolutional layer, X represents the EEG signal after data preprocessing in step 2 or the mirror EEG signal after the left and right hand movement imagination labels are swapped generated in step 3; 4.
2. Use the multi-head self-attention module based on sliding time window to establish a multi-head self-attention layer along the time dimension to capture the discriminative features of EEG signals and control the computational load at the same time; the multi-head self-attention module based on sliding time window includes a multi-head self-attention module based on time domain window and a multi-head self-attention module based on sliding time domain window in turn. The specific work of each sub-module is as follows: 4.2.
1. A multi-head self-attention module based on a time domain window is used to calculate the local time domain attention score; the multi-head self-attention module based on a time domain window is composed of a first normalization layer, a time domain window multi-head self-attention layer integrated in a residual network structure, a second normalization layer, and a multi-layer perceptron layer integrated in a residual network structure. The following describes in detail the working content of each layer in the multi-head self-attention module based on a time domain window: 1) The first normalization layer: perform layer normalization on the output result F of the feature extraction module based on the convolutional neural network in step 4.1 to normalize the value of the feature dimension of each EEG signal; 2) Time-domain window multi-head self-attention layer: It is used to extract the local time-domain attention score of the input vector in the time dimension and calculate the attention score in a single window. Specifically, it takes the output features of 1) the first normalization layer as input, and then divides it into a set of non-overlapping local windows of size M; performs self-attention score calculation in each local window to enhance the model's perception of local information and improve computational efficiency, as shown in Figure 3; the window size M of the time-domain window multi-head self-attention layer is set to 8; The specific calculation steps for performing self-attention score calculation in each local window are as follows: First, the output features of the first normalization layer are linearly transformed to obtain three representations: query Q, key K and value V, as shown in formula (2); then, attention calculation is performed on query Q, key K and value V. The attention calculation is to obtain the attention score by calculating the dot product similarity between the query and the key, and then obtain the attention weight by performing a Softmax operation. The value vector is then weighted and summed using the attention weight to obtain the self-attention representation of each position, as shown in formula (3); finally, the self-attention representation of each attention head is weightedly concatenated and residually connected with the output result F of the feature extraction module based on the convolutional neural network in step 4.1 to obtain the output result O, as shown in formula (4); Among them, the expressions of formula (2) to formula (4) are as follows: In formula (2), 1) represents the output features of the first normalization layer, LN represents the layer normalization layer, W i Q represents the weight matrix corresponding to the i-th attention head query Q, W i K represents the weight matrix corresponding to the i-th attention head key K, W i V represents the weight matrix corresponding to the i-th attention head value V, Q i represents the query of the ith attention head, K i represents the key of the i-th attention head, V i Represents the value of the i-th attention head; In formula (3), h i represents the self-attention representation of the i-th attention head, Attention represents the self-attention calculation, Q i ,K i ,V i Consistent with formula (2), Softmax represents the Softmax operation, d k represents the dimension of the key (K), and T represents the transpose operation; O=concat(h1,…,h i ,…,h H )W o +F (4) In formula (4), h i represents the self-attention representation of the i-th attention head, i ranges from 1 to H, H represents the number of attention heads, W o represents the linear transformation matrix, F represents the output result of the feature extraction module based on convolutional neural network in 4.1; 3) The second normalization layer: perform layer normalization on the output result O of the time domain window multi-head self-attention layer, normalize the value of the feature dimension of each EEG signal, and obtain the output result of the second normalization layer 4) Multilayer perceptron layer: It is composed of a fully connected layer, a GELU activation function layer, and another fully connected layer in sequence; the output result of the second normalization layer is calculated, and the output result of the second normalization layer is added to the output result of the multilayer perceptron layer to generate the final output A of the multi-head self-attention module based on the time domain window. The specific operation process is expressed as follows: In formula (5), A represents the final output of the multi-head self-attention module based on the time domain window, O represents the output result of the multi-head self-attention layer of the time domain window, FC represents the fully connected layer, GELU represents the GELU activation function layer, Represents the output result of the second normalization layer; 4.2.
2. Use a multi-head self-attention module based on a sliding time domain window to promote information exchange between adjacent local windows; the multi-head self-attention module based on a sliding time domain window is composed of a third normalization layer, a sliding time domain window multi-head self-attention layer integrated in a residual network structure, a fourth normalization layer, and a multi-layer perceptron layer integrated in a residual network structure; the following describes in detail the working content of each layer in the multi-head self-attention module based on a sliding time domain window: 1) The third normalization layer: the final output A of the multi-head self-attention module based on the time domain window is normalized to normalize the value of the feature dimension of each EEG signal; 2) Sliding time-domain window multi-head self-attention layer: Calculate the attention score in the cyclically shifted local window for information interaction; The sliding time-domain window multi-head self-attention layer shifts the time-domain window to the right with a step size of M / 2 to obtain a new local window, and calculates the self-attention score in the new local window; The specific calculation steps for performing self-attention score calculation in each new local window are as follows: First, the output features of the third normalization layer are linearly transformed to obtain three representations: query Q, key K and value V; then, attention calculation is performed on query Q, key K and value V. The attention calculation is to obtain the attention score by calculating the dot product similarity between the query and the key, and then obtain the attention weight through Softmax operation. The value vector is weighted and summed using the attention weight to obtain the self-attention representation of each position; finally, the self-attention representation of each attention head is weightedly spliced, and the output result A of the multi-head self-attention module based on the time domain window in step 4.2.1 is residually connected to obtain the final output result of the sliding time domain window multi-head self-attention layer; 3) The fourth normalization layer: normalizes the output results of the sliding time domain window multi-head self-attention layer, and normalizes the value of each EEG signal on the feature dimension; 4) Multilayer perceptron layer: It consists of a fully connected layer, a GELU activation function layer, and another fully connected layer in sequence. It calculates the output of the fourth normalization layer and adds the output of the fourth normalization layer to the output of the multilayer perceptron layer to generate the output of the multi-head self-attention module based on the sliding time window. 4.
3. The output results of the multi-head self-attention module based on the sliding time window are input into the classification module to calculate the predicted probability of the motor imagery task; the classification module includes a square activation function layer, an average pooling layer, a log activation layer and a fully connected layer in sequence, and the specific work of each submodule is as follows; First, the output results of the multi-head self-attention module based on the sliding time window A square activation function layer is used for nonlinear transformation; secondly, an average pooling layer is used to reduce the dimension of the feature; then, a log activation layer is used for nonlinear transformation; finally, the feature is passed to a fully connected layer and the final prediction probability of the model is calculated by Softmax. The calculation process of the classification module is expressed as follows: In formula (8), P represents the predicted probability, Softmax represents the Softmax operation, FC represents the fully connected layer, Log represents the logarithmic activation layer, AvgPool represents the average pooling layer, and Square represents the square activation function layer. Represents the output of the multi-head self-attention module based on the sliding time window.
7. The motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer according to claim 6, characterized in that: Step 5 is as follows: The output features of the multi-head self-attention module based on the sliding time window in step 4 Input into calculation formula (9) to calculate the Euclidean distance between every two sample pairs in a batch; then use calculation formula (10) to perform weighted summation of the Euclidean distances of all sample pairs to obtain the final image comparison loss; The calculation formula of Euclidean distance is as follows: In formula (9), D ij represents the Euclidean distance between sample pairs (i, j), represents the output feature of the multi-head self-attention module based on the sliding time window of the i-th sample, represents the output feature of the multi-head self-attention module based on the sliding time window of the j-th sample, and n represents the n-th coordinate; The calculation formula of image contrast loss is as follows: L d =w o ∑ i,j∈OE g ij D ij +w m ∑ i∈ME,j∈OE g ij D ij (10) In formula (10), L d represents the image contrast loss, w o represents the weight of the contrast loss of the original EEG signal, w m represents the weight of the contrast loss of the mirror EEG signal, w o and w m It is set to 0.2 and 0.3 according to experience, i, j represents two sample pairs from the original EEG signal set or the mirror EEG signal set, OE represents the original EEG signal set, ME represents the mirror EEG signal set, D ij Represents the Euclidean distance between sample pairs (i, j); when i and j are positive sample pairs, the variable g ij The value is 1; when i, j is a negative sample pair, the variable g ij The value is -1.
8. The motor imagery EEG signal recognition algorithm based on mirror contrast loss and sliding window Transformer according to claim 7 is characterized in that: In step 6, the classification loss calculation process is: The final output prediction probability p of the sliding window Transformer model in step 4 for each motor imagery EEG trial i The actual label is input into the classification loss for calculation, and the mirror contrast loss generated in step 5 is added to obtain the final loss function of the model training. The specific calculation method is as follows: Among them, L c represents the classification loss, N is the total number of motor imagery EEG trials, i represents the i-th motor imagery EEG trial, y i represents the true label of the ith motor imagery EEG trial, P = {p1,…,p i ,…,p N }, P represents the final output prediction probability of the sliding window Transformer model, p i represents the predicted probability of the ith motor imagery EEG trial, and ln represents logarithmic calculation; In step 6, the final loss function of the confirmed model training is the sum of the image contrast loss and the classification loss: L f =L c +L d (12) In formula (12), L f Represents the final loss function of model training, L c represents the classification loss obtained in step 6, L d represents the mirror contrast loss obtained in step 5.
