{'fraction_non_alphanumeric': 0.027755821043835678, 'fraction_numerical': 0.0081076808419341, 'mean_word_length': 4.779169929522318, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 1, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Weakly Supervised Arrhythmia Detection Based on Deep Convolutional Neural Network

Supervised deep learning has been widely used in the studies of automatic ECG classification, which largely benefits from sufficient annotation of large datasets. However, most of the existing large ECG datasets are roughly annotated, so the classification model trained on them can only detect the existence of abnormalities in a whole recording, but cannot determine their exact occurrence time. In addition, it may take huge time and economic cost to construct a fine-annotated ECG dataset. Therefore, this study proposes weakly supervised deep learning models for detecting abnormal ECG events and their occurrence time. The available supervision information for the models is limited to the event types in an ECG record, excluding the specific occurring time of each event. By leverage of feature locality of deep convolution neural network, the models first make predictions based on the local features, and then aggregate the local predictions to infer the existence of each event during the whole record. Through training, the local predictions are expected to reflect the specific occurring time of each event. To test their potentials, we apply the models for detecting cardiac rhythmic and morphological arrhythmias by using the AFDB and MITDB datasets, respectively. The results show that the models achieve beat-level accuracies of 99.09% in detecting atrial fibrillation, and 99.13% in detecting morphological arrhythmias, which are comparable to that of fully supervised learning models, demonstrating their effectiveness. The local prediction maps revealed by this method are also helpful to analyze and diagnose the decision logic of record-level classification models.

consuming and error-prone, constituting a challenge to process big volume of data with an acceptable time and economic cost. Therefore, automatic ECG analysis technology is in demand to cope with this challenge.
In recent years, the deep learning technology (aka, deep neural network, DNN) has greatly promoted the development of automatic ECG analysis, especially automatic ECG classification. Some studies claim to have model performance better than that of average cardiologists on detection of a wide spectrum of ECG abnormalities [2]. The success of DNN in ECG classification is largely benefited from the large-scale annotated ECG datasets, such as PhysioNet/CinC challenge 2017 dataset [3], China physiological signal challenge 2018 dataset [4]. Generally, these datasets just contain annotations about the rhythm types existing in a whole record, but no specific time information of their occurrence of each arrhythmic episode. As a result, the models trained with these datasets are designed just to identify the types of rhythm occurred in a record, but not to specify their occurring time.
However, the occurrence time of ECG abnormalities is very important to the diagnosis, especially when the recording is quite long (over 48 hour's recording for Holter monitor). Even for a short recording, the detected occurrence time of each rhythm can also help to uncover the decision logic of the DNN model, and assist in model diagnosis and improvement. However, fully supervised learning method for this problem requires fine labeling of the occurrence time of each cardiac event, which would entail huge labor and time costs. Besides, the large amount of electronic medical record data accumulated by medical institutions are basically roughly annotated. How to make full use of these data resources is still an open question.
In this study, we propose a weakly supervised learning (WSL) method for arrhythmias detection in order to reduce the dependency of the model on training by fine labeled dataset. During the model training, the new method just uses available labels of an ECG recording to get information about the absence/presence of heart rhythms in the recording. But, by constraining the decision-making process of the model, the model is forced to infer for itself the exact timing of each rhythm. The decision process of the DNN model is divided structurally into two stages, namely local prediction and global aggregation. In the local prediction stage, the model's output (hidden in the network) is a sequence of vectors, each indicating probabilities of rhythms in a local time period. And then, in the global aggregation stage, the local predictions are aggregated into a single vector in which each element indicates the probability for a corresponding rhythm to exist in the whole record. Therefore, we can get the occurrence time information of rhythms from the local predictions of the model, although the model training isn't supervised by the time information.
The main contributions of this work are summarized as follows: 1) We propose weakly supervised detectors for cardiac arrhythmia that can detect the exact occurrence time of arrhythmias but are trained using only coarse record-level labels. To our knowledge, this is the first publicly accessible work that deals with the problem of arrhythmia detection in a weakly supervised manner.
2) We design special aggregation mechanisms for rhythmic arrhythmias and morphological arrhythmias, and achieve performances comparable to the state-of-the-art of fully supervised methods on the AFDB and MITDB databases.
3) Our proposed models accept data with variable input lengths, and are more efficient than models that make predictions by using sliding windows.
4) The local prediction maps generated by the proposed models can be used to reveal the patterns recognized by the model, which is helpful to interpret and diagnose the DNN model even though the purpose is only record-level classification.
The remainder of this paper is organized as follows. In section II, we introduce related works, including weakly supervised learning and cardiac arrhythmia detection. Section II describes our WSL method for arrhythmia detection in details. Section III presents a series of experimental results to evaluate the performance of our method. In section IV, we discuss the effectiveness and limitation of the proposed method, and discuss potential future research issues. Finally, we conclude this work in section V.

