LazyConv1d
Conv1dLazyConv1d(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)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_channelsintkernel_sizeintstrideint= 11.paddingint or str= 0"same" / "valid" string specifier.
Default: 0.dilationint= 11.groupsint= 11.biasbool= TrueTrue, a learnable bias is added after materialization.
Default: True.padding_modestr= 'zeros'"zeros", "reflect", "replicate", or "circular".
Default: "zeros".deviceDeviceLike= NoneNone.dtypeDTypeLike= NoneNone.Attributes
weightParameter or NoneNone until first forward; afterwards shape
(out_channels, in_channels // groups, kernel_size).biasParameter or NoneNone until first forward; afterwards shape
(out_channels,) if bias=True, else remains None.in_channelsint or NoneNone before materialization; set to the inferred value on
the first forward pass.Notes
Input:
— is
inferred automatically.
Output:
— 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)
3Methods (3)
__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)Initialise the LazyConv1d 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.