LazyConv1d
_LazyConvMixinConv1dLazyConv1d(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_ 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)
3Used by 1
Constructors
1__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
4Return a string representation of the layer's configuration.
The eager layer's forward; the first call sizes in_channels from input.
initialize_parameters
→Nonefrom _LazyConvMixininitialize_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*argsobject= ()**kwargsobject= ()Re-draw the values; nothing to do until the layer has been sized.