-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
232 additions
and
10 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import numpy as np | ||
|
||
|
||
def n_samples(dim: int | tuple[int, ...] | None, shape: tuple[int, ...]) -> int: | ||
if isinstance(dim, int): | ||
return shape[dim] | ||
if dim is None: | ||
return np.prod(shape).item() | ||
return np.prod([shape[d] for d in dim]).item() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import Generator | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import micrograd_pp as mpp | ||
|
||
BATCH_SZ = 64 | ||
NUM_FEATURES = 10 | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def run_before_and_after_tests() -> Generator[None, None, None]: | ||
np.random.seed(0) | ||
yield | ||
|
||
|
||
@pytest.mark.parametrize("momentum", [0.1, None]) | ||
def test_batch_norm_1d_track_running_stats(momentum: float) -> None: | ||
num_iters = 1_000 | ||
shift = np.random.randn(10) | ||
scale = np.random.randn(10) | ||
bn = mpp.BatchNorm1d(NUM_FEATURES, affine=False, momentum=momentum) | ||
for _ in range(num_iters): | ||
x = scale * np.random.randn(BATCH_SZ, NUM_FEATURES) + shift | ||
x_ = mpp.Constant(x) | ||
bn(x_) | ||
assert bn._running_mean is not None | ||
assert bn._running_var is not None | ||
np.testing.assert_allclose(bn._running_mean, shift, atol=0.1, rtol=0.0) | ||
np.testing.assert_allclose(bn._running_var, scale * scale, atol=0.1, rtol=0.0) | ||
|
||
|
||
def test_batch_norm_1d_standardize() -> None: | ||
shift = np.random.randn(10) | ||
scale = np.random.randn(10) | ||
bn = mpp.BatchNorm1d(NUM_FEATURES, affine=False) | ||
x = scale * np.random.randn(BATCH_SZ, NUM_FEATURES) + shift | ||
x_ = mpp.Constant(x) | ||
y_ = bn(x_) | ||
np.testing.assert_allclose(y_.value.mean(axis=0), 0.0, atol=1e-6, rtol=0.0) | ||
np.testing.assert_allclose(y_.value.var(axis=0), 1.0, atol=1e-3, rtol=0.0) | ||
|
||
|
||
def test_batch_norm_1d_eval() -> None: | ||
shift = np.random.randn(10) | ||
scale = np.random.randn(10) | ||
bn = mpp.BatchNorm1d(NUM_FEATURES, affine=False) | ||
x = scale * np.random.randn(BATCH_SZ, NUM_FEATURES) + shift | ||
x_ = mpp.Constant(x) | ||
with mpp.eval(): | ||
y_ = bn(x_) | ||
# The input should be close to the output since the batch norm scale and shift are 1 and 0 at initialization | ||
np.testing.assert_allclose(x_.value, y_.value, atol=1e-4, rtol=0.0) |