class

Conv1d

extendsModule
Conv1d(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)
source

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:

y[n,cout,l]=cin=0Cin/g1k=0K1x ⁣[n,cin,ls+kd]W ⁣[cout,cin,k]+b ⁣[cout]y[n, c_{\text{out}}, l] = \sum_{c_{\text{in}}=0}^{C_{\text{in}}/g - 1} \sum_{k=0}^{K-1} x\!\left[n,\, c_{\text{in}},\, l \cdot s + k \cdot d\right] \cdot W\!\left[c_{\text{out}},\, c_{\text{in}},\, k\right] + b\!\left[c_{\text{out}}\right]

where ss is the stride, dd is the dilation factor, gg is the number of groups, and KK is the kernel size.

Parameters

in_channelsint
Number of channels in the input signal.
out_channelsint
Number of channels produced by the convolution (i.e. number of independent filters).
kernel_sizeint
Length of the 1D convolving kernel.
strideint= 1
Step size between consecutive kernel placements along the length dimension. Default: 1.
paddingint or str= 0
Zero-padding added to both sides of the input before the convolution. Can also be "same" (output length equals ceil(L_in / stride), requires stride=1) or "valid" (no padding; identical to padding=0). Default: 0.
dilationint= 1
Spacing between kernel elements. A dilation of d inserts d - 1 zeros between consecutive kernel weights, expanding the effective receptive field without increasing parameter count (also called atrous convolution). Default: 1.
groupsint= 1
Splits the input and output channels into groups independent paths. in_channels and out_channels must both be divisible by groups. Setting groups = in_channels gives depthwise convolution. Default: 1.
biasbool= True
If True, a learnable bias of shape (out_channels,) is added to the output. Default: True.
padding_modestr= 'zeros'
Padding strategy when padding is an integer. One of "zeros", "reflect", "replicate", or "circular". Default: "zeros".
deviceDeviceLike= None
Device on which to allocate parameters. Default: None.
dtypeDTypeLike= None
Data type for the parameters. Default: None.

Attributes

weightParameter
Learnable filter tensor of shape (out_channels, in_channels // groups, kernel_size). Initialized with Kaiming uniform: fan_in=CingK,WU ⁣[6fan_in,  6fan_in]\text{fan\_in} = \frac{C_{\text{in}}}{g} \cdot K, \quad W \sim \mathcal{U}\!\left[ -\sqrt{\tfrac{6}{\text{fan\_in}}},\; \sqrt{\tfrac{6}{\text{fan\_in}}} \right] (using a=5a = \sqrt{5} in the Kaiming formula).
biasParameter or None
Learnable bias of shape (out_channels,), or None if bias=False.

Notes

Input: (N,Cin,L)(N, C_{\text{in}}, L) Output: (N,Cout,Lout)(N, C_{\text{out}}, L_{\text{out}}) 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)

dunder

__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)
source

Initialise the Conv1d module. See the class docstring for parameter semantics.

fn

forward

Tensor
forward(x: Tensor)
source

Apply the convolution to the input tensor.

Parameters

inputTensor
Input tensor of shape (N,Cin,)(N, C_{\text{in}}, *).

Returns

Tensor

Output tensor of shape (N,Cout,)(N, C_{\text{out}}, *) with spatial dimensions determined by stride, padding, dilation, and kernel size.

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.