Implement a BatchNorm1d layer that correctly handles both 2D inputs of shape (N, C) and 3D inputs of shape (N, L, C). Normalization statistics (mean and variance) are computed per channel C:
(N, C): reduce over axis=0.(N, L, C): reduce over axes=(0, 1).The layer supports two modes:
Training mode (training=True):
mu and batch variance var (use the biased estimator, i.e. divide by the number of reduced elements, not by n-1).x_hat = (x - mu) / sqrt(var + eps).out = gamma * x_hat + beta.running_mean = (1 - momentum) * running_mean + momentum * murunning_var = (1 - momentum) * running_var + momentum * varEvaluation mode (training=False):
running_mean and running_var:
x_hat = (x - running_mean) / sqrt(running_var + eps), then apply affine.Use eps = 1e-5 and momentum = 0.1 as defaults. Broadcasting must work so that gamma, beta, running_mean, running_var (all shape (C,)) line up against the channel axis of the input.
Return a dictionary with three keys:
'out': the normalized + affine-transformed tensor as a nested Python list (use .tolist()).'running_mean': updated running mean as a Python list.'running_var': updated running variance as a Python list.In evaluation mode the running statistics are returned unchanged.