II. Related Works
Computer-aid diagnosis of cardiac arrhythmia has been studied for several decades, and in recent years, it has been remarkably boosted by the deep learning technology. In this work, we also follow the methodology of deep learning to deal with this problem, but in a weakly supervised approach. Although the concept of weak supervised learning has not been applied in the field of ECG analysis, it has been studied in the field of computer vision (CV), especially for purposes of object localization and semantic segmentation. We will introduce some relevant works regarding arrhythmia detection and weakly supervised learning in this section.

A. Cardiac Arrhythmia Detection
From the manifestations in ECG, cardiac arrhythmias can be categorized as morphological arrhythmia or rhythmic arrhythmia.
Morphological arrhythmia is manifested as an abnormal heart beat in ECG, e.g., premature ventricular contraction (PVC), while rhythmic arrhythmia is formed a series of heart beats with irregular intervals, e.g., atrial fibrillation (AF). Research works based on deep learning has been published for detection of both types of arrhythmias, and some have achieved significant improvements in performance compared with the traditional methods [5][6][7].
For detection of morphological arrhythmias, the input of a DNN model is typically a short ECG segment containing just the heartbeat that need to be classified or also the heartbeats that are adjacent to it. A diversity of DNN architectures have been developed for this task. Among them, convolutional neural network (CNN) and its variants are most frequently used in this domain [8][9][10]. CNN can model the structure of data in its receptive field with relatively few parameters [11]. Besides, CNN is easy to be processed in parallel, which makes it have good acceleration effect on GPU and other massively parallel processors. The DNN architectures that have been applied on this task also include restricted Boltzmann machine (RBM) [12] and recurrent neural network (RNN) [13]. The above methods almost all rely on other QRS detection methods to locate the heartbeats, but a novel Unet based DNN proposed by [14] solved the problems of QRS detection and heartbeat classification simultaneously.
When the goal is to detect rhythmic arrhythmias, the input of the model is usually not the ECG signal for a single heartbeat, but a long segment with a length ranging from a few seconds to tens of seconds as the rhythmic arrhythmias generally span a long time period. Some architectures that are developed for morphological arrhythmia detection can also be used for detection of rhythmic arrhythmias. CNN is also the most commonly used architecture for this task, but usually needs a wider receptive field [6,15]. Therefore, many researchers have introduced new mechanisms to make the CNN have deeper structure and stronger recognition capability. A deep 1D residual neural network (1D ResNet) with up to 34 convolutional layers was first introduced by [2] for detection of 12 types of cardiac rhythms and achieved high diagnostic performance similar to that of cardiologists. 1D ResNet has also been combined with RNN [16] and attention mechanisms [17] for further performance improvements.

B. Weakly Supervised Learning
The technology of WSL has been intensively studied for object localization and semantic segmentation in the field of computer vision, where the training process is just supervised by the existence of object instances in the image. The solution of this problem will obviate the need for large-scale fine-annotated datasets, thus can save lots of labor and time costs for labeling. However, in the field of ECG analysis, although a large number of clinical test records can be potentially leveraged for WSL just with little effort of annotation, to the best of our knowledge, the WSL method hasn't been studied for detection of ECG abnormalities.
CNNs have been widely studied for weakly supervised learning [18][19][20]. The architecture of CNN is basically constituted by a stack of pattern searchers, and its intermediate outputs represent the spatial distribution of features of a certain level, i.e., feature maps. Therefore, CNN has the potential to locate the objects despite be trained on image-level labels. In order for the network to be trained end-to-end, a mechanism is necessary to link the local predictions, i.e., the feature maps, to the image-level labels. The mechanism will determine the distribution of gradients on the feature map during the model training, and thus it has a very important impact on the localization performance. In many studies, the mechanism is implemented by aggregating the local predictions into the global prediction, e.g., global average pooling (GAP) [21] and global maximum pooling (GMP) [18,22]. Some researchers also proposed mediate mechanisms between GAP and GMP, such as Log-Sum-Exp (LSE) [19] and global weighted rank pooling (GWRP) [23]. Besides the aggregation mechanism, the image-level supervision can also be transferred to constraints on the feature maps. For example, researchers in [24] proposed constrained CNN (CCNN) in which a generalized constraint mechanism is capable to model any linear constraints on the predicted label distribution, such as presence, absence and proportion of each label.

