Skip to content
DeepForgeby svx
All problems
MediumDeep Learningdl-025

BatchNorm Forward (Train)

Problem

Training-mode batch normalization for a single feature over a batch: y_i = gamma * (x_i - mean) / sqrt(var + eps) + beta, using the biased batch variance (divide by n).

The signature is batchnorm_forward(x, gamma, beta, eps=1e-5).

Hint

Compute the mean and variance over the batch, then normalize with eps inside the square root.

Test cases

case 1
in: [[1,2,3,4],1,0]
expected: [-1.34163542,-0.4472118067,0.4472118067,1.34163542]
case 2
in: [[0,0,0],2,1]
expected: [1,1,1]
case 3
in: [[1,1],1,-1]
expected: [-1,-1]
case 4
in: [[1,-1],3,0]
expected: [2.9999850001,-2.9999850001]
case 5
in: [[2,4],1,2]
expected: [1.000005,2.999995]
Starter codePython
import math
def batchnorm_forward(x, gamma, beta, eps=1e-5):
    # Your code here
    pass

5 test cases · run your code against them in the editor