Method for identifying QRS waves in electrocardiosignals based on multi-head attention mechanism GRU network
By introducing a multi-head attention mechanism GRU network into the recurrent neural network, the problem of low accuracy of single-head attention mechanism for long-term time series recognition is solved, the recognition accuracy and recognition speed of QRS waves are improved, and the recognition speed of QRS waves is achieved. Prediction accuracy and stability.
Patent Information
- Application Number
- CN202411108815.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-13
- Publication Date
- 2024-12-10
AI Technical Summary
In the prior art, the recurrent neural network using a single-head attention mechanism has low accuracy in recognition of long-term sequences, resulting in low accuracy in recognition of QRS waves.
Introduce a GRU network to learn the time series dependence relationship through the GRU layer. The multi-head attention layer weights the fusion information according to the importance of each time step, and uses the feedforward neural network layer for forward propagation to improve identification accuracy. Spend.
It improves the recognition accuracy and recognition speed of QRS waves, can better capture important information in the electrocardiogram signal, and improves the accuracy and stability of prediction.
Smart Images

Figure CN119097324A_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of electrocardiogram signal recognition, and specifically is a method for recognizing QRS waves in electrocardiogram signals based on a multi-head attention mechanism GRU network. Background Art
[0002] The electrocardiogram (ECG) records the electrophysiological signals of the heartbeat. The waveform curve changes in each cardiac cycle on a normal ECG are regular. The most important section is called the QRS wave, which reflects the changes in the depolarization potential and time of the left and right ventricles.
[0003] The invention patent application with the publication number CN109871742A discloses an ECG signal positioning method based on an attention recurrent neural network. The method constructs an attention recurrent neural network based on a heartbeat feature vector sequence, trains the attention recurrent neural network based on a cross entropy loss function, and obtains a target ECG signal; the target ECG signal is divided into a number of time-continuous target heartbeat segment signals according to the position of the heartbeat in the target ECG signal, and the characteristic value of each target heartbeat segment signal is extracted, and the characteristic values of all target heartbeat segment signals are combined into a target heartbeat feature vector sequence in time order; the target heartbeat feature vector sequence is input into the trained attention recurrent neural network, and the classification result and abnormal heartbeat position corresponding to the target heartbeat feature vector are obtained. The method uses a recurrent neural network to identify QRS waves. Since the recurrent neural network uses a single attention mechanism, less important information can be identified for a long time sequence, so the recognition accuracy of the QRS wave is not high.
[0004] The present application discloses a method for identifying QRS waves in electrocardiogram signals based on a multi-head attention mechanism GRU network to solve the above-mentioned technical problems. Summary of the invention
[0005] The present application aims to solve at least one of the technical problems existing in the prior art; to this end, the present application proposes a QRS wave recognition method in an ECG signal based on a multi-head attention mechanism GRU network, which is used to solve the technical problem that the prior art uses a single-head attention mechanism to recognize a long time series, which can only recognize a small amount of important information, resulting in low recognition accuracy of the QRS wave in the ECG signal. By introducing a multi-head attention mechanism, the present application can better capture important information in the ECG signal and improve the accuracy and stability of the prediction.
[0006] To achieve the above objectives, the first aspect of the present application provides a QRS wave recognition method in an electrocardiogram signal based on a multi-head attention mechanism GRU network, comprising:
[0007] Acquire an ECG signal and mark it as an original signal; process the original signal through a bandpass filter to obtain a filtered signal; downsample the filtered signal to obtain a sampled signal; wherein the downsampling factor is 4;
[0008] The sampling signal is randomly intercepted to obtain a number of samples; the positions in the samples are marked with features as labels of the samples; the sample groups are divided into a test set and a training set according to a set ratio; wherein the size of the sample is determined by the sampling duration and the sampling rate, and the sample group includes the sample and the label corresponding to the sample;
[0009] Constructing a basic recognition model; training the basic recognition model through a training set to obtain a QRS recognition model; wherein the basic recognition model includes a GRU layer, a multi-head attention layer, and a feedforward neural network layer in sequence;
[0010] The recognition result of the QRS recognition model is evaluated for accuracy through a test set; after the accuracy evaluation is qualified, the QRS recognition model is saved for use in identifying the QRS wave in the electrocardiogram signal.
[0011] Preferably, the frequency range of the bandpass filter is [0.5 Hz-40 Hz].
[0012] Preferably, the set ratio includes 4:1 or 3:2.
[0013] Preferably, randomly intercepting the sampling signal comprises:
[0014] Set the capture parameters, including capture duration and sampling rate.
[0015] The filtered signal is randomly intercepted based on the interception parameter to obtain a number of samples; wherein the size of the samples is determined by the interception parameter.
[0016] Preferably, the setting of interception parameters includes:
[0017] Set the capture duration to 12s and the sampling frequency to 500Hz.
[0018] Preferably, the step of marking the positions in the plurality of samples includes:
[0019] The positions of the Q wave, R wave and S wave in the sample are marked as Z1, Z2 and Z3 respectively, and the positions other than the Q wave, R wave and S wave are marked as Z4; wherein Z1, Z2, Z3 and Z4 are all integers;
[0020] The position marks of the Q wave, R wave, S wave and other positions in the sample are used as labels of the sample; wherein the label is consistent with the size of the corresponding sample.
[0021] Preferably, the parameters of the GRU layer are set as follows: the signal length of the input signal is 6000, the batch size is set to 8, the number of hidden layer features is set to 4, and the nonlinear function is set to tanh; the GRU function in PyTorch is used, and batch_first is set to True.
[0022] Preferably, the parameter setting of the multi-head attention layer is: set 8 attention heads, and the weight matrix W of each attention head is Q , W K , W V The dimension of the matrix is [4,3];
[0023] The calculation of the query matrix Q, key matrix K and value matrix V of each attention head includes:
[0024] Q=X*W Q , K=X*W K , V=X*W V ; Among them, the dimensions of Q, K, and V are [4, 6000, 3], and X is the output data of the GRU layer;
[0025] Z0=Softmax(Q*K T / sqrt(d))*V; the dimension of Z0 matrix is [4,6000,3], which is K T The transpose of the key matrix K, d is the hidden layer feature dimension, which is 4;
[0026] Concatenate 8 groups of Z0 matrices to obtain Z1 matrix, the matrix dimension of Z1 matrix is [4,6000,24]; set the matrix dimension of weight W0 to [24,8], and calculate matrix Z by formula Z=Z1*W0; where matrix Z is the matrix after all header information is fused, and the dimension of matrix Z is [4,6000,8].
[0027] Preferably, the feedforward neural network layer includes a fully connected layer FC1, an activation layer, a Dropout layer, a fully connected layer FC2, and a Sigmoid layer in sequence;
[0028] Fully connected layer FC1: the number of input features is set to 48000, the number of output features is set to 24000, and the dimension of the output data is [4, 24000];
[0029] Activation layer: Use ReLU activation function, the dimensions of input data and output data remain unchanged;
[0030] Fully connected layer FC2: the number of input features is set to 24000, the number of output features is set to 6000, and the dimension of the output data is [4, 6000];
[0031] Dropout layer: parameter is set to 0.2;
[0032] Sigmoid layer: The classification threshold is set to 0.8; when it is greater than 0.8, the output is 1, otherwise the output is 0.
[0033] Preferably, training the basic recognition model through a training set includes:
[0034] Inputting the training set into the basic recognition model for training, and using a binary cross entropy loss function to calculate the loss value of each sample during the training process;
[0035] The Adam optimizer is used to optimize the network parameters of the basic recognition model until the loss value and the accuracy rate tend to be stable, and the training is stopped; the network parameters and model structure information of the current basic recognition model are saved to obtain the QRS recognition model.
[0036] Preferably, the accuracy of the recognition result of the QRS recognition model is evaluated by a test set, including:
[0037] Input the test set into the QRS recognition model, and run the QRS recognition model to obtain an output vector corresponding to the sample in the test set;
[0038] The output vector is compared with the QRS wave time point of the sample corresponding label to calculate the accuracy; when the accuracy is greater than the set threshold, the accuracy evaluation is qualified.
[0039] Compared with the prior art, the beneficial effects of the present application are: the basic recognition model of the present application includes a GRU layer, a multi-head attention layer and a feedforward neural network layer in sequence; the GRU layer is used to learn the temporal dependencies in the time series, thereby capturing the long-term dependency information in the sequence; the multi-head attention layer performs weighted fusion on the output of the GRU layer according to the importance of each time step in the sequence; the feedforward neural network layer forward propagates the matrix that incorporates the multi-head attention information, and finally gives a prediction result; the present application can extract important information on the time series, improve the recognition accuracy of the QRS wave, and at the same time improve the recognition speed. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0041] Figure 1 This is a schematic diagram of the network structure of the basic recognition model in the embodiment of the present application;
[0042] Figure 2 Schematic diagram of the network structure of the feedforward neural network layer of an embodiment of the present application. DETAILED DESCRIPTION
[0043] The technical solution of the present application will be described clearly and completely below in conjunction with the embodiments. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field without creative work are within the scope of protection of the present application.
[0044] See also Figure 1-Figure 2 The first aspect of the present application provides a method for identifying QRS waves in an electrocardiogram signal based on a multi-head attention mechanism GRU network, including:
[0045] Acquire an ECG signal and mark it as an original signal; process the original signal through a bandpass filter to obtain a filtered signal; downsample the filtered signal to obtain a sampled signal; wherein the downsampling factor is 4;
[0046] The sampling signal is randomly intercepted to obtain a number of samples; the positions in the samples are marked with features as labels of the samples; the sample groups are divided into a test set and a training set according to a set ratio; wherein the size of the sample is determined by the sampling duration and the sampling rate, and the sample group includes the sample and the label corresponding to the sample;
[0047] Constructing a basic recognition model; training the basic recognition model through a training set to obtain a QRS recognition model; wherein the basic recognition model includes a GRU layer, a multi-head attention layer, and a feedforward neural network layer in sequence;
[0048] The recognition result of the QRS recognition model is evaluated for accuracy through a test set; after the accuracy evaluation is qualified, the QRS recognition model is saved for use in identifying the QRS wave in the electrocardiogram signal.
[0049] Next, the QRS wave recognition method in the electrocardiogram signal based on the multi-head attention mechanism GRU network proposed in this application is described in detail.
[0050] 1) Obtain an ECG signal and mark it as an original signal; input the original signal into a designed bandpass filter for filtering to remove baseline drift and noise interference, and obtain a filtered signal. In this embodiment, the frequency range of the bandpass filter is [0.5 Hz-40 Hz].
[0051] It should be noted that the ECG signal can be obtained through an electrocardiograph or a wearable device, and of course the ECG signal data stored in an existing database can also be used.
[0052] 2) Down-sample the filtered signal, reducing the sampling frequency of 2000Hz to 500Hz, that is, the down-sampling factor is 4, to obtain a sampled signal. On the one hand, the waveform trend of the down-sampled signal is consistent with the original waveform, and the error of the QRS wave does not exceed 1ms; on the other hand, the data set is reduced by 4 times, which can greatly reduce the amount of calculation of the subsequent model and improve the model recognition speed.
[0053] 3) The interception time is set to 12s, and the sampling frequency of the ECG signal is 500Hz; the filtered signal is randomly intercepted according to the set interception time and sampling frequency to obtain a number of samples; the size of each sample is 6000 (12×500=6000).
[0054] It should be noted that the random interception of the signal specifically refers to determining the interception window according to the interception duration, randomly selecting a starting point in the continuous ECG signal, and then determining the signal segment according to the interception window, and then extracting the sampling points in the signal segment according to the sampling rate. Specifically in this embodiment, the interception window is randomly determined in the signal with a length of 12s, and then the sampling points are extracted in the interception window at a sampling rate of 500Hz, and a total of 6000 sampling points can be extracted from each interception window.
[0055] 4) Mark the positions of the Q wave, R wave, and S wave in each sample as 1, 2, and 3 respectively, and mark the positions of other non-Q waves, R waves, and S waves in the sample as 0, and use these position marks as the label of the sample. Obviously, the label size is consistent with the sample size, both of which are 6000.
[0056] 5) Take each sample and its corresponding label as a sample group, and divide the sample group according to the set ratio to obtain the training set and the test set. The optimal ratio is 4:1, that is, the ratio of the number of sample groups in the training set and the test set is 4:1. Of course, other ratios can also be selected, such as 3:2.
[0057] 6) Build a basic recognition model according to the structure of GRU layer → multi-head attention layer → feedforward neural network layer. Specific parameter settings include:
[0058] Parameter settings for the GRU layer: the signal length of the input signal is 6000, the batch size is set to 8, the number of hidden layer features is set to 4, and the nonlinear function is set to tanh; the GRU function in PyTorch is used, and batch_first is set to True; the dimension of the input data of this layer is [4, 6000, 1]; the dimension of the output data is [4, 6000, 4], and the dimension of the output hidden layer data is [1, 4, 4];
[0059] Parameter setting of multi-head attention layer: Set 8 attention heads, and the weight matrix W of each attention head Q , W K , WV The matrix dimensions are [4,3]; the calculation of the query matrix Q, key matrix K, and value matrix V of each attention head includes:
[0060] Q=X*W Q , K=X*W K , V=X*W V ; Among them, the dimensions of Q, K, and V are [4, 6000, 3], and X is the output data of the GRU layer;
[0061] Z0=Softmax(Q*K T / sqrt(d))*V; the dimension of Z0 matrix is [4,6000,3], which is K T The transpose of the key matrix K, d is the hidden layer feature dimension, which is 4;
[0062] Concatenate 8 groups of Z0 matrices to obtain Z1 matrix, the matrix dimension of which is [4,6000,24]; set the matrix dimension of weight W0 to [24,8], and calculate matrix Z using the formula Z=Z1*W0; where matrix Z is the matrix after all header information is fused, and the dimension of matrix Z is [4,6000,8];
[0063] The feedforward neural network layers include the fully connected layer FC1, activation layer, Dropout layer, fully connected layer FC2, and Sigmoid layer in sequence;
[0064] Fully connected layer FC1: the number of input features is set to 48000, the number of output features is set to 24000, and the dimension of the output data is [4, 24000];
[0065] Activation layer: Use ReLU activation function, the dimensions of input data and output data remain unchanged;
[0066] Fully connected layer FC2: the number of input features is set to 24000, the number of output features is set to 6000, and the dimension of the output data is [4, 6000];
[0067] Dropout layer: parameter is set to 0.2;
[0068] Sigmoid layer: The classification threshold is set to 0.8; when it is greater than 0.8, the output is 1, otherwise the output is 0.
[0069] The basic recognition model of this embodiment includes a GRU layer, a multi-head attention layer and a feedforward neural network layer in sequence. The GRU layer is used to learn the temporal dependencies in the time series, so as to capture the long-term dependency information in the sequence and solve the problem that each time step of the input sequence is treated equally without considering the importance differences of different time steps. The multi-head attention layer performs weighted fusion on the output of the GRU layer according to the importance of each time step in the sequence; the information of different time steps can be weighted according to the importance of each time step in the sequence, so that the model can pay more attention to those time steps that have a greater impact on the prediction results. For long time series, the introduction of multi-head attention further enhances the model's ability to pay attention to different positions, and provides multiple subspaces of the attention layer, thereby improving the accuracy of the prediction. The feedforward neural network layer forward propagates the matrix that integrates the multi-head attention information, and finally gives the prediction result.
[0070] 7) Network training; input the training set into the basic recognition model for training, and use the binary cross entropy loss function to calculate the loss value of each sample during the training process; use the Adam optimizer to optimize the network parameters of the basic recognition model until the loss value and accuracy are stable near a certain value, then stop the training; save the network parameters and model structure information of the current basic recognition model to obtain the QRS recognition model.
[0071] 8) Network test: Input all the divided test set samples into the saved QRS recognition model, turn off the Dropout layer in the network, and run the QRS recognition model to obtain the predicted value vector output y_pred corresponding to the samples in the test set, compare the QRS wave time point with the sample label, and calculate the accuracy. If the accuracy is 99%, set the threshold to 97%, because 99%>97%, the accuracy is qualified.
[0072] In this embodiment, since the signal sampling rate is 500Hz, if the predicted value appears in a window with a total length of 150 centered on each of the marked QRS points, it is accurate. This accuracy evaluation method refers to the American national standard formulated by the Association for the Advancement of Medical Devices, that is, if the QRS wave detection point obtained by the algorithm is within 150ms before and after the marked position, it can be determined that the detected QRS wave position is accurate. Specifically, 150ms before and after the QRS wave detection point, that is, the time window length that can be allowed for the detection point to be accurately determined is 150*2=300ms, that is, 0.3s. Since the sampling rate is 2000hz, the number of sampling points corresponding to 0.3s is: 2000*0.3=600, that is, the sampling window length that can be allowed for the detection point to be accurately determined is 600.
[0073] The above embodiments are only used to illustrate the technical method of the present application and are not intended to limit it. Although the present application has been described in detail with reference to the preferred embodiments, a person of ordinary skill in the art should understand that the technical method of the present application may be modified or replaced by equivalents without departing from the spirit and scope of the technical method of the present application.
Claims
1. A QRS wave recognition method in ECG signals based on a multi-head attention mechanism GRU network, characterized in that: include: Acquire an electrocardiogram signal and mark it as an original signal; process the original signal through a bandpass filter to obtain a filtered signal; Downsampling the filtered signal to obtain a sampled signal; wherein the downsampling factor is 4; The sampling signal is randomly intercepted to obtain a number of samples; the positions in the samples are marked with features as labels of the samples; the sample groups are divided into a test set and a training set according to a set ratio; wherein the size of the sample is determined by the sampling duration and the sampling rate, and the sample group includes the sample and the label corresponding to the sample, and the set ratio is 4:1; Constructing a basic recognition model; training the basic recognition model through a training set to obtain a QRS recognition model; wherein the basic recognition model includes a GRU layer, a multi-head attention layer, and a feedforward neural network layer in sequence; The recognition result of the QRS recognition model is evaluated for accuracy through a test set; after the accuracy evaluation is qualified, the QRS recognition model is saved for use in identifying the QRS wave in the electrocardiogram signal.
2. The QRS wave recognition method in the electrocardiogram signal based on the multi-head attention mechanism GRU network according to claim 1 is characterized in that: The frequency range of the bandpass filter is [0.5 Hz-40 Hz].
3. The QRS wave recognition method in the electrocardiogram signal based on the multi-head attention mechanism GRU network according to claim 1 is characterized in that: Randomly intercepting the sampling signal, comprising: Set the capture parameters, including capture duration and sampling rate. The filtered signal is randomly intercepted based on the interception parameter to obtain a number of samples; wherein the size of the samples is determined by the interception parameter.
4. The QRS wave recognition method in the electrocardiogram signal based on the multi-head attention mechanism GRU network according to claim 3 is characterized in that: The setting of interception parameters includes: Set the capture duration to 12s and the sampling frequency to 500Hz.
5. The QRS wave recognition method in the electrocardiogram signal based on the multi-head attention mechanism GRU network according to claim 1 is characterized in that: The feature marking of the positions in the samples comprises: The positions of the Q wave, R wave and S wave in the sample are marked as Z1, Z2 and Z3 respectively, and the positions other than the Q wave, R wave and S wave are marked as Z4; wherein Z1, Z2, Z3 and Z4 are all integers; The position marks of the Q wave, R wave, S wave and other positions in the sample are used as labels of the sample; wherein the label is consistent with the size of the corresponding sample.
6. The QRS wave recognition method in the electrocardiogram signal based on the multi-head attention mechanism GRU network according to claim 1 is characterized in that: The parameter settings of the GRU layer are as follows: the signal length of the input signal is 6000, the batch size is set to 8, the number of hidden layer features is set to 4, and the nonlinear function is set to tanh; the GRU function in PyTorch is used, and batch_first is set to True.
7. The QRS wave recognition method in the electrocardiogram signal based on the multi-head attention mechanism GRU network according to claim 1 is characterized in that: The parameter setting of the multi-head attention layer: set 8 attention heads, and the weight matrix W of each attention head Q , W K , W V The dimension of the matrix is [4,3]; The calculation of the query matrix Q, key matrix K and value matrix V of each attention head includes: Q=X*W Q , K=X*W K , V=X*W V ; Among them, the dimensions of Q, K, and V are [4, 6000, 3], and X is the output data of the GRU layer; Z0=Softmax(Q*K T / sqrt(d))*V; the dimension of Z0 matrix is [4,6000,3], which is K T The transpose of the key matrix K, d is the hidden layer feature dimension; Concatenate 8 groups of Z0 matrices to obtain Z1 matrix, the matrix dimension of Z1 matrix is [4,6000,24]; set the matrix dimension of weight W0 to [24,8], and calculate matrix Z by formula Z=Z1*W0; where matrix Z is the matrix after all header information is fused, and the dimension of matrix Z is [4,6000,8].
8. The QRS wave recognition method in the electrocardiogram signal based on the multi-head attention mechanism GRU network according to claim 1 is characterized in that: The feedforward neural network layer includes a fully connected layer FC1, an activation layer, a Dropout layer, a fully connected layer FC2, and a Sigmoid layer in sequence; Fully connected layer FC1: the number of input features is set to 48000, the number of output features is set to 24000, and the dimension of the output data is [4, 24000]; Activation layer: Use ReLU activation function, the dimensions of input data and output data remain unchanged; Fully connected layer FC2: the number of input features is set to 24000, the number of output features is set to 6000, and the dimension of the output data is [4, 6000]; Dropout layer: parameter is set to 0.2; Sigmoid layer: The classification threshold is set to 0.8; when it is greater than 0.8, the output is 1, otherwise the output is 0.
9. The QRS wave recognition method in the electrocardiogram signal based on the multi-head attention mechanism GRU network according to claim 1 is characterized in that: The basic recognition model is trained by a training set, including: Inputting the training set into the basic recognition model for training, and using a binary cross entropy loss function to calculate the loss value of each sample during the training process; The Adam optimizer is used to optimize the network parameters of the basic recognition model until the loss value and the accuracy rate tend to be stable, and the training is stopped; the network parameters and model structure information of the current basic recognition model are saved to obtain the QRS recognition model.
10. The QRS wave recognition method in the electrocardiogram signal based on the multi-head attention mechanism GRU network according to claim 1 is characterized in that: The accuracy of the recognition result of the QRS recognition model is evaluated by using a test set, including: Input the test set into the QRS recognition model, and run the QRS recognition model to obtain an output vector corresponding to the sample in the test set; The output vector is compared with the QRS wave time point of the sample corresponding label to calculate the accuracy; when the accuracy is greater than the set threshold, the accuracy evaluation is qualified.
Citation Information
Patent Citations
Electrocardiosignal positioning method based on an attention recurrent neural network
CN109871742A
Cited By
Electrocardio gating scanning control method and system based on multi-model fusion algorithm
CN121667735A
Electrocardiogram-gated scanning control method and system based on multi-model fusion algorithm
CN121667735B