A. Problem Formulation
We define an ECG recording as a series of sampling points = { 1 , … , } where n is the number of sampling points in the record. The goal is to construct a model D that generates labels = { 1 , … , } for an input X. ∈ = {1, … , } denotes the rhythm type of xi, and L is the set of considered rhythm types. Here, we consider D as a parametric probabilistic model which generates a probability distribution of yi for each xi: D(X; θ)i,j = p(yi=j | X), where θ is the parameters of D. To find a suitable θ, we need to train the model on a dataset. Ideally, the training set provides the ground truth of Y for each X in it. However, in this study, we try to solve the problem in a weakly supervised setting: each training example is a tuple (X, LX) in which LX ⸦ L only indicates the rhythm types present in X.   The above formulation is applicable to both tasks of detecting rhythmic and morphological arrhythmias. However, when considering that detection of morphological arrhythmias is at the heartbeat level, we can extend the above formulation to the case in which heartbeat is the location time unit for the detection. In this situation, the target labels for X is = { 1 , … , } where b is the number of heartbeats in X, and yi is the rhythm type of the i th heartbeat. As the heartbeat in an ECG recording is orders of magnitude less than the sampling points, the space of the possible solutions for YB is far smaller than that for Y. Therefore, this transformation significantly simplifies the original problem.

B. Base Architecture
We propose a novel DNN architecture for weakly supervised arrhythmia detection, as shown in Fig. 1. The main body of this architecture is a 1D ResNet, which has been demonstrated effective in the supervised arrhythmias detection [16,17]. To make it suitable for weakly supervised learning, we make two important extensions to it: one is local prediction before aggregation, the other is concatenation of features of different scale. We will explain both of the extensions in the following.
The input of the model is an ECG signal in a form of n × c, where n is the number of sampling points and c is the number of channels (or leads) for the signal. The input is processed by a stack of convolutional blocks, each belongs to one of two types: type I and type II. The structures of both types of the convolutional blocks are shown in Fig. 2. Each type of block contains two convolutional layers and other assistant layers. The output of a block's last convolutional layer is merged with its input by elementwise addition, then a max pooling layer with a pool size of 2 compresses the output to half of its original length. The difference of the two types is that the input of a type II block is processed by 3 layers (i.e., batch normalization, ReLU activation and dropout) before entering to its first convolutional layer, while in a type I block, these 3 layers are omitted. In the whole architecture, only the first convolutional block is in type I, the others are all of type II. Due to the max pooling layers, the feature map shrinks as processed by the blocks. For example, feature map outputted by the first block is in a form of n/2 × f, where n/2 is the length of the feature map, and f is the number of channels of the feature map (different from the number of channels of the model input, i.e., c) which is determined by the number of filters in the convolutional layer. As a result, the resolution of the final block output is much lower than that of the input, and thus affect the precision of local predictions. To solve this problem, we upsample the output of each block to the length of the input by simply repeating its elements, then concatenate the outputs from different blocks along the channel dimension. The mechanism is similar to the layer fusion in [25], but the difference is that, in this mechanism, features at different levels are concatenated rather than added up. This design makes features of different levels accessible to the local prediction layer, and enable the local prediction layer to learn a logic that selects features at the appropriate levels for the prediction.
After the concatenation, we get a feature map in a form of n × kf, where k is the number of convolutional blocks, and kf is the number of channels (or features) of the feature map. Then, we make local predictions base on it by using convolutional layer and softmax activation. The number of filters in the convolutional layer is m, i.e., the number of classes (or label types) considered by the model. The kernel size of each filter is only 1, which means that the prediction is made with only a single feature vector of the feature map. Then softmax activation is applied along the channel axis, which generates the probability distribution of labels for each sampling point in the input. All the probabilities formed a local prediction map in a shape of n × m, as shown in Fig. 1. Given a local prediction map, we can easily get the local prediction labels, i.e., Y for X in the formulation, by finding the position of maximum activation value at each sampling point.
To make the model trainable with record-level labels, we add an aggregation layer after the local predictions to generate recordlevel predictions. This is one of the main differences of our model with the general supervised ones which typically first aggregate local features to global features and then make record-level predictions based on the global feature vector. Here, we restrict the aggregation to be done along the time axis, and the output is a vector with length of m indicating the predicted probability of the existence of each rhythm. The aggregation mechanisms are described in detail in the next subsection. The loss of the prediction can be computed by the loss functions that are designed for supervised record-level prediction models. We note that the loss function should support multi-label classifications, as multiple rhythms may coexist in a single ECG record. Therefore, we use the binary cross entropy as the loss function for training of our models.

