|
|
|
@ -69,6 +69,7 @@ class TestNNMseLoss(unittest.TestCase):
|
|
|
|
|
for dim in [[10, 10], [2, 10, 10], [3, 3, 10, 10]]:
|
|
|
|
|
input_np = np.random.uniform(0.1, 0.5, dim).astype("float32")
|
|
|
|
|
label_np = np.random.uniform(0.1, 0.5, dim).astype("float32")
|
|
|
|
|
paddle.enable_static()
|
|
|
|
|
prog = fluid.Program()
|
|
|
|
|
startup_prog = fluid.Program()
|
|
|
|
|
place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda(
|
|
|
|
@ -106,6 +107,7 @@ class TestNNMseLoss(unittest.TestCase):
|
|
|
|
|
for dim in [[10, 10], [2, 10, 10], [3, 3, 10, 10]]:
|
|
|
|
|
input_np = np.random.uniform(0.1, 0.5, dim).astype("float32")
|
|
|
|
|
label_np = np.random.uniform(0.1, 0.5, dim).astype("float32")
|
|
|
|
|
paddle.enable_static()
|
|
|
|
|
prog = fluid.Program()
|
|
|
|
|
startup_prog = fluid.Program()
|
|
|
|
|
place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda(
|
|
|
|
@ -143,6 +145,7 @@ class TestNNMseLoss(unittest.TestCase):
|
|
|
|
|
for dim in [[10, 10], [2, 10, 10], [3, 3, 10, 10]]:
|
|
|
|
|
input_np = np.random.uniform(0.1, 0.5, dim).astype("float32")
|
|
|
|
|
label_np = np.random.uniform(0.1, 0.5, dim).astype("float32")
|
|
|
|
|
paddle.enable_static()
|
|
|
|
|
prog = fluid.Program()
|
|
|
|
|
startup_prog = fluid.Program()
|
|
|
|
|
place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda(
|
|
|
|
@ -177,5 +180,112 @@ class TestNNMseLoss(unittest.TestCase):
|
|
|
|
|
self.assertTrue(dy_result.shape, [1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestNNFunctionalMseLoss(unittest.TestCase):
|
|
|
|
|
def test_NNFunctionalMseLoss_mean(self):
|
|
|
|
|
for dim in [[10, 10], [2, 10, 10], [3, 3, 10, 10]]:
|
|
|
|
|
input_np = np.random.uniform(0.1, 0.5, dim).astype("float32")
|
|
|
|
|
target_np = np.random.uniform(0.1, 0.5, dim).astype("float32")
|
|
|
|
|
paddle.enable_static()
|
|
|
|
|
prog = paddle.static.Program()
|
|
|
|
|
startup_prog = paddle.static.Program()
|
|
|
|
|
place = paddle.CUDAPlace(0) if core.is_compiled_with_cuda(
|
|
|
|
|
) else paddle.CPUPlace()
|
|
|
|
|
with paddle.static.program_guard(prog, startup_prog):
|
|
|
|
|
input = paddle.data(name='input', shape=dim, dtype='float32')
|
|
|
|
|
target = paddle.data(name='target', shape=dim, dtype='float32')
|
|
|
|
|
mse_loss = paddle.nn.functional.mse_loss(input, target, 'mean')
|
|
|
|
|
|
|
|
|
|
exe = paddle.static.Executor(place)
|
|
|
|
|
exe.run(startup_prog)
|
|
|
|
|
static_result = exe.run(
|
|
|
|
|
prog,
|
|
|
|
|
feed={"input": input_np,
|
|
|
|
|
"target": target_np},
|
|
|
|
|
fetch_list=[mse_loss])
|
|
|
|
|
|
|
|
|
|
paddle.disable_static()
|
|
|
|
|
dy_ret = paddle.nn.functional.mse_loss(
|
|
|
|
|
paddle.to_variable(input_np),
|
|
|
|
|
paddle.to_variable(target_np), 'mean')
|
|
|
|
|
dy_result = dy_ret.numpy()
|
|
|
|
|
|
|
|
|
|
sub = input_np - target_np
|
|
|
|
|
expected = np.mean(sub * sub)
|
|
|
|
|
self.assertTrue(np.allclose(static_result, expected))
|
|
|
|
|
self.assertTrue(np.allclose(static_result, dy_result))
|
|
|
|
|
self.assertTrue(np.allclose(dy_result, expected))
|
|
|
|
|
self.assertTrue(dy_result.shape, [1])
|
|
|
|
|
|
|
|
|
|
def test_NNFunctionalMseLoss_sum(self):
|
|
|
|
|
for dim in [[10, 10], [2, 10, 10], [3, 3, 10, 10]]:
|
|
|
|
|
input_np = np.random.uniform(0.1, 0.5, dim).astype("float32")
|
|
|
|
|
target_np = np.random.uniform(0.1, 0.5, dim).astype("float32")
|
|
|
|
|
paddle.enable_static()
|
|
|
|
|
prog = paddle.static.Program()
|
|
|
|
|
startup_prog = paddle.static.Program()
|
|
|
|
|
place = paddle.CUDAPlace(0) if core.is_compiled_with_cuda(
|
|
|
|
|
) else paddle.CPUPlace()
|
|
|
|
|
with paddle.static.program_guard(prog, startup_prog):
|
|
|
|
|
input = paddle.data(name='input', shape=dim, dtype='float32')
|
|
|
|
|
target = paddle.data(name='target', shape=dim, dtype='float32')
|
|
|
|
|
mse_loss = paddle.nn.functional.mse_loss(input, target, 'sum')
|
|
|
|
|
|
|
|
|
|
exe = paddle.static.Executor(place)
|
|
|
|
|
exe.run(startup_prog)
|
|
|
|
|
static_result = exe.run(
|
|
|
|
|
prog,
|
|
|
|
|
feed={"input": input_np,
|
|
|
|
|
"target": target_np},
|
|
|
|
|
fetch_list=[mse_loss])
|
|
|
|
|
|
|
|
|
|
paddle.disable_static()
|
|
|
|
|
dy_ret = paddle.nn.functional.mse_loss(
|
|
|
|
|
paddle.to_variable(input_np),
|
|
|
|
|
paddle.to_variable(target_np), 'sum')
|
|
|
|
|
dy_result = dy_ret.numpy()
|
|
|
|
|
|
|
|
|
|
sub = input_np - target_np
|
|
|
|
|
expected = np.sum(sub * sub)
|
|
|
|
|
self.assertTrue(np.allclose(static_result, expected))
|
|
|
|
|
self.assertTrue(np.allclose(static_result, dy_result))
|
|
|
|
|
self.assertTrue(np.allclose(dy_result, expected))
|
|
|
|
|
self.assertTrue(dy_result.shape, [1])
|
|
|
|
|
|
|
|
|
|
def test_NNFunctionalMseLoss_none(self):
|
|
|
|
|
for dim in [[10, 10], [2, 10, 10], [3, 3, 10, 10]]:
|
|
|
|
|
input_np = np.random.uniform(0.1, 0.5, dim).astype("float32")
|
|
|
|
|
target_np = np.random.uniform(0.1, 0.5, dim).astype("float32")
|
|
|
|
|
paddle.enable_static()
|
|
|
|
|
prog = paddle.static.Program()
|
|
|
|
|
startup_prog = paddle.static.Program()
|
|
|
|
|
place = paddle.CUDAPlace(0) if core.is_compiled_with_cuda(
|
|
|
|
|
) else paddle.CPUPlace()
|
|
|
|
|
with paddle.static.program_guard(prog, startup_prog):
|
|
|
|
|
input = paddle.data(name='input', shape=dim, dtype='float32')
|
|
|
|
|
target = paddle.data(name='target', shape=dim, dtype='float32')
|
|
|
|
|
mse_loss = paddle.nn.functional.mse_loss(input, target, 'none')
|
|
|
|
|
|
|
|
|
|
exe = paddle.static.Executor(place)
|
|
|
|
|
exe.run(startup_prog)
|
|
|
|
|
static_result = exe.run(
|
|
|
|
|
prog,
|
|
|
|
|
feed={"input": input_np,
|
|
|
|
|
"target": target_np},
|
|
|
|
|
fetch_list=[mse_loss])
|
|
|
|
|
|
|
|
|
|
paddle.disable_static()
|
|
|
|
|
dy_ret = paddle.nn.functional.mse_loss(
|
|
|
|
|
paddle.to_variable(input_np),
|
|
|
|
|
paddle.to_variable(target_np), 'none')
|
|
|
|
|
dy_result = dy_ret.numpy()
|
|
|
|
|
|
|
|
|
|
sub = input_np - target_np
|
|
|
|
|
expected = sub * sub
|
|
|
|
|
self.assertTrue(np.allclose(static_result, expected))
|
|
|
|
|
self.assertTrue(np.allclose(static_result, dy_result))
|
|
|
|
|
self.assertTrue(np.allclose(dy_result, expected))
|
|
|
|
|
self.assertTrue(dy_result.shape, [1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|
|
|
|
|