Batch Normalization Forward Pass
Implement the forward pass of batch normalization using PyTorch tensors.
Batch normalization normalizes activations across the batch dimension:
Where:
- (batch mean)
- (batch variance)
- are learnable parameters
- is for numerical stability
In this simplified version, inference mode (training=False) also normalizes with the current batch's mean and variance; no running statistics need to be stored.
Return a float tensor with the same shape as x, with every value rounded to the nearest integer.
Example
x = [[1, 2], [3, 4], [5, 6]] # batch=3, features=2 gamma = [1, 1] beta = [0, 0] training = True
[[-1.0, -1.0], [0.0, 0.0], [1.0, 1.0]]
- Inputs:
xis a batch of samples with 2 features each, so each column is one feature measured across the batch.gamma=[1, 1]andbeta=[0, 0]are the per-feature scale and shift;trainingisTrue. - Feature 1 (first column) holds : and , so .
- Normalizing gives , and .
- Feature 2 holds : every value is 1 higher, so , the variance is still , and the normalized column is identical. Notice that batch norm erases a constant per-feature offset.
- With and , . Rounding each value to the nearest integer turns into , giving
[[-1.0, -1.0], [0.0, 0.0], [1.0, 1.0]]: a float tensor, the same shape asx, with each column centred on 0.
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