class
LazyBatchNorm2d
extends
_LazyBatchNormMixinLazyBatchNorm2d(eps: float = 1e-05, momentum: float | None = 0.1, affine: bool = True, track_running_stats: bool = True, device: DeviceLike = None, dtype: DTypeLike = None, bias: bool = True)BatchNorm2d with lazy num_features inference.
Identical to BatchNorm2d except that num_features is
inferred from x.shape[1] on the first forward pass.
Parameters
epsfloat= 1e-05Numerical stability constant. Default:
1e-5.momentumfloat or None= 0.1EMA factor for running statistics.
None uses cumulative
averaging. Default: 0.1.affinebool= TrueIf
True, lazily allocates learnable scale and shift.
Default: True.track_ running_ statsbool= TrueIf
True, lazily allocates running statistics buffers.
Default: True.deviceDeviceLike= NoneDevice for lazily allocated tensors. Default:
None.dtypeDTypeLike= NoneData type for lazily allocated tensors. Default:
None.Notes
- Input: — same as
BatchNorm2d.Cis inferred on the first forward pass. - Output: same shape as the input.
Examples
>>> import lucid
>>> import lucid.nn as nn
>>> bn = nn.LazyBatchNorm2d()
>>> x = lucid.randn(8, 128, 32, 32)
>>> out = bn(x) # num_features=128 inferred on first call
>>> out.shape
(8, 128, 32, 32)
Suitable for dynamic architectures where channel count depends on
a configuration value not available at module construction:
>>> def make_norm(affine: bool = True) -> nn.LazyBatchNorm2d:
... return nn.LazyBatchNorm2d(affine=affine)Used by 1
Instance methods
5Return a string representation of the layer's configuration.
Size the layer from input and allocate its parameters, if it has not been yet.
The step the first forward takes, exposed so a model can be sized without running it. Does nothing once the layer is sized.
Parameters
inputTensorAn input of the shape the layer will see; only the axis that
carries the layer's size is read.
Reset the running statistics, then weight to ones and bias to zeros.
Examples
>>> import lucid
>>> import lucid.nn as nn
>>> bn = nn.BatchNorm1d(2)
>>> _ = bn(lucid.randn(4, 2))
>>> bn.reset_parameters()
>>> bn.running_mean.tolist(), int(bn.num_batches_tracked.item())
([0.0, 0.0], 0)Put running_mean to zeros, running_var to ones and the count to zero.
In place, so anything holding a buffer sees the reset. Nothing to do
without track_running_stats, or on a lazy layer before its first
input.