C. Aggregation Mechanisms
The aggregation mechanism is critical to the success of a DNN model for weakly supervised learning. In the field of computer vision, different methods have been proposed, such as GAP, GMP and LSE. GAP averages all the local predictions as in (1): where D is a shorthand of D(X; θ). We argue that GAP is not suitable for use in the weakly supervised arrhythmia learning, as some arrhythmias just self-terminate in a very short time period of an ECG record. For example, premature ventricular contractions (PVC) are generally very rare in a ECG record, and are mostly isolated. When an ECG record contains only one PVC, the application of GAP will cause the activation value for the PVC too low and mislead the gradient during training.
In contrast, GMP selects only the maximum value for a class as its record-level activation as in (2): By using GMP, a rhythm is considered to be present in a record as long as it occurs at some time in the record regardless of duration. One possible problem of GMP is that it only back propagates the gradient to the position that has the maximum activation.
When an arrhythmia exists in a record, it seems that the gradient for the loss of its detection will vanish as long as one of the activation values for the arrhythmia is close to 1 during the whole record. For this reason, some researchers suggests that GMP can lead to underestimation of the true region of an object [23]. However, in our architecture, softmax makes the activations of all classes sum to 1 at any position. Consequently, the loss from classes that are not present in a record will force the activation values to be transferred to the classes that do exist in the record. Therefore, we argue that GMP doesn't cause underestimation of the period of a rhythm, and thus may be a practical aggregation mechanism for the proposed architecture.
The aggregation can also be done in a mechanism between GAP and GMP. For example, LSE is a convex approximation of the max function [19]: where r > 0 is a hyper-parameter that controls the degree of the approximation to GMP. When r is in a high value, this function is similar to GMP, but when r is very low, this function is similar to GAP.
All the mechanisms described above, which we call ordinary aggregation mechanisms in this paper, just focus on the activation values, and are not discriminatory for positions. In fact, it is unnecessary to make detection at every sampling point of an ECG recording. As mentioned above, morphological arrhythmias are generally detected in unit of heartbeat, thus we can select only one prediction in each heartbeat for the aggregation. Specifically, we mask out all the local predictions except the prediction at the R peaks which are in the middle region of the corresponding heartbeats and can usually be accurately recognized by algorithms such as the Pan-Tompkins algorithm [26]. On the selected local predictions, the aggregation methods mentioned above can also be applied. Then, we get the corresponding masked aggregation mechanisms: global average R-peak predictions pooling (GMAP) as in Equation (4), global maximum R-peak predictions pooling (GMRP) as in Equation (5), and Log-Sum-Exp on R-peak predictions (LSER) as in Equation (6).
where B denotes the set of R peak positions. These mechanisms lead the model to learn features around the R peak, and thus can reduce the difficulty of pattern recognition. Furthermore, the number of local predictions that are used for aggregation are significantly reduced, which can help to simplify the problem of weakly supervised arrhythmia detection for reasons discussed in the section of problem formulation.

IV. Experiments
We evaluate the proposed methods on the detection tasks of rhythmic and morphological arrhythmias. In addition to the evaluation of the overall architecture, we also assess the effects of different aggregation methods, different model depth and different data distribution on the performance of the models.

