(C) PLOS One This story was originally published by PLOS One and is unaltered. . . . . . . . . . . Data distribution impacts the performance and generalisability of contrastive learning-based foundation models of electrocardiograms [1] ['Gul Rukh Khattak', 'National Heart', 'Lung Institute', 'Imperial College London', 'London', 'United Kingdom', 'Konstantinos Patlatzoglou', 'Joseph Barker', 'Libor Pastika', 'Boroumand Zeidaabadi'] Date: 2026-09 Abstract Contrastive learning is a widely adopted self-supervised pretraining strategy, yet its dependence on cohort composition remains underexplored. We present Contrasting by Augmented Patient Electrocardiograms (CAPE) foundation model and pretrain on four cohorts (n = 5,203,269), from diverse populations across three continents (North America, South America, Asia). We systematically assess how cohort demographics, health status, and population diversity influence the downstream performance for prediction tasks also including two additional cohorts from another continent (Europe). We find that downstream performance depends on the distributional properties of the pretraining cohort, including demographics and health status. Moreover, while pretraining with a multi-centre, demographically diverse cohort improves in-distribution accuracy, it reduces out-of-distribution (OOD) generalisation of our contrastive approach by encoding cohort-specific artifacts. To address this, we propose the In-Distribution Batch (IDB) strategy, which preserves intra-cohort consistency during pretraining, discourages learning of spurious cohort-specific features, and instead promotes clinically meaningful variability within cohorts. This leads to improved out-of-distribution robustness, with gains of 9–40% in downstream label prediction performance. This work provides insights into pretraining strategies for more clinically deployable and generalisable foundation models. Author summary Artificial intelligence (AI) can learn from large collections of electrocardiograms (ECGs) and support the development of new diagnostic tools without relying on extensive manual annotation. However, it remains unclear how the data used to pretrain these models influences their ability to generalise across different patient populations. We developed the Contrasting by Augmented Patient Electrocardiograms (CAPE) foundation model and explored how the choice of training data influences downstream performance. Using over five million ECGs from diverse healthcare cohorts, we found that the learned representations capture not only clinically relevant cardiac information but also characteristics of the populations and healthcare systems from which the data originated. While increasing dataset size and diversity improved performance on similar populations, it could reduce accuracy when models were applied to unseen cohorts. We also developed an In-Distribution Batch (IDB) approach to help models focus on meaningful ECG patterns rather than differences specific to individual cohorts. This improved model performance on unseen populations by 9–40%. These findings highlight that the composition of pretraining data is a key determinant of model generalisation and provide a strategy for developing ECG foundation models that are more reliable across diverse patient populations. Citation: Khattak GR, Patlatzoglou K, Barker J, Pastika L, Zeidaabadi B, Birdi AR, et al. (2026) Data distribution impacts the performance and generalisability of contrastive learning-based foundation models of electrocardiograms. PLOS Digit Health 5(9): e0001623. https://doi.org/10.1371/journal.pdig.0001623 Editor: Jie-Zhi Cheng, Shanghai United Imaging Intelligence Co Ltd, CHINA Received: January 31, 2026; Accepted: July 14, 2026; Published: September 2, 2026 Copyright: © 2026 Khattak et al. This is an open access article distributed under the terms of the Creative Commons Attribution License, which permits unrestricted use, distribution, and reproduction in any medium, provided the original author and source are credited. Data Availability: A subset of the CODE dataset, comprising 15% of patients, is publicly available at https://zenodo.org/records/4916206. The PTB-XL dataset is open access and can be obtained from https://physionet.org/content/ptb-xl/1.0.1/. Access to the UK Biobank data is available upon approved application through https://www.ukbiobank.ac.uk. Due to ethical and legal restrictions, the remaining datasets used in this study are not publicly available. The results underlying the figures and tables in this manuscript will be made available by the authors upon request, where possible. Funding: British Heart Foundation (BHF), UK: programme grant funding to FSN and NSP (RG/F/22/110078), clinical research training fellowship to AS, JB and AEM (FS/CRTF/21/24183 and FS/CRTF/24/24624), BHF Centre of Research Excellence funding to FSN, AS and NSP (RE/18/4/34215 and RE/24/130023). Medical Research Council (MRC), UK funding to LP (MR/Y000803/1). AS is also funded by the Academy of Medical Sciences (SGCL033\1014), Cardiomyopathy UK (C2502) and an NIHR Academic Clinical Lectureship. YL is funded by Shanghai Municipal Key Clinical Specialty (shslczdzk01701) and Fudan University AI4S Project (FudanX24AI056). The authors are supported by the National Institute for Health Research Imperial Biomedical Research Centre. FSN and ALPR received funding from the British Council through the Brazil–UK Research in DiGital HEalth and AI-ECG (BRIDGE-ECG) project (Grant No. RE2025-4590). Competing interests: I have read the journal’s policy and the authors of this manuscript have the following competing interests: J.W.W. and D.B.K. were previously on the advisory board for Heartcor Solutions LLC, for whom they remain independent consultants. J.W.W. also reports research funding from Anumana and is a consultant for HeartBeam SInc. F.S.N. reports speaker fees from GE Healthcare and is on the advisory board for J&J Medtech. A.S., L.P., B.Z., and F.S.N. declare inventorship on a patent application relating to AI-ECG methods. A.S., L.P., B.Z., F.S.N., D.B.K., J.W.W., and N.S.P.S hold equity shares in Cardiovolt.ai Limited. The remaining authors declare no conflicts of interest. Introduction Electrocardiograms (ECGs) provide a non-invasive and widely accessible means of recording the heart’s electrical activity, capturing myocardial depolarisation and repolarisation across multiple leads positioned on the skin. Despite the potential to reveal important physiological insights, the clinical utility of ECGs remains in part constrained by the need for expert interpretation. Additionally, recent studies have demonstrated the potential for artificial intelligence-enhanced ECG (AI-ECG) analysis to identify undiagnosed disease and predict risk of adverse events better than human experts [1–4]. Foundation models learn general patterns from large datasets, which enhances performance, accelerates learning, and reduces the need for extensive task-specific annotations [5]. While they can be pretrained with supervised learning [6], self-supervised learning (SSL) has become the predominant paradigm, with common approaches including contrastive learning [7] and masked modeling [8]. These SSL methods enable models to learn rich, transferable representations that generalise across a wide range of tasks and domains. The abundance of unlabelled ECG data, coupled with the high cost of expert annotation, highlights the promise of SSL as an effective pretraining strategy for ECG analysis. Among SSL approaches, contrastive learning has gained prominence in the medical domain [9], for its ability to learn meaningful representation (features) by promoting similarity between contextually positive pairs and dissimilarity from negatives [7]. It has been applied to ECGs with various definitions of positive and negative pairs, involving contrasting different signals [10–12] or applying a range of signal transformations (augmentations) to the same signal [13–15]. A key question is whether the characteristics of the pretraining data significantly affect foundation model performance. In computer vision, prior work has shown that dataset composition can affect representations learned through contrastive self-supervised learning, while also demonstrating a degree of robustness across different dataset types [16]. Similarly, in the ECG domain, a prior study using self-supervised pretraining across medium-sized secondary-care datasets reported comparable out-of-distribution performance despite substantial distributional differences between cohorts [17]. However, it remains unclear whether these observations generalise to datasets derived from different healthcare levels with varying diversity and size. This question is particularly relevant in ECG analysis, where signals from healthy individuals are often relatively homogeneous, while pathological conditions introduce structured but heterogeneous deviations. A large-scale systematic investigation of ECG data is therefore needed. Deep learning models often struggle with generalisation, particularly when evaluated on out-of-distribution (OOD) data [18,19]. This is complicated further by the ability to learn complex patterns beyond human capabilities, as demonstrated by the ability to predict patient age or sex from ECG data [4]. However, this strength also makes them prone to overfitting the training data. Performance has been shown to vary with patient health status [20] and may be influenced by demographic factors such as ethnicity, which have been associated with identifiable signatures in ECG signals [21]. This challenge is further compounded by significant heterogeneity across cohorts, stemming from differences in age distribution, clinical profiles, recording devices, and institutional practices [20,22]. The ECG-based models experience degraded performance when tested on data from different sources, proving that domain shifts across the ECG cohorts can affect model generalisation [23]. Foundation models must overcome domain shift to be clinically viable, a challenge observed across multiple medical modalities. For example, a retinal image-based foundation model trained on British patient data demonstrated poor transferability to Chinese cohorts [24]. In parallel, evidence of socio-demographic bias has also been reported in foundation models used for medical diagnosis based on large language models [23], highlighting broader concerns regarding fairness and robustness across diverse populations. Together, these findings underscore the critical need for robust generalisation across diverse populations to ensure the practical utility of foundation models in biomedical applications. Several approaches have been proposed to improve domain generalisation [25] across diverse domains. Uncertainty-aware and ensemble-based foundation models [26] improve external validation performance in glioblastoma classification by leveraging multiple models and uncertainty estimation to better distinguish tumours from their mimics. Contrastive learning with fairness objectives [27] enhances fairness in pathology AI systems by incorporating fairness constraints into the learning objective. For ECG tasks, self-supervised contrastive learning [17] and aggregation of features from multiple intermediate layers [28] have been crucial for improving generalisation, collectively highlighting the need for models that generalise across diverse clinical environments. Contrastive learning performance inherently depends on how positive and negative samples are defined, so we hypothesise that cohort distribution can significantly influence the quality of the learned features. We explore the generalisation and robustness of our Contrasting by Augmented Patient Electrocardiograms (CAPE) foundation model [29], which integrates within-patient contrastive strategy [11] with signal-preserving augmentations such as random cropping and zero masking. Using a multi-centre cohort of over five million ECGs, we investigate how data diversity, label distribution, clinical setting, and cohort composition during pretraining affect the robustness and generalisability of ECG representations. To address the challenges introduced by heterogeneous data sources, we further propose the In-Distribution Batch (IDB) approach, which constrains contrastive learning batches to single cohorts to reduce distributional noise. This work aims to guide the development of clinically meaningful foundation models that can generalise effectively across diverse patient populations and healthcare environments. Discussion To our knowledge, this is the first systematic exploration of how different pretraining cohorts influence learned feature representations, and how varying downstream cohorts affect predictive performance on target labels. Within our contrastive learning framework, we find that combining multiple pretraining cohorts markedly reduces the generalisation ability of downstream models to out-of-distribution (OOD) data. To mitigate this effect, we introduce an In-Distribution Batch (IDB) strategy, which explicitly aligns pretraining distributions with the intended downstream application. Foundation models leverage large-scale cohorts to learn rich and generalisable feature representations. This capability is particularly valuable in medical applications, where annotated datasets are typically limited in size and exhibit significant class imbalance or skewed distributions. The role of foundation models in medicine is rapidly expanding [34], demonstrating enhanced performance in diagnostic tasks across modalities such as X-rays [35], CT scans [36], and electrocardiograms [14]. Despite these advances, a fundamental challenge remains achieving robust generalisation to real-world clinical data, which frequently exhibit substantial distributional shifts [37]. Models trained on medical data from one cohort could not be generalised to another population from another continent across diverse modalities [23,24]. Such limitations have motivated approaches to improve generalisation, including the use of intermediate-layer features from residual networks [28], ensemble-based foundation models [25], and fairness-aware contrastive learning [26]. Although the current work focuses on contrastive learning, cohort-dependent effects on representation learning and downstream generalisation may also extend to alternative foundation model paradigms, including masked autoencoders and generative pretraining approaches. Pretraining data distribution affects the quality of the learned representations The influence of health status and demographic factors on learned representations is of critical importance for successful clinical translation. Within supervised learning paradigms, model performance has been observed to differ significantly between healthy and unhealthy populations, with models typically yielding superior results for healthy subjects [20]. Prior work has demonstrated the demographic sensitivity of foundation models, particularly in vision-language tasks involving chest X-ray classification [38], as well as in large language models applied to medical decision-making [23]. Distributional differences were found between the pretraining cohorts, namely PTB-XL (n = 21,837) and Chapman (n = 10,646) for self-supervised learning; however, downstream model performance was not substantially affected by the choice of the pretraining cohort [17]. Previous studies have primarily focused on interpreting results within specific population subsets [20], applying transfer-learning for model pretrained on one cohort to supervised tasks in another cohort [24,28,38], or testing pretrained models on external cohorts [28]. Comparisons between pretraining cohorts have been limited to just two small datasets, both sourced from secondary care settings [17]. However, to date, there has been no comprehensive investigation into how the characteristics of pretraining cohorts influence the quality of learned representations in medical datasets. A central contribution of our study lies in its novel comparative analysis of CAPE models pretrained on distinct population cohorts. Our findings reveal that along with the cohort size and the pretraining methodology, the health composition and demographic diversity of the training cohort emerge as pivotal determinants for learning robust features. We assume that the primary-care and volunteer cohorts comprise relatively healthier individuals compared with secondary-care patients, a notion further supported by their higher five-year mortality burden (Table 2). The CAPE models pretrained in secondary care cohorts outperform those pretrained in larger but healthier populations. A plausible explanation for this is that healthier populations tend to exhibit reduced variability, whereas secondary care cohorts encompass a diverse range of cardiac signal patterns. Our contrastive pretraining uses a single pair of ECGs from a patient in each epoch randomly sampled at the training time yet the performance is not solely attributable to the number of patients but also to more ECGs per patient. For example, BIDMC has a smaller number of patients as compared to VUMC but more ECGs per patient, displays a similar performance. This suggests that random sampling of patient ECGs during training can effectively exploit intra-patient ECG diversity. This was further supported by comparison with a strategy that used the same ECG pair across all epochs, which resulted in inferior performance (S4 Table). Pretraining our CAPE foundation model [29] on the large composite BCSV cohort, consistently outperforms models pretrained on individual cohorts (Fig 2). Therefore, we conclude that the effectiveness of contrastive pretraining depends heavily on the diversity, size, and health characteristics of the underlying population. Labelled data distribution affects performance evaluation Next, we address the performance evaluation for comparison between pretraining models. In a number of prior works [14], we find that a task-based comparison is undertaken on diverse cohorts. We observe that the same task across diverse cohort is not comparable as the task complexity greatly depends upon the cohort composition. To further illustrate how the performance is affected by cohort distribution, we examine the UKB cohort, which consistently achieves the highest performance across age and sex prediction for all CAPE models (Fig 2). The UKB stands out due to its narrow interquartile age range (IQR) and its composition as a cohort of healthy volunteers with low five-year mortality (Table 2). Prior studies suggest that models generally perform better on normal ECGs typical of healthy individuals, and struggle more with learning mappings from abnormal cardiac waveforms [20]. Factors such as the cohort’s overall health status, limited age variability, and high data quality likely contribute to the superior model performance observed in this setting. Therefore, performance comparison is only meaningful when evaluated on the same data, ideally using identical train/test splits. Combining diverse cohorts for a more effective pretraining of a foundation model with improved generalisation We explore the impact of pretraining data composition on OOD test performance. Prior work on ECG domain generalisation has explicitly treated data from different hospitals or sources as distinct domains, demonstrating the presence of substantial distributional shifts and corresponding performance degradation under such shifts [28]. Building on this, we evaluate downstream generalisation for our CAPE model trained on diverse cohorts. We trained lightweight MLP networks for target tasks on BIDMC and then externally validated on the rest of cohorts. We observed that combining diverse cohorts for contrastive pretraining significantly reduced the ability of downstream models to generalise to other cohorts (Table 3). We used explainability techniques to understand how the CAPE features map to different cohorts and identified cohort-related clustering (Fig 4A). These patterns aligned more strongly with the ECG recording device than with subject ethnicity (Figs 4B and 4C). Further analysis of ECGs acquired from two devices in CODE (S1–S4 Figs) revealed subtle differences in their frequency spectra that were not visually apparent, and aligning the spectra reduced the observed clustering. Our in-distribution batch (IDB) strategy rejects learning spurious cohort-specific features but learns more robust features that retain the performance when tested on external cohort. The CAPE model, when trained with batches formed from multiple cohorts, may inadvertently learn cohort-specific signals such as device characteristics, acquisition settings, or preprocessing artefacts. Although not clinically meaningful, these can be exploited during contrastive learning, leading to intra-batch distribution confusion where multiple underlying distributions coexist within a batch and the model relies on spurious correlation features rather than robust physiological patterns, potentially harming generalisation to unseen cohorts. In contrast, CAPE-Z enforces cohort-level consistency within each batch, reducing distributional heterogeneity and limiting the use of cohort-specific artefacts as discriminative shortcuts, thereby promoting more stable and clinically relevant representations. The performance improvement is most pronounced in datasets belonging to the pretraining cohorts, which strengthens the hypothesis that patient contrastive pretraining captures confounding distributional signals. However, a significant though smaller performance gain is also observed in other cohorts. A similar pattern is also seen in fine-tuned models (S5 Table). The demographic labels of age and sex were selected based on their availability across all cohorts for performance assessment. We further evaluated performance using cardiac labels, including reduced ejection fraction and five-year mortality, in cohorts with available data. The IDB approach outperformed in external validation, and the same performance trends observed for age and sex were also present for these cardiac labels. We also explored sex-stratified performance for five-year mortality and did not observe consistent subgroup-specific differences between CAPE-X and CAPE-Z. This suggests that the improvements associated with the IDB strategy are not driven by demographic subgroup effects but are more likely related to improved robustness to cohort-level distribution shifts. The observed variability across cohorts appears to be influenced by factors such as outcome prevalence and dataset-specific characteristics rather than model bias towards a particular subgroup. We observe that the IDB demonstrates robust performance for external cohorts (Table 3) but the performance for UKB is still higher than other cohorts. We examine the age distributions across cohorts (Fig 6A) and the mean absolute error (MAE) for age prediction normalised per age bin (Fig 6B). The UKB is not only limited to individuals aged 65–85 but has a skewed distribution with most subjects between 64 and 66 years, interestingly, this also corresponds to the region with the lowest MAE (Fig 6B). These findings suggest that the superior performance of UKB is not solely due to the health status of its participants (as reflected by very low five-year mortality), but also to the over-representation of individuals in the age associated with minimum MAE. This may explain why CODE and SHZS, which also consist of relatively healthy subjects, do not achieve similar performance due to wider age distributions (Fig 5A). By contrast, all-cause mortality prediction in the UKB yields a lower AUC, likely due to high imbalance due to low number of cardiovascular deaths. PNG larger image TIFF original image Download: Fig 6. Impact of age distribution on prediction error for CAPE-Z embeddings with a prediction head trained on BIDMC and tested on external cohorts. A) Age distributions across different datasets, illustrating mean age and range. B) Mean absolute error (MAE) normalized within each age bin, highlighting the age with minimum MAE and the overall age range. The distribution of labels significantly influences the aggregated performance metrics, as error varies across the age spectrum. Notably, the UK Biobank dataset exhibits a narrower age range with distinct peaks corresponding to ages associated with the lowest MAE. https://doi.org/10.1371/journal.pdig.0001623.g006 In this study, we investigated several critical and compelling questions concerning the generalisation capabilities of our CAPE foundational model. Specifically, we examined the role of cohort composition in shaping the robustness of the pretraining process, the performance of downstream supervised models, and their ability to generalise across diverse settings. Building on these insights, we also propose the IDB strategy aimed at enhancing downstream generalisation. We envision that future research can build upon this direction to further advance the understanding of unbiased performance evaluation and the development of more robust foundation models. Limitations This study aims to investigate how the performance of our contrastive learning methodology is affected by the distributions of the pretraining and label cohorts. While the insights gained are expected to be relevant to other contrastive learning settings, further experimentation is required to validate these findings across different learning paradigms, including supervised learning and alternative foundation model approaches such as masked autoencoders and generative pretraining methods. Our current work explores pretraining cohorts that differ across multiple complex factors, including ethnicity, socio-economic status, health status, hospital systems, and recording devices. We suggest that the individual contribution of each factor should be systematically investigated in future studies. Our analysis is limited to pretraining strategies, while performance remains influenced by bias in downstream datasets, which could be addressed through more detailed subgroup analyses and improved optimisation of downstream supervised training. We further acknowledge that preprocessing-based strategies may offer a means to mitigate device-specific artefacts. However, exploring such approaches lies beyond the scope of the present study and represents an important avenue for future investigation. Notably, these methods typically require explicit identification of the underlying sources of variation being encoded, which may extend beyond device-related factors. In contrast, IDB does not rely on prior knowledge of these sources. This study is not intended to provide state-of-the-art label prediction performance across cohorts, but rather to evaluate how supervised training behaves when using features from different pretraining methodologies while keeping labelled dataset sizes comparable. We therefore use random splits of equal size from each cohort for training and validation, based on features extracted from a frozen backbone, to ensure fair comparisons. Training the prediction head on the full cohort was not directly aligned with our primary research objective and future work may establish state-of-the-art performance for each cohort. Another limitation of the current work is that several cohorts used in the study are private datasets and therefore cannot be accessed as open-source resources. While we provide sufficient methodological detail to support replication, the trained model cannot be shared at present owing to the use of private data and ongoing related work. Conclusions This study emphasises the central importance of generalisation in contrastive learning for clinical applications. We find that the ability of a model to perform well across diverse patient cohorts depends critically on the composition of the pretraining data. Specifically, demographic and clinical heterogeneity in the pretraining cohort significantly influences the learned representations and downstream performance. Combining multiple cohorts during contrastive pretraining improves in-distribution performance but reduces out-of-distribution generalisation for our foundation model. We propose In Distribution (ID batch) method to effectively mitigate this degradation. These findings highlight that careful cohort design and evaluation protocols are essential for building foundation models that generalise reliably across diverse clinical populations, ultimately advancing the development of robust machine learning systems in healthcare. Materials and methods Ethical approvals All relevant ethical permissions have been obtained for all cohorts explored in the current study. The BIDMC cohort approval is provided by the Beth Israel Deaconess Medical Centre Committee on Clinical Investigations, IRB protocol # 2023P000042. The CODE study is approved by the Research Ethics Committee of the Universidade Federal de Minas Gerais, protocol 49368496317.7.0000.5149. The Institutional Research Board of Zhongshan Hospital (No. 2023-253R) approved the use of SHZS data with a waiver of patient consent. The Vanderbilt component of this study was reviewed and approved by the Institutional Review Board (#212147). The UKB has approval from the Northwest Multi-Centre Research Ethics Committee (application ID 48666). For PTB-XL, the Institutional Ethics Committee approved the publication of the anonymous data in an open-access database (PTB-2020–1). Preprocessing We used leads (I, II, V1-V6), as the remaining four leads (III, aVR, aVF, aVL) do not impart additional information [39]. We applied a bandpass filter (0.5 to 100 Hz) and a notch filter relevant to the mains frequency, re-sampled ECGs from different sources to a standard sampling frequency of 400 Hz on a millivolt scale. The final input to the contrastive learning model was a 7-s signal with dimensions of 2800 × 8. Preprocessing was standardised across all cohorts included in the study. Pretraining Architecture. Fig 7 presents an overview of the CAPE approach. We follow the patient contrastive learning (PCLR) [11], where ECGs from the same patient are treated as positive pairs, while all other ECGs in the batch are treated as negatives. We used a four-layer ResNet (Residual Network) architecture from prior works [11,40] as feature generating backbone. The contrastive loss was applied to the non-linear projections of the ECG embeddings (Fig 7). The resulting 256-dimensional ECG embeddings can subsequently be used for downstream supervised tasks. PNG larger image TIFF original image Download: Fig 7. Overview of the CAPE pretraining framework. Patient A has two ECG recordings (ECG1A and ECG2A), and Patient B, another patient in the same batch, has ECG1B and ECG2B. Each ECG undergoes temporal augmentations before being encoded by the network into a 256-dimensional feature vector. The contrastive loss encourages embeddings from the same patient’s ECGs to be closer together while pushing apart embeddings from different patients within the batch. https://doi.org/10.1371/journal.pdig.0001623.g007 Contrastive loss. The contrastive loss employed in this work was the InfoNCE loss [41], applied to nonlinear projections (similar to SimCLR [7]. Given that and are the non-linear projections of representations from two different augmented ECGs belonging to the same patient, the similarity between and is maximized over all other instances in the batch under a softmax function [42], over the similarity scores. The loss function is defined as follows in Equation (1): (1) Here, τ is a temperature parameter controlling the sharpness of the distribution, with a value of 0.1 in the current work. N denotes the number of positive pairs in the batch, and is an indicator function that evaluates to 1 when . The similarity function is defined using cosine similarity in Equation 2: (2) For batches drawn from a single distribution, we modify the loss as follows. Suppose the training cohort consists of multiple distributions . Let and be the projections of augmented ECGs from the same patient, both belonging to the distribution X. As each batch contains samples from only one distribution, the similarity between and is maximized over all other instances in the batch that also belong to distribution X. The modified loss is shown in Equation 3: (3) The total loss over all batches is given in Equation 4: (4) Training During standard CAPE pretraining, batches were randomly sampled across datasets of varying sizes, with each epoch comprising iterations equal to the total number of unique patients. In contrast the in-distribution batch (IDB) constrained each batch to samples drawn from a single dataset. Fig 8 depicts the IDB pretraining procedure: each cohort is partitioned into batches comprising two randomly selected ECG recordings per patient, and batches are randomly sampled during training. The CAPE pretraining randomly samples two ECGs per subject in each epoch, such that different epochs may include different recordings from the same subject. PNG larger image TIFF original image Download: Fig 8. Flowchart of the in-distribution batch (IDB) technique: for each epoch, batches are constructed from the same cohort using two randomly selected ECGs per patient. https://doi.org/10.1371/journal.pdig.0001623.g008 The contrastive loss performs best with larger batches due to the larger variation of the negative instances [7], but the computation resources limit the batch size. We used a batch size of 1024 (512 patients), as suggested in prior research [11] and trained the model for 200 epochs using the Adam optimiser [43]. The model was implemented in Tensorflow [44] (2.10.1). The initial learning rate was 0.1 and then decayed according to a half-period cosine schedule [45] (similar to previous approaches [11]. The training time in minutes per epoch on NVIDIA GeForce RTX 3090 was approximately 5 for the BIDMC dataset and 50 for the BCSV dataset. Prediction head The prediction head used a multi-layer perceptron (MLP) to learn nonlinear mappings from input features. The MLP architecture was optimised using a grid search over hidden layer sizes of [32, 64, 128, 256] for a two-layer network. For age prediction, the MLP consists of two layers with 256 and 128 neurons, respectively. For sex prediction, both layers contained 256 neurons. The model was trained to predict the target labels using a learning rate of 0.0001, with learning rate decay and early stopping applied to prevent overfitting. The architecture and hyperparameters, including learning rates, were optimised through a limited grid search. Performance metrics Classification performance for sex prediction was evaluated using the area under the receiver operating characteristic curve (AUC). This metric reflects the ability of the model to distinguish between classes across all possible classification thresholds. The AUC is computed by integrating the true positive rate against the false positive rate over varying thresholds. For age regression, model performance was assessed using the mean absolute error (MAE). MAE measures the average absolute difference between predicted and actual values. To compare cohort characteristics and CAPE models pretrained on different datasets, statistical significance was assessed using the Kruskal–Wallis test, with pairwise comparisons corrected using the Holm–Bonferroni method. Comparisons between the In-Distribution Batch (IDB) strategy and training with randomly sampled batches was performed using the Wilcoxon signed-rank test for age prediction (measured by MAE) and the DeLong’s test applied to model prediction scores for sex classification, with each comparison evaluated as a single hypothesis test. Code availability Due to ethical constraints associated with private datasets, the CAPE models used in this study cannot be publicly released. However, to support transparency and reproducibility, we provide a publicly accessible, small-scale working example that demonstrates the key findings of this study. This example utilizes a publicly available subset of the CODE dataset (https://zenodo.org/records/4916206) alongside the PTB-XL dataset and is available at https://github.com/gulrukhk/CAPE-test/. The example includes an external validation scenario, demonstrating performance both with and without the IDB training procedure. We provide CAPE features extracted from both datasets, together with multilayer perceptrons (MLPs) trained on features originally derived from the BIDMC dataset, for age and sex prediction. In addition, we include supervised learning experiments on the PTB-XL dataset to further demonstrate the effectiveness of the CAPE representations. These experiments comprise age regression, sex classification, and diagnostic classification at both the superclass and subclass levels. Together, the provided example and detailed methodological descriptions are sufficient to reproduce the core results and support the main conclusions of this study. Acknowledgments The authors would also like to thank the InSIGHT Core in the Center for Healthcare Delivery Science at Beth Israel Deaconess Medical Center for assistance in obtaining primary data. We also acknowledge computational resources and support provided by the Imperial College Research Computing Service (http://doi.org/10.14469/hpc/2232) [END] --- [1] Url: https://journals.plos.org/digitalhealth/article?id=10.1371/journal.pdig.0001623 Published and (C) by PLOS One Content appears here under this condition or license: Creative Commons - Attribution BY 4.0. via Magical.Fish Gopher News Feeds: gopher://magical.fish/1/feeds/news/plosone/