Demonstrate batch normalization in training mode using torch.nn.BatchNorm1d.
Signature: def batchnorm_train_forward(x)
x: training batch (N, C) as nested list(N, C)Steps:
torch.nn.BatchNorm1d(num_features=C) with default weight=1, bias=0bn.train() to set training modex through the BatchNorm layer.detach().tolist()BatchNorm in train mode normalizes each feature across the batch:
y = (x - batch_mean) / sqrt(batch_var + eps)
where batch_mean and batch_var are computed per-feature over the batch dimension. With default weight=1 and bias=0, the output has approximately zero mean and unit variance per feature.
This problem covers train mode; eval mode (with running stats) is a follow-up exercise.
Math
Asked at
import numpy as np
def batchnorm_train_forward(...):
pass
Premium problem
Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.
Already premium?