Batch Normalization (Forward)
Batch Normalization (Forward)
Implement the forward pass of BatchNorm for inputs shaped either:
- (N, D): normalize each feature over the batch axis (axis=0)
- (N, C, H, W): normalize each channel over axes (0,2,3)
Mathematical Definition
For each feature/channel:
μ=m1i=1∑mxiσ2=m1i=1∑m(xi−μ)2x^i=σ2+εxi−μyi=γx^i+βExamples
Input: x = [[1, 2], [3, 6], [5, 10]], gamma = [1, 0.5], beta = [0, 1]
Output: [[-1.2247, 0.3876], [0.0, 1.0], [1.2247, 1.6124]]
2D case (N=3, D=2): normalize over batch axis. Col 1 mean=3, var=2.667. Col 2 mean=6, var=10.667. Then scale by gamma, shift by beta.
Input: x = [[[[1]],[[2]]], [[[3]],[[4]]]], gamma = [1, 0.5], beta = [0, -1]
Output: [[[[-1.0]],[[-1.5]]], [[[1.0]],[[-0.5]]]]
4D case (2,2,1,1): normalize over axes (0,2,3) per channel. Ch0: [1,3] → mean=2, var=1 → [-1, 1]. Ch1: [2,4] → mean=3, var=1 → scaled by 0.5, shifted by -1 → [-1.5, -0.5].
Hint 1
Use keepdims=True when computing mean and variance for proper broadcasting.
Hint 2
For 4D inputs, reshape gamma and beta to (1,C,1,1) for broadcasting.
Requirements
- Vectorized; no loops over N/H/W
- Correct broadcasting for both shapes
- Use per-feature/channel mean/var as specified
Constraints
- Input shapes up to (1000, 100) for 2D or (100, 10, 32, 32) for 4D
- Use only NumPy
- Handle numerical stability with eps parameter
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: array
Accepts: number
Batch Normalization (Forward)
Batch Normalization (Forward)
Implement the forward pass of BatchNorm for inputs shaped either:
- (N, D): normalize each feature over the batch axis (axis=0)
- (N, C, H, W): normalize each channel over axes (0,2,3)
Mathematical Definition
For each feature/channel:
μ=m1i=1∑mxiσ2=m1i=1∑m(xi−μ)2x^i=σ2+εxi−μyi=γx^i+βExamples
Input: x = [[1, 2], [3, 6], [5, 10]], gamma = [1, 0.5], beta = [0, 1]
Output: [[-1.2247, 0.3876], [0.0, 1.0], [1.2247, 1.6124]]
2D case (N=3, D=2): normalize over batch axis. Col 1 mean=3, var=2.667. Col 2 mean=6, var=10.667. Then scale by gamma, shift by beta.
Input: x = [[[[1]],[[2]]], [[[3]],[[4]]]], gamma = [1, 0.5], beta = [0, -1]
Output: [[[[-1.0]],[[-1.5]]], [[[1.0]],[[-0.5]]]]
4D case (2,2,1,1): normalize over axes (0,2,3) per channel. Ch0: [1,3] → mean=2, var=1 → [-1, 1]. Ch1: [2,4] → mean=3, var=1 → scaled by 0.5, shifted by -1 → [-1.5, -0.5].
Hint 1
Use keepdims=True when computing mean and variance for proper broadcasting.
Hint 2
For 4D inputs, reshape gamma and beta to (1,C,1,1) for broadcasting.
Requirements
- Vectorized; no loops over N/H/W
- Correct broadcasting for both shapes
- Use per-feature/channel mean/var as specified
Constraints
- Input shapes up to (1000, 100) for 2D or (100, 10, 32, 32) for 4D
- Use only NumPy
- Handle numerical stability with eps parameter
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: array
Accepts: number