A. Datasets
Atrial fibrillation (AF), as one of the most common rhythmic arrhythmias, is chosen in our experiment as the object to be detected in the task of rhythmic arrhythmias detection. The model for this task is evaluated on the MIT-BIH Atrial Fibrillation Database (AFDB) [27,28]. For convenience of model training and testing, we split the recordings into segments of 20 seconds.
The segments that contain a rhythm with length less than 3 seconds are discarded, because the period of the rhythm in such a segment may be too short for the detection. If we extract segments evenly on the recordings, then the segments of a single rhythm will be far more than that of multiple rhythms. However, this will lead to the results evaluated on these segments would not clearly reflect whether a model has the WSL ability to take advantage of the supervisory information of unspecified locations. Therefore, we take another method which extracts segments densely (in a stride of 5 seconds) in the rhythm transition region, but sparsely (in a stride of 250 seconds) in other regions. In this way, we get 1219 segments of pure AF rhythm, 1812 segments of other rhythms, and 1360 segments of both AF and other rhythms, as shown in Fig. 3(a).
As for the task of morphological arrhythmias detection, we utilize the MIT-BIH Arrhythmia Database (MITDB) [28,29] for the evaluation. The rhythm type of each heartbeat in the database is annotated by cardiologists. As some types of heartbeat are very rare, here we only consider to detect 5 types of heartbeats: normal beat (N), left bundle branch block beat (LBBB), right bundle branch block beat (RBBB), atrial premature beat (APB) and premature ventricular contraction (PVC), according to a common practice in this domain [14,[30][31][32]. The recordings in this dataset are also split into segments of 20 seconds without overlap.
Segments that don't contains heartbeat belonging to the above 5 classes are discarded, and the remaining segments are 4120 in total, as shown in Fig. 3(b). In our experiments, only the first channel of ECG in both datasets is used for model training and testing, although the processing of double-channel data is also supported by our design. The denotation of labels are as follows: "AF" denotes atrial fibrillation, "Other" denotes rhythms other than AF, "N" denotes normal rhythm, "L" denotes LBBB, "R" denotes RBBB, "A" denotes APB, and "V" denotes PVC. "A+B" indicates both rhythm "A" and rhythm "B" are present in the segment.

B. Metrics
The models for both detection tasks are evaluated in a 5-fold cross validation setting. The test results of all the folds are aggregated together for the metrics calculation. The metrics for detection of rhythmic and morphological arrhythmias are both calculated in beat level. The metrics used in our experiments includes sensitivity (Se) as in (7), specificity (Sp) as in (8), positive predictivity (Ppr) as in (9), and accuracy (Acc) as in (10).
= + , where, for a certain class, TP, short for true positive, is the number of correctly detected beats of the class; FP, short for false positive, is the number of beats that are falsely predicted as the class; FN, short for false negative, is the number of beats that belong to the class but are not detected by the model, and TN, short for true negative, is the number of correctly detected beats that don't belong to the class. For multi-class classification, we also calculate the overall value across all classes of each metric, which is the average weighted by the frequency of each class.

C. Implementation of Models
In our experiments, the hyper-parameters of convolutional blocks are shared by all the tested models. For both types of blocks, each convolutional layer contains 32 filters, and the kernel size of each filter is 32. The weights of convolutional layers are initialized by the method proposed in [33]. The dropout rate of each dropout layer is 0.25 which is chosen experimentally. The depth and aggregation mechanism vary from model to model, which is discussed in particular in the following subsections. Besides, in each model, the part responsible for making local predictions consists of two convolutional layers: the number of filters in the first is 32, while that in the second is equal to the number of classes.
We implement the models based on the Keras framework [34] and train the models on a workstation with one CPU running at 3.5 GHz, an NVIDIA Quadro k6000 GPU, and 64 Gb of memory. The method used for model optimization is Adaptive Moment Estimation (Adam) [35], where β1 is 0.9, β2 is 0.999, and the learning rate is 0.001. The training of a model is terminated when no decrease of the loss value on the validation set is observed over 10 epochs.

D. Assessment of Aggregation Mechanisms
The results of models with different aggregation mechanisms for AF detection are shown in Table I Table II, III, IV, VI, VII and IV.
From the results in Table I, we find that, among all the models, the model with GMP achieves the highest performance in each metric of the two segment sets. Specifically, the Se (95.71%) of this model is significantly higher than that of other models by over 8 percentage points on all segments and by over 15 percentage points on rhythm transition segments. For each model, its metrics evaluated on the rhythm transition segments are all lower than that on all segments, which confirms our previous inference that rhythm transition region is prone to misclassification. Nevertheless, the model with GMP exceeds 90% in terms of Se, Ppr and Acc on the rhythm transition segments. Therefore, GMP has a significant performance advantage compared with other evaluated aggregation mechanisms in the weakly supervised detection of AF.  In order to compare the influences of different aggregation methods on the local predictions more intuitively, we plot the local prediction maps on a rhythm transition (from other rhythms to AF) segment as shown in Fig. 4. The predictions of different models in the other rhythms period before the 800 th sampling point are all stable and similar to each other. However, when time approaches the transition point of rhythm, the predictions of different models show significant differences: the AF probability predicted by the model with GMP is roughly monotonically increasing, while those predicted by other models fluctuate wildly. Then, in the AF period after the transition, the predictions of the model with GMP are still obviously more unstable than that of other models. It can be seen that the local prediction of the model with GMP is more in line with our expectation of a WSL model.
The experimental results for morphological arrhythmias detection are shown in Table II. The common base structure for these models consists of 5 convolutional blocks. The aggregation mechanisms evaluated for this task include GAP, GMP, LSER, and also their masked versions (i.e., GARP, GMRP and LSER). Among all the evaluated models, the model with GMRP achieves the highest performance in detecting all the 5 types of heartbeats with Se and Ppr both exceeding 95% for all the heartbeats except APB. Although its Se for APB is just 72.3%, it's still 17.6 points higher than that (54.7%) of the second-placed model, i.e., LSER (r=5). The models with masked aggregation mechanisms generally achieve higher Se in detecting APB and PVC than that with ordinary aggregation mechanisms. One possible explanation is that the fragments manifesting APB or PVC usually occupy only a small fraction of an ECG signal, thus they tend to be overwhelmed by the predictions of other parts when using ordinary aggregation methods.
The local predictions by different models on a multi-label segment are shown in Fig. 5 (using ordinary aggregation mechanisms) and Fig. 6 (using masked aggregation mechanisms). We find that the predictions of the model with GMP, as in Fig. 5(b), at the R peaks are not very differentiated between the categories, but its predictions in the area immediately after the R peaks are more differentiated. It reflects that the predictions of the model for the beats are not well aligned with the R peaks, which may be an important reason for the poor performance of this model. By contrast, the predictions of models with GAP or LSE are well differentiated most of the time, and have good identification ability for RBBB. However, their predicted positions of APB are distributed over almost every heartbeat cycle, but the true location is not identified. This suggests that these models failed to recognize the real characteristics of APB, which are also reflected in their ultra-low scores in detecting APB as reported in Table   II.
For the models with masked aggregation mechanisms, their predictions their predictions only make sense around the R peaks, because predictions elsewhere are masked out before aggregation during the training process. In consequence, we only need to look at their predictions around the R peaks, which are zoomed in horizontally for the convenience of observation as shown in the dashed boxes in Fig. 6. The model with GMRP gives very differentiated predictions at each R peak of the segment, which is in sharp contrast to the predictions of the model with GMP. The predictions of other models in Fig. 6 exhibit similar patterns to that of the model with GMRP, but with ambiguities or errors at some R peaks. Therefore, compared with other aggregation mechanisms, GMRP is more effective for the weakly supervised detection of morphological arrhythmias.

E. Relevance of Model Depth
In this subsection, we evaluate the influence of model depth to the performance of proposed WSL models. As the main body of our network consists of convolutional blocks, we measure the model depth by the number of convolutional blocks in the network.
In theory, model depth can affect the performance of a model in at least two ways. On one hand, model depth is positively correlated with the model complexity and also their ability to abstract data. On the other hand, the size of the receptive field is also affected by the model depth: usually the deeper the model, the larger the receptive field.
In our experiments, 5 models of depth from 5 to 9 blocks are trained to detect AF, with the results shown in Table III. The performance of these models is similar to that of each other, except for the model with 5 blocks whose Se and Acc are obviously lower than that of other models. It indicates that, for the weakly supervised detection of AF, a too shallow model can lead to significant performance degradation, while as the model deepens gradually and reaches a certain depth, its performance tends to be stable. One possible reason for this is that the receptive field of a too shallow model is not sufficient to extract the solid features to detect AF, in view of that a rhythmic abnormality usually tasks a little longer (e.g., several seconds) to fully manifest.
The results of models of different depths for the detection of morphological arrhythmias are shown in Table IV. The model with 5 blocks achieves the best performance in detecting each of the 5 types of beats. Both the deeper or shallower models show some performance degradation, especially for the detection of APB. This tells us that, for the weakly supervised detection of abnormal is set to 5. The denotation of labels are as follows: "N" denotes normal rhythm, "L" denotes LBBB, "R" denotes RBBB, "A" denotes APB, and "V" denotes PVC. heartbeats, the model depth is critical to the performance of a model. It makes logical sense, since the features of a heartbeat is usually manifested in a relatively fixed time scale, and too large or too small a receptive field can affect the extraction of its features. "blks", short for blocks, is the unit of measurement for the model depth.

F. Influence of Data Distribution
Data distribution is another important factor to be considered when evaluating a WSL model. In the previous subsection, we have seen the impacts of different distributions of testing data (all segments vs. rhythm transition segments) on the model performance, now we examine the effects of different distributions of the training data. We consider the data distributions in the segment level, i.e., the distribution of segment labels which are the sources of the supervision information available to the models.
The original distributions of segments extracted from AFDB and MITDB are shown in Fig. 3. We classify the segments into 2 types: single-label segment and multiple-label segment. A single-label segment has only one type of label in its annotation, e.g., AF or LBBB, while a multiple-label segment is annotated by a combination of several labels, e.g., AF+Other or N+APB. Each of our original segment sets contains these two types of segments simultaneously. However, for WSL, the intensity of supervision provided by these two kinds of segments is different. Theoretically speaking, the more labels a segment has, the more possible permutations of the labels in its time period, and thus the weaker supervision it provides. If a dataset contains only single-label segments, the problem actually departs into full supervised learning. Therefore, by changing the data distribution, we can better understand the real effect of WSL.
Here, we propose 4 distribution modifications as shown in Table V. The basic idea behind these modifications is to change the ratio between different types of segments by reducing the number of segments of a certain type. To facilitate the comparison of effects across distributions, in each fold of the cross validation, we adjust only the training set and the validation set, but leave the testing set unchanged. Among these modifications, Single50 and OnlyMultiple reduce the number of single-label segments to 50% and 0% of their original, respectively. Similarly, Multiple50 and OnlySingle remove 50% and 100% of multiple-label segments, respectively.
Three modified data distributions are used to evaluate the performance of AF detection with the results shown in Table VI.
OnlyMultiple is not used for this evaluation, because its every segment has the same label combination (i.e., AF+Other) and thus cannot provide any supervision information for the model. The model to evaluate consists of 7 convolutional blocks and uses GMP as the aggregation mechanism, and its results on the original dataset have also been reported in Table II and III. From Table VI, we see that the performance of the model trained on Multiple50 is obviously lower than that on the original dataset, while that on OnlySingle is even worse. It indicates that the model does use the weak supervisory information from the multiple-label segments  Single50  50  100  50  100  100  100  Multiple50  100  50  100  50  100  100  OnlySingle  100  0  100  0  100  100  OnlyMultiple  0  100  0  100  100  100 "SL", short for single label, denotes segments with only one label. "ML", short for multiple labels, denotes segments with multiple labels. Each figure in this table represents the percentage of the corresponding segments retained in the corresponding dataset after the modification. to optimize its parameters. The model trained on Single50 also show some performance degradation which reflects the important role of single label samples in the model training.
For the detection of morphological arrhythmias, all the 4 modified distributions are used to evaluate the model's performance.
The model used in these experiments consists of 5 convolutional blocks, and use GMRP as the aggregation mechanism.

G. Comparison with Other Works
To further validate the performance of the method, we compare the results with that of other studies, although they are either manually designed algorithms or fully supervised learning methods. Based on the previous analysis, two models are selected for the comparison with other studies: the model with 7 convolutional blocks and GMP is trained to detect AF, while the model with 5 convolutional blocks and GMRP is trained for the detection of morphological arrhythmias. Because most studies for AF detection based on AFDB reported their results on the whole dataset or on a random subset of the dataset, we also report the results of testing our model across the whole AFDB dataset, as shown in Table VIII. The model is trained in one fold of the cross validation as stated above, with the training data accounting for about 6% of the whole dataset. As for the model for morphological arrhythmias detection, its confusion matrix is an aggregation of the smaller confusion matrixes of each fold in the cross validation, and then is used to compute the scores on each metric, because the whole MITDB dataset has been used in the cross validation. The results of this model are shown in Table IX, where the overall values (i.e., the averages weighted by the frequency of each class) of the metrics are present on the bottom line.
The results of different studies for AF detection are shown in Table X. The methodologies adopted by these studies range from manually designed algorithms [36][37][38], traditional machine learning methods [39,40], to DNN-based methods [5,15,16,41]. In particular, all the machine learning based methods are fully supervised. We note that different studies usually have different prediction units, i.e., the resolution of their predictions in time. For example, the method proposed by Xia et al. [15] makes   [32], faster R-CNN [42], U-net [14], LSTM [43,44] to auto-encoder [7,44]. Many other studies that have sought to achieve similar goals are not reported in this  The confusion matrix is the aggregation of the sub-matrixes that are computed based on the test data of the corresponding folds in the cross validation.  almost the same classification goals, the beat-level distributions of their testing set are different from each other. Some studies tested their methods on a subset of the original dataset, and some studies even changed the ratio between sample sizes for each category. These changes will undoubtedly have impacts on the results, so we present the distribution of testing set of each study in table XI to get the reader's attention. Besides, due to some studies didn't provide their results on some metrics, we calculate these results, marked with "*" in the table, by ourselves based on the confusion matrixes provided in their paper. The results show that the performance of our model is comparable to that of the fully supervised methods. Among these studies, Oh et al. [14] and Hou et al. [7] use almost the same testing set as ours, thus the comparison between these three studies makes more sense. Our model ranks the second on each metric among the three studies, although we only use a weakly supervised methodology.

V. Discussion
The experimental results demonstrate the efficiency of the proposed weakly supervised methods for the detection of AF and morphological arrhythmias. The framework essentially uses the global labels as a constraint on local predictions and optimizes the local predictions indirectly by optimizing the performance of global predictions. When the sample size of the training set is large enough, the parameters of the model will converge to a small range, and stable predictions will be given. The aggregation mechanism in the framework is critical especially when the sample size is not enough to make the model parameters fully convergent. So, we see that the masked aggregation mechanisms significantly improved the detection performance for beat-level arrhythmias, since they reduced the number of possible solutions for local predictions. Considering that there are a large number of simply labeled ECG data in clinical practice, this technique is expected to play an important role in the exploration and utilization of these data.
Another advantage of our method is that it can process input of any length at a stroke, and thus are more efficient than methods that make predictions in sliding windows. Generally, in order to make the predictions more detailed in time scale, many methods detect arrhythmias in small windows that slide along time. However, the sliding windows, usually overlapping with its neighbors, can not only lead to greater computational overhead, but also break the context required for the predictions. By contrast, our method can make detailed predictions for input of any length, hence avoids the drawbacks of sliding window.
This method can also play an important role to interpret and diagnose the model even if detailed local predictions are not the goals. Recall the example shown in Fig. 4, the model with GAP makes some wrong predictions after the rhythm transition point, which reveals its weakness to recognize some features of AF. By using this method, researchers can locate the defects of a model more quickly and find targeted solutions.
The proposed method also has its limitations especially on the detection of morphological arrhythmias. The masked aggregation mechanisms depend on the positions of QRS complexes. Although many methods have achieved very high accuracy for the QRS detection on the MITDB, their performance may degrade when the signal quality is very poor. Therefore, the dependence on the third-party QRS detection algorithms will bring uncertainty to the performance of our model. The imbalance between categories is another problem that needs to be further addressed, even for fully supervised learning. The low performance for the APB detection is partly due to its small sample size compared with other categories. In future studies, we will explore new methods to enhance the model's performance in dealing with low quality signals and imbalanced categories.

VI. Conclusion
In this work, we proposed a WSL framework for the detection of both rhythmic and morphological arrhythmias. Our model only requires record-level annotations as the supervision during training but can efficiently generate detailed local predictions for the testing data with the performance comparable to that of the fully supervised methods. We revealed the influence of aggregation mechanisms, model depths and data distributions on the performance of our method through experiments. In particular, the masked aggregation mechanism plays an important role to improve the performance for the detection of morphological arrhythmias. Our model has great potential in exploiting the existing ECG recording data, reducing the cost of manual labeling and diagnosing the DNN models.