class

LazyConv1d

extendsConv1d
LazyConv1d(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

A Conv1d that infers in_channels from the first input.

Lazy modules defer the allocation and initialization of weight and bias until the first call to forward (or until a compatible state_dict is loaded). This removes the need to know in_channels at construction time, simplifying sequential model building and automatic architecture search.

Materialization happens exactly once: on the first forward call the channel count is read from x.shape[1], weights are allocated and Kaiming-uniform initialized, and subsequent calls behave identically to a fully-initialized Conv1d.

Parameters

out_channelsint
Number of output channels.
kernel_sizeint
Length of the 1D convolving kernel.
strideint= 1
Stride of the convolution. Default: 1.
paddingint or str= 0
Zero-padding or "same" / "valid" string specifier. Default: 0.
dilationint= 1
Spacing between kernel elements. Default: 1.
groupsint= 1
Number of blocked connections. Default: 1.
biasbool= True
If True, a learnable bias is added after materialization. Default: True.
padding_modestr= 'zeros'
"zeros", "reflect", "replicate", or "circular". Default: "zeros".
deviceDeviceLike= None
Device used when allocating weights at materialization time. Default: None.
dtypeDTypeLike= None
Data type used when allocating weights. Default: None.

Attributes

weightParameter or None
None until first forward; afterwards shape (out_channels, in_channels // groups, kernel_size).
biasParameter or None
None until first forward; afterwards shape (out_channels,) if bias=True, else remains None.
in_channelsint or None
None before materialization; set to the inferred value on the first forward pass.

Notes

Input: (N,Cin,L)(N, C_{\text{in}}, L)CinC_{\text{in}} is inferred automatically. Output: (N,Cout,Lout)(N, C_{\text{out}}, L_{\text{out}}) — same formula as Conv1d.

State-dict loading. Loading a state_dict that contains a weight key of shape (out_channels, in_channels // groups, kernel_size) also triggers materialization, so a lazy module can be restored from a checkpoint without ever calling forward.

groups constraint. in_channels (inferred at runtime) must be divisible by groups; this is checked inside _initialize.

Examples

Lazy conv in a sequential model:
>>> import lucid
>>> import lucid.nn as nn
>>> model = nn.Sequential(
...     nn.LazyConv1d(out_channels=32, kernel_size=3, padding=1),
...     nn.ReLU(),
... )
>>> x = lucid.zeros(4, 16, 64)   # in_channels=16 inferred here
>>> y = model(x)
>>> y.shape
(4, 32, 64)
Check that in_channels was inferred:
>>> import lucid
>>> import lucid.nn as nn
>>> lazy = nn.LazyConv1d(out_channels=8, kernel_size=5, padding=2)
>>> print(lazy.in_channels)
None
>>> _ = lazy(lucid.zeros(1, 3, 20))
>>> print(lazy.in_channels)
3

Methods (3)

dunder

__init__

None
__init__(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 LazyConv1d 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.