The ECG encoder module provides the foundational backbone for all downstream tasks in SSRL-ECG. It defines a six-layer 1D convolutional encoder that maps raw multi-lead ECG signals to dense temporal feature maps, along with a supervised classifier head and a masked-reconstruction pretraining model. All classes live inDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Tumo505/SSL-for-ECG-classification/llms.txt
Use this file to discover all available pages before exploring further.
ssrl_ecg.models.cnn.
ConvBlock
ConvBlock is the atomic building block used throughout the encoder. Each block applies a 1D convolution, batch normalization, and ReLU activation in sequence. Padding is computed automatically as kernel_size // 2 to preserve temporal resolution when stride=1.
Constructor Parameters
Number of input channels (e.g., 12 for a standard 12-lead ECG).
Number of output channels produced by the convolution.
Width of the 1D convolutional kernel. Padding is set to
kernel_size // 2
so temporal length is preserved when stride=1.Stride of the convolution. Use
stride=2 to halve temporal resolution.Forward
Input tensor of shape
[batch, in_ch, time].Output tensor of shape
[batch, out_ch, time'], where time' = ceil(time / stride).ECGEncoder1DCNN
ECGEncoder1DCNN is a six-layer 1D CNN that serves as the primary backbone for both self-supervised pretraining (SimCLR, BYOL) and supervised fine-tuning. Three stride-2 layers reduce temporal resolution by a factor of 8 overall, while channel width progressively doubles twice to capture increasingly abstract features.
Architecture
| Layer | Kernel | Stride | In channels | Out channels |
|---|---|---|---|---|
| 1 | 11 | 2 | in_ch | width |
| 2 | 7 | 1 | width | width |
| 3 | 7 | 2 | width | width × 2 |
| 4 | 5 | 1 | width × 2 | width × 2 |
| 5 | 5 | 2 | width × 2 | width × 4 |
| 6 | 3 | 1 | width × 4 | width × 4 |
width=64, the encoder produces 256-channel feature maps (out_channels = width * 4). The overall temporal stride is 8× (three stride-2 layers), so a 1 000-sample input yields feature maps of length 125.
Constructor Parameters
Number of input ECG leads. Standard 12-lead ECGs use
12; single-lead setups may use 1.Base channel width that controls model capacity. Final output channels equal
width * 4
(256 at the default). Reduce to 32 for a lighter model; increase to 128 for higher capacity.Attributes
| Attribute | Type | Description |
|---|---|---|
features | nn.Sequential | The six-block convolutional stack. |
out_channels | int | Number of output channels (width * 4). |
Forward
Raw ECG tensor of shape
[batch, in_ch, time].Feature map of shape
[batch, width*4, time/8]. For the default settings this is
[batch, 256, time/8].Example Usage
ECGClassifier
ECGClassifier wraps any ECGEncoder1DCNN backbone with an adaptive average-pooling layer and a linear classification head. It is the standard model for supervised fine-tuning after SSL pretraining.
Constructor Parameters
A pretrained (or randomly initialised)
ECGEncoder1DCNN instance. The head dimension
is inferred from encoder.out_channels.Number of output classes. For the PTB-XL five-class diagnostic task, use the default
5.Forward
ECG tensor of shape
[batch, in_ch, time].Raw (un-sigmoidised) class logits of shape
[batch, n_classes].
Apply torch.sigmoid for multi-label probabilities or torch.softmax for
mutually exclusive classes.Fine-tuning Example
SSLReconstructionModel
SSLReconstructionModel pairs the ECGEncoder1DCNN backbone with a mirrored transposed-convolution decoder for masked reconstruction pretraining. The decoder progressively upsamples the latent feature map back to the original signal length, restoring all input leads. This is an alternative SSL objective to contrastive methods like SimCLR and BYOL.
Decoder Architecture
The decoder mirrors the encoder’s 8× downsampling using threeConvTranspose1d layers (each with stride 2), followed by a final Conv1d that projects back to in_ch leads:
c = encoder.out_channels. Output length is clamped or padded to exactly match the input length.
Constructor Parameters
Number of ECG leads in both input and reconstructed output.
Base channel width passed through to the internal
ECGEncoder1DCNN.Forward
Masked ECG tensor of shape
[batch, in_ch, time]. Masking (e.g., zeroing out
random segments) should be applied before this call.Reconstructed ECG tensor of shape
[batch, in_ch, time], with identical length to the input.