Example #1
0
 def train_one_iter(grad):
     grad_tensor = flow.Tensor(grad, requires_grad=False)
     loss = x * grad_tensor
     loss = flow.sum(x * grad_tensor)
     loss.backward()
     sgd.step()
     sgd.zero_grad()
Example #2
0
 def train_one_iter(grad):
     grad_tensor = flow.Tensor(grad,
                               requires_grad=False,
                               device=flow.device(device))
     loss = flow.sum(x * grad_tensor)
     loss.backward()
     adam.step()
     adam.zero_grad()
Example #3
0
def _test_sum_impl(test_case, device):
    input = flow.Tensor(np.random.randn(2, 3),
                        dtype=flow.float32,
                        device=flow.device(device))
    of_out = flow.sum(input, dim=0)
    np_out = np.sum(input.numpy(), axis=0)
    test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5))

    input = flow.Tensor(np.random.randn(2, 3),
                        dtype=flow.float32,
                        device=flow.device(device))
    of_out = flow.sum(input, dim=0)
    np_out = np.sum(input.numpy(), axis=0)
    test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5))

    input = flow.Tensor(np.random.randn(2, 3),
                        dtype=flow.float32,
                        device=flow.device(device))
    of_out = flow.sum(input, dim=1)
    of_out2 = input.sum(dim=1)
    np_out = np.sum(input.numpy(), axis=1)
    test_case.assertTrue(
        np.allclose(of_out2.numpy(), of_out.numpy(), 1e-5, 1e-5))
    test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5))

    input = flow.Tensor(
        np.random.randn(4, 5, 6),
        dtype=flow.float32,
        device=flow.device(device),
        requires_grad=True,
    )
    of_out = flow.sum(input, dim=(2, 1))
    np_out = np.sum(input.numpy(), axis=(2, 1))
    test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5))
    of_out = of_out.sum()
    of_out.backward()
    np_grad = np.ones((4, 5, 6))
    test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, 1e-5, 1e-5))
Example #4
0
    def test_sum(test_case):
        input = flow.Tensor(np.random.randn(2, 3), dtype=flow.float32)
        of_out = flow.sum(input, dim=0)
        np_out = np.sum(input.numpy(), axis=0)
        test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))

        input = flow.Tensor(np.random.randn(2, 3), dtype=flow.float32)
        of_out = flow.sum(input, dim=0)
        np_out = np.sum(input.numpy(), axis=0)
        test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))

        input = flow.Tensor(np.random.randn(2, 3), dtype=flow.float32)
        of_out = flow.sum(input, dim=1)
        of_out2 = input.sum(dim=1)
        np_out = np.sum(input.numpy(), axis=1)
        test_case.assertTrue(
            np.allclose(of_out2.numpy(), of_out.numpy(), 1e-4, 1e-4))
        test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))

        input = flow.Tensor(np.random.randn(4, 5, 6), dtype=flow.float32)
        of_out = flow.sum(input, dim=(2, 1))
        np_out = np.sum(input.numpy(), axis=(2, 1))
        test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))
Example #5
0
 def training_step(self, batch, **kwargs):
     return (flow.sum(self(batch)), list(self.m.parameters())[0])