Skip to content

Commit

Permalink
add unit test for batchnorm1d
Browse files Browse the repository at this point in the history
  • Loading branch information
EsdeathYZH committed Jun 11, 2022
1 parent 7adb696 commit 3c26e1e
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions python/paddle/fluid/tests/unittests/test_batch_norm_op_v2.py
Expand Up @@ -110,11 +110,43 @@ def compute_v2(x):
y.backward()
return y.numpy(), x1.gradient()

x = np.random.randn(*shape).astype("float32")
y1, g1 = compute_v1(x)
y2, g2 = compute_v2(x)
self.assertTrue(np.allclose(g1, g2))
self.assertTrue(np.allclose(y1, y2))
x = np.random.randn(*shape).astype("float32")
y1, g1 = compute_v1(x)
y2, g2 = compute_v2(x)
self.assertTrue(np.allclose(g1, g2))
self.assertTrue(np.allclose(y1, y2))

def test_eager_api_1d(self):
places = [fluid.CPUPlace()]
if core.is_compiled_with_cuda():
places.append(fluid.CUDAPlace(0))
for p in places:
shape = [200000, 4]

def compute_v1(x):
with fluid.dygraph.guard(p):
bn = fluid.dygraph.BatchNorm(shape[1])
x1 = paddle.to_tensor(x)
x1.stop_gradient = False
y = bn(x1)
y.backward()
return y.numpy(), x1.gradient()

def compute_v2(x):
with fluid.dygraph.guard(p):
with _test_eager_guard():
bn = paddle.nn.BatchNorm1D(shape[1])
x1 = paddle.to_tensor(x)
x1.stop_gradient = False
y = bn(x1)
y.backward()
return y.numpy(), x1.gradient()

x = np.random.randn(*shape).astype("float32")
y1, g1 = compute_v1(x)
y2, g2 = compute_v2(x)
self.assertTrue(np.allclose(g1, g2))
self.assertTrue(np.allclose(y1, y2))

def test_dygraph(self):
places = [fluid.CPUPlace()]
Expand Down

0 comments on commit 3c26e1e

Please sign in to comment.