Skip to content
BIASED
Back to topic

Optional

BatchNorm reduction axes

NumPy: mean/variance over (N,H,W) per channel; verify standardization. Self-check in the notebook.

Version
v1
Minutes
~15
Evidence
Self-check in the notebook

Until platform auto-check exists, compare cell output with the expected numbers in the notebook. This is not a debt and does not affect mastery.

Download .ipynb

Download the notebook and run it locally. Its environment requirements are listed inside.

Notebook contents

Readable on your phone. The code below has not been executed in the browser; download the notebook to run it.

DL · Оси BatchNorm / BatchNorm reduction axes

Activity version: 1 · Seed: 0 · Данные: синтетический тензор N×C×H×WN\times C\times H\times W.

BatchNorm стандартизует каждый канал по батчу и пространству. Для (N,C,H,W)(N,C,H,W) среднее/дисперсия — по осям (0,2,3)(0,2,3).

Ожидаемые числа (seed 0, shape (4,3,2,2)(4,3,2,2))
Quantity Value Tolerance
mu.shape (3,) exact
mu ≈ [-0.18480, 0.22477, 0.16835] abs ≤ 1e-5
mean / var of normalized ch. 0 0 / 1 abs ≤ 1e-4
Самопроверка в ноутбуке

Не влияет на сертификат.

import numpy as np
 
rng = np.random.default_rng(0)
x = rng.normal(size=(4, 3, 2, 2))
eps = 1e-5
mu = x.mean(axis=(0, 2, 3))
var = x.var(axis=(0, 2, 3))
print("mu", mu)
print("var", var)
assert mu.shape == (3,)
assert np.allclose(mu, [-0.1848019, 0.2247695, 0.1683502], atol=1e-5)
 
x_hat = (x - mu[None, :, None, None]) / np.sqrt(var[None, :, None, None] + eps)
ch0 = x_hat[:, 0, :, :]
assert abs(ch0.mean()) < 1e-7
assert abs(ch0.var() - 1.0) < 1e-4  # eps in denom → slightly under 1
print("wrong-axis demo shape", x.mean(axis=1).shape)  # (N,H,W) — not BN
print("OK: BatchNorm axes")
 
Самопроверка в ноутбуке
  • mu/var имеют форму (C,)
  • нормированный канал: mean≈0, var≈1
  • отличие от нормализации «по каналам в пикселе» сформулировано