class

LazyBatchNorm1d

extends_LazyBatchNormMixin
LazyBatchNorm1d(eps: float = 1e-05, momentum: float | None = 0.1, affine: bool = True, track_running_stats: bool = True, device: DeviceLike = None, dtype: DTypeLike = None)
source

BatchNorm1d with lazy num_features inference.

Identical to BatchNorm1d except that num_features need not be specified at construction time. On the first call to forward(x), num_features is automatically set to x.shape[1] and all parameters and buffers are allocated.

This is convenient when the number of channels is not known at model definition time, for example when building networks programmatically or when constructing sub-modules before the input shape is fixed.

Parameters

epsfloat= 1e-05
Numerical stability constant. Default: 1e-5.
momentumfloat or None= 0.1
EMA factor for running statistics. None uses cumulative averaging. Default: 0.1.
affinebool= True
If True, lazily allocates learnable scale and shift. Default: True.
track_running_statsbool= True
If True, lazily allocates running mean/variance and batch counter. Default: True.
deviceDeviceLike= None
Device for lazily allocated tensors. Default: None.
dtypeDTypeLike= None
Data type for lazily allocated tensors. Default: None.

Notes

  • Input: (N,C)(N, C) or (N,C,L)(N, C, L) — same as BatchNorm1d. C is inferred on the first forward pass.
  • Output: same shape as the input.

Examples

Construct without specifying the channel count:
>>> import lucid
>>> import lucid.nn as nn
>>> bn = nn.LazyBatchNorm1d()
>>> x = lucid.randn(16, 64, 100)   # (N, C, L)
>>> out = bn(x)    # num_features=64 is inferred here
>>> out.shape
(16, 64, 100)
Reload from a checkpoint saved by a :class:`BatchNorm1d`:
>>> bn2 = nn.LazyBatchNorm1d()
>>> # bn2.load_state_dict(state)  # num_features inferred from saved tensors