LazyConv2d
_LazyConvMixinConv2dLazyConv2d(out_channels: int, kernel_size: _Size2d, stride: _Size2d = 1, padding: _Size2d | str = 0, dilation: _Size2d = 1, groups: int = 1, bias: bool = True, padding_mode: PaddingMode = 'zeros', device: DeviceLike = None, dtype: DTypeLike = None)A Conv2d that infers in_channels from the first input.
Lazy initialization allows building 2D convolutional networks without
knowing the input channel count in advance. The module registers
weight and bias as None parameters and allocates them on
the first forward call, reading C_in from
x.shape[1].
Parameters
out_ channelsintkernel_ sizeint or tuple[int, int]strideint or tuple[int, int]= 11.paddingint, tuple[int, int], or str= 0"same" / "valid" string specifier.
Default: 0.dilationint or tuple[int, int]= 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 before materialization; shape
(out_channels, in_channels // groups, K_H, K_W) afterwards.biasParameter or NoneNone before materialization; shape (out_channels,) if
bias=True, else remains None.in_ channelsint or NoneNone before the first forward pass; inferred channel count
afterwards.Notes
Input:
— is
inferred automatically.
Output:
—
same formula as Conv2d.
State-dict materialization. Loading a state_dict with a
4-D weight tensor triggers materialization before parameter
copying, enabling round-trip checkpoint compatibility.
Examples
Dynamic channel inference in a feature extractor:
>>> import lucid
>>> import lucid.nn as nn
>>> model = nn.Sequential(
... nn.LazyConv2d(out_channels=64, kernel_size=3, padding=1),
... nn.ReLU(),
... nn.LazyConv2d(out_channels=128, kernel_size=3, padding=1),
... )
>>> x = lucid.zeros(2, 3, 32, 32) # in_channels=3 inferred here
>>> y = model(x)
>>> y.shape
(2, 128, 32, 32)
Verify inferred in_channels after forward:
>>> import lucid
>>> import lucid.nn as nn
>>> lazy = nn.LazyConv2d(out_channels=16, kernel_size=3, padding=1)
>>> _ = lazy(lucid.zeros(1, 8, 16, 16))
>>> print(lazy.in_channels)
8Used by 1
Constructors
1__init__
→None__init__(out_channels: int, kernel_size: _Size2d, stride: _Size2d = 1, padding: _Size2d | str = 0, dilation: _Size2d = 1, groups: int = 1, bias: bool = True, padding_mode: PaddingMode = 'zeros', device: DeviceLike = None, dtype: DTypeLike = None)Initialise the LazyConv2d 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.