Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/mlx/nn/layers/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def __call__(self, x):


class BatchNorm(Module):
r"""Applies Batch Normalization over a 2D or 3D input.
r"""Applies Batch Normalization over a 2D, 3D or 4D input.

Computes

Expand Down
10 changes: 10 additions & 0 deletions python/tests/test_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,16 @@ def test_batch_norm(self):
self.assertIn("weight", bn_trainable)
self.assertIn("bias", bn_trainable)

# test with 4D input (NHWC)
mx.random.seed(42)
x = mx.random.normal((2, 3, 3, 6), dtype=mx.float32)
bn = nn.BatchNorm(num_features=6, affine=True)
y = bn(x)
self.assertTrue(x.shape == y.shape)
# batch norm over an NHWC input normalizes each channel across N, H, W
self.assertTrue(mx.allclose(y.mean(axis=(0, 1, 2)), mx.zeros((6,)), atol=1e-5))
self.assertTrue(mx.allclose(y.var(axis=(0, 1, 2)), mx.ones((6,)), atol=1e-2))

def test_batch_norm_stats(self):
batch_size = 2
num_features = 4
Expand Down
Loading