Repository navigation
Expand file tree
/
Copy pathbatch_normalization.py
More file actions
44 lines (33 loc) · 2.59 KB
/
Copy pathbatch_normalization.py
File metadata and controls
44 lines (33 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Batch normalisation in training mode
Given a batch `X` (N × D, N ≥ 2), parameters `gamma`, `beta`, and running estimates `running_mean`, `running_var` (each length D), return `[out, new_mean, new_var]` where
- out = γ ⊙ (X − μ) / √(σ² + eps) + β, with μ and σ² the per-feature mean and **biased** variance over the batch;
- new_mean = (1 − momentum)·running_mean + momentum·μ;
- new_var = (1 − momentum)·running_var + momentum·σ²_(unbiased), where σ²_(unbiased) = σ²·N/(N − 1).
These are PyTorch's conventions for BatchNorm1d in training mode.
Hints, the reference solution and a live editor:
https://academicquant.co.uk/ml-batch-normalization?utm_source=github&utm_medium=repo
"""
import numpy as np
def batch_norm_train(X, gamma, beta, running_mean, running_var, momentum, eps):
# Return [out, new_running_mean, new_running_var].
pass
# ---- tests: python batch_normalization.py ----
if __name__ == "__main__":
import json
import numpy as np
TESTS = json.loads("[{\"args\":[[[1,10],[3,30]],[1,1],[0,0],[0,0],[1,1],0.1,0],\"out\":[[[-1,-1],[1,1]],[0.2,2],[1.1,20.9]]},{\"args\":[[[1],[2],[3],[4]],[2],[1],[0],[1],1,0.00001],\"out\":[[[-1.6832708399378538],[0.105576386687382],[1.894423613312618],[3.6832708399378538]],[2.5],[1.6666666666666667]]},{\"args\":[[[0.5,-1],[0.5,3]],[1,1],[0,0],[10,10],[5,5],0,0.00001],\"out\":[[[0,-0.9999987500023437],[0,0.9999987500023437]],[10,10],[5,5]]},{\"args\":[[[0.2,1.5,-3],[1.1,-0.4,2.2],[0.7,0.9,0],[-1.3,2.2,1.1],[0,0,0.5]],[1,0.5,2],[0.1,0,-0.2],[0.3,-0.1,0],[1.2,0.8,2],0.1,0.00001],\"out\":[[[0.17349884217267156,0.34661838606324363,-3.8281058391564793],[1.2759814747627445,-0.6512224223006398,2.1421949088225367],[0.7859891936116008,0.031510762369385756,-0.3837015614762775],[-1.6639722121441163,0.7142439470394112,0.8792466736731299],[-0.0714972984029002,-0.44115067317140105,0.19036581813708947]],[0.28400000000000003,-0.0059999999999999915,0.016000000000000007],[1.1633,0.8333000000000002,2.1793]]}]")
def close(a, b, rt=1e-9, at=1e-9):
if isinstance(b, list):
a = a.tolist() if hasattr(a, "tolist") else list(a)
return len(a) == len(b) and all(close(x, y, rt, at) for x, y in zip(a, b))
return abs(float(a) - float(b)) <= at + rt * abs(float(b))
passed = 0
for i, t in enumerate(TESTS):
try:
ok = close(batch_norm_train(*t["args"]), t["out"])
except Exception as e:
ok = False
print(i, "error:", e)
passed += ok
print(i, "pass" if ok else "FAIL")
print(f"{passed}/{len(TESTS)} passed")