## 3 Materials and Methods
### Beat-Level Training Phase  
In the beat-level training phase, the PI-beat classifier is trained using a source dataset, denoted as \(D_{s}=\left\{(\mathbf{x}_{i}^{s},\mathbf{b}_{i}^{s},\mathbf{p}_{i}^{s})\right\}_{ i=1}^{n}\), where n is the number of beat segment samples. \(\mathbf{x}_{i}^{s}\) denotes the \(i\)-th sample of a 2.4 s ECG segment (referred to as beat segment) labeled with a beat type \(\mathbf{b}_{i}^{s}\) for a patient with the identifier \(\mathbf{p}_{i}^{s}\). The vectors \(\mathbf{b}^{s}\) and \(\mathbf{p}^{s}\) are represented as a one-hot encoding vector, each denoted as \(\mathbf{b}_{i}^{s}=[\,b_{i,1}\,,\,b_{i,2}\,,\,\,\ldots,\mathbf{b}_{i,\text{B}}]\) (B: the number of beat types) and \(\mathbf{p}_{i}^{s}=[\,p_{i,1}\,,\,p_{i,2}\,,\,\,\ldots,\mathbf{b}_{i,\text{P}}]\) (P: the number of patients), respectively.  
Beat Loss (\(L_{beat}\))  
\(L_{beat}\) is the loss to quantify the disparity between the predicted and actual beat classes and is calculated using the categorical cross-entropy, defined as:  
\[L_{beat}=-\frac{1}{N}\sum_{i=1}^{N}\sum_{j=1}^{B}b_{i,j}\cdot\log\left(\hat{b }_{i,j}\right) \tag{1}\]  
Figure 2: The overview of our proposed method. (**a**) In the beat-level training phase, we pre-train the patient-independent beat classifier. The classifier is trained using an adversarial framework to avoid learning patient-specific features of the ECG signal at the beat level. (**b**) In the rhythm-level training phase, beat-score vectors are generated using the pre-trained patient-independent beat classifier. These vectors are concatenated to generate a PI-BSM for a 10 s ECG signal, which serves as input to the rhythm classifier.  
In Equation (1), \(N\) is the number of samples, and \(b_{i,j}\) and \(\hat{b}_{i,j}\) represent the ground-truth value and the prediction score for the \(i\)-th sample of the \(j\)-th beat class, respectively. By minimizing \(L_{beat}\) during training, the model can enhance its ability to predict beat classes. Patient Loss (\(L_{patient}\))  
\(L_{patient}\) is the loss to measure the discriminability between the predicted and actual patient labels. Since we intend to train the model so that it predicts patients poorly, \(L_{patient}\) needs to be maximized during training. To calculate \(L_{patient}\), we utilized categorical cross-entropy, which is defined as:  
\[L_{patient}=-\frac{1}{N}\sum_{i=1}^{N}\sum_{j=1}^{P}p_{i,j}\cdot\log\left(\hat {p}_{i,j}\right) \tag{2}\]  
In Equation (2), \(N\) represents the total count of the samples. \(p_{i,j}\) denotes the ground truth, while \(\hat{p}_{i,j}\) signifies the prediction score of the \(i\)-th sample of the \(j\)-th patient ID. Total Loss (\(L_{total}\))  
\(L_{total}\) is defined as the weighted sum of \(L_{beat}\) and \(L_{patient}\). The trade-off between beat classification ability and patient invariance needs to be carefully compromised to optimize the model parameters. To address this concern, the two losses are not assigned equal weights, and weighting coefficients \(\lambda_{1}\) and \(\lambda_{2}\) are introduced to \(L_{beat}\) and \(L_{patient}\), respectively. Consequently, \(L_{total}\) is defined as:  
\[L_{Total}= \lambda_{1}\cdot L_{beat}-\lambda_{2}\cdot L_{patient} \tag{3}\]  
To train the PI-beat classifier, we employed a SE-ResNet model [29]. The SE-ResNet model combines the ResNet [30] architecture with a squeeze-and-excitation (SE) block. The SE block assigns varying weights to feature maps, emphasizing more crucial features to enhance performance. Considering its ability to suppress patient-related features, we deemed it appropriate to adopt SE-ResNet for inter-patient scenarios [31]. Figure 3 depicts the configuration of the PI-beat classifier.  
Figure 3: The overall architecture of the patient-independent beat classifier.