Electrocardiogram sequence data classification method based on convolutional neural network and multi-head attention mechanism
By combining convolutional neural networks with multi-head attention mechanisms, local and global features of electrocardiogram (ECG) signals are extracted, solving the problem that existing models cannot effectively model global temporal relationships. This achieves high accuracy and stability in ECG classification and is suitable for ECG analysis in various hardware environments.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- SECOND AFFILIATED HOSPITAL OF COLLEGE OF MEDICINEOF XIAN JIAOTONG UNIV
- Filing Date
- 2026-01-30
- Publication Date
- 2026-06-02
AI Technical Summary
Existing deep learning models have failed to effectively model the global temporal relationships of ECG signals in ECG classification, resulting in limited classification accuracy, poor generalization performance, and difficulty in meeting clinical needs.
An ECG sequence data classification method based on convolutional neural networks and multi-head attention mechanism is adopted. Local features are extracted through one-dimensional convolution operation, global feature weights are calculated by combining multi-head self-attention mechanism, and adaptive feature fusion is performed to construct ECGNet composite model.
It significantly improves the accuracy of multi-class classification of electrocardiograms and the ability to distinguish complex electrocardiogram signals, enhances the stability and adaptability of the model, and is suitable for real-time analysis and automatic diagnosis in various hardware environments.
Smart Images

