示例#1
0
def _test_add_forward(test_case, device):
    x = flow.Tensor(np.random.randn(2, 3), device=flow.device(device))
    y = flow.Tensor(np.random.randn(2, 3), device=flow.device(device))
    of_out = flow.add(x, y)
    np_out = np.add(x.numpy(), y.numpy())
    test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))

    x = 5
    y = flow.Tensor(np.random.randn(2, 3), device=flow.device(device))
    of_out = flow.add(x, y)
    np_out = np.add(x, y.numpy())
    test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))

    x = flow.Tensor(np.random.randn(2, 3), device=flow.device(device))
    y = 5
    of_out = flow.add(x, y)
    np_out = np.add(x.numpy(), y)
    test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))

    x = flow.Tensor(np.random.randn(2, 3), device=flow.device(device))
    y = flow.Tensor(np.array([5.0]), device=flow.device(device))
    of_out = flow.add(x, y)
    np_out = np.add(x.numpy(), y.numpy())
    test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))

    x = flow.Tensor(np.random.randn(1, 1), device=flow.device(device))
    y = flow.Tensor(np.random.randn(2, 3), device=flow.device(device))
    of_out = flow.add(x, y)
    np_out = np.add(x.numpy(), y.numpy())
    test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))
示例#2
0
    def test_add(test_case):
        x = flow.Tensor(np.random.randn(2, 3))
        y = flow.Tensor(np.random.randn(2, 3))
        of_out = flow.add(x, y)
        np_out = np.add(x.numpy(), y.numpy())
        test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))

        x = 5
        y = flow.Tensor(np.random.randn(2, 3))
        of_out = flow.add(x, y)
        np_out = np.add(x, y.numpy())
        test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))

        x = flow.Tensor(np.random.randn(2, 3))
        y = 5
        of_out = flow.add(x, y)
        np_out = np.add(x.numpy(), y)
        test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))

        x = flow.Tensor(np.random.randn(2, 3))
        y = flow.Tensor(np.array([5.0]))
        of_out = flow.add(x, y)
        np_out = np.add(x.numpy(), y.numpy())
        test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))

        x = flow.Tensor(np.random.randn(1, 1))
        y = flow.Tensor(np.random.randn(2, 3))
        of_out = flow.add(x, y)
        np_out = np.add(x.numpy(), y.numpy())
        test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))
示例#3
0
def _test_add_backward(test_case, device):
    x = 5
    y = flow.Tensor(
        np.random.randn(2, 3), requires_grad=True, device=flow.device(device)
    )
    of_out = flow.add(x, y).sum()
    of_out.backward()
    test_case.assertTrue(np.allclose(y.grad.numpy(), np.ones((2, 3)), 1e-4, 1e-4))