Models API¶
Auto-generated from docstrings.
ecgen.families.base¶
Shared base class for generative-model families.
Capabilities
dataclass
¶
What a family supports; checked when a config is built.
Attributes:
| Name | Type | Description |
|---|---|---|
conditional |
bool
|
Accepts condition fields (a |
cfg |
bool
|
Supports classifier-free guidance at sampling time. |
inpainting |
bool
|
Can fill in unobserved parts of an ECG at sampling time. |
likelihood |
bool
|
Can evaluate (a bound on) log-likelihood. |
stages |
int
|
Number of training stages (2 for VQ-VAE + prior). |
Source code in src/ecgen/families/base.py
GenerativeModule
¶
Bases: LightningModule
Base LightningModule for every family.
Each family owns its own training logic (loss, optimizers, manual vs. automatic optimization), so this class only fixes the shared contract:
__init__(config=None, **kwargs)built withutils.config.resolve_config, soload_from_checkpoint(path)works from the flat saved hyperparameters;- batches are read with :meth:
get_signal, which accepts :class:~ecgen.types.ECGBatchand the legacy dict / tuple formats; - conditional families build :attr:
conditionerfrom theirconditioningconfig with :meth:init_conditioning, and get conditions with :meth:condition(training) or :meth:prepare_condition(raw values at sampling time); - :attr:
data_info(lead names, fs, normalizers of the training data) is set by the runner and stored in checkpoints; - :meth:
samplereturns[n, C, T]generated ECGs.
Class attributes
family: Family name.
supports: :class:Capabilities.
accepts: ConditionOutput slots the network consumes
(subset of {"vector", "tokens", "dense"}).
Source code in src/ecgen/families/base.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
fixed_seq_length
property
¶
Signal length the network is built for, or None if any length works.
init_conditioning
¶
Build :attr:conditioner from a conditioning config (None = unconditional).
Source code in src/ecgen/families/base.py
condition
¶
Encode a training batch's conditions (None for unconditional models).
Source code in src/ecgen/families/base.py
prepare_condition
¶
prepare_condition(cond: Optional[Mapping[str, Any]], n: int) -> Tuple[Dict[str, Tensor], Dict[str, Tensor]]
Turn raw condition values into model inputs for n samples.
Numeric fields with a stored normalizer are given in raw units, either as
a list/array of length F (broadcast to all samples) or [n, F]; NaN
or None marks a missing value. Other fields are passed as tensors (or
anything torch.as_tensor accepts), broadcast if they have no batch
dimension. Fields not given are left out (the model uses its null
embedding for them).
Returns:
| Type | Description |
|---|---|
Tuple[Dict[str, Tensor], Dict[str, Tensor]]
|
|
Source code in src/ecgen/families/base.py
encode_condition
¶
encode_condition(n: int, cond: Optional[Mapping[str, Any]] = None, obs: Optional[Tensor] = None, obs_mask: Optional[Tensor] = None, drop: Drop = None) -> Optional[ConditionOutput]
Raw sampling-time conditions → :class:ConditionOutput (None if unconditional).
Source code in src/ecgen/families/base.py
set_data_info
¶
ecgen.nn.blocks¶
ResidualBlock1D
¶
Bases: Module
1D Residual block with group normalization
Source code in src/ecgen/nn/blocks.py
ecgen.nn.encoders¶
Encoder1D
¶
Bases: Module
1D Encoder for ECG signals
Source code in src/ecgen/nn/encoders.py
ecgen.nn.decoders¶
Decoder1D
¶
Bases: Module
1D Decoder for ECG signals
Source code in src/ecgen/nn/decoders.py
ecgen.families.vae.config¶
ecgen.families.vae.vae_1d¶
GlobalPrior
¶
Bases: Module
Learned p(z | c) for a global latent [B, D]; zero-initialised (starts as N(0, I)).
Source code in src/ecgen/families/vae/vae_1d.py
ConditionalPrior
¶
Bases: Module
Learned prior p(z | c): a Gaussian per latent position.
Global conditions give a per-channel mean/log-variance; dense conditions (at latent resolution) add a per-position term. Zero-initialised, so it starts as N(0, I).
Source code in src/ecgen/families/vae/vae_1d.py
VAE1D
¶
Bases: Module
Variational Autoencoder for ECG signals; a conditional VAE when
cond_dim (global conditions, via FiLM) or dense_channels
(time-aligned conditions, concatenated) is set.
latent_type="spatial" (default) keeps a [latent_channels, T / f]
latent map with a per-position Gaussian prior — good reconstructions and a
first stage for latent priors, but samples straight from the prior are not
coherent ECGs. latent_type="global" uses one [global_latent_dim]
vector per ECG (flattened encoder output -> linear; linear -> decoder input
map), whose prior samples decode to coherent ECGs; it needs seq_length.
Source code in src/ecgen/families/vae/vae_1d.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
latent_dense
¶
Dense conditions pooled to the latent length.
encode
¶
encode(x: Tensor, c: Optional[Tensor] = None, dense: Optional[Tensor] = None) -> tuple[Tensor, Tensor]
Encode input to latent distribution
Source code in src/ecgen/families/vae/vae_1d.py
reparameterize
¶
decode
¶
Decode latent to reconstruction (dense at full resolution).
Source code in src/ecgen/families/vae/vae_1d.py
prior_params
¶
prior_params(length: int, c: Optional[Tensor], dense: Optional[Tensor], batch_size: int, device) -> tuple[Tensor, Tensor]
Mean and log-variance of p(z | c) (zeros for the standard normal prior).
length is the latent map length (ignored for a global latent).
Source code in src/ecgen/families/vae/vae_1d.py
latent_shape
¶
Shape of one sample's latent.
forward
¶
forward(x: Tensor, c: Optional[Tensor] = None, dense: Optional[Tensor] = None) -> tuple[Tensor, Tensor, Tensor]
Full forward pass
Source code in src/ecgen/families/vae/vae_1d.py
encode_to_latent
¶
decode_from_latent
¶
ecgen.families.vae.vae_lightning¶
VAELightning
¶
Bases: GenerativeModule
PyTorch Lightning wrapper for VAE1D model.
With config.conditioning it is a conditional VAE: global conditions
(vector) modulate the encoder and decoder with FiLM, time-aligned
conditions (dense, e.g. a masked ECG) are concatenated to their inputs,
and learned_prior replaces N(0, I) with p(z | c).
Source code in src/ecgen/families/vae/vae_lightning.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
reconstruct
¶
Reconstruct input ECG signals (unconditional models).
sample
¶
sample(n_samples: int = 16, seq_length: int = 5000, cond: Optional[Mapping[str, Any]] = None, obs: Optional[Tensor] = None, obs_mask: Optional[Tensor] = None, paste_observed: bool = True, **kwargs: Any) -> Tensor
Sample ECGs: z from the prior (p(z | c) if learned), then decode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_samples
|
int
|
Number of ECGs. |
16
|
seq_length
|
int
|
Length (multiple of the downsampling factor). |
5000
|
cond
|
Optional[Mapping[str, Any]]
|
Raw condition values (see :meth: |
None
|
obs
|
Optional[Tensor]
|
Partially observed ECG |
None
|
obs_mask
|
Optional[Tensor]
|
Mask of |
None
|
paste_observed
|
bool
|
Copy the observed part of |
True
|
Source code in src/ecgen/families/vae/vae_lightning.py
ecgen.families.vqvae.config¶
VQVAEConfig
dataclass
¶
Configuration for VQ-VAE model.
Source code in src/ecgen/families/vqvae/config.py
PriorConfig
dataclass
¶
Configuration for PixelCNN prior model.
Source code in src/ecgen/families/vqvae/config.py
ecgen.families.vqvae.vector_quantizer¶
VectorQuantizer
¶
Bases: Module
Vector Quantizer module for VQ-VAE.
Implements discrete latent space quantization using a learned codebook. Uses straight-through estimator for gradient flow during backpropagation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_embeddings
|
int
|
Size of the codebook (number of discrete codes) |
512
|
embedding_dim
|
int
|
Dimensionality of each code vector |
64
|
commitment_cost
|
float
|
Weight for commitment loss (encourages encoder outputs to commit to codes) |
0.25
|
Source code in src/ecgen/families/vqvae/vector_quantizer.py
forward
¶
Quantize continuous latent vectors to discrete codes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
Tensor
|
Encoder output [B, C, L] where C = embedding_dim |
required |
Returns:
| Name | Type | Description |
|---|---|---|
vq_loss |
Tensor
|
Vector quantization loss (codebook + commitment) |
quantized |
Tensor
|
Quantized latent codes [B, C, L] |
indices |
Tensor
|
Codebook indices [B, L] |
Source code in src/ecgen/families/vqvae/vector_quantizer.py
get_codebook_entry
¶
Get quantized vectors from codebook indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
Tensor
|
Codebook indices [B, L] |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Quantized vectors [B, C, L] |
Source code in src/ecgen/families/vqvae/vector_quantizer.py
ecgen.families.vqvae.vqvae_1d¶
VQVAE1D
¶
Bases: Module
Vector Quantized Variational Autoencoder for ECG signals.
Uses discrete latent representations instead of continuous ones. Architecture follows the existing VAE1D design but with vector quantization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Number of input channels (ECG leads) |
12
|
base_channels
|
int
|
Base number of channels in encoder/decoder |
64
|
latent_channels
|
int
|
Number of channels in latent space (embedding_dim) |
64
|
channel_multipliers
|
tuple[int, ...]
|
Channel multipliers for each downsampling stage |
(1, 2, 4, 4)
|
num_res_blocks
|
int
|
Number of residual blocks per stage |
2
|
num_embeddings
|
int
|
Size of the discrete codebook |
512
|
commitment_cost
|
float
|
Weight for commitment loss in VQ |
0.25
|
Source code in src/ecgen/families/vqvae/vqvae_1d.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
encode
¶
Encode input to continuous latent representation.
Note: For VQ-VAE, encoder outputs a single tensor (not mean/logvar like VAE). We return it twice for API compatibility with VAE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input ECG signals [B, C, L] |
required |
Returns:
| Name | Type | Description |
|---|---|---|
z |
Tensor
|
Continuous latent [B, latent_channels, L//downsample_factor] |
z |
Tensor
|
Same tensor (for compatibility) |
Source code in src/ecgen/families/vqvae/vqvae_1d.py
quantize
¶
Quantize continuous latent to discrete codes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
Tensor
|
Continuous latent [B, latent_channels, L_latent] |
required |
Returns:
| Name | Type | Description |
|---|---|---|
vq_loss |
Tensor
|
Vector quantization loss |
z_q |
Tensor
|
Quantized latent [B, latent_channels, L_latent] |
indices |
Tensor
|
Codebook indices [B, L_latent] |
Source code in src/ecgen/families/vqvae/vqvae_1d.py
decode
¶
Decode quantized latent to reconstruction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z_q
|
Tensor
|
Quantized latent [B, latent_channels, L_latent] |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Reconstructed ECG [B, in_channels, L] |
Source code in src/ecgen/families/vqvae/vqvae_1d.py
forward
¶
Full forward pass: encode -> quantize -> decode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input ECG signals [B, in_channels, L] |
required |
Returns:
| Name | Type | Description |
|---|---|---|
recon |
Tensor
|
Reconstructed ECG [B, in_channels, L] |
vq_loss |
Tensor
|
Vector quantization loss |
indices |
Tensor
|
Codebook indices [B, L_latent] |
Source code in src/ecgen/families/vqvae/vqvae_1d.py
encode_to_indices
¶
Encode input ECG to discrete codebook indices. Used for training the prior model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input ECG signals [B, in_channels, L] |
required |
Returns:
| Name | Type | Description |
|---|---|---|
indices |
Tensor
|
Codebook indices [B, L_latent] |
Source code in src/ecgen/families/vqvae/vqvae_1d.py
decode_from_indices
¶
Decode from discrete codebook indices to ECG. Used for generation from the prior model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
Tensor
|
Codebook indices [B, L_latent] |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Reconstructed ECG [B, in_channels, L] |
Source code in src/ecgen/families/vqvae/vqvae_1d.py
ecgen.families.vqvae.vqvae_lightning¶
VQVAELightning
¶
Bases: GenerativeModule
PyTorch Lightning wrapper for VQ-VAE model.
Handles training, validation, and sampling for Stage 1 (VQ-VAE training).
Source code in src/ecgen/families/vqvae/vqvae_lightning.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
downsample_factor
property
¶
Temporal downsampling between ECG and latent (one stride-2 stage per multiplier after the first).
latent_length
¶
Number of latent codes for an ECG of seq_length samples.
Source code in src/ecgen/families/vqvae/vqvae_lightning.py
reconstruct
¶
encode_to_indices
¶
Encode ECG to discrete codebook indices. Used for training the prior model.
decode_from_indices
¶
Decode from discrete codebook indices. Used for generation from the prior model.
Source code in src/ecgen/families/vqvae/vqvae_lightning.py
sample
¶
Sample by randomly selecting codebook indices.
Note: This is not a good sampling method. For proper generation, use the trained prior model (Stage 2).
Source code in src/ecgen/families/vqvae/vqvae_lightning.py
ecgen.families.vqvae.pixelcnn_prior¶
CausalGatedBlock
¶
Bases: Module
Residual gated causal 1D convolution (WaveNet-style).
Output at position t depends only on inputs at positions <= t: the input is left-padded by (kernel_size - 1) * dilation, so no future positions are seen. Uses gated activation: tanh(W_f * x) * sigmoid(W_g * x)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channels
|
int
|
Number of input/output channels |
required |
kernel_size
|
int
|
Size of convolutional kernel |
2
|
dilation
|
int
|
Dilation of the convolution |
1
|
Source code in src/ecgen/families/vqvae/pixelcnn_prior.py
forward
¶
forward(x: Tensor, c: Optional[Tensor] = None, dense: Optional[Tensor] = None) -> tuple[Tensor, Tensor]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor [B, C, L] |
required |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
(residual output [B, C, L], skip output [B, C, L]) |
Source code in src/ecgen/families/vqvae/pixelcnn_prior.py
PixelCNNPrior
¶
Bases: Module
Autoregressive prior for VQ-VAE discrete codes.
Learns the distribution p(z) over discrete latent codes. The embedded codes
are shifted right by one position, then passed through causal dilated
convolutions, so the logits at position t depend only on codes < t and
predict code t. Dilations double every layer (1, 2, 4, ...) and restart
every dilation_cycle layers, so the receptive field grows exponentially
with depth (see receptive_field).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_embeddings
|
int
|
Size of the codebook (vocabulary size) |
512
|
hidden_dim
|
int
|
Hidden dimension for the network |
128
|
num_layers
|
int
|
Number of gated causal conv layers |
10
|
kernel_size
|
int
|
Kernel size of the causal convolutions |
2
|
dilation_cycle
|
int
|
Number of layers after which the dilation resets to 1 |
10
|
Source code in src/ecgen/families/vqvae/pixelcnn_prior.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
receptive_field
property
¶
Number of preceding codes that can influence the prediction of a code.
forward
¶
Compute logits for each discrete code given the codes before it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
Tensor
|
Discrete code indices [B, L] |
required |
c
|
Optional[Tensor]
|
Optional global condition [B, cond_dim] |
None
|
dense
|
Optional[Tensor]
|
Optional condition at code resolution [B, dense_channels, L] |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Logits over codebook [B, num_embeddings, L]; position t predicts |
Tensor
|
indices[:, t] from indices[:, :t] |
Source code in src/ecgen/families/vqvae/pixelcnn_prior.py
sample
¶
sample(batch_size: int = 1, latent_length: int = 625, temperature: float = 1.0, device: str = 'cuda', c: Optional[Tensor] = None, dense: Optional[Tensor] = None, c_uncond: Optional[Tensor] = None, dense_uncond: Optional[Tensor] = None, guidance: float = 1.0) -> Tensor
Sample discrete codes autoregressively from the prior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Number of samples to generate |
1
|
latent_length
|
int
|
Length of latent sequence |
625
|
temperature
|
float
|
Sampling temperature (higher = more random) |
1.0
|
device
|
str
|
Device to generate samples on |
'cuda'
|
c
|
Optional[Tensor]
|
Global condition (see :meth: |
None
|
dense
|
Optional[Tensor]
|
Condition at code resolution, covering all |
None
|
c_uncond
|
Optional[Tensor]
|
Null global condition, for classifier-free guidance. |
None
|
dense_uncond
|
Optional[Tensor]
|
Null dense condition, for classifier-free guidance. |
None
|
guidance
|
float
|
CFG scale |
1.0
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Sampled indices [B, L] |
Source code in src/ecgen/families/vqvae/pixelcnn_prior.py
ecgen.families.vqvae.prior_lightning¶
PriorLightning
¶
Bases: GenerativeModule
PyTorch Lightning wrapper for PixelCNN prior model.
Handles training and sampling for Stage 2 (prior training). Requires a trained VQ-VAE checkpoint to extract discrete codes.
Source code in src/ecgen/families/vqvae/prior_lightning.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
compute_loss
¶
Cross-entropy of each code given the codes before it (logits at t predict code t).
Source code in src/ecgen/families/vqvae/prior_lightning.py
sample
¶
sample(n_samples: int = 16, seq_length: int = 5000, temperature: float = 1.0, cond: Optional[Mapping[str, Any]] = None, obs: Optional[Tensor] = None, obs_mask: Optional[Tensor] = None, guidance: float = 1.0, **kwargs: Any) -> Tensor
Generate ECG samples by sampling from the prior and decoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_samples
|
int
|
Number of samples to generate |
16
|
seq_length
|
int
|
Length of ECG sequence |
5000
|
temperature
|
float
|
Sampling temperature |
1.0
|
cond
|
Optional[Mapping[str, Any]]
|
Raw condition values (conditional priors) |
None
|
obs
|
Optional[Tensor]
|
Partially observed ECG (dense conditions) |
None
|
obs_mask
|
Optional[Tensor]
|
Mask of |
None
|
guidance
|
float
|
Classifier-free guidance scale (1 = off) |
1.0
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Generated ECG samples [B, C, L] |
Source code in src/ecgen/families/vqvae/prior_lightning.py
ecgen.families.gan.pulse2pulse¶
WaveGANGenerator
¶
Bases: Module
Pulse2Pulse generator from the original WaveGAN implementation, configured for 8‑lead ECG.
Source code in src/ecgen/families/gan/pulse2pulse.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
PhaseShuffle
¶
Bases: Module
Phase shuffling as in WaveGAN: random circular shifts with reflection padding.
Source code in src/ecgen/families/gan/pulse2pulse.py
WaveGANDiscriminator
¶
Bases: Module
Pulse2Pulse discriminator with dynamic final linear layer to accommodate sequence length.
Source code in src/ecgen/families/gan/pulse2pulse.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
features
¶
Convolutional features [B, 100 * model_size, L'] (input incl. dense channels).
Source code in src/ecgen/families/gan/pulse2pulse.py
Pulse2PulseGAN
¶
Bases: GenerativeModule
PyTorch Lightning wrapper around the Pulse2Pulse WaveGAN generator + discriminator.
With config.conditioning it is a conditional WGAN-GP:
- global conditions (
vector) modulate the generator's decoder with FiLM and enter the critic through a projection head; the critic has its own copy of the conditioner (trained by the critic's optimizer, without dropout); - time-aligned conditions (
dense, e.g.masked_signal) are concatenated to the generator's input noise and to the critic's input, which makes it a paired (pix2pix-style) translator, e.g. 1 lead -> 8 leads; setl1_weight(e.g. 100) to add an L1 term on the unobserved part.
Source code in src/ecgen/families/gan/pulse2pulse.py
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 | |
generate_samples
¶
Generate ECG samples from random noise (unconditional models).
Source code in src/ecgen/families/gan/pulse2pulse.py
sample
¶
sample(n_samples: int = 16, cond: Optional[dict[str, Any]] = None, obs: Optional[Tensor] = None, obs_mask: Optional[Tensor] = None, paste_observed: bool = True, **kwargs: Any) -> Tensor
Generate ECGs; with conditioning, given raw cond values and/or a
partially observed obs ([n, C, T]) with obs_mask.
Source code in src/ecgen/families/gan/pulse2pulse.py
calc_gradient_penalty
¶
WGAN‑GP gradient penalty.
Source code in src/ecgen/families/gan/pulse2pulse.py
ecgen.families.continuous¶
One LightningModule for every continuous-time generative method (diffusion,
flow matching): the method is a :class:~ecgen.processes.Process, the network
a registered backbone, the conditions a :class:~ecgen.conditioning.CompositeConditioner.
ContinuousConfig
dataclass
¶
Attributes:
| Name | Type | Description |
|---|---|---|
in_channels |
int
|
Leads. |
process |
Dict[str, Any]
|
|
backbone |
Dict[str, Any]
|
|
conditioning |
Optional[Dict[str, Any]]
|
See :mod: |
lr, |
(weight_decay, b1, b2)
|
AdamW. |
warmup_steps |
int
|
Linear learning-rate warm-up. |
ema_decay |
float
|
EMA of the weights used for sampling (0 = off). |
loss_on |
str
|
|
clean_observed |
bool
|
SSSD-style partial-ECG training: the observed entries of the noisy input are replaced by the clean signal (and again at every sampling step). |
sample_steps |
int
|
Default sampling steps. |
guidance |
float
|
Default classifier-free guidance scale (1 = off). |
Source code in src/ecgen/families/continuous/module.py
ContinuousGenerativeModule
¶
Bases: GenerativeModule
Diffusion / flow-matching family. Registered as diffusion (default
process ddpm) and flow_matching (default process rectified_flow).
Training: draw noise, let the process build (x_t, t, target, weight),
regress the backbone output on the target. Sampling: the process integrates
the backbone from noise, with optional classifier-free guidance, and
partial-ECG conditioning either learned (masked_signal encoder,
clean_observed) or training-free (inpaint="replace" | "repaint").
Source code in src/ecgen/families/continuous/module.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
to_model_space
¶
from_model_space
¶
adapt_condition
¶
Resample dense conditions to the model-space length.
Source code in src/ecgen/families/continuous/module.py
sample
¶
sample(n_samples: int = 16, seq_length: Optional[int] = None, cond: Optional[Mapping[str, Any]] = None, obs: Optional[Tensor] = None, obs_mask: Optional[Tensor] = None, guidance: Optional[float] = None, steps: Optional[int] = None, inpaint: Optional[str] = None, jumps: int = 5, paste_observed: bool = True, use_ema: bool = True, seed: Optional[int] = None, **process_kwargs: Any) -> Tensor
Generate [n, C, T] ECGs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_samples
|
int
|
Number of ECGs. |
16
|
seq_length
|
Optional[int]
|
Length; defaults to the training data's. |
None
|
cond
|
Optional[Mapping[str, Any]]
|
Raw condition values (see :meth: |
None
|
obs
|
Optional[Tensor]
|
Partially observed ECG. Used by a |
None
|
obs_mask
|
Optional[Tensor]
|
Mask of |
None
|
guidance
|
Optional[float]
|
Classifier-free guidance scale (default |
None
|
steps
|
Optional[int]
|
Sampling steps (default |
None
|
inpaint
|
Optional[str]
|
Training-free inpainting for models without a learned observation
condition: |
None
|
jumps
|
int
|
RePaint resampling count. |
5
|
paste_observed
|
bool
|
Copy the observed part of |
True
|
use_ema
|
bool
|
Sample with the EMA weights. |
True
|
seed
|
Optional[int]
|
Seed for the sampling noise. |
None
|
process_kwargs
|
Any
|
Passed to the process sampler (e.g. |
{}
|
Source code in src/ecgen/families/continuous/module.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
DiffusionModule
¶
Bases: ContinuousGenerativeModule
Gaussian diffusion (default process ddpm); see :class:ContinuousGenerativeModule.
Source code in src/ecgen/families/continuous/module.py
FlowMatchingConfig
dataclass
¶
Bases: ContinuousConfig
:class:ContinuousConfig with the rectified-flow process as default.
Source code in src/ecgen/families/continuous/module.py
FlowMatchingModule
¶
Bases: ContinuousGenerativeModule
Flow matching (default process rectified_flow); see :class:ContinuousGenerativeModule.
Source code in src/ecgen/families/continuous/module.py
Latent diffusion / flow matching: the process runs in the latent space of a frozen, pre-trained autoencoder (stage 1), as in latent diffusion models (Rombach et al., 2022).
LatentConfig
dataclass
¶
Bases: ContinuousConfig
:class:ContinuousConfig plus the frozen stage-1 autoencoder.
Attributes:
| Name | Type | Description |
|---|---|---|
autoencoder_checkpoint |
str
|
Stage-1 checkpoint ( |
autoencoder_family |
str
|
|
autoencoder_config |
Optional[Dict[str, Any]]
|
Filled in from the checkpoint, so latent checkpoints restore the autoencoder from their own weights (stage 1 is not needed again). |
latent_scale |
Optional[float]
|
Latents are multiplied by this before the process ( |
Source code in src/ecgen/families/continuous/latent.py
LatentContinuousModule
¶
Bases: ContinuousGenerativeModule
Diffusion / flow in a frozen autoencoder's latent space.
vaestage 1: the encoder's posterior mean is the latent; decode with the decoder.vqvaestage 1: the quantized embeddings are the latent; sampled latents are snapped to the codebook before decoding.
Dense conditions are average-pooled to the latent length; observation masks
and training-free inpainting are signal-space features and are not used here
(a masked_signal condition still works).
Source code in src/ecgen/families/continuous/latent.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
LatentDiffusionModule
¶
Bases: LatentContinuousModule
Latent diffusion (default process ddpm).
Source code in src/ecgen/families/continuous/latent.py
LatentFlowMatchingModule
¶
Bases: LatentContinuousModule
Latent flow matching (default process rectified_flow).
Source code in src/ecgen/families/continuous/latent.py
ecgen.processes¶
Corruption processes for continuous generative models (diffusion, flow matching).
A :class:Process defines how training pairs are made and how samples are
drawn; the family module (:class:ecgen.families.continuous.ContinuousGenerativeModule)
owns the network, conditioning, EMA, and optimisation, so adding a new
diffusion/flow method means adding a Process, not a family.
model_fn(x, t) passed to :meth:Process.sample returns the raw network
output at time t ([B], in the process's own time units); guidance is
applied by the caller (see :func:cfg_model_fn).
TrainingPair
dataclass
¶
What one training step needs: the network input, its target, and per-sample weights.
Source code in src/ecgen/processes/base.py
Inpainting
dataclass
¶
Training-free inpainting at sampling time.
Attributes:
| Name | Type | Description |
|---|---|---|
obs |
Tensor
|
Observed signal |
mask |
Tensor
|
1 = observed. |
mode |
str
|
|
jumps |
int
|
RePaint resampling count per step. |
Source code in src/ecgen/processes/base.py
Process
¶
Bases: Module
Base class; see the module docstring.
Source code in src/ecgen/processes/base.py
cfg_model_fn
¶
cfg_model_fn(model_fn_cond: ModelFn, model_fn_uncond: Optional[ModelFn], guidance: float) -> ModelFn
Classifier-free guidance: u + w (c - u) on the network output.
Source code in src/ecgen/processes/base.py
randn
¶
Gaussian noise on device, drawn from generator (on its own device) if given.
Source code in src/ecgen/processes/base.py
Discrete-time Gaussian diffusion (DDPM, Ho et al., 2020) with DDIM sampling.
DDPMProcess
¶
Bases: Process
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_steps
|
int
|
Diffusion steps |
1000
|
schedule
|
str
|
|
'cosine'
|
prediction
|
str
|
Network target, |
'v'
|
min_snr_gamma
|
Optional[float]
|
Min-SNR loss weighting (Hang et al., 2023); |
5.0
|
clip_x0
|
Optional[float]
|
Clamp the predicted |
None
|
Source code in src/ecgen/processes/ddpm.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
to_x0_eps
¶
Network output at integer steps t → (predicted x0, predicted noise).
Source code in src/ecgen/processes/ddpm.py
timesteps
¶
Evenly spaced steps from T - 1 down to 0.
Source code in src/ecgen/processes/ddpm.py
sample
¶
sample(model_fn: ModelFn, shape: tuple[int, ...], steps: int = 50, device: device = torch.device('cpu'), generator: Optional[Generator] = None, inpaint: Optional[Inpainting] = None, eta: float = 0.0, x_init: Optional[Tensor] = None, start_step: Optional[int] = None, **kwargs) -> Tensor
DDIM sampling (Song et al., 2021) over steps evenly spaced steps;
eta=1 is ancestral (DDPM-like) sampling, eta=0 deterministic.
x_init / start_step start from a partially noised signal (SDEdit).
Source code in src/ecgen/processes/ddpm.py
Flow matching / rectified flow (Lipman et al., 2023; Liu et al., 2023).
Time runs from t = 0 (noise x0 ~ N(0, I)) to t = 1 (data x1):
x_t = t * x1 + (1 - (1 - sigma_min) * t) * x0, target velocity = x1 - (1 - sigma_min) * x0
and sampling integrates dx/dt = v(x, t) with a fixed-step ODE solver.
RectifiedFlowProcess
¶
Bases: Process
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t_sampling
|
str
|
|
'logit_normal'
|
logit_mean
|
float
|
Mean of the logit-normal time distribution. |
0.0
|
logit_std
|
float
|
Standard deviation of the logit-normal time distribution. |
1.0
|
sigma_min
|
float
|
Width of the conditional path at |
0.0
|
ot_coupling
|
bool
|
Pair noise and data within each batch by optimal transport (minibatch OT-CFM, Tong et al., 2024) for straighter paths. |
False
|
time_scale
|
float
|
The network sees |
1000.0
|
solver
|
str
|
Default ODE solver, |
'euler'
|
Source code in src/ecgen/processes/flow.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
sample
¶
sample(model_fn: ModelFn, shape: tuple[int, ...], steps: int = 50, device: device = torch.device('cpu'), generator: Optional[Generator] = None, inpaint: Optional[Inpainting] = None, solver: Optional[str] = None, x_init: Optional[Tensor] = None, t_start: float = 0.0, **kwargs) -> Tensor
Integrate from noise (t = t_start) to data (t = 1) in steps steps.
Inpainting: "replace" overwrites the observed part with the point on
the straight path from the initial noise to the observation; "clean"
with the observation itself. (RePaint is a diffusion method; use replace.)
x_init / t_start start mid-path (SDEdit-style editing).
Source code in src/ecgen/processes/flow.py
ot_pairing
¶
Permutation of x0 minimising the summed squared distance to x1 (exact assignment).
Source code in src/ecgen/processes/flow.py
ecgen.nn.backbones¶
1D U-Net denoiser / velocity network (ADM-style) for ECGs.
forward(x, t, cond) takes the noisy signal [B, C, T], the time [B]
(any float scale; it is embedded sinusoidally) and an optional
:class:~ecgen.types.ConditionOutput:
cond.vectoris added to the time embedding (AddToTimeEmb), which modulates every residual block through AdaGN;cond.tokensare attended to by cross-attention at the attention levels;cond.denseis concatenated to the input.
ResBlock
¶
Bases: Module
GN-SiLU-Conv, AdaGN(time+condition)-SiLU-Dropout-Conv(zero init), residual.
Source code in src/ecgen/nn/backbones/unet1d.py
UNet1d
¶
Bases: Module
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Signal channels (leads). |
12
|
out_channels
|
Optional[int]
|
Output channels (default |
None
|
base_channels
|
int
|
Width of the first level. |
64
|
channel_mults
|
Sequence[int]
|
Per-level width multipliers; the signal length must be
divisible by |
(1, 2, 4, 4)
|
num_res_blocks
|
int
|
Residual blocks per level. |
2
|
attn_levels
|
Sequence[int]
|
Levels (0 = full resolution) with self-attention (and
cross-attention when |
(3,)
|
heads
|
int
|
Attention heads. |
4
|
dropout
|
float
|
Dropout inside residual blocks. |
0.0
|
cond_dim
|
int
|
Size of |
0
|
context_dim
|
int
|
Size of |
0
|
dense_channels
|
int
|
Channels of |
0
|
Source code in src/ecgen/nn/backbones/unet1d.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
DiT-style transformer for 1D signals (Peebles & Xie, 2023).
The signal (plus dense conditions) is cut into non-overlapping patches of
patch_size samples, embedded as tokens with fixed sinusoidal positions, and
processed by transformer blocks whose LayerNorms are modulated by the time +
condition embedding (AdaLN-Zero); condition tokens enter through
cross-attention in every block.
DiT1d
¶
Bases: Module
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Signal (or latent) channels. |
12
|
out_channels
|
Optional[int]
|
Default |
None
|
patch_size
|
int
|
Samples per token; the length must be divisible by it. |
25
|
dim
|
int
|
Token width. |
256
|
depth
|
int
|
Transformer blocks. |
8
|
heads
|
int
|
Attention heads. |
8
|
mlp_ratio
|
float
|
MLP width / |
4.0
|
cond_dim
|
int
|
Size of the condition vector (0 = none). |
0
|
context_dim
|
int
|
Size of condition tokens (0 = none). |
0
|
dense_channels
|
int
|
Channels of dense conditions (0 = none). |
0
|
Source code in src/ecgen/nn/backbones/dit1d.py
State-space backbone (SSSD-style; Alcaraz & Strodthoff, 2023) built from S4D layers (Gu et al., 2022) in pure PyTorch.
An S4D layer is a diagonal linear state-space model applied per channel as a
long convolution: its kernel K[l] = 2 Re(sum_n C_n (exp(dt A_n) - 1) / A_n * exp(dt A_n l))
is computed once per forward pass and applied with an FFT, so the cost is
O(L log L) and the receptive field is the whole signal. bidirectional
adds a second kernel run over the time-reversed input (a non-causal model, as
denoisers should be).
S4D
¶
Bases: Module
Diagonal state-space layer [B, H, L] -> [B, H, L].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channels
|
int
|
|
required |
state_size
|
int
|
|
64
|
dt_min
|
float
|
Smallest initial step size (learned, log-uniform initialised). |
0.001
|
dt_max
|
float
|
Largest initial step size. |
0.1
|
bidirectional
|
bool
|
Also run a kernel over the reversed sequence. |
True
|
Source code in src/ecgen/nn/backbones/ssm.py
kernel
¶
[k, H, L] convolution kernels.
Source code in src/ecgen/nn/backbones/ssm.py
SSMResidualBlock
¶
Bases: Module
SSSD residual block: (+ time/condition) -> S4D -> gated activation -> residual + skip.
Source code in src/ecgen/nn/backbones/ssm.py
SSMBackbone
¶
Bases: Module
Stack of S4D residual blocks at full resolution (no down-sampling).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Signal (or latent) channels. |
12
|
out_channels
|
Optional[int]
|
Default |
None
|
channels
|
int
|
Width. |
128
|
depth
|
int
|
Residual blocks. |
8
|
state_size
|
int
|
S4D state size |
64
|
bidirectional
|
bool
|
Non-causal (two-sided) kernels. |
True
|
dropout
|
float
|
Dropout after the gate. |
0.0
|
cond_dim
|
int
|
Size of the condition vector, added to the time embedding (0 = none). |
0
|
dense_channels
|
int
|
Channels of dense conditions, concatenated to the input. Tokens are not supported. |
0
|
Source code in src/ecgen/nn/backbones/ssm.py
MambaBackbone
¶
Bases: SSMBackbone
:class:SSMBackbone with bidirectional Mamba layers (Gu & Dao, 2023) in
place of S4D. Needs the mamba-ssm package (CUDA only; ecgen[ssm]).
Source code in src/ecgen/nn/backbones/ssm.py
ecgen.training.ema¶
Exponential moving average of model weights.
EMA
¶
Keeps shadow = decay * shadow + (1 - decay) * param for the parameters
of some modules; :meth:swap temporarily loads the averaged weights.
The shadow is a plain dict of tensors (not a submodule), so the owner
stores it in checkpoints itself (:meth:state_dict / :meth:load_state_dict).
Source code in src/ecgen/training/ema.py
swap
¶
Use the averaged weights inside the with block.