BatchNorm1d
_BatchNormBaseBatchNorm1d(num_features: int, 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)Implementing kernel
C++ engine symbols that back this Python API.Batch normalization over a 2-D or 3-D input (N, C) or (N, C, L).
Normalises each channel across the batch (and, for 3-D inputs, the length) dimension:
For a 3-D input , the statistics and are computed over the axes for each channel . For a 2-D input only the batch axis is reduced.
During training, batch statistics are used and running statistics are updated via an exponential moving average:
During evaluation (model.eval()), the stored
running_mean and running_var are used instead, making
inference independent of batch composition.
Parameters
num_ featuresintepsfloat= 1e-051e-5.momentumfloat or None= 0.1None uses a cumulative moving average. Default: 0.1.affinebool= TrueTrue, learns per-channel scale and shift
. Default: True.track_ running_ statsbool= TrueTrue, maintains running_mean, running_var,
and num_batches_tracked. Default: True.deviceDeviceLike= NoneNone.dtypeDTypeLike= NoneNone.Attributes
weightParameter or None(num_features,).
None when affine=False.biasParameter or None(num_features,).
None when affine=False.running_ meanTensor or None(num_features,).
None when track_running_stats=False.running_ varTensor or None(num_features,).
None when track_running_stats=False.num_ batches_ trackedTensor or Noneint64 counting batches seen during training.
None when track_running_stats=False.Notes
- Input: or
- Output: same shape as the input.
Examples
2-D input (e.g. a linear layer's activations):
>>> import lucid
>>> import lucid.nn as nn
>>> bn = nn.BatchNorm1d(128)
>>> x = lucid.randn(32, 128)
>>> out = bn(x)
>>> out.shape
(32, 128)
3-D input — temporal sequence with channels:
>>> bn_seq = nn.BatchNorm1d(64)
>>> x_seq = lucid.randn(16, 64, 200) # (N, C, L)
>>> out_seq = bn_seq(x_seq)
>>> out_seq.shape
(16, 64, 200)Used by 1
Instance methods
4Return a string representation of the layer's configuration.
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.