CausalConv1D#
- class CausalConv1D(*args, is_causal=True, **kwargs)#
Bases:
Conv1d1D convolution with configurable causal (left-only) or symmetric padding.
Subclasses
torch.nn.Conv1dand overridesforward()to apply explicit left-only padding rather than the built-inpaddingargument. This guarantees thatoutput[b, :, i]depends only oninput[b, :, 0 … i](the causal constraint).Padding formulae
For a kernel of size
Kwith dilationD:Causal (
is_causal=True):left_pad = (K-1)*D,right_pad = 0. Output length equals input length for stride=1.Symmetric (
is_causal=False):sym_pad = ((K-1)*D) // 2on both sides. Equivalent toConv1d(padding='same')for oddK.
Pass
padding=0to the parent constructor (the default) — padding is handled explicitly inforward().- Parameters:
is_causal (bool)
Example:
conv = CausalConv1D(in_channels=16, out_channels=16, kernel_size=7, groups=16) x = torch.randn(2, 16, 100) # [B, C, L] y = conv(x) # [B, 16, 100] — same length, strictly causal
Notes
Stride > 1 reduces the output length as in standard convolutions.
For depthwise convolutions set
groups=in_channels.The
paddingargument of the parent constructor is ignored; always passpadding=0(or omit it) when constructing this class.
- __init__(*args, is_causal=True, **kwargs)#
Initialise CausalConv1D.
- Parameters:
*args – Positional arguments forwarded to
torch.nn.Conv1d(in_channels,out_channels,kernel_size, …).is_causal (bool) – If
True(default), apply left-only (causal) padding. IfFalse, apply symmetric same-padding.**kwargs – Keyword arguments forwarded to
torch.nn.Conv1d.paddingshould be0(or omitted) sinceforward()handles padding explicitly.
- Return type:
None