Figure CN122136013A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of electrocardiogram (ECG) classification technology, and more specifically, to a method for classifying ECG sequence data based on convolutional neural networks and multi-head attention mechanisms. Background Technology
[0002] Cardiovascular disease is one of the most prevalent and fatal diseases worldwide. Early detection of cardiac abnormalities through electrocardiogram (ECG) is crucial for preventing disease onset and reducing mortality. ECG, as a non-invasive technique for recording cardiac electrical activity, analyzes the morphology, amplitude, and interval relationships of waveforms to determine the physiological or pathological state of the heart, and has become a core tool for cardiovascular disease screening and diagnosis. Traditional ECG analysis relies on manual interpretation by physicians, which is not only time-consuming and labor-intensive but also influenced by subjective experience, making it difficult to meet clinical efficiency requirements when facing large-scale screening needs. Therefore, developing automated, highly accurate ECG classification methods has significant clinical application value.
[0003] With the development of machine learning and deep learning technologies, researchers have proposed various automatic electrocardiogram (ECG) classification methods. Early distance-based methods, such as single nearest neighbor dynamic time warping, classify ECGs by defining sequence similarity metrics, but rely solely on simple distance judgments, resulting in weak feature extraction capabilities and limited classification performance on publicly available ECG datasets. Feature-based methods, such as feature combination models, can fuse multiple features, but their complex structures make it difficult to achieve end-to-end temporal relationship modeling. In recent years, deep learning-based convolutional neural network models have been widely used in ECG classification. These models can automatically extract local waveform features of ECG signals, such as waveform amplitude and morphology, through convolution operations, thus improving classification accuracy to some extent.
[0004] However, existing deep learning models still have significant shortcomings in electrocardiogram (ECG) classification. Current convolutional neural network models primarily extract local features near a single time point through local convolutional kernels, focusing on capturing the local waveform morphology of ECG signals, but failing to effectively model the global dependencies between different time points. Considering that ECG signals are inherently time series with periodicity and long-range dependence, there are close physiological correlations between different waveforms within the same cardiac cycle and between adjacent cardiac cycles. These global relational features are crucial for accurately identifying pathological states such as arrhythmias.
[0005] Meanwhile, due to the lack of explicit modeling capabilities for global temporal relationships, existing models struggle to fully extract the periodic patterns and long-term trend information contained in electrocardiogram (ECG) signals. This results in limited classification accuracy, poor generalization performance, and overall classification precision that fails to meet the actual clinical needs for highly reliable automated ECG diagnosis. Therefore, effectively capturing the global temporal relationships of ECG signals while extracting local waveform features has become a pressing technical challenge for improving the performance of automated ECG classification.
[0006] No effective solutions have yet been proposed to address the problems in the relevant technologies. Summary of the Invention
[0007] To address the problems in related technologies, this invention proposes a classification method for electrocardiogram sequence data based on convolutional neural networks and multi-head attention mechanisms, in order to overcome the aforementioned technical problems existing in existing related technologies.
[0008] Therefore, the specific technical solution adopted by the present invention is as follows:
[0009] A classification method for electrocardiogram (ECG) sequence data based on convolutional neural networks and multi-head attention mechanism, comprising:
[0010] Multivariate time-series ECG data were acquired and preprocessed to obtain preprocessed time-series signal data. This preprocessed time-series signal data was then divided into training, validation, and test sets. A convolutional neural network (CNN) local feature extraction layer was constructed. The preprocessed time-series signal data was input into this CNN, and local feature vectors were extracted through one-dimensional convolution operations. A multi-head attention mechanism relational feature layer was constructed. The local feature vectors were input into this relational feature layer, and the correlation weights between time points were calculated using a multi-head self-attention mechanism combined with an electrophysiological prior weight matrix to obtain a global feature representation. The local feature vectors and global feature representations were adaptively fused to obtain fused features. These fused features were then input into a fully connected layer for classification output to obtain the classification results of the ECG sequence data. A composite model was constructed. The composite model was trained based on the training set, and its parameters were optimized using the validation set. The test set was then input into the trained composite model for performance evaluation.
[0011] Furthermore, the electrocardiogram (ECG) multivariate time series data is preprocessed to obtain preprocessed time series signal data, which is then divided into training, validation, and test sets. This includes: collecting time series signal data from the target domain as ECG multivariate time series data; this time series signal data includes univariate or multivariate signal sequences; performing denoising, normalization, and time synchronization processing on the time series signal data to obtain a time series matrix with a preset length and a preset feature dimension; labeling each sample in the time series matrix with ECG category labels; and stratifying the time series matrix into training, validation, and test sets according to a preset ratio to ensure that the proportion of each ECG category label is consistent with the original proportion of labels in the time series matrix.
[0012] Furthermore, the preprocessed time-series signal data is input into the local feature extraction layer of a convolutional neural network, and the local feature vector is extracted through one-dimensional convolution operations. This includes setting several convolutional blocks in the local feature extraction layer of the convolutional neural network, each convolutional block consisting of two one-dimensional convolutional layers, a batch normalization layer, and an activation layer; inputting the preprocessed time-series signal data into the convolutional blocks, performing convolution calculations on the time axis through convolution kernels, extracting the local temporal features of the preprocessed time-series signal data, and obtaining a feature map; and performing dimensionality reduction processing on the feature map through a pooling layer to obtain the local feature vector.
[0013] Furthermore, the local feature vectors are input into the relational feature layer. A multi-head self-attention mechanism combined with an electrophysiological prior weight matrix is used to calculate the correlation weights between time points, resulting in a global feature representation. This representation includes: mapping local feature vectors to queries, keys, and values in the relational feature layer; segmenting the local feature vectors along the time axis according to the cardiac cycle based on peak location and average cardiac cycle information in the preprocessed time-series signal data to construct an electrophysiological prior weight matrix; calculating the correlation score between different time points within each attention head of the relational feature layer; fusing the correlation score element-wise with the electrophysiological prior weight matrix to obtain a weighted correlation matrix; and concatenating the outputs of all attention heads in the relational feature layer and outputting the global feature representation through a linear transformation.
[0014] Furthermore, within each attention head of the relation feature layer, the correlation score between different time points is calculated. The correlation score is then fused element-wise with the electrocardiographic prior weight matrix to obtain a weighted correlation matrix. This process includes: obtaining an initial correlation score by performing a dot product operation on the query and the key; scaling the initial correlation score to avoid excessively large dot product values that could cause gradient vanishing; multiplying the scaled correlation score element-wise with the electrocardiographic prior weight matrix to obtain a fused correlation score; normalizing the fused correlation score to obtain an attention weight matrix, thereby converting the fused correlation score into non-negative weights that sum to a preset value; and multiplying the attention weight matrix by the values to obtain the attention output, thus selectively emphasizing the electrocardiographic feature waveform.
[0015] Furthermore, adaptive feature fusion of local feature vectors and global feature representations is performed to obtain fused features, which are then input into a fully connected layer for classification output. This process includes: aligning local feature vectors and global feature representations according to the time dimension and cardiac cycle; weighting the aligned local feature vectors and global feature representations at the lead level based on the multi-lead structure of the electrocardiogram; inputting the weighted local feature vectors and global feature representations into a gating unit to generate dynamic fusion coefficients; performing weighted fusion of the weighted local feature vectors and global feature representations based on the dynamic fusion coefficients to obtain fused features; and inputting the fused features into a fully connected layer, which, after nonlinear transformation, outputs the multi-class classification probabilities of the electrocardiogram sequence data.
[0016] The beneficial effects of this invention are as follows:
[0017] (1) By setting up a local feature extraction layer of a convolutional neural network and a relational feature layer of a multi-head attention mechanism in parallel, this invention can simultaneously extract the local waveform features of the electrocardiogram signal and the global dependency relationship between different time points, solving the problem that the prior art only focuses on local features at a single time point and ignores the global temporal relationship; at the same time, by adaptively fusing the local feature vector output by the convolutional layer with the global feature representation output by the relational feature layer and then feeding it into the fully connected layer for classification, the joint modeling of local morphological information of the electrocardiogram waveform and global rhythm information across time points is realized, which significantly improves the accuracy of multi-class classification of electrocardiogram and the ability to distinguish complex electrocardiogram signals.
[0018] (2) In this invention, an electrophysiological prior weight matrix is introduced into the relation feature layer. The time point pairs that satisfy the electrophysiological sequence relationship within the same cardiac cycle and in adjacent cardiac cycles are given higher weights. The weights of time point pairs with long time intervals or weak physiological correlations are attenuated. This enables the multi-head self-attention mechanism to integrate the physiological laws of the electrocardiogram signal when calculating the correlation weights between time points. In addition, the combination of residual connection and layer normalization operations enhances the stability of model training, enabling the model to effectively capture the periodic features and long-term temporal dependencies of the electrocardiogram signal, and improves the ability to identify complex rhythm changes and cardiac conduction abnormalities.
[0019] (3) The local feature extraction layer of the convolutional neural network and the relational feature layer of the multi-head attention mechanism adopted in this invention are both designed with a one-dimensional structure. The model has a moderate number of parameters and high computational efficiency, avoiding the problems of complex structure and high computational resource consumption of traditional deep learning models. The model can be trained and inferred on conventional computing devices, which makes it easy to deploy in various hardware environments such as hospital information systems, portable electrocardiogram monitors and wearable devices, meeting the needs of real-time analysis and automatic diagnosis of electrocardiograms in different application scenarios.
[0020] (4) This invention assigns higher weights to clinically high-risk ECG categories such as atrial fibrillation and premature ventricular contractions, which have fewer samples, by introducing a cross-entropy loss function with category weights. It also adopts a difficult sample resampling mechanism to increase the sampling probability of samples that are continuously misclassified, which effectively solves the problem of model bias towards the majority class caused by the uneven distribution of ECG data categories. By monitoring accuracy and recall in real time on the validation set and using an early stop strategy to roll back to the optimal model parameters, the overfitting phenomenon of the model is avoided. After optimization by reasonable feature fusion method and training strategy, it has good generalization and robustness and is suitable for various multi-lead ECG sequence classification scenarios. Attached Figure Description
[0021] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0022] Figure 1 This is a diagram of the ECGNet composite model framework in the electrocardiogram sequence data classification method based on convolutional neural network and multi-head attention mechanism according to an embodiment of the present invention;
[0023] Figure 2 This is a diagram of the relational feature block architecture in the electrocardiogram sequence data classification method based on convolutional neural network and multi-head attention mechanism according to an embodiment of the present invention;
[0024] Figure 3 This is a diagram of the convolutional residual block architecture in the electrocardiogram sequence data classification method based on convolutional neural networks and multi-head attention mechanism according to an embodiment of the present invention;
[0025] Figure 4 This is a specific implementation diagram of the classification output of the fully connected layer in the electrocardiogram sequence data classification method based on convolutional neural network and multi-head attention mechanism according to an embodiment of the present invention;
[0026] Figure 5 This is a flowchart illustrating the electrocardiogram sequence data classification method based on convolutional neural networks and multi-head attention mechanism according to an embodiment of the present invention.
[0027] Figure 6 This is a block diagram illustrating the principle of an electrocardiogram sequence data classification system based on convolutional neural networks and multi-head attention mechanisms according to an embodiment of the present invention. Detailed Implementation
[0028] To further illustrate the various embodiments, the present invention provides accompanying drawings, which are part of the disclosure of the present invention. These drawings are mainly used to illustrate the embodiments and can be used in conjunction with the relevant descriptions in the specification to explain the operating principles of the embodiments. With reference to these drawings, those skilled in the art should be able to understand other possible implementation methods and the advantages of the present invention. The components in the drawings are not drawn to scale, and similar component symbols are generally used to represent similar components.
[0029] According to embodiments of the present invention, a method for classifying electrocardiogram sequence data based on convolutional neural networks and multi-head attention mechanisms is provided.
[0030] The present invention will now be further described in conjunction with the accompanying drawings and specific embodiments, such as... Figure 5As shown, according to an embodiment of the present invention, a method for classifying electrocardiogram (ECG) sequence data based on convolutional neural networks and multi-head attention mechanisms is provided. This method includes: acquiring multivariate ECG time-series data; preprocessing the multivariate ECG time-series data to obtain preprocessed time-series signal data; and dividing the preprocessed time-series signal data into training sets, validation sets, and test sets; constructing a local feature extraction layer of a convolutional neural network; inputting the preprocessed time-series signal data into the local feature extraction layer of the convolutional neural network; and extracting local feature vectors through one-dimensional convolution operations; and constructing a relational feature layer based on a multi-head attention mechanism, inputting the local feature vectors into the local feature extraction layer of the convolutional neural network. The relation feature layer calculates the correlation weights between time points using a multi-head self-attention mechanism combined with the electrocardiographic prior weight matrix to obtain a global feature representation. Local feature vectors and the global feature representation are adaptively fused to obtain fused features, which are then input into a fully connected layer for classification output, yielding the classification results for the electrocardiogram sequence data. A composite model is constructed, trained on this training set, optimized using this validation set, and its performance evaluated by inputting the test set into the trained composite model. This composite model includes at least a convolutional neural network local feature extraction layer, a relation feature layer, a feature fusion module, and a classification module. The classification module includes a fully connected layer and a Softmax classification layer.
[0031] It should be added that the ECGNet composite model in this invention is an end-to-end composite network for electrocardiogram sequence data classification, and its overall architecture is as follows: Figure 1 As shown, the ECGNet composite model includes at least an input layer, a CNNLayer (a local feature extraction layer of the convolutional neural network), a RelationshipLayer (a relation feature extraction layer), a feature fusion module, and a classification module. The CNNLayer extracts local feature vectors L from the preprocessed time-series signal data through one-dimensional convolution. The RelationshipLayer calculates the correlation weights between time points from the preprocessed time-series signal data using a multi-head self-attention mechanism combined with an electrocardiographic prior weight matrix, resulting in a global feature representation G. The feature fusion module includes a concatenation module and a gated weighting unit, used to adaptively fuse the local feature vectors L and the global feature representation G to obtain a fused feature F. The classification module includes a fully connected layer and a Softmax classification layer, used to classify the fused feature F to obtain the classification result of the electrocardiogram sequence data.
[0032] Specifically, the input layer receives multi-lead ECG timing signals, where T is the time step and D is the number of leads. In this invention, the number of leads D=12, corresponding to the standard 12-lead set {I,II,III,aVR,aVL,aVF,V1~V6}. Among them, lead II is a limb lead, equal to the potential difference between the right arm RA and the left lower limb LL, used for representing heart rhythm / rhythm information; V1–V3 are precordial leads, used to reflect local electrical activity characteristics such as ventricular depolarization in the precordial region. Figure 1 The gating unit in the ECG dynamically adjusts the feature weights of each lead. Based on the characteristics of the input ECG signal, the gating mechanism calculates a weighting coefficient between 0 and 1, which is used to weight each lead, thereby improving diagnostic accuracy. The gating unit helps to adaptively weight ECG leads at different time steps, making the diagnostic results more precise.
[0033] Specifically, the CNNLayer consists of sequentially connected convolutional layers, several convolutional blocks (CNNBlocks), and a first flatten layer, used for one-dimensional convolutional feature extraction of the preprocessed multivariate ECG time series to obtain a local feature vector L; the RelationshipLayer consists of positional encoding, several relational feature blocks (RelationshipBlocks), and a second flatten layer, used for global dependency modeling of the preprocessed multivariate ECG time series to obtain a global feature representation G; the feature fusion module is used to concatenate the local feature vector L with the global feature representation G to obtain a concatenated vector, and optionally, a weighted fusion is performed on the concatenated vector using a gating unit to obtain a fused feature F; the classification module consists of at least one fully connected layer and a softmax classification layer, used to output the predicted probability of each ECG category based on the fused feature F and provide the classification result. Wherein, as Figure 1 As shown, CNNLayer and RelationshipLayer are parallel structures, taking the input time series as input and outputting L and G respectively. After being flattened, L and G are merged at the feature fusion module to obtain F, and then the classification module outputs the classification result.
[0034] In one embodiment, preprocessing electrocardiogram (ECG) multivariate time series data to obtain preprocessed time series signal data, and dividing the preprocessed time series signal data into training set, validation set, and test set, includes: collecting time series signal data in the target domain as ECG multivariate time series data; the time series signal data includes univariate or multivariate signal sequences; performing denoising, normalization, and time synchronization processing on the time series signal data to obtain a time series matrix with a preset length and a preset feature dimension; labeling each sample in the time series matrix with ECG category labels; and stratifying the time series matrix into training set, validation set, and test set according to a preset ratio to ensure that the proportion of each category label is consistent with the original proportion of labels in the time series matrix.
[0035] In one embodiment, inputting preprocessed time-series signal data into a local feature extraction layer of a convolutional neural network and extracting local feature vectors through one-dimensional convolution operations includes: setting several convolutional blocks in the local feature extraction layer of the convolutional neural network, each convolutional block consisting of two one-dimensional convolutional layers, a batch normalization layer, and an activation layer; inputting the preprocessed time-series signal data into the convolutional blocks, performing convolution calculations on the time axis through convolution kernels to extract local temporal features of the preprocessed time-series signal data, obtaining a feature map; and performing dimensionality reduction processing on the feature map through a pooling layer to obtain local feature vectors; the expression for the convolution calculation is:
[0036] ;
[0037] In the formula, For the first Layer convolution kernel; For bias terms; This is the activation function for the activation layer.
[0038] In one embodiment, local feature vectors are input into the relational feature layer. A multi-head self-attention mechanism combined with an electrophysiological prior weight matrix is used to calculate the correlation weights between time points, resulting in a global feature representation. This includes: mapping local feature vectors to queries, keys, and values in the relational feature layer; segmenting local feature vectors along the time axis according to cardiac cycles based on peak localization and average cardiac cycle information in the preprocessed time-series signal data to construct an electrophysiological prior weight matrix; calculating the correlation score between different time points within each attention head of the relational feature layer; fusing the correlation score element-wise with the electrophysiological prior weight matrix to obtain a weighted correlation matrix; and concatenating the outputs of all attention heads in the relational feature layer and outputting the global feature representation through a linear transformation.
[0039] In one embodiment, constructing the electrophysiological prior weight matrix includes: assigning a first prior weight to time point pairs that satisfy the electrophysiological sequence relationship within the same cardiac cycle; assigning a second prior weight to time point pairs that satisfy the electrophysiological sequence relationship in adjacent cardiac cycles; and performing weight decay on time point pairs whose time interval exceeds a preset interval threshold or whose physiological correlation is lower than a preset correlation threshold; wherein the first prior weight is greater than the second prior weight.
[0040] In one embodiment, the correlation score between different time points is calculated within each attention head of the relation feature layer. The correlation score is then fused element-wise with the electrocardiogram (ECG) prior weight matrix to obtain a weighted correlation matrix. This process includes: obtaining an initial correlation score by performing a dot product operation on the query and the key; scaling the initial correlation score to avoid excessively large dot product values that could cause gradient vanishing; multiplying the scaled correlation score element-wise with the ECG prior weight matrix to obtain a fused correlation score; normalizing the fused correlation score to obtain an attention weight matrix, thereby converting the fused correlation score into non-negative weights that sum to a preset value; and multiplying the attention weight matrix by the values to obtain the attention output, thereby selectively emphasizing ECG feature waveforms.
[0041] In one embodiment, adaptive feature fusion of local feature vectors and global feature representations to obtain fused features, and inputting the fused features into a fully connected layer for classification output, includes: aligning local feature vectors and global feature representations according to the time dimension and cardiac cycle; weighting the aligned local feature vectors and global feature representations at the lead level based on the multi-lead structure of the electrocardiogram; inputting the weighted local feature vectors and global feature representations into a gating unit to generate dynamic fusion coefficients; performing weighted fusion of the weighted local feature vectors and global feature representations based on the dynamic fusion coefficients to obtain fused features; and inputting the fused features into a fully connected layer, outputting the multi-class classification probability of the electrocardiogram sequence data after nonlinear transformation.
[0042] Specifically, the multi-lead ECG structure refers to using a 12-lead ECG signal, including 12 standard ECG leads: V1-V6, I, II, III, aVR, aVL, and aVF. Lead II and leads V1-V3 represent different electrode configurations for ECG. Lead II is generally located between the left and right legs and is commonly used for acquiring systemic electrical signals. Leads V1-V3 are chest electrode configurations used to assess regional cardiac electrical activity, such as ventricular electrical activity. Specifically, lead II is typically used to detect systemic cardiac electrical signals, while leads V1-V3 focus on the chest region and are typically used to analyze ventricular and regional cardiac activity. These leads represent observations of cardiac electrical activity from different perspectives, providing rich cardiac electrophysiological information. In this invention, the multi-lead ECG structure is used to extract ECG signal features from different leads and process them through a specific feature extraction layer to further fuse features from different leads, thereby improving diagnostic accuracy.
[0043] In one embodiment, inputting the weighted local feature vector and global feature representation into the gating unit to generate dynamic fusion coefficients includes: concatenating the weighted local feature vector and global feature representation at each time step along the feature dimension to obtain a fused input vector; inputting the fused input vector into a fully connected layer, combining it with a learnable weight vector and a bias term to obtain a gating output value; and applying an activation function to the gating output value to obtain dynamic fusion coefficients ranging from zero to one; wherein the learnable weight vector and bias term of the fully connected layer are learned during the training of the composite model using a backpropagation algorithm; the expression for the dynamic fusion coefficients is:
[0044] ;
[0045] ;
[0046] In the formula, The dynamic fusion coefficient; This is the gated output value; For activation functions; A learnable weight vector; For bias terms; To fuse the input vectors.
[0047] In one embodiment, training the composite model based on the training set and optimizing the model parameters of the composite model using the validation set includes: setting training hyperparameters, training the composite model using an adaptive optimization algorithm, and decaying the learning rate according to a preset strategy during training; extracting samples from the training set according to the sample proportion of each ECG category based on a stratified sampling method to obtain mini-batch training data; calculating the error between the predicted value and the true label using a cross-entropy loss function with class weights; performing forward propagation on the current sample in the mini-batch training data, performing backpropagation using the cross-entropy loss function, and updating the model parameters of the composite model; calculating accuracy and recall on the validation set, and using the model parameters corresponding to the optimal validation metrics as candidate optimal model parameters; when the validation metrics on the validation set do not improve within a preset number of consecutive rounds, rolling back to the candidate optimal model parameters based on an early stopping strategy.
[0048] In one embodiment, calculating the error between the predicted value and the true label using a cross-entropy loss function with introduced category weights includes: counting the number of samples for each ECG category in the training set; calculating initial weights based on the number of samples; when an ECG category belongs to a preset set of clinically high-risk categories and the number of samples is less than a preset sample number threshold, adjusting the initial weight of that ECG category with a risk enhancement coefficient as the adjusted weight, while keeping the initial weights of the remaining ECG categories unchanged as the adjusted weights; normalizing the adjusted weights of all ECG categories to obtain the category weights of the corresponding ECG categories; calculating a weighted cross-entropy loss as the error based on the category weights, true labels, and predicted probabilities; increasing the sampling probability of samples that are continuously misclassified in several training rounds in subsequent training rounds; the expression for calculating the weighted cross-entropy loss based on the category weights, true labels, and predicted probabilities is as follows:
[0049] ;
[0050] In the formula, The weighted cross-entropy loss; The number of samples; Number of categories; For the first Category weights for each ECG category; For the first The sample at the th Real labels for each ECG category; For the first The sample belongs to the first Predicted probabilities for each ECG category.
[0051] Specifically, the process of determining the category weights includes: denoting the number of samples for each ECG category in the training set as... Calculate the initial weights When a certain ECG category belongs to a predefined set of clinical high-risk categories H and satisfies < At that time, the initial weight of the category is adjusted by a risk enhancement factor. Adjusted to , >1), the remaining categories remain For all categories Normalization is performed to obtain class weights .For example, Take 10 -6 To avoid division by zero; preset sample threshold Total number of training samples 1% to 10%; risk enhancement coefficient The values can be 1.2 to 5; the above parameters can be adjusted according to the degree of imbalance in the dataset.
[0052] It should be noted that this invention discloses a method for classifying electrocardiogram (ECG) sequence data based on convolutional neural networks and multi-head attention mechanisms. This method obtains local temporal features of the ECG signal through a convolutional feature extraction module, acquires global dependencies between different time points through a relational feature extraction module, and combines local and global features in a feature fusion module. Finally, the classification module outputs the category result of the ECG sequence. The method proposed in this invention has a clear structure, strong feature extraction capabilities, and facilitates automatic classification and assisted diagnosis of multi-lead ECG sequences, making it applicable to automated screening and diagnosis of cardiovascular diseases.
[0053] The purpose of this invention is to solve the problem that existing electrocardiogram classification methods cannot simultaneously take into account local waveform features and global temporal dependency modeling, resulting in low classification accuracy and poor generalization ability.
[0054] To achieve the above objectives, this invention proposes a method for classifying electrocardiogram (ECG) sequence data based on convolutional neural networks and multi-head attention mechanisms. The technical solution includes: acquiring multivariate time-series ECG data; preprocessing the data and dividing it into training, validation, and test sets; constructing an ECGNet composite model integrating convolutional layers and relational feature layers; training the model using the training set and optimizing the model parameters using the validation set; inputting the test set into the trained ECGNet composite model to classify the ECG data and output cardiac physiological / pathological state categories.
[0055] The present invention specifically includes the following steps:
[0056] Step 1: Dataset construction and partitioning. Collect multivariate time series signal datasets from the target domain, organize the data into a multidimensional time series format according to time order, and randomly partition the dataset into training set, validation set and test set for model training, optimization and evaluation.
[0057] Step 2: Construct a local feature extraction layer for a convolutional neural network (CNN). Input the multivariate temporal data processed in Step 1 into the CNN module and extract local temporal features through one-dimensional convolution operations.
[0058] Step 3: Construct a relation feature layer with a multi-head attention mechanism. Input the local feature vectors extracted by the convolutional layer in Step 2 into the relation feature layer. Calculate the correlation weights between time points through the multi-head self-attention mechanism to capture global dependencies.
[0059] Step 4, Fusion Layer and Classification Output: The local features output from the convolutional layer and the global features output from the multi-head attention layer are concatenated and fed into the fully connected layer for classification output.
[0060] Step 5, Model Training and Optimization;
[0061] Step 6: Model performance evaluation. Use metrics such as accuracy, recall, and F1-score to evaluate the model's performance.
[0062] Specifically, step 1 of this invention involves the following steps: Step 1.1: Collect time-series signal data from the target domain, including univariate or multivariate signal sequences; Step 1.2: Perform denoising, normalization, and time synchronization processing on the original signals, representing them as a time-series matrix of length T and feature dimension D; Step 1.3: Stratify all data into training, validation, and test sets using a ratio of 8:1.6:0.4, ensuring that the proportion of each label class is consistent with the original dataset to avoid class imbalance affecting training performance. In step 1.2, after preprocessing the input ECG time-series signal data, the R-peak location and mean cardiac cycle information (TRR) are obtained. Specifically, the R-peak position is first extracted from the time-series signal using a feature extraction algorithm based on the signal peaks; then, the mean cardiac cycle (TRR) is calculated, which is based on the time interval between the R-peak positions and describes the periodic fluctuations in the ECG signal. This information plays a crucial role in subsequent model training and feature extraction, ensuring accurate modeling and analysis of the ECG signal.
[0063] Specifically, step 2 in this invention is as follows: Step 2.1, input the time-series signal data constructed in step 1 into the convolutional neural network; Step 2.2, set multiple convolutional blocks in the convolutional layer, each convolutional block consisting of two one-dimensional convolutional layers, a batch normalization layer, and a ReLU activation layer; Step 2.3, perform convolution calculation on the time axis using convolution kernels to extract the local temporal features of the signal, and the convolution calculation formula is shown in equation (1-1):
[0064] (1-1)
[0065] In the formula, For the first Layer convolution kernel; For bias terms; The activation function is set to the activation function of the activation layer. In step 2.4, the feature map output by the convolutional layer obtained in step 2.3 is processed by the pooling layer to reduce its dimensionality, thereby obtaining the local feature vector of the time series signal.
[0066] Specifically, step 3 involves the following steps: Step 3.1: The local features output in step 2.4 are used as input and fed into the multi-head self-attention mechanism layer; Step 3.2: The input feature sequences are mapped to query Q. h Key K h Sum V h Combined with the R peak location and mean cardiac cycle T obtained in step 1.2 RR Information is segmented along the time axis according to the cardiac cycle to construct the electrophysiological prior weight matrix M. phy Time points that satisfy the P-QRS-T physiological ordering relationship within the same cardiac cycle and in adjacent cardiac cycles are assigned larger prior weights, while time points with larger time intervals or weaker physiological correlations are weighted at a reduced level. Subsequently, correlation scores between different time points are calculated within each attention head and compared with M. phy Element-by-element fusion yields a weighted correlation matrix, which serves as the attention weight matrix. The formula for calculating the attention weights is as follows:
[0067] (1-2)
[0068] in, This represents the dimension of each head. The scaling factor is used to prevent the softmax gradient from vanishing due to excessively large dot product values. The softmax function is used to scale the dot product. The correlation scores are normalized to transform them into non-negative weights that sum to 1, thereby reflecting the relative importance of different time points in feature aggregation under the prior constraints of electrocardiography, and realizing the selective emphasis of effective features such as P waves, QRS complex waves, and ST-T segments by the attention layer; Step 3.3: The outputs of multiple attention heads are concatenated to obtain matrix Z, and the global feature representation is output after linear transformation; Step 3.4: The relation feature layer introduces residual connections and layer normalization operations to enhance training stability and prevent gradient vanishing.
[0069] Specifically, step 4 of this invention involves the following process: Step 4.1, aligning the local features obtained in step 2 with the global features obtained in step 3 according to the time dimension and cardiac cycle. Based on this, adaptive feature fusion is performed by combining ECG multi-leads and temporal structure: First, a learnable lead weighting system for ECG multi-leads is constructed, where lead II and leads V1-V3 represent different electrode configurations in the ECG, with leads V1-V3 primarily used for chest electrode configurations to assess local cardiac electrical activity. The gating unit adaptively adjusts the contribution of each lead based on the input ECG data. Softmax normalizes the lead features to determine their weights, ensuring that the contributions of leads II and V1-V3 have a greater impact on model prediction during diagnosis. Then, the weighted local features and global features are input into the gating unit to generate dynamic fusion coefficients for each time step. Specifically, the gated unit adopts a structure of fully connected layers and sigmoid activation: for the local feature vector L(t) and global feature vector G(t) at time step t, they are first concatenated along the feature dimension to obtain the fused input vector:
[0070] (1-3)
[0071] Equation (1-3) has a dimension of 2d. Input a gated fully connected layer, calculate:
[0072] (1-4)
[0073] in For learnable weight vectors, This is the bias term; then the dynamic fusion coefficients are obtained through the Sigmoid activation function:
[0074] (1-5)
[0075] Since the output range of the Sigmoid function is (0,1), therefore the... Natural satisfaction The weights With bias During model training, the parameters of convolutional layers, attention layers, etc., are automatically learned through backpropagation algorithm, thereby adaptively adjusting the weights of local and global features at different heart rhythm patterns and times.
[0076] (1-6)
[0077] Then, following the form of equation (1-6), local features are completed at the time step level. With global features The weighted fusion method relies more on high-resolution local morphological information near key ECG events such as QRS complexes and ST segments, and utilizes global relational features more in cross-cycle dependent areas such as rhythm changes. The fused feature sequences... The temporal structure and complementary information of the ECG signal are preserved, providing a more comprehensive representation that conforms to the electrocardiographic characteristics for subsequent classification. In step 4.2, the fused features are input into the fully connected layer, and after nonlinear transformation and a Softmax classification layer, multi-class classification results are output. The Softmax output calculation formula is as follows:
[0078] (1-7)
[0079] in, For the number of categories, For the first The sample at the th The output obtained after Softmax normalization of the feature mapping results on the class. That is, the first The sample belongs to the first The predicted probability of the class. For each sample The length can be obtained as Predicted probability vector In subsequent steps, the weighted cross-entropy loss function of equation (1-8) is substituted into the model for training.
[0080] Specifically, step 5 in this invention involves the following steps: Step 5.1, setting training hyperparameters based on the multi-class distribution and rhythm characteristics of the electrocardiogram, and using the Adam optimization algorithm for model training. The initial learning rate is set to... During training, the error is gradually reduced with each training round according to a preset strategy to avoid oscillations in the later stages. When constructing small batches of data, a stratified sampling method is used, extracting samples from the training set according to the sample proportion of each ECG category. This ensures that each batch contains both normal rhythms and multiple abnormal rhythms, and adjacent cardiac cycles in the same record are arranged in the same batch as much as possible, ensuring both basic balance of samples in each category and preserving the continuity of the ECG signal on the time axis. In step 5.2, considering the multi-category and unevenly distributed nature of ECGs, a cross-entropy loss function with category weights is used to calculate the error between the predicted value and the true label. Let the first... The number of samples in the training set is The class weights are obtained by normalizing their reciprocals. If higher weights are assigned to clinically high-risk categories such as atrial fibrillation and premature ventricular contractions (PVCs) with smaller sample sizes, then the loss function is:
[0081] (1-8)
[0082] in, For the sample size, For the number of categories, For the first The sample at the th The real labels on the class are encoded using one-hot encoding: when the first... The true category of each sample is hour ,otherwise In this invention, without changing the definition of other parameters, the change in the logarithmic base of equations (1-8) only leads to the overall scaling of the loss function, without affecting the optimal solution of the model parameters or the convergence direction of the training process. The first step is to obtain the output of Softmax as described in step 4.2. The sample belongs to the first Predicted probability of class: for samples The output layer feature mapping vector is obtained through convolutional layers, relational feature layers, and fully connected layers:
[0083] (1-9)
[0084] Then, normalize according to equation (1-7) and calculate:
[0085] (1-10)
[0086] This allows us to obtain the predicted probability distribution of each sample across all categories. And substitute it into the above weighted cross-entropy loss function to measure the difference between the predicted result and the true label; For the first The class weights are obtained by normalizing the inverse of the number of samples of that class in the training set, and are used to increase the weights of classes with fewer samples but higher clinical risk, such as atrial fibrillation and premature ventricular contractions, during the optimization process. In step 5.3, in each training round, forward propagation is performed on the current small batch of samples, backpropagation is performed using the weighted cross-entropy loss from step 5.2, and the model parameters are updated. After each training round, the overall accuracy and recall of key classes such as atrial fibrillation and premature ventricular contractions are calculated on the validation set, and the set of model parameters corresponding to the optimal validation metrics is selected as the candidate optimal model. When the validation set metrics do not improve within several consecutive rounds, an early stop strategy is triggered and the model is rolled back to the parameters with the best validation performance. At the same time, the sampling probability of cardiac cycles that are continuously misclassified in multiple training rounds is automatically increased in subsequent training, forming a difficult sample resampling mechanism, thereby enhancing the model's ability to discriminate complex and ambiguous ECG waveforms, accelerating convergence, and further improving ECG classification performance.
[0087] Specifically, the process of step 6 in this invention is as follows: Step 6.1, using the test set as input to the trained model, calculate indicators such as accuracy, recall, and F1 score; Step 6.2, judging the model's classification performance and generalization ability for time-series signals based on the test results.
[0088] It should be noted that the proposed ECGNet composite model can effectively address the shortcomings of existing ECG classification models that only focus on local features at a single time point: it extracts local features of ECG signals through convolutional layers and captures global relational features with the help of relational feature layers, achieving deep feature fusion; on the PTB-XL dataset, the accuracy reaches 95.54%, significantly outperforming baseline models such as 1NN-DTW, ResNet, and LRFC-Eud; ablation experiments show that adding 4 relational feature blocks improves the accuracy to 94.2% compared to 82% without this module, and parameter sensitivity tests prove the model's robustness; in engineering, it is compatible with conventional GPU training, and the preprocessed data conforms to the clinical ECG format, which can efficiently support automated screening of cardiovascular diseases and improve diagnostic accuracy and efficiency.
[0089] To facilitate understanding of the above technical solutions of the present invention, the following detailed explanation uses the PTB-XL electrocardiogram dataset as an example.
[0090] The ECG sequence data classification method based on convolutional neural networks and multi-head attention mechanism provided in this embodiment is implemented according to the following steps:
[0091] Step 1: First, acquire and preprocess multivariate time-series ECG data. Standardized ECG data is acquired and subjected to noise removal, dimensionality unification, and label conversion to provide high-quality input for the model. Finally, training, validation, and test sets are defined. Step 1.1: In the dataset input stage, this embodiment selects the publicly available clinical ECG dataset PTB-XL as the original data source. This dataset contains 21,837 12-lead ECG records from 18,885 patients. Each record is 10 seconds long, with an original sampling frequency of 500Hz, waveform precision of 16 bits, and resolution of 1μV / LSB. Metadata covers patient age ranges from 0 to 95 years (median age 62 years), gender ratio of 52% male and 48% female, and diagnostic labels. The diagnostic labels conform to the SCP-ECG standard, with core categories including normal rhythm (NORM), myocardial infarction (CD), ST / T changes (STTC), conduction disorders (MI), and hypertrophic hypertrophic hysterosalpingography (HYP), corresponding to core scenarios in clinical cardiovascular disease screening.
[0092] Step 1.2, in the data preprocessing stage, the preprocessing module is used to eliminate noise interference, dimensional differences, and data biases in multi-lead ECG signals, providing stable and standardized input for the model. Step 1.2.1, in the noise removal stage, corresponding processing measures are taken for the three common types of interference in ECG signals: For periodic power frequency interference generated by the power grid, a 50Hz notch filter is used for suppression, with the center frequency set to 50Hz and the quality factor Q=30; For slow baseline drift caused by breathing and body swaying, wavelet transform is used for correction, and the signal is decomposed into 8 layers using the db4 wavelet basis. Soft thresholding is applied to the low-frequency approximation coefficients after decomposition, with the threshold value set to 0.05 × the maximum amplitude of the signal. Baseline drift is eliminated after signal reconstruction; For high-frequency electromyographic noise generated by muscle contraction, a third-order Butterworth low-pass filter is used for filtering, with the cutoff frequency set to 35Hz, to preserve the effective frequency band characteristics of the ECG signal and remove high-frequency noise interference. Step 1.2.2: Because the original 500Hz sampling frequency results in a time step of 5000 (10 seconds × 500Hz) for a single signal, the computational cost is relatively high. Linear downsampling is used to reduce the frequency to 100Hz. At this time, the time step of a single signal becomes 1000, and the signal dimension is unified as (1000, 12), where 1000 is the time step and 12 is the number of 12 lead channels. This preserves key waveform features such as QRS complexes and ST segments while reducing model training costs. Step 1.2.3: Since the amplitude of chest leads is usually higher than that of limb leads, the signal amplitude range of the 12 leads (limb leads I / II / III and chest leads V1-V6) differs by 2-3 times. Mean and standard deviation normalization is performed independently for each lead channel. Step 1.2.4: In the label processing and data storage stage, the original SCP-ECG standard string labels in the dataset, such as NORM and MI, are mapped to integer codes, specifically: NORM→0, CD→1, STTC→2, MI→3, HYP→4; then, samples with missing diagnostic labels or poor signal quality ratings are filtered out, retaining 19,653 valid samples; the standardized signals and labels are stored in .npz compressed format for easy subsequent fast loading, and the storage structure includes signal and label.
[0093] Step 1.3: In the dataset partitioning stage, stratified sampling is performed at a ratio of 8:1.6:0.4 to obtain 15,722 training sets, 3,144 validation sets, and 3,931 test sets. This ensures that the proportion of labels for each class is consistent with the original dataset and avoids class imbalance from affecting the training effect.
[0094] Step 2: Next, construct the local feature extraction layer of the convolutional neural network. Figure 1 This is a schematic diagram of the overall structure of the ECGNet composite model proposed in this invention, as shown below. Figure 1As shown, the composite model ECGNet uses a preprocessed electrocardiogram time series as the input time series, and sets up a convolutional neural network local feature extraction layer as the convolutional feature extraction branch CNNLayer and a multi-head attention mechanism relationship feature extraction layer as the relationship feature extraction branch RelationshipLayer in parallel. The convolutional feature extraction branch CNNLayer consists of a Convolution Layer, several convolutional blocks CNNBlocks, and a first flatten layer. The residual structure of a single convolutional block CNNBlock is illustrated in the diagram. Figure 3 As shown, local convolution, inter-layer nonlinear transformation, and feature flattening are performed sequentially to extract local morphological features of ECG signals such as the P wave, QRS complex, and ST-T segment. The RelationshipLayer, which extracts relational features, consists of positional encoding, several RelationshipBlocks, and a second flattening layer. By incorporating positional encoding and relational feature blocks, it models the global dependencies between the same cardiac cycle and adjacent cardiac cycles. The one-dimensional feature vectors output from the two branches are concatenated in the Concatenate module to obtain fused features. These fused features are then sequentially input into the Fully connected layers and the Classification:Softmax layer, which outputs the probability of each ECG category and provides the final classification result.
[0095] Step 2.1, in the CNNLayer construction stage, the convolutional layer is the core of local feature extraction, consisting of 5 stacked CNNBlocks. Each convolutional block is based on a 1D-CNN design with residual connections, used to capture local temporal features of ECG signals, such as QRS complex morphology and ST segment shift. The specific implementation is as follows: Step 2.1.1, convolutional block structure initialization stage, such as... Figure 3As shown, each convolutional block, CNNBlock, consists of two sequentially connected 1D convolutional layers, a batch normalization layer, and a ReLU activation function forming the main branch. An optional 1×1 one-dimensional convolutional layer is set on the residual branch to ensure the residual connections remain effective even when the number of channels and time steps are mismatched. The 1D convolutional layers use 3×3 kernels to adapt to capturing local correlations in temporal signals. The batch normalization layer standardizes the convolutional output, avoids training bias, and accelerates convergence. The ReLU activation function introduces nonlinearity to fit complex local features such as QRS group morphology and ST segment offset. The optional 1×1 convolutional layer is only activated when the input and output dimensions of the residual connections are mismatched, adjusting the tensor shape through linear transformation to prevent gradient vanishing. Step 2.1.2, in the convolutional block forward propagation process, the convolutional block, as the core unit for extracting local features of ECG multivariate time series data from the ECGNet model's convolutional layer CNNLayer, has a forward propagation process closely designed around the stepwise extraction of local features, feature distribution standardization, and residual information fusion. This process is fully adapted to the one-dimensional temporal characteristics and multi-lead data structure of ECG signals. Let the input tensor of the convolutional block be... ;in For batch size, Input the number of channels. For the time step, the forward propagation process is as follows: 1) First convolution and normalization, the core of which is to initially capture the local temporal features of the ECG signal through 1D-CNN. First, a 3×1 1D convolutional layer is input. The design rationale for choosing a 3×1 convolutional kernel is as follows: the first convolutional layer uses a 3×1 one-dimensional convolutional kernel that slides along the time axis to extract local temporal features of the electrocardiogram signal. The time step size in the convolutional block is set according to the position of the convolutional block. For the first convolutional block, a stride size is used. , This ensures that the time step remains constant after convolution, and its calculation formula is shown in equation (2-1):
[0096] (2-1)
[0097] In some convolutional blocks, the stride is set to This is achieved by downsampling the time dimension, halving the sequence length. The convolutional layer output is then processed by a batch normalization layer and a ReLU activation function to obtain preliminary local features X. relu1 2) The second convolutional layer uses a 3×1 one-dimensional convolutional kernel with a stride of [missing information]. , This is used to further extract local temporal features. The convolutional output is batch normalized to obtain a stable feature distribution. No activation function is applied to the convolutional layer output in this step, resulting in the normalized feature X. bn23) To ensure consistency between the input and output of the residual connection in both the number of channels and the time step, this step adjusts the input features based on the dimensionality. When the number of channels in the input and output is inconsistent, a 1×1 one-dimensional convolutional layer is used to map the input features to the same number of channels as the output of the convolutional block. When the time steps are inconsistent, a 1×1 convolutional layer with a set stride is used to downsample the time dimension to make the time steps consistent. If the input is matched in both dimensions, the input is directly used as the adjusted feature X. adjusted 4) According to the residual connection formula ,Right now The output features of the second convolutional layer are added to the dimension-adjusted input features to complete the residual information fusion. The fused features are then processed by the ReLU activation function to obtain the final output feature X of the convolutional block. out Its output dimension is Step 2.1.3: The convolutional feature extraction module consists of multiple convolutional blocks stacked sequentially. The first convolutional block maintains the same time step and maps the number of feature channels to 128; subsequent convolutional blocks downsample the time dimension in some residual paths through convolutions with a stride of 2, thus gradually reducing the time step. The final output is a local feature tensor. This is used as input for the subsequent relation feature extraction module.
[0098] Step 3: Then, construct the multi-head attention relationship feature layer. In addition to the convolutional local feature extraction branch, the proposed ECG network composite model also includes a relationship feature extraction branch, RelationshipLayer. This branch consists of positional encoding, several relationship feature blocks, and a second flattening layer. Through positional encoding and a multi-head self-attention mechanism, it explicitly models the global dependencies between the same cardiac cycle and adjacent cardiac cycles. The internal structure of the relationship feature blocks is as follows: Figure 2As shown; each RelationshipBlock contains, in sequence, a Multi-head attention sublayer, a PositionWiseFFN sublayer, and two AddNorm residual normalization units. The input feature X is first mapped to query Q, key K, and value V in the Multi-head attention sublayer, obtaining a weighted relational representation between time steps through the attention mechanism. After being added to and normalized by the first residual normalization unit, it is fed into the PositionWiseFFN sublayer for a time-step nonlinear transformation, and then the residual is normalized by the second residual normalization unit. The final output serves as the result of the current RelationshipBlock and is used by subsequent RelationshipBlocks or feature fusion layers. Specific steps include: Step 3.1, the RelationshipBlock consists of four sequentially stacked RelationshipBlocks. Each RelationshipBlock includes a Multi-head attention sublayer, a feedforward neural network sublayer, and a layer normalization structure, used to extract global temporal dependencies from the time series. Step 3.1.1: To introduce location information into the temporal dimension, the preprocessed feature X is added to the sinusoidal position code PE to obtain the input X with added location information. pe The location code is calculated using the following formula:
[0099] (3-1)
[0100] (3-2)
[0101] Where t is the time step index ( ), k is the dimension index ( ); the input after injection is This ensures the model captures temporal correlations. Step 3.1.2: In the multi-head self-attention calculation phase, the first sub-layer of each RelationshipBlock is a multi-head self-attention layer, X. pe Simultaneously serving as query Q, key K, and value V, the specific steps are: 1) Linear projection, that is, projecting X through 3 learnable linear layers. pe If mapped to Q, K, V, then , , ;in, 1) Learnable parameter matrix. 2) Multi-head splitting and attention calculation: Q, K, and V are split according to the number of heads H, and the scaled dot product attention is calculated independently for each head. The specific formula is as follows:
[0102] (3-3)
[0103] in, This represents the dimension of each head. The scaling factor is used to avoid excessively large dot product values that could cause the Softmax gradient to vanish. 3) Multi-head concatenation and linear transformation: The attention outputs of the four heads are concatenated along the channel dimension and mapped to global attention features through a linear layer. The specific formula is as follows:
[0104] (3-4)
[0105] in, The output is the linear layer parameters; Attn represents the attention output of a single attention head, which is obtained by normalizing the attention weights using Softmax and then applying them to the corresponding value vector V. h The weighted sum is used to represent the feature aggregation result of the input sequence at different time positions. Concat is used to concatenate the output features of each attention head according to the channel dimension, forming a joint feature representation containing multi-head information, so as to further extract comprehensive features in subsequent linear transformations. Step 3.1.3, in the AddNorm and feedforward neural network stage, includes: 1) layer normalization and residual fusion, that is, for The AddNorm operation is executed using the following formula:
[0106] (3-5)
[0107] LayerNorm is a layer normalization operation that normalizes the mean and variance of the input along the feature dimension, ensuring consistent numerical scale of the output features and thus improving the stability of subsequent network computations. Dropout is a random deactivation function that randomly masks input features according to a preset probability during the training phase, temporarily setting some feature channels to zero, thereby reducing co-fitness between features and improving the stability of the network structure; this operation is no longer effective during the inference phase to maintain complete feature representation. Considering that the PTB-XL dataset has sufficient sample size and no additional regularization is needed, the Dropout rate is set to 0, and layer normalization standardizes the channel dimension to ensure stable feature distribution. 2) Position-wise feedforward neural network FFN, where FFN is a two-layer fully connected network that independently performs nonlinear transformations on features at each time point:
[0108] (3-6)
[0109] in, , , , The 24-dimensional hidden layer balances feature fitting capability and computational efficiency. ReLU, the Modified Linear Unit function, performs element-wise processing on the input values, setting values less than zero to zero and leaving values greater than zero unchanged, thereby achieving non-linear activation and enhancing the feature representation capability of the feedforward network. 3) Secondary AddNorm: The AddNorm operation is performed again on the FFN output to obtain the RelationshipBlock output. The specific formula is as follows:
[0110] (3-7)
[0111] Step 3.1.4: In the overall output stage of the relation feature layer, the four relation feature blocks are stacked sequentially and then output as a global relation feature tensor. After performing average pooling on the time dimension, the global feature tensor is obtained. .
[0112] Step 4: Next, feature fusion and classification output are performed. The local features output by the convolutional feature extraction branch and the global features output by the relational feature extraction branch are first weighted and combined according to time step and lead dimension in the feature fusion module, and then the predicted probability of each ECG category is output through a fully connected layer and a softmax layer. The specific steps include: Step 4.1, Feature Fusion and Fully Connected Classification Module. This module first performs gated fusion of local features and global features, and then outputs the classification result through a fully connected layer. Specifically, in Step 4.1.1, in the feature flattening and concatenation stage, let the local feature vector output by the convolutional feature extraction branch at time step t be L(t), and the global feature vector output by the relational feature extraction branch at the same time step be G(t). For each time step t, the two are first concatenated along the feature dimension to obtain:
[0113] (4-1)
[0114] Then, a biased linear unit layer is constructed, and z(t) is weighted and summed to obtain a scalar:
[0115] (4-2)
[0116] in For learnable weight vectors, This is the bias term; then the dynamic fusion coefficients are obtained through the Sigmoid activation function:
[0117] (4-3)
[0118] Since the output range of the Sigmoid function is (0,1), therefore The value naturally ranges between 0 and 1. This coefficient is used to weight and superimpose local and global features to obtain the fused feature vector at time step t:
[0119] (4-4)
[0120] Performing the above calculations step-by-step over the entire cardiac cycle yields the fused time-series feature F(t). After obtaining the fused sequence F(t), to facilitate integration with subsequent fully connected layers, F(t) is first pooled or converged along the time dimension to obtain a three-dimensional fused feature tensor with a batch size of B. Simultaneously, the relation feature sequence G(t) is pooled in the same manner to obtain the global feature tensor. Then, these two tensors are flattened into one-dimensional vectors and concatenated along the feature dimension, as shown in the following formula:
[0121] (4-5)
[0122] Here, Flatten indicates that the three-dimensional local feature tensor output by the convolutional layer is expanded in a fixed order and converted into a one-dimensional vector for subsequent feature concatenation and fully connected classification layer processing.
[0123] (4-6)
[0124] Flatten is used to expand the two-dimensional global feature tensor output by the relation feature layer into a one-dimensional vector for fusion with local features. The fused features are obtained by concatenating along the feature dimensions. The specific formula is as follows:
[0125] (4-7)
[0126] Step 4.1.2, in the fully connected classification layer design phase, the classification layer consists of two hidden layers and a Softmax output layer. The first hidden layer has an input dimension of 4480 and an output dimension of 1024; the second hidden layer has an input dimension of 1024 and an output dimension of 256. The final output layer has a dimension of 5. The Softmax layer exponentially normalizes the 5-dimensional output vector of each sample to obtain the predicted probability distribution of the sample in each category. ,in This represents the predicted probability that the b-th sample belongs to class c. In step 4.1.3, during the predicted label generation stage, the class with the highest Softmax probability is selected as the final predicted label for each sample:
[0127] (4-8)
[0128] in, Let b be the predicted label for the b-th sample. Let be the probability that the b-th sample belongs to the c-th class, and argmax be the maximum index function used to select the class number with the largest probability value in the probability distribution P(b,c) of the classification output as the predicted class of the b-th sample.
[0129] Step 5 involves model training and performance validation. This step optimizes the entire network parameters end-to-end based on the aforementioned convolutional local feature extraction module and ECG-constrained multi-head attention relationship feature module, and validates the model performance on different heart rhythm types. During training, an early stopping strategy is employed in conjunction with the validation set to suppress overfitting while ensuring sufficient model convergence, with particular attention paid to the ability to identify clinically high-risk arrhythmia categories. Step 5.1 configures the training hyperparameters and loss function as follows: The Adam optimizer is used during training, with an initial learning rate of 0.0001, a batch size of 128, and a maximum of 20 training epochs. Model parameters are randomly initialized using the Xavier Uniform method, with the bias term initialized to 0. The CrossEntropyLoss loss function is used to measure the difference between the model output and the true label. To alleviate the problem of imbalanced distribution of ECG multi-class samples, class weights are introduced into the CrossEntropy loss, with the reciprocal of the number of samples in the training set for each class normalized and used as the weight coefficient. Suppose there are C classes in the training set, and the number of samples in class c is n. c First, calculate the reciprocal of the number of samples in each category and normalize it to obtain the weights:
[0130] (5-1)
[0131] The class with fewer samples has a higher weight. Let the true label of the i-th sample in class c be denoted as . Using one-hot encoding, when the true class of the sample is c ,otherwise After the fully connected layer and the Softmax layer in step 4, the predicted probability of the i-th sample in class c can be obtained. When a batch contains N samples, the weighted cross-entropy loss is defined as:
[0132] (5-2)
[0133] During training, the goal is to minimize the loss function L, and the network parameters are updated using the backpropagation algorithm. The main training hyperparameter configurations are shown in Table 1.
[0134] Table 1 - Experimental Training Parameters Training parameters value Learning rate 0.0001 Batch size 128 Number of training rounds 20 Optimizer Adam Model parameter initialization xavier_uniform_ loss function CrossEntropyLoss
[0135] Xavier_uniform is a uniform distribution method for initializing neural network weights, designed to avoid the vanishing or exploding gradient problem. It is suitable for layers with non-linear activation functions such as ReLU. Initializing weights based on a uniform distribution of the number of input and output nodes ensures gradient stability during training. CrossEntropyLoss, on the other hand, is a widely used loss function in multi-class classification tasks. It measures the difference between the model's predicted class probability distribution and the true label, aiming to minimize this difference and thus improve the model's classification accuracy. During training, cross-entropy loss is used to optimize the classifier, enabling the model to better match the actual class.
[0136] Step 5.2, in the model training and validation process, includes: Step 5.2.1, loading the preprocessed training, validation, and test sets; organizing the ECG sequences into training samples based on complete cardiac cycles according to the cardiac cycle segmentation results or R-R interval alignment, avoiding splitting the P wave, QRS complex, and ST-T segment of the same cardiac cycle into different samples, thus ensuring that the convolutional layer and relational feature layer can learn the complete ECG waveform morphology. Simultaneously, initializing the model parameters of the convolutional layer, relational feature layer, and classification layer, and setting the early arrest monitoring index and learning rate decay strategy. Step 5.2.2, in each training round, the training set is sequentially fed into the network in batches for forward propagation, passing through the convolutional local feature extraction module, the multi-head attention relational feature module with cardiac cycle prior constraints, and the classification output module, to obtain the predicted class probability distribution of each batch of ECG samples; calculating the weighted cross-entropy loss based on the true label and class weights, and performing backpropagation, using the Adam optimizer to update the model parameters. After each training round, forward inference is performed on the validation set to calculate metrics such as overall classification accuracy, macro-average recall, and macro-average F1 score. A snapshot of the model parameters that performed best on the validation set is recorded. If the validation set metrics do not improve after several consecutive training rounds, early stopping is triggered, terminating subsequent training and rolling back to the model parameters with the best validation performance, thereby reducing the risk of overfitting. Step 5.2.3, the testing phase, loads the model parameters with the best validation performance during training, performs forward prediction on batches of test set samples, and outputs the classification label and probability of each class for each sample. The classification accuracy metric is calculated on the test set, defined as shown in equation (3-3):
[0137] (5-3)
[0138] Here, Accuracy represents the classification accuracy, calculated by comparing the number of samples whose predicted categories match the actual categories with the total number of samples tested, thus determining the correct classification percentage. In addition to overall accuracy, this embodiment also calculates the recall, precision, and F1 score for each rhythm category to evaluate the model's sensitivity and reliability in identifying key pathological rhythms such as atrial fibrillation and premature ventricular contractions. Through experimental comparison with a model without prior electrophysiological constraints on the same dataset, it can be verified that this invention significantly improves the ability to identify clinically high-risk arrhythmia categories while maintaining overall accuracy, demonstrating the substantial features and significant progress of this application's solution in electrocardiogram sequence classification tasks.
[0139] Step 6: Finally, model validation and ECG classification are performed. In this embodiment, the model performance under different hyperparameter configurations and with or without relational feature blocks is compared and validated on the same ECG dataset. The ECG classification process is also given to illustrate the effectiveness and robustness of the method of the present invention in practical applications.
[0140] Step 6.1: To verify the model's performance under different structural configurations, this step involves configuring the number of relational feature blocks in the model differently, while keeping the other model parameters consistent. Examples of key adjustable parameters for the model are shown in Table 2.
[0141] Table 2 - ECGNet Model Parameters Model parameters value Hidden layer dimensions of location-based feedforward networks 24 Number of relational feature blocks k Number of output channels of convolutional layer 128 Length of output sequence of convolutional layer 32 residuals of convolutional blocks 2 Number of convolutional blocks 5
[0142] The model parameters in Table 2 are not necessarily optimal; they are parameters set to demonstrate the effectiveness of relation feature blocks. Specifically, the ECGNet model without relation feature blocks has 0 relation feature blocks, while the complete ECGNet model has relation feature blocks k of 0, 1, 2, 3, 4, 5, and 6. The model structures under different configurations obtain output features through forward computation and perform classification operations according to a unified process; the model inference process is consistent across all configurations.
[0143] Step 6.2, in the hyperparameter robustness verification stage, the models under different parameter configurations all perform forward inference according to the same steps, and the classification output is determined by the class corresponding to the maximum Softmax probability. The specific steps are as follows: Step 6.2.1, the hidden layer dimension test of the feedforward network is set with hidden layer dimensions of 12, 24, and 36. The results show that the accuracies are 91.2%, 91.7%, and 91.4% respectively, with a difference of <0.5%, proving that the model is robust to this parameter. Step 6.2.2, the residual number test of the convolutional block is set with residual numbers of 1, 2, 3, and 4. The results show that the accuracies are 89.3%, 91.7%, 92.3%, and 92.8% respectively. Increasing the residual number can improve the local feature extraction ability, but the increase gradually decreases. When the residual number is >4, the accuracy does not improve significantly. The results prove the robustness of the model to hyperparameter changes.
[0144] Step 6.3, in the test set classification and performance evaluation stage, includes: Step 6.3.1, loading the trained model parameters, inputting the test samples into the model, and obtaining the class probability distribution: The final predicted label is Step 6.3.2: Classification performance is measured using accuracy. The predicted labels of the test samples are compared with the true labels to obtain the overall accuracy.
[0145] like Figure 6 As shown, according to another embodiment of the present invention, an electrocardiogram (ECG) sequence data classification system based on convolutional neural networks and multi-head attention mechanisms is also provided. This ECG sequence data classification system based on convolutional neural networks and multi-head attention mechanisms includes:
[0146] The data partitioning module is used to acquire multivariate time series data of electrocardiogram, preprocess the multivariate time series data of electrocardiogram to obtain preprocessed time series signal data, and divide the preprocessed time series signal data into training set, validation set and test set.
[0147] The local extraction module is used to construct the local feature extraction layer of the convolutional neural network. It inputs the preprocessed time-series signal data into the local feature extraction layer of the convolutional neural network and extracts the local feature vector through one-dimensional convolution operation.
[0148] The global feature module is used to construct the relational feature layer of the multi-head attention mechanism. Local feature vectors are input into this relational feature layer, and the correlation weights between time points are calculated by combining the multi-head self-attention mechanism with the electrophysiological prior weight matrix to obtain the global feature representation.
[0149] The fusion classification module is used to adaptively fuse local feature vectors with global feature representations to obtain fused features, and then input the fused features into a fully connected layer for classification output to obtain the classification results of electrocardiogram sequence data;
[0150] The training and evaluation module is used to construct a composite model, train the composite model based on the training set, optimize the model parameters of the composite model through the validation set, and input the test set into the trained composite model for performance evaluation. The composite model includes at least a convolutional neural network local feature extraction layer, a relation feature layer, a feature fusion module, and a classification module.
[0151] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
Claims
1. A method for classifying electrocardiogram (ECG) sequence data based on convolutional neural networks and multi-head attention mechanisms, characterized in that, include: Acquire multivariate time series data of electrocardiogram, preprocess the multivariate time series data of electrocardiogram to obtain preprocessed time series signal data, and divide the preprocessed time series signal data into training set, validation set and test set; A local feature extraction layer of a convolutional neural network is constructed. The preprocessed time-series signal data is input into the local feature extraction layer of the convolutional neural network, and the local feature vector is extracted through one-dimensional convolution operation. A relational feature layer with a multi-head attention mechanism is constructed. Local feature vectors are input into this relational feature layer. The correlation weights between time points are calculated by combining the multi-head self-attention mechanism with the electrophysiological prior weight matrix to obtain the global feature representation. The local feature vector and the global feature representation are adaptively fused to obtain the fused feature, and the fused feature is input into the fully connected layer for classification output to obtain the classification result of the electrocardiogram sequence data; A composite model is constructed, trained on the training set, optimized using the validation set, and its performance is evaluated by inputting the test set into the trained composite model. The composite model includes at least a convolutional neural network local feature extraction layer, a relational feature layer, a feature fusion module, and a classification module. The classification module includes a fully connected layer and a Softmax classification layer.
2. The ECG sequence data classification method based on convolutional neural networks and multi-head attention mechanism according to claim 1, characterized in that, The process of preprocessing multivariate time series data of electrocardiogram to obtain preprocessed time series signal data, and dividing the preprocessed time series signal data into training set, validation set and test set includes: Time-series signal data from the target domain are collected as multivariate time-series data for electrocardiograms; this time-series signal data includes univariate or multivariate signal sequences. The time series signal data is denoised, normalized and time synchronized to obtain a time series matrix with a preset length and a preset feature dimension. Label each sample in the time series matrix with an ECG category label. Divide the time series matrix into a training set, a validation set, and a test set by stratified sampling according to a preset ratio, so that the proportion of each ECG category label is consistent with the original proportion of the labels in the time series matrix.
3. The ECG sequence data classification method based on convolutional neural networks and multi-head attention mechanism according to claim 1, characterized in that, The step of inputting the preprocessed time-series signal data into the local feature extraction layer of a convolutional neural network and extracting local feature vectors through one-dimensional convolution operations includes: Several convolutional blocks are set in the local feature extraction layer of the convolutional neural network. Each convolutional block consists of two one-dimensional convolutional layers, a batch normalization layer and an activation layer. The preprocessed time series signal data is input into the convolution block, and convolution calculation is performed on the time axis by the convolution kernel to extract the local time features of the preprocessed time series signal data and obtain the feature map. The feature map is reduced in dimensionality by a pooling layer to obtain local feature vectors; The expression for the convolution calculation is: ; In the formula, For the first Layer convolution kernel; For bias terms; This is the activation function for the activation layer.
4. The ECG sequence data classification method based on convolutional neural networks and multi-head attention mechanism according to claim 1, characterized in that, The process of inputting local feature vectors into the relational feature layer, and calculating the correlation weights between time points using a multi-head self-attention mechanism combined with the electrophysiological prior weight matrix to obtain the global feature representation includes: In the relational feature layer, local feature vectors are mapped to queries, keys, and values, respectively. Based on the peak location and average cardiac cycle information in the preprocessed time-series signal data, the local feature vector is segmented according to the cardiac cycle on the time axis to construct an electrophysiological prior weight matrix. Within each attention head of the relation feature layer, the correlation score between different time points is calculated, and the correlation score is fused element by element with the electrophysiological prior weight matrix to obtain the weighted correlation matrix. The outputs of all attention heads in the relation feature layer are concatenated and a global feature representation is output through a linear transformation.
5. The ECG sequence data classification method based on convolutional neural networks and multi-head attention mechanism according to claim 4, characterized in that, The construction of the prior weight matrix for cardiac electrophysiology includes: Assign a first prior weight to time points within the same cardiac cycle that satisfy the electrophysiological sequence relationship; A second prior weight is assigned to time point pairs in adjacent cardiac cycles that satisfy the electrophysiological sequence relationship. Weight decay is applied to time points where the time interval exceeds a preset interval threshold or the physiological relevance is lower than a preset relevance threshold. Wherein, the first prior weight is greater than the second prior weight.
6. The ECG sequence data classification method based on convolutional neural networks and multi-head attention mechanism according to claim 4, characterized in that, The correlation score between different time points is calculated within each attention head of the relation feature layer. The correlation score is then fused element-wise with the prior electrophysiological weight matrix to obtain a weighted correlation matrix, which includes: An initial relevance score is obtained by performing a dot product operation on the query and the key; The initial correlation scores are scaled to avoid the gradient vanishing due to excessively large dot product values. The scaled correlation score is multiplied element-wise with the electrophysiological prior weight matrix to obtain the fused correlation score. The fused relevance scores are normalized to obtain the attention weight matrix, thereby transforming the fused relevance scores into non-negative weights that sum to a preset value. The attention weight matrix is multiplied by the value to obtain the attention output, thereby achieving selective emphasis on ECG feature waveforms.
7. The ECG sequence data classification method based on convolutional neural networks and multi-head attention mechanism according to claim 1, characterized in that, The step of adaptively fusing local feature vectors and global feature representations to obtain fused features, and then inputting the fused features into a fully connected layer for classification output, includes: Align local feature vectors with global feature representations according to the time dimension and cardiac cycle; Based on the multi-lead structure of electrocardiogram, lead-level weighting is applied to the aligned local feature vectors and global feature representations. The weighted local feature vector and global feature representation are input into the gating unit to generate dynamic fusion coefficients; The weighted local feature vector and global feature representation are weighted and fused based on the dynamic fusion coefficient to obtain the fused feature. The fused features are input into a fully connected layer, and after nonlinear transformation, the multi-class classification probability of the electrocardiogram sequence data is output.
8. The ECG sequence data classification method based on convolutional neural networks and multi-head attention mechanism according to claim 7, characterized in that, The step of inputting the weighted local feature vector and global feature representation into the gating unit to generate dynamic fusion coefficients includes: The weighted local feature vectors and global feature representations at each time step are concatenated along the feature dimension to obtain the fused input vector; The fused input vector is input into the fully connected layer, and the gated output value is obtained by combining the learnable weight vector and the bias term. The gating output value is processed by an activation function to obtain a dynamic fusion coefficient with a value between zero and one. Among them, the learnable weight vector and bias term of the fully connected layer are learned during the training of the composite model through the backpropagation algorithm; The expression for the dynamic fusion coefficient is: ; ; In the formula, The dynamic fusion coefficient; This is the gated output value; For activation functions; A learnable weight vector; For bias terms; To fuse the input vectors.
9. The ECG sequence data classification method based on convolutional neural networks and multi-head attention mechanism according to claim 1, characterized in that, The process of training the composite model based on the training set and optimizing the model parameters of the composite model using the validation set includes: Set training hyperparameters, use an adaptive optimization algorithm to train the composite model, and decay the learning rate according to a preset strategy during the training process; Based on the stratified sampling method, samples are extracted from the training set according to the sample proportion of each ECG category to obtain small batch training data; The error between the predicted value and the true label is calculated using the cross-entropy loss function with class weights. Perform forward propagation on the current sample in the mini-batch training data, use the cross-entropy loss function for backpropagation, and update the model parameters of the composite model. Calculate accuracy and recall on the validation set, and use the model parameters corresponding to the optimal validation metrics as candidate optimal model parameters. If the validation metrics on the validation set do not improve within a preset number of consecutive rounds, the system will roll back to the candidate optimal model parameters based on the early stop strategy.
10. The ECG sequence data classification method based on convolutional neural networks and multi-head attention mechanism according to claim 9, characterized in that, The calculation of the error between the predicted value and the true label using the cross-entropy loss function with class weights includes: Count the number of samples for each ECG category in the training set; The initial weights are calculated based on the sample size. When a certain ECG category belongs to a preset set of clinical high-risk categories and the sample size is less than a preset sample size threshold, the initial weight of that ECG category is adjusted by a risk enhancement coefficient and used as the adjusted weight. The initial weights of the other ECG categories remain unchanged and are used as the adjusted weights. The adjusted weights for all ECG categories are normalized to obtain the category weights for the corresponding ECG categories; The weighted cross-entropy loss is calculated as the error based on the class weights, the true labels, and the predicted probabilities. For samples that are consistently misclassified in several training rounds, increase the sampling probability in subsequent training rounds; The expression for calculating the weighted cross-entropy loss based on class weights, true labels, and predicted probabilities is as follows: ; In the formula, The weighted cross-entropy loss; The number of samples; Number of categories; For the first Category weights for each ECG category; For the first The sample at the th Real labels for each ECG category; For the first The sample belongs to the first Predicted probabilities for each ECG category.