Conv1d
ModuleConv1d(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int | str = 0, dilation: int = 1, groups: int = 1, bias: bool = True, padding_mode: str = 'zeros', device: DeviceLike = None, dtype: DTypeLike = None)Applies a 1D convolution over a sequence of input signals.
A 1D convolution slides a learned filter (kernel) across a 1D sequence and computes a dot product at each position. Strictly speaking the operation is cross-correlation:
where is the stride, is the dilation factor, is the number of groups, and is the kernel size.
Parameters
in_channelsintout_channelsintkernel_sizeintstrideint= 11.paddingint or str= 0"same" (output length equals
ceil(L_in / stride), requires stride=1) or "valid"
(no padding; identical to padding=0). Default: 0.dilationint= 1d inserts
d - 1 zeros between consecutive kernel weights, expanding the
effective receptive field without increasing parameter count
(also called atrous convolution). Default: 1.groupsint= 1groups independent
paths. in_channels and out_channels must both be
divisible by groups. Setting groups = in_channels
gives depthwise convolution. Default: 1.biasbool= TrueTrue, a learnable bias of shape (out_channels,) is
added to the output. Default: True.padding_modestr= 'zeros'padding is an integer. One of
"zeros", "reflect", "replicate", or "circular".
Default: "zeros".deviceDeviceLike= NoneNone.dtypeDTypeLike= NoneNone.Attributes
weightParameter(out_channels, in_channels // groups, kernel_size).
Initialized with Kaiming uniform:
(using in the Kaiming formula).biasParameter or None(out_channels,), or None if
bias=False.Notes
Input: Output: where
$$
L_{\text{out}} = \left\lfloor \frac{L + 2p - d(K - 1) - 1}{s} + 1 \right\rfloor
and $p$ is the (symmetric) padding, $d$ is dilation, $K$ is kernel size, $s$ is stride.Groups and depthwise convolution. When groups = g > 1, the
input channels are split into g groups of size
in_channels // g, each group is convolved independently, and the
results are concatenated. Depthwise convolution (groups = in_channels) applies one filter per input channel, dramatically
reducing the parameter count compared to a full convolution.
Dilated (atrous) convolution. Setting dilation > 1 inserts
gaps between kernel taps, enlarging the receptive field without
additional parameters or pooling. This is commonly used in semantic
segmentation and WaveNet-style audio models.
Padding modes. "reflect" and "circular" padding
avoid boundary artefacts that zero-padding can introduce;
"replicate" repeats edge values. These modes are applied via
a pre-padding step followed by a padding=0 convolution.
Examples
Basic 1D convolution:
>>> import lucid
>>> import lucid.nn as nn
>>> conv = nn.Conv1d(in_channels=1, out_channels=8, kernel_size=3, padding=1)
>>> x = lucid.zeros(4, 1, 16) # (N, C_in, L)
>>> y = conv(x)
>>> y.shape
(4, 8, 16)
Depthwise 1D convolution (one filter per channel):
>>> import lucid
>>> import lucid.nn as nn
>>> depthwise = nn.Conv1d(
... in_channels=32, out_channels=32,
... kernel_size=5, padding=2, groups=32
... )
>>> x = lucid.zeros(2, 32, 64)
>>> y = depthwise(x)
>>> y.shape
(2, 32, 64)Methods (3)
__init__
→None__init__(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int | str = 0, dilation: int = 1, groups: int = 1, bias: bool = True, padding_mode: str = 'zeros', device: DeviceLike = None, dtype: DTypeLike = None)Initialise the Conv1d module. See the class docstring for parameter semantics.
forward
→Tensorforward(x: Tensor)Apply the convolution to the input tensor.
Parameters
inputTensorReturns
TensorOutput tensor of shape with spatial dimensions determined by stride, padding, dilation, and kernel size.
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.