Skip to content
class

LazyConv1d

extends_LazyConvMixinConv1d
LazyConv1d(out_channels: int, kernel_size: int | tuple[int], stride: int | tuple[int] = 1, padding: int | tuple[int] | str = 0, dilation: int | tuple[int] = 1, groups: int = 1, bias: bool = True, padding_mode: PaddingMode = 'zeros', device: DeviceLike = None, dtype: DTypeLike = None)

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

Used by 1

Constructors

1
dunder

__init__

→None
__init__(out_channels: int, kernel_size: int | tuple[int], stride: int | tuple[int] = 1, padding: int | tuple[int] | str = 0, dilation: int | tuple[int] = 1, groups: int = 1, bias: bool = True, padding_mode: PaddingMode = 'zeros', device: DeviceLike = None, dtype: DTypeLike = None)

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

Instance methods

4
fn

extra_repr

→strfrom _LazyConvMixin
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensor
forward(input: Tensor)

The eager layer's forward; the first call sizes in_channels from input.

fn

initialize_parameters

→Nonefrom _LazyConvMixin
initialize_parameters(input: Tensor, args: object = (), kwargs: object = {})

Size in_channels from input and allocate the weights, if not sized yet.

Takes the extra positional and keyword arguments the reference's lazy convolutions accept, and ignores them, as it does.

Parameters

inputTensor
An input of the shape the layer will see.
*argsobject= ()
Accepted for signature parity; unused.
**kwargsobject= ()
Accepted for signature parity; unused.
fn

reset_parameters

→Nonefrom _LazyConvMixin
reset_parameters()

Re-draw the values; nothing to do until the layer has been sized.