PIXELBANKv9.1.0
Menu

Batch Normalization Forward Pass

Implement the forward pass of batch normalization using PyTorch tensors.

Batch normalization normalizes activations across the batch dimension:

x^i=xi−μBσB2+ϵ\hat{x}_i = \frac{x_i - \mu_B}{\sqrt{\sigma_B^2 + \epsilon}} yi=γx^i+βy_i = \gamma \hat{x}_i + \beta

Where:

  • μB=1m∑i=1mxi\mu_B = \frac{1}{m}\sum_{i=1}^{m} x_i (batch mean)
  • σB2=1m∑i=1m(xi−μB)2\sigma_B^2 = \frac{1}{m}\sum_{i=1}^{m} (x_i - \mu_B)^2 (batch variance)
  • γ,β\gamma, \beta are learnable parameters
  • ϵ\epsilon is for numerical stability

Also track running mean/variance for inference mode.

Example:

Input:
x = [[1, 2], [3, 4], [5, 6]]  # batch=3, features=2
gamma = [1, 1]
beta = [0, 0]
training = True
Output:
[[-1.22, -1.22], [0, 0], [1.22, 1.22]]
Reasoning:

Feature 1: values [1,3,5], mean=3, std=1.63 Normalized: [-1.22, 0, 1.22]

Feature 2: values [2,4,6], mean=4, std=1.63 Normalized: [-1.22, 0, 1.22]

With gamma=1, beta=0: output equals normalized values.

Constraints:

  • x: Input tensor of shape (batch_size, features)
  • gamma, beta: Learnable parameters (features,)
  • training: Boolean for train vs inference mode
  • Return: Normalized output tensor
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.