TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

107. PyTorch: BatchNorm Train Mode

Medium

Demonstrate batch normalization in training mode using torch.nn.BatchNorm1d.

Signature: def batchnorm_train_forward(x)

  • x: training batch (N, C) as nested list
  • Returns: normalized output as nested list (N, C)

Steps:

  1. Create torch.nn.BatchNorm1d(num_features=C) with default weight=1, bias=0
  2. Call bn.train() to set training mode
  3. Pass x through the BatchNorm layer
  4. Return .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

x^i​=σB2​+ε​xi​−μB​​,yi​=γx^i​+β

Asked at

NumPy

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.

Upgrade to PremiumBack to problems

Already premium?