Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for BatchNorm to handle batches of size one #5530

Merged
merged 12 commits into from
Sep 30, 2022
Prev Previous commit
update
  • Loading branch information
rusty1s committed Sep 30, 2022
commit 611edb02e76b268dafb2fd5de369a9851914a7df
15 changes: 11 additions & 4 deletions test/nn/norm/test_batch_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ def test_batch_norm(conf):
out = norm(x)
assert out.size() == (100, 16)


def test_batch_norm_single_element():
x = torch.randn(1, 16)
with pytest.raises(ValueError):
_ = norm(x)

norm = BatchNorm(16, affine=conf, track_running_stats=conf,
allow_single_element=True)
norm = BatchNorm(16)
with pytest.raises(ValueError, match="Expected more than 1 value"):
norm(x)

with pytest.raises(ValueError, match="requires 'track_running_stats'"):
norm = BatchNorm(16, track_running_stats=False,
allow_single_element=True)

norm = BatchNorm(16, track_running_stats=True, allow_single_element=True)
out = norm(x)
assert torch.allclose(